Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sample-safe-t-cut
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pongpana.se
sample-safe-t-cut
Commits
28e7e574
Commit
28e7e574
authored
Jun 17, 2019
by
pongpana.se
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
210 additions
and
0 deletions
+210
-0
safe-t-cut.ino
+210
-0
No files found.
safe-t-cut.ino
0 → 100644
View file @
28e7e574
#include <ArduinoJson.h>
#include <WiFi.h>
#include <PubSubClient.h>
/*const char* text = {"data":{ "MD_STA":{
"ML1": 0,
"ML2": 0,
"ML3": 0,
"ML4": 0,
"RMD9": 0,
"SM1" : 0
},
"ML1":{
"L1B1": 0,
"L1B2": 0,
"L1B3": 0,
"L1B4": 0,
"L1B5": 0
},
"ML2":{
"L2B1": 0,
"L2B2": 0,
"L2B3": 0,
"L2B4": 0,
"L2B5": 0
},
"ML3":{
"L3B1": 0,
"L3B2": 0,
"L3B3": 0,
"L3B4": 0,
"L3B5": 0
},
"ML4":{
"L4B1": 0,
"L4B2": 0,
"L4B3": 0,
"L4B4": 0,
"L4B5": 0
}
}
} */
const
char
*
ssid
=
"PT123"
;
const
char
*
password
=
"$Thailand0"
;
const
char
*
mqtt_server
=
"broker.nexpie.io"
;
const
char
*
key
=
"ebe7a38e-de1e-45df-9f23-51937231153b"
;
const
char
*
token
=
"3N3haeMX9XDyhvEfufmHCri1sdR4djss"
;
WiFiClient
espClient
;
PubSubClient
client
(
espClient
);
void
callback
(
char
*
topic
,
byte
*
payload
,
unsigned
int
length
)
{
payload
[
length
]
=
'\0'
;
/* payload */
String
topic_str
=
topic
,
payload_str
=
(
char
*
)
payload
;
Serial
.
println
(
"["
+
topic_str
+
"]: "
+
payload_str
);
const
size_t
capacity
=
JSON_OBJECT_SIZE
(
1
)
+
5
*
JSON_OBJECT_SIZE
(
5
)
+
JSON_OBJECT_SIZE
(
6
)
+
170
;
StaticJsonDocument
<
capacity
>
doc
;
DeserializationError
err
=
deserializeJson
(
doc
,
(
char
*
)
payload
);
if
(
err
)
{
Serial
.
print
(
F
(
"deserializeJson() failed with code "
));
Serial
.
println
(
err
.
c_str
());
}
JsonObject
data
=
doc
[
"data"
];
JsonObject
data_MD_STA
=
data
[
"MD_STA"
];
int
data_MD_STA_ML1
=
data_MD_STA
[
"ML1"
];
int
data_MD_STA_ML2
=
data_MD_STA
[
"ML2"
];
int
data_MD_STA_ML3
=
data_MD_STA
[
"ML3"
];
int
data_MD_STA_ML4
=
data_MD_STA
[
"ML4"
];
int
data_MD_STA_RMD9
=
data_MD_STA
[
"RMD9"
];
int
data_MD_STA_SM1
=
data_MD_STA
[
"SM1"
];
JsonObject
data_ML1
=
data
[
"ML1"
];
int
data_ML1_L1B1
=
data_ML1
[
"L1B1"
];
int
data_ML1_L1B2
=
data_ML1
[
"L1B2"
];
int
data_ML1_L1B3
=
data_ML1
[
"L1B3"
];
int
data_ML1_L1B4
=
data_ML1
[
"L1B4"
];
int
data_ML1_L1B5
=
data_ML1
[
"L1B5"
];
JsonObject
data_ML2
=
data
[
"ML2"
];
int
data_ML2_L2B1
=
data_ML2
[
"L2B1"
];
int
data_ML2_L2B2
=
data_ML2
[
"L2B2"
];
int
data_ML2_L2B3
=
data_ML2
[
"L2B3"
];
int
data_ML2_L2B4
=
data_ML2
[
"L2B4"
];
int
data_ML2_L2B5
=
data_ML2
[
"L2B5"
];
JsonObject
data_ML3
=
data
[
"ML3"
];
int
data_ML3_L3B1
=
data_ML3
[
"L3B1"
];
int
data_ML3_L3B2
=
data_ML3
[
"L3B2"
];
int
data_ML3_L3B3
=
data_ML3
[
"L3B3"
];
int
data_ML3_L3B4
=
data_ML3
[
"L3B4"
];
int
data_ML3_L3B5
=
data_ML3
[
"L3B5"
];
JsonObject
data_ML4
=
data
[
"ML4"
];
int
data_ML4_L4B1
=
data_ML4
[
"L4B1"
];
int
data_ML4_L4B2
=
data_ML4
[
"L4B2"
];
int
data_ML4_L4B3
=
data_ML4
[
"L4B3"
];
int
data_ML4_L4B4
=
data_ML4
[
"L4B4"
];
int
data_ML4_L4B5
=
data_ML4
[
"L4B5"
];
}
void
setup_wifi
()
{
delay
(
10
);
// We start by connecting to a WiFi network
Serial
.
println
();
Serial
.
print
(
"Connecting to "
);
Serial
.
println
(
ssid
);
WiFi
.
begin
(
ssid
,
password
);
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
delay
(
500
);
Serial
.
print
(
"."
);
}
Serial
.
println
(
""
);
Serial
.
println
(
"WiFi connected"
);
Serial
.
println
(
"IP address: "
);
Serial
.
println
(
WiFi
.
localIP
());
}
void
reconnect
()
{
// Loop until we're reconnected
while
(
!
client
.
connected
())
{
Serial
.
print
(
"Attempting MQTT connection..."
);
// Attempt to connect
if
(
client
.
connect
(
key
,
token
,
""
))
{
Serial
.
println
(
"connected"
);
// ... and resubscribe
client
.
subscribe
(
"@private/#"
);
client
.
subscribe
(
"@shadow/data/updated"
);
}
else
{
Serial
.
print
(
"failed, rc="
);
Serial
.
print
(
client
.
state
());
Serial
.
println
(
" try again in 5 seconds"
);
// Wait 5 seconds before retrying
delay
(
5000
);
}
}
}
void
setup
()
{
Serial
.
begin
(
115200
);
setup_wifi
();
client
.
setServer
(
mqtt_server
,
1883
);
client
.
setCallback
(
callback
);
}
void
loop
()
{
if
(
!
client
.
connected
())
{
reconnect
();
const
size_t
capacity
=
JSON_OBJECT_SIZE
(
1
)
+
5
*
JSON_OBJECT_SIZE
(
5
)
+
JSON_OBJECT_SIZE
(
6
);
StaticJsonDocument
<
capacity
>
doc
;
JsonObject
data
=
doc
.
createNestedObject
(
"data"
);
JsonObject
data_MD_STA
=
data
.
createNestedObject
(
"MD_STA"
);
data_MD_STA
[
"ML1"
]
=
0
;
data_MD_STA
[
"ML2"
]
=
0
;
data_MD_STA
[
"ML3"
]
=
0
;
data_MD_STA
[
"ML4"
]
=
0
;
data_MD_STA
[
"RMD9"
]
=
0
;
data_MD_STA
[
"SM1"
]
=
0
;
JsonObject
data_ML1
=
data
.
createNestedObject
(
"ML1"
);
data_ML1
[
"L1B1"
]
=
0
;
data_ML1
[
"L1B2"
]
=
0
;
data_ML1
[
"L1B3"
]
=
0
;
data_ML1
[
"L1B4"
]
=
0
;
data_ML1
[
"L1B5"
]
=
0
;
JsonObject
data_ML2
=
data
.
createNestedObject
(
"ML2"
);
data_ML2
[
"L2B1"
]
=
0
;
data_ML2
[
"L2B2"
]
=
0
;
data_ML2
[
"L2B3"
]
=
0
;
data_ML2
[
"L2B4"
]
=
0
;
data_ML2
[
"L2B5"
]
=
0
;
JsonObject
data_ML3
=
data
.
createNestedObject
(
"ML3"
);
data_ML3
[
"L3B1"
]
=
0
;
data_ML3
[
"L3B2"
]
=
0
;
data_ML3
[
"L3B3"
]
=
0
;
data_ML3
[
"L3B4"
]
=
0
;
data_ML3
[
"L3B5"
]
=
0
;
JsonObject
data_ML4
=
data
.
createNestedObject
(
"ML4"
);
data_ML4
[
"L4B1"
]
=
0
;
data_ML4
[
"L4B2"
]
=
0
;
data_ML4
[
"L4B3"
]
=
0
;
data_ML4
[
"L4B4"
]
=
0
;
data_ML4
[
"L4B5"
]
=
0
;
client
.
publish
(
"@shadow/data/update"
,
(
doc
.
as
<
String
>
()).
c_str
());
Serial
.
println
((
doc
.
as
<
String
>
()).
c_str
());
}
client
.
loop
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment