-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_esp8266_uart_bridge.ino
412 lines (322 loc) · 9.23 KB
/
mqtt_esp8266_uart_bridge.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* ESP8266 - UART-MQTT bridge
*
* (C) 2023 Dan White <dan.white@valpo.edu>
*
* Take frames on the UART RX pin and publish them via MQTT.
* Subscribe to MQTT topic and send contents out UART TX pin.
* Perodically publish a /status message.
*
* See config-example.h for configuration constants then copy to config.h and
* edit as appropriate.
*
* GPL-3.0 License
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include "time.h"
/*
* Extra libraries installed from the Library Manager
*/
// https://github.com/arduino-libraries/NTPClient
#include <NTPClient.h>
// https://arduinojson.org/
#include <ArduinoJson.h>
/*
* configuration includes passwords/etc
* include separately to not leak private information
*/
#include "config.h"
/* https://github.com/fabianoriccardi/git-describe-arduino
* add a GIT_VERSION string that is updated every compile event
*/
#include "git-version.h"
/*
* Globals
*/
String topic;
struct tm timeinfo;
uint8_t mac[6];
String my_mac;
// blink-per-message housekeeping
unsigned int nBlinks = 0;
bool led_state = 1;
bool in_blink = false;
typeof(millis()) last_blink = 0;
// status update housekeeping
typeof(millis()) last_status = 30000; // nonzero to defer our first status until triggered
unsigned long inPackets = 0;
unsigned long outPackets = 0;
WiFiClient wifi;
ESP8266WiFiMulti wifiMulti;
WiFiUDP udp;
NTPClient ntpClient(udp, NTP_SERVER, 0, NTP_UPDATE_INTERVAL);
PubSubClient mqtt(wifi);
typeof(millis()) last_wifi_check = -WIFI_RETRY_DELAY;
unsigned long last_mqtt_check = 0;
/*
* Read a line from the UART port.
*
* Assumes that the bytes of a frame are sent within CHAR_TIMEOUT of each
* other, and that the frame is terminated by a newline character '\n'.
*/
String serialRead() {
String buf = "";
char inChar;
uint32_t char_time = millis();
while (Serial.available() || millis() - char_time < CHAR_TIMEOUT) {
inChar = (char)Serial.read();
if (inChar == '\n') {
break;
} else if (inChar == '\r') {
char_time = millis();
continue;
} else if ((int)inChar != 255) {
buf += inChar;
char_time = millis();
}
}
return buf;
}
/*
* Return a string in RFC3339 format of the current time.
* Will return a placeholder if there is no network connection to an
* NTP server.
*/
String getIsoTime()
{
char timeStr[21] = {0}; // NOTE: change if strftime format changes
time_t time_now = ntpClient.getEpochTime();
localtime_r(&time_now, &timeinfo);
if (timeinfo.tm_year <= (2016 - 1900)) {
// seems to be a bogus value, so return a placeholder
return String("YYYY-MM-DDTHH:MM:SSZ");
} else {
strftime(timeStr, sizeof(timeStr), "%Y-%m-%dT%H:%M:%SZ", &timeinfo);
return String(timeStr);
}
}
/*
* Given a byte array of length (n), return the ASCII hex representation
* and properly zero pad values less than 0x10.
* String(0x08, HEX) will yield '8' instead of the expected '08' string
*/
String hexToStr(uint8_t* arr, int n)
{
String result;
result.reserve(2*n);
for (int i = 0; i < n; ++i) {
if (arr[i] < 0x10) {result += '0';}
result += String(arr[i], HEX);
}
return result;
}
/*
* Called whenever a payload is received from a subscribed MQTT topic.
*/
void mqtt_receive_callback(char* topic, byte* payload, unsigned int length) {
if (strcmp(topic, (MQTT_TOPIC_PREFIX + my_mac + MQTT_CONTROL_TOPIC).c_str()) == 0) {
// TODO: use this to change behavior
} else if (strcmp(topic, (MQTT_TOPIC_PREFIX + my_mac + MQTT_DOWNLINK_TOPIC).c_str()) == 0) {
// Directly pass through the bytes
// TODO: use COBS or other method to better denote payload frame
Serial.write(payload, length);
}
inPackets++;
// LED blinks once per payload
nBlinks += 1;
}
/*
* Check WiFi connection, attempt to reconnect.
* This blocks until a connection is (re)established.
*/
bool check_wifi()
{
if (WiFi.status() != WL_CONNECTED) {
if ((millis() - last_wifi_check) >= WIFI_RETRY_DELAY) {
Serial.print("[INFO] WiFi connecting.");
int retries = 10;
while (retries > 0 && wifiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
retries--;
}
if (retries == 0) {
String msg = "[INFO] WiFi: failed, waiting for " + String(WIFI_RETRY_DELAY/1000) + " seconds before trying again";
Serial.println(msg);
} else {
Serial.println(" " + WiFi.localIP().toString());
}
last_wifi_check = millis();
} // retry delay
} // !connected
return (WiFi.status() == WL_CONNECTED);
}
/*
* Check the MQTT connection state and attempt to reconnect.
* If we do reconnect, then subscribe to MQTT_CONTROL_TOPIC and
* MQTT_DOWNLINK_TOPIC and publish to MQTT_STATUS_TOPIC with status type and
* telemetry.
*/
bool check_mqtt() { if (mqtt.connected()) { return true; }
// no point to continue if no network
if (WiFi.status() != WL_CONNECTED) { return false; }
// reconnect
Serial.print("[INFO] MQTT reconnect...");
// Attempt to connect
int connect_status = mqtt.connect(my_mac.c_str(), MQTT_USER, MQTT_PASS,
(MQTT_TOPIC_PREFIX + my_mac + MQTT_STATUS_TOPIC).c_str(),
2, // willQoS
1, // willRetain
"{\"state\":\"disconnected\"}");
if (connect_status) {
// let everyone know we are alive
pub_status_mqtt("connected");
// ... and resubscribe to control and downlink topics
mqtt.subscribe((MQTT_TOPIC_PREFIX + my_mac + MQTT_CONTROL_TOPIC).c_str());
mqtt.subscribe((MQTT_TOPIC_PREFIX + my_mac + MQTT_DOWNLINK_TOPIC).c_str());
} else {
Serial.print("failed, rc=");
}
Serial.println(mqtt.state());
return mqtt.connected();
}
/*
* Publish a status message with system telemetry.
*/
bool pub_status_mqtt(const char *state)
{
bool retval = false;
// JSON formatted payload
StaticJsonDocument<512> status_json;
status_json["state"] = state;
status_json["time"] = getIsoTime();
status_json["uptime_ms"] = millis();
status_json["packets_in"] = inPackets;
status_json["packets_out"] = outPackets;
status_json["ssid"] = WiFi.SSID();
status_json["rssi"] = WiFi.RSSI();
status_json["ip"] = WiFi.localIP().toString();
status_json["hostname"] = WiFi.getHostname();
status_json["version"] = GIT_VERSION;
status_json["heap_free"] = ESP.getFreeHeap();
uint8_t buf[512];
size_t len = serializeJson(status_json, buf);
//Serial.println(buf);
if (mqtt.connected()) {
retval = mqtt.publish(
(MQTT_TOPIC_PREFIX + my_mac + MQTT_STATUS_TOPIC).c_str(),
buf,
len,
true);
}
if (retval) { nBlinks++; }
return retval;
}
/***********************************************************
* SETUP
***********************************************************/
void setup() {
Serial.begin(115200);
Serial.setTimeout(SERIAL_TIMEOUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, led_state);
Serial.println();
Serial.println();
/*
* setup WiFi
*/
//WiFi.mode(WIFI_STA);
for (int i=0; i<NUM_WLANS; i++) {
wifiMulti.addAP(WLAN_SSID[i], WLAN_PASS[i]);
}
WiFi.macAddress(mac);
my_mac = hexToStr(mac, 6);
String msg = "[INFO] MAC: " + my_mac;
Serial.println(msg);
msg = "mqtt8266bridge-" + my_mac;
WiFi.setHostname(msg.c_str());
// Connect to WiFi
check_wifi();
/*
* setup MQTT
*/
mqtt.setServer(MQTT_SERVER, MQTT_PORT);
mqtt.setCallback(mqtt_receive_callback);
mqtt.setBufferSize(512);
topic = MQTT_TOPIC_PREFIX + my_mac + MQTT_UPLINK_TOPIC;
delay(1000);
/*
* setup NTP time sync
*/
ntpClient.begin();
ntpClient.update();
}
/***********************************************************
* LOOP
***********************************************************/
void loop() {
bool wifi_good;
bool mqtt_good;
String rxBuffer;
wifi_good = check_wifi();
mqtt_good = check_mqtt();
if (wifi_good) {
ntpClient.update();
mqtt.loop();
}
if (mqtt_good) {
//celebrate!
}
rxBuffer = serialRead();
if (rxBuffer.length() > 0) {
mqtt.publish((MQTT_TOPIC_PREFIX + my_mac + MQTT_UPLINK_TOPIC).c_str(),
(uint8_t *)rxBuffer.c_str(),
rxBuffer.length(),
false);
outPackets++;
nBlinks++;
}
/*
* Handle periodic events
*/
unsigned long now = millis();
// Handle blinking without using delay()
if (nBlinks > 0) {
if (now - last_blink >= BLINK_MS) {
last_blink = now;
if (in_blink) { // then finish
digitalWrite(LED_PIN, led_state);
nBlinks--;
in_blink = false;
} else {
digitalWrite(LED_PIN, !led_state);
in_blink = true;
}
}
}
static int no_packet_intervals = NO_PACKETS_INTERVALS_ZOMBIE_RESTART;
if (now - last_status >= STATUS_INTERVAL) {
pub_status_mqtt("status");
if (inPackets > 0 || outPackets > 0) {
// something happened, so things are ok?
no_packet_intervals = NO_PACKETS_INTERVALS_ZOMBIE_RESTART;
} else {
no_packet_intervals--;
if (no_packet_intervals == 0) {
// no heard packets is a potential problem
pub_status_mqtt("restarting");
ESP.restart();
}
}
inPackets = 0;
outPackets = 0;
last_status = millis();
}
/*
* end periodic events
*/
delay(1);
}