-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESP8266Demo.ino
217 lines (179 loc) · 6.19 KB
/
ESP8266Demo.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Basic ESP8266 MQTT example with Thingsup
It connects to an Thingusup MQTT broker then:
- publishes "hello world" to the topic "outTopic" every two seconds
- subscribes to the topic "inTopic", printing out any messages
it receives.
It will reconnect to the server if the connection is lost using a blocking
reconnect function.
The code is tested with ESP8266 NodeMCU board
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecureBearSSL.h>
#include <PubSubClient.h>
#include <time.h>
// Update these with values suitable for your network.
char* ssid = ""; // WIFI SSID of network
char* password = ""; // WIFI password of the newtowk
const char* mqtt_server = "mqtt.thingsup.io"; // Thingsup Broker URL
const char* mqtt_password = ""; // Device Password set in the device addition stage in thingsup
const char* mqtt_username = ""; // Device Key set in the device addition stage in thingsup
String clientId = ""; // Client ID set in the device addition stage in thingsup
unsigned int mqtt_port = 1883; // MQTT port for SSL connection in thingsup
String accountID = "";
/**
* SSL Certificate for Validation to Prevent Man-In-Middle Attack.
*/
static const char digicert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDQTCCA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LnoQdeXeGADbkpy
rqXRfboQnoZsG4q5WTP468SQvvG5
-----END CERTIFICATE-----
)EOF";
BearSSL::X509List cert(digicert);
long current_millis = 0;
BearSSL::WiFiClientSecure espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
/*
* This function sets ESP in STA mode and connects to the WIFI
* Args: Null
* Returns: Null
*/
void setup_wifi() {
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//Use this if client id is kept as * in Thingsup Dashboard
//clientId = "mqttdevice-" + String(ESP.getChipId(), HEX);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/*
* This function is callback function which gets triggered with data is recived on the MQTT subscribed topic
* Args: Null
* Returns: Topic name, Payload, Length
*/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
/*
* This function Reconnects to MQTT when there is disconnection
* Args: Null
* Returns: Null
*/
void reconnect() {
// Loop until we're reconnected
if (!client.connected()) {
Serial.print("Attempting MQTT connection...");
Serial.print("clientId : ");
Serial.println(clientId);
Serial.print("mqtt_user=");
Serial.println(mqtt_username);
Serial.print("mqtt_pass=");
Serial.println(mqtt_password);
if (client.connect(clientId.c_str(), mqtt_username, mqtt_password))
{
Serial.println("connected");
// Once connected, publish an announcement...
String publish_topic = "/" + accountID + "/" + "outTopic"; // The topic name should be always start with /accountID/
client.publish((char*)publish_topic.c_str(), "hello world");
String sub_topic = "/" + accountID + "/" + "inTopic";
client.subscribe(sub_topic.c_str());
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
}
}
}
/*
* This is required for SSL/TLS Verification to check expiry date of certificate.
*/
void setClock() {
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
}
void setup() {
Serial.begin(115200);
setup_wifi();
/*
* SSL Verification Code
*/
//espClient.setInsecure(); // Use This for Testing
/*
* Recommended Approch
*/
espClient.setTrustAnchors(&cert); //Comment this if using espClient.setInsecure();
setClock(); //Comment this if using espClient.setInsecure();
/*
* SSL Verification Code
*/
client.setClient(espClient);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (millis() - current_millis > 5000)
{
current_millis = millis();
if (client.connected())
{
Serial.print("Publishing message: ");
String publish_topic = "/" + accountID + "/" + "outTopic"; // The topic name should be always start with /accountID/
if (client.publish((char*)publish_topic.c_str(), "hello world") == true)
{
Serial.println("Success");
}
}
}
}