-
Notifications
You must be signed in to change notification settings - Fork 8
/
uart2mqtt-arduinomega.ino
295 lines (240 loc) · 6.98 KB
/
uart2mqtt-arduinomega.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
// undefine this if WIFI settings are hardcoded into nodemcu lua script
#define ESP_INIT
// WIFI settings to pass towards esp
#define SSIDNAME "MySSID"
#define SSIDPASS "WIFIpassword"
// line separator - this must match UART_TERMINATOR1 definition from nodemcu lua script
#define UART_TERMINATOR1 '\r'
// baudrate towards computer (USB) and towards esp
#define USB_BAUDRATE 115200
#define ESP_BAUDRATE 115200
// restart nodemcu each time arduino is restarted
#define ESP_RESTART
// define if you want detailed explanation over USB of STATUS_REGISTER from nodemcu
#undef DEBUG_SREG_VERBOSE
// define both lines if nodemcu needs to manually run lua script
#undef ESP_RUNSCRIPT_NAME uart2mqtt-R3.lua
#undef ESP_RUNSCRIPT
// reserve MAX_MSGLEN bytes in arduino RAM for receiving content
#define MAX_MSGLEN 128
// periodically send data to esp (in milliseconds)
unsigned long interval = 1000;
// stop unless you want to modify the code :)
int x = 0;
static String content1 = "";
static String content2 = "";
const byte ledPin = LED_BUILTIN;
static char sreg = 0;
static unsigned long previousMillis = 0;
static unsigned long currentMillis = 0;
static boolean connected = 0;
void esp_restart() {
// reboot esp
Serial.println("rebooting esp...");
Serial.flush();
Serial1.write(0xf3);
Serial1.write(UART_TERMINATOR1);
// Serial.findUntil() function returns true if the
// target string is found and false if it times out
// wait for LUA prompt
// Serial1.setTimeout(1000);
while (!Serial1.find('> '));
Serial.print(content1);
Serial.flush();
delay(500);
}
void esp_init() {
Serial.println("passing WIFI credentials to esp...");
Serial.flush();
Serial1.println(SSIDNAME);
Serial1.flush();
delay(10);
Serial1.println(SSIDPASS);
Serial1.flush();
delay(10);
// wait until IP address is obtained and connected to MQTT
Serial.println("Waiting for init to complete");
while (sreg != 0x7d) {
Serial.print(".");
delay(100);
// wait for STATUS_REGISTER and UART_TERMINATOR1 to arrive from esp
if (Serial1.available() > 0) {
content1 = Serial1.readStringUntil(UART_TERMINATOR1);
if (content1.charAt(0) != sreg) {
sreg = content1.charAt(0);
debugStatusRegister(sreg);
}
}
}
content1 = "";
content2 = "";
connected = 1;
Serial.println("esp init done");
}
void setup() {
// dont expect incoming messages to be longer than MAX_MSGLEN
content1.reserve(MAX_MSGLEN);
content2.reserve(MAX_MSGLEN);
pinMode(ledPin, OUTPUT);
// Serial = USB (console)
// Serial1 = tx1/rx1 - esp
Serial.begin(USB_BAUDRATE);
Serial1.begin(ESP_BAUDRATE);
// wait for serial hardware to finish setup
while (!Serial);
while (!Serial1);
// check if esp is present and whats its status
Serial.println("polling esp status...");
Serial.println("0xfd");
Serial1.write(0xfd);
Serial.println("UART_TERMINATOR1");
Serial1.write(UART_TERMINATOR1);
// read SREG or timeout after setTimeout() period
content1 = Serial1.readStringUntil(UART_TERMINATOR1);
sreg = content1.charAt(0);
debugStatusRegister(sreg);
// if not connected and stuff then restart and init
if (sreg != 0x7d) {
#ifdef ESP_RESTART
esp_restart();
#endif
#ifdef ESP_RUNSCRIPT
// lua dofile - start uart2mqtt script
Serial.println("dofile(\"ESP_RUNSCRIPT_NAME\")\r\n");
Serial1.println("dofile(\"ESP_RUNSCRIPT_NAME\")\r\n");
Serial1.flush();
delay(100);
#endif
#ifdef ESP_INIT
/*
at this point esp will try to:
- connect to AP
- obtain IP address from DHCP
- connect to MQTT broker
- publish last-will-and-testament message to BCASTTOPIC
- subscribe to RXTOPIC
- subscribe to CMDTOPIC
- publish IP address to BCASTTOPIC
*/
esp_init();
#endif
}
Serial.println("Entering loop...");
Serial.flush();
}
void loop() {
currentMillis = millis();
// read analog value from pin A5
if ( (currentMillis - previousMillis) > interval ) {
Serial.print("!");
if (connected) {
x = analogRead(5);
// send to esp
Serial1.print(x);
Serial1.write(UART_TERMINATOR1);
Serial1.flush();
// print on usb console
Serial.print(">");
Serial.println(x);
Serial.flush();
}
previousMillis = currentMillis;
}
// read SREG + anything? until UART_TERMINATOR1
if (Serial1.available()) {
content1 = Serial1.readStringUntil(UART_TERMINATOR1);
if (content1.charAt(0) != sreg) {
Serial.print("SREG change from: ");
Serial.println(sreg, HEX);
sreg = content1.charAt(0);
debugStatusRegister(sreg);
}
// pass to usb if there is anything more than just SREG
if (content1.length() > 1) {
content1 = content1.substring(1);
Serial.print("CONTENT1=");
Serial.println(content1);
Serial.flush();
}
}
// if STATUS_REGISTER suggests that we are not connected anymore then disable sending data from arduino to esp
if ( (connected) && (sreg != 0x7d) ) {
connected = 0;
Serial.println("disconnected");
if (sreg == 5) {
Serial.println("sreg == 5, esp restarted?");
esp_init();
}
} else if ( (!connected) && (sreg != 0x7d) ) {
if (sreg == 5) {
Serial.println("sreg == 5, esp restarted?");
esp_init();
}
} else if ( (!connected) && (sreg == 0x7d) ) {
// enable sending data if esp reconnects
connected = 1;
Serial.println("connected");
}
// did we receive anything from internet?
if (content1 == "1") {
digitalWrite(ledPin, HIGH);
} else if (content1 == "0") {
digitalWrite(ledPin, LOW);
}
content1 = "";
// read from usb and pass to esp
if (Serial.available()) {
content2 = Serial.readStringUntil(UART_TERMINATOR1);
}
if (content2 != "") {
if (content2 == "STATUS") {
Serial1.write(0xf0);
Serial1.write(UART_TERMINATOR1);
} else if (content2 == "REBOOT") {
Serial1.write(0xf3);
Serial1.write(UART_TERMINATOR1);
} else {
Serial1.print(content2);
Serial1.write(UART_TERMINATOR1);
Serial1.flush();
}
content2 = "";
}
}
int debugStatusRegister (char sreg) {
Serial.print("SREG=");
Serial.println(sreg, HEX);
#ifdef DEBUG_SREG_VERBOSE
char s1;
s1 = sreg & 7;
Serial.print("wifi.sta.status()=");
if (s1 == 0) {
Serial.println("STATION_IDLE");
}
else if (s1 == 1) {
Serial.println("STATION_CONNECTING");
}
else if (s1 == 2) {
Serial.println("STATION_WRONG_PASSWORD");
}
else if (s1 == 3) {
Serial.println("STATION_NO_AP_FOUND");
}
else if (s1 == 4) {
Serial.println("STATION_CONNECT_FAIL");
}
else if (s1 == 5) {
Serial.println("STATION_GOT_IP");
}
Serial.print("wifi.sta.getip()= ");
Serial.println((sreg & 8) >> 3);
Serial.print("SSID_RECEIVED_FLAG= ");
Serial.println((sreg & 16) >> 4);
Serial.print("WIFIPASSWORD_RECEIVED_FLAG= ");
Serial.println((sreg & 32) >> 5);
Serial.print("MQTT_CONNECTED_FLAG= ");
Serial.println((sreg & 64) >> 6);
Serial.println();
Serial.flush();
#endif
}