arduino-sketches – Diff between revs 8 and 17
?pathlinks?
Rev 8 | Rev 17 | |||
---|---|---|---|---|
1 | /*************************************************************************/ |
1 | /*************************************************************************/ |
|
2 | /* Copyright (C) 2022 Wizardry and Steamworks - License: GNU GPLv3 */ |
2 | /* Copyright (C) 2023 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
3 | /*************************************************************************/ |
3 | /*************************************************************************/ |
|
- | 4 | |
||
- | 5 | // Removing comment for debugging over the first serial port. |
||
4 | |
6 | // #define DEBUG 1 |
|
5 | // The AP to connect to via Wifi. |
7 | // The AP to connect to via Wifi. |
|
6 | #define STA_SSID "" |
8 | #define STA_SSID "" |
|
7 | // The AP Wifi password. |
9 | // The AP Wifi password. |
|
8 | #define STA_PSK "" |
10 | #define STA_PSK "" |
|
9 | // The MQTT broker to connect to. |
11 | // The MQTT broker to connect to. |
|
10 | #define MQTT_HOST "" |
12 | #define MQTT_HOST "" |
|
11 | // The MQTT broker username. |
13 | // The MQTT broker username. |
|
12 | #define MQTT_USERNAME "" |
14 | #define MQTT_USERNAME "" |
|
13 | // The MQTT broker password. |
15 | // The MQTT broker password. |
|
14 | #define MQTT_PASSWORD "" |
16 | #define MQTT_PASSWORD "" |
|
15 | // The MQTT broker port. |
17 | // The MQTT broker port. |
|
16 | #define MQTT_PORT 1883 |
18 | #define MQTT_PORT 1883 |
|
17 | // The default MQTT client ID is "esp-CHIPID" where CHIPID is the ESP8266 |
19 | // The default MQTT client ID is "esp-CHIPID" where CHIPID is the ESP8266 |
|
18 | // or ESP32 chip identifier. |
20 | // or ESP32 chip identifier. |
|
19 | #define MQTT_CLIENT_ID() String("esp-" + String(GET_CHIP_ID(), HEX)) |
21 | #define MQTT_CLIENT_ID() String("esp-" + String(GET_CHIP_ID(), HEX)) |
|
20 | // The authentication password to use for OTA updates. |
22 | // The authentication password to use for OTA updates. |
|
21 | #define OTA_PASSWORD "" |
23 | #define OTA_PASSWORD "" |
|
22 | // The OTA port on which updates take place. |
24 | // The OTA port on which updates take place. |
|
23 | #define OTA_PORT 8266 |
25 | #define OTA_PORT 8266 |
|
24 | // The default topic that the sketch subscribes to is "esp/CHIPID" where |
26 | // The default topic that the sketch subscribes to is "esp/CHIPID" where |
|
25 | // CHIPID is the ESP8266 or ESP32 chip identifier. |
27 | // CHIPID is the ESP8266 or ESP32 chip identifier. |
|
26 | #define MQTT_TOPIC() String("esp/" + String(GET_CHIP_ID(), HEX)) |
28 | #define MQTT_TOPIC() String("esp/" + String(GET_CHIP_ID(), HEX)) |
|
27 | |
29 | |
|
28 | // Platform specific defines. |
30 | // Platform specific defines. |
|
29 | #if defined(ARDUINO_ARCH_ESP8266) |
31 | #if defined(ARDUINO_ARCH_ESP8266) |
|
30 | #define GET_CHIP_ID() (ESP.getChipId()) |
32 | #define GET_CHIP_ID() (ESP.getChipId()) |
|
31 | #elif defined(ARDUINO_ARCH_ESP32) |
33 | #elif defined(ARDUINO_ARCH_ESP32) |
|
32 | #define GET_CHIP_ID() ((uint16_t)(ESP.getEfuseMac()>>32)) |
34 | #define GET_CHIP_ID() ((uint16_t)(ESP.getEfuseMac() >> 32)) |
|
33 | #endif |
35 | #endif |
|
34 | |
36 | |
|
35 | // Miscellaneous defines. |
37 | // Miscellaneous defines. |
|
36 | //#define CHIP_ID_HEX (String(GET_CHIP_ID()).c_str()) |
38 | //#define CHIP_ID_HEX (String(GET_CHIP_ID()).c_str()) |
|
37 | #define HOSTNAME() String("esp-" + String(GET_CHIP_ID(), HEX)) |
39 | #define HOSTNAME() String("esp-" + String(GET_CHIP_ID(), HEX)) |
|
38 | |
40 | |
|
39 | // Platform specific libraries. |
41 | // Platform specific libraries. |
|
40 | #if defined(ARDUINO_ARCH_ESP8266) |
42 | #if defined(ARDUINO_ARCH_ESP8266) |
|
41 | #include <ESP8266WiFi.h> |
43 | #include <ESP8266WiFi.h> |
|
42 | #include <ESP8266mDNS.h> |
44 | #include <ESP8266mDNS.h> |
|
43 | #elif defined(ARDUINO_ARCH_ESP32) |
45 | #elif defined(ARDUINO_ARCH_ESP32) |
|
44 | #include <WiFi.h> |
46 | #include <WiFi.h> |
|
45 | #include <ESPmDNS.h> |
47 | #include <ESPmDNS.h> |
|
46 | #endif |
48 | #endif |
|
47 | // General libraries. |
49 | // General libraries. |
|
48 | #include <WiFiUdp.h> |
50 | #include <WiFiUdp.h> |
|
49 | #include <ArduinoOTA.h> |
51 | #include <ArduinoOTA.h> |
|
50 | #include <PubSubClient.h> |
52 | #include <PubSubClient.h> |
|
51 | #include <ArduinoJson.h> |
53 | #include <ArduinoJson.h> |
|
52 | #if defined(ARDUINO_ARCH_ESP32) |
54 | #if defined(ARDUINO_ARCH_ESP32) |
|
53 | #include <FS.h> |
55 | #include <FS.h> |
|
54 | #include <SPIFFS.h> |
56 | #include <SPIFFS.h> |
|
55 | #endif |
57 | #endif |
|
56 | |
- | ||
57 | const char *sta_ssid = STA_SSID; |
- | ||
58 | const char *sta_psk = STA_PSK; |
- | ||
59 | const char *mqtt_host = MQTT_HOST; |
- | ||
60 | const char *mqtt_username = MQTT_USERNAME; |
- | ||
61 | const char *mqtt_password = MQTT_PASSWORD; |
- | ||
62 | const int mqtt_port = MQTT_PORT; |
- | ||
63 | const char *ota_password = OTA_PASSWORD; |
- | ||
64 | const int ota_port = OTA_PORT; |
- | ||
65 | |
58 | |
|
66 | WiFiClient espClient; |
59 | WiFiClient espClient; |
|
67 | PubSubClient mqttClient(espClient); |
60 | PubSubClient mqttClient(espClient); |
|
68 | |
61 | |
|
69 | // Define GPIO pins for supported architectures. |
62 | // Define GPIO pins for supported architectures. |
|
70 | #if defined(ARDUINO_ARCH_ESP8266) |
63 | #if defined(ARDUINO_ARCH_ESP8266) |
|
71 | int PINS[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8 }; |
64 | int PINS[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8 }; |
|
72 | #elif defined(ARDUINO_ARCH_ESP32) |
65 | #elif defined(ARDUINO_ARCH_ESP32) |
|
73 | int PINS[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
66 | int PINS[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
|
74 | 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, |
67 | 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, |
|
75 | 23, 25, 26, 27, 32, 33, 34, 35, 36, 37, |
68 | 23, 25, 26, 27, 32, 33, 34, 35, 36, 37, |
|
76 | 38, 39 |
69 | 38, 39 }; |
|
77 | }; |
- | ||
78 | #endif |
70 | #endif |
|
79 | |
71 | |
|
80 | String mqttSerialize(StaticJsonDocument<256> msg) { |
72 | String mqttSerialize(StaticJsonDocument<256> msg) { |
|
81 | char output[256]; |
73 | char output[256]; |
|
82 | serializeJson(msg, output, 256); |
74 | serializeJson(msg, output, 256); |
|
83 | return String(output); |
75 | return String(output); |
|
84 | } |
76 | } |
|
85 | |
77 | |
|
86 | void mqttCallback(char *topic, byte *payload, unsigned int length) { |
78 | void mqttCallback(char *topic, byte *payload, unsigned int length) { |
|
87 | String msgTopic = String(topic); |
79 | String msgTopic = String(topic); |
|
88 | // payload is not null terminated and casting will not work |
80 | // payload is not null terminated and casting will not work |
|
89 | char msgPayload[length + 1]; |
81 | char msgPayload[length + 1]; |
|
90 | snprintf(msgPayload, length + 1, "%s", payload); |
82 | snprintf(msgPayload, length + 1, "%s", payload); |
|
- | 83 | #ifdef DEBUG |
||
91 | Serial.println("Message received on topic: " + String(topic) + " with payload: " + String(msgPayload)); |
84 | Serial.println("Message received on topic: " + String(topic) + " with payload: " + String(msgPayload)); |
|
- | 85 | #endif |
||
92 | |
86 | |
|
93 | // Parse the payload sent to the MQTT topic as a JSON document. |
87 | // Parse the payload sent to the MQTT topic as a JSON document. |
|
94 | StaticJsonDocument<256> doc; |
88 | StaticJsonDocument<256> doc; |
|
- | 89 | #ifdef DEBUG |
||
95 | Serial.println("Deserializing message...."); |
90 | Serial.println("Deserializing message...."); |
|
- | 91 | #endif |
||
96 | DeserializationError error = deserializeJson(doc, msgPayload); |
92 | DeserializationError error = deserializeJson(doc, msgPayload); |
|
97 | if (error) { |
93 | if (error) { |
|
- | 94 | #ifdef DEBUG |
||
98 | Serial.println("Failed to parse MQTT payload as JSON: " + String(error.c_str())); |
95 | Serial.println("Failed to parse MQTT payload as JSON: " + String(error.c_str())); |
|
- | 96 | #endif |
||
99 | return; |
97 | return; |
|
100 | } |
98 | } |
|
101 | |
99 | |
|
102 | // Do not process messages without an action key. |
100 | // Do not process messages without an action key. |
|
103 | if (!doc.containsKey("action")) { |
101 | if (!doc.containsKey("action")) { |
|
104 | return; |
102 | return; |
|
105 | } |
103 | } |
|
106 | |
104 | |
|
107 | String action = (const char *)doc["action"]; |
105 | String action = (const char *)doc["action"]; |
|
108 | if(action == "set") { |
106 | if (action == "set") { |
|
109 | String state = (const char *)doc["state"]; |
107 | String state = (const char *)doc["state"]; |
|
110 | const int pin = (const int)doc["pin"]; |
108 | const int pin = (const int)doc["pin"]; |
|
111 | |
109 | #ifdef DEBUG |
|
112 | Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); |
110 | Serial.println("Setting pin: " + String(pin) + " to state: " + String(state)); |
|
113 | |
111 | #endif |
|
114 | pinMode(PINS[pin], OUTPUT); |
112 | pinMode(PINS[pin], OUTPUT); |
|
115 | |
113 | |
|
116 | if (state == "on") { |
114 | if (state == "on") { |
|
117 | digitalWrite(PINS[pin], HIGH); |
115 | digitalWrite(PINS[pin], HIGH); |
|
118 | int status = digitalRead(PINS[pin]); |
116 | int status = digitalRead(PINS[pin]); |
|
- | 117 | #ifdef DEBUG |
||
119 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
118 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
|
- | 119 | #endif |
||
120 | return; |
120 | return; |
|
121 | } |
121 | } |
|
122 | |
122 | |
|
123 | digitalWrite(PINS[pin], LOW); |
123 | digitalWrite(PINS[pin], LOW); |
|
124 | int status = digitalRead(PINS[pin]); |
124 | int status = digitalRead(PINS[pin]); |
|
- | 125 | #ifdef DEBUG |
||
125 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
126 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
|
- | 127 | #endif |
||
126 | return; |
128 | return; |
|
127 | } |
129 | } |
|
128 | |
130 | |
|
129 | if(action == "get") { |
131 | if (action == "get") { |
|
130 | const int pin = (const int)doc["pin"]; |
132 | const int pin = (const int)doc["pin"]; |
|
131 | |
133 | #ifdef DEBUG |
|
132 | Serial.println("Getting pin: " + String(pin) + " state."); |
134 | Serial.println("Getting pin: " + String(pin) + " state."); |
|
133 | |
135 | #endif |
|
134 | int status = digitalRead(PINS[pin]); |
136 | int status = digitalRead(PINS[pin]); |
|
- | 137 | #ifdef DEBUG |
||
135 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
138 | Serial.println("Pin " + String(pin) + " state is now: " + String(status)); |
|
136 | |
139 | #endif |
|
137 | // Announce the action. |
140 | // Announce the action. |
|
138 | StaticJsonDocument<256> msg; |
141 | StaticJsonDocument<256> msg; |
|
139 | msg["pin"] = pin; |
142 | msg["pin"] = pin; |
|
140 | switch(status) { |
143 | switch (status) { |
|
141 | case 1: |
144 | case 1: |
|
142 | msg["state"] = "on"; |
145 | msg["state"] = "on"; |
|
143 | break; |
146 | break; |
|
144 | case 0: |
147 | case 0: |
|
145 | msg["state"] = "off"; |
148 | msg["state"] = "off"; |
|
146 | break; |
149 | break; |
|
147 | default: |
150 | default: |
|
148 | msg["state"] = "unknown"; |
151 | msg["state"] = "unknown"; |
|
149 | break; |
152 | break; |
|
150 | } |
153 | } |
|
151 | |
154 | |
|
152 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
155 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
|
153 | return; |
156 | return; |
|
154 | } |
157 | } |
|
155 | } |
158 | } |
|
156 | |
159 | |
|
157 | bool mqttConnect() { |
160 | bool mqttConnect() { |
|
- | 161 | #ifdef DEBUG |
||
158 | Serial.println("Attempting to connect to MQTT broker: " + String(mqtt_host)); |
162 | Serial.println("Attempting to connect to MQTT broker: " + String(MQTT_HOST)); |
|
- | 163 | #endif |
||
159 | mqttClient.setServer(mqtt_host, mqtt_port); |
164 | mqttClient.setServer(MQTT_HOST, MQTT_PORT); |
|
160 | |
165 | |
|
161 | StaticJsonDocument<256> msg; |
166 | StaticJsonDocument<256> msg; |
|
- | 167 | if (mqttClient.connect(MQTT_CLIENT_ID().c_str(), MQTT_USERNAME, MQTT_PASSWORD)) { |
||
162 | if (mqttClient.connect(MQTT_CLIENT_ID().c_str(), mqtt_username, mqtt_password)) { |
168 | #ifdef DEBUG |
|
- | 169 | Serial.println("Established connection with MQTT broker using client ID: " + MQTT_CLIENT_ID()); |
||
163 | Serial.println("Established connection with MQTT broker using client ID: " + MQTT_CLIENT_ID()); |
170 | #endif |
|
164 | mqttClient.setCallback(mqttCallback); |
171 | mqttClient.setCallback(mqttCallback); |
|
165 | msg["action"] = "connected"; |
172 | msg["action"] = "connected"; |
|
166 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
173 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
|
- | 174 | #ifdef DEBUG |
||
167 | Serial.println("Attempting to subscribe to MQTT topic: " + MQTT_TOPIC()); |
175 | Serial.println("Attempting to subscribe to MQTT topic: " + MQTT_TOPIC()); |
|
- | 176 | #endif |
||
168 | if (!mqttClient.subscribe(MQTT_TOPIC().c_str())) { |
177 | if (!mqttClient.subscribe(MQTT_TOPIC().c_str())) { |
|
- | 178 | #ifdef DEBUG |
||
169 | Serial.println("Failed to subscribe to MQTT topic: " + MQTT_TOPIC()); |
179 | Serial.println("Failed to subscribe to MQTT topic: " + MQTT_TOPIC()); |
|
- | 180 | #endif |
||
170 | return false; |
181 | return false; |
|
171 | } |
182 | } |
|
- | 183 | #ifdef DEBUG |
||
172 | Serial.println("Subscribed to MQTT topic: " + MQTT_TOPIC()); |
184 | Serial.println("Subscribed to MQTT topic: " + MQTT_TOPIC()); |
|
- | 185 | #endif |
||
173 | msg.clear(); |
186 | msg.clear(); |
|
174 | msg["action"] = "subscribed"; |
187 | msg["action"] = "subscribed"; |
|
175 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
188 | mqttClient.publish(MQTT_TOPIC().c_str(), mqttSerialize(msg).c_str()); |
|
176 | return true; |
189 | return true; |
|
177 | } |
190 | } |
|
178 | |
191 | #ifdef DEBUG |
|
179 | Serial.println("Connection to MQTT broker failed with MQTT client state: " + String(mqttClient.state())); |
192 | Serial.println("Connection to MQTT broker failed with MQTT client state: " + String(mqttClient.state())); |
|
180 | |
193 | #endif |
|
181 | return false; |
194 | return false; |
|
182 | } |
195 | } |
|
183 | |
196 | |
|
184 | bool loopWifiConnected() { |
197 | bool loopWifiConnected() { |
|
185 | // Process OTA loop first since emergency OTA updates might be needed. |
198 | // Process OTA loop first since emergency OTA updates might be needed. |
|
186 | ArduinoOTA.handle(); |
199 | ArduinoOTA.handle(); |
|
187 | |
200 | |
|
188 | // Process MQTT client loop. |
201 | // Process MQTT client loop. |
|
189 | if (!mqttClient.connected()) { |
202 | if (!mqttClient.connected()) { |
|
190 | // If the connection to the MQTT broker has failed then sleep before carrying on. |
203 | // If the connection to the MQTT broker has failed then sleep before carrying on. |
|
191 | if (!mqttConnect()) { |
204 | if (!mqttConnect()) { |
|
192 | return false; |
205 | return false; |
|
193 | } |
206 | } |
|
194 | } |
207 | } |
|
195 | mqttClient.loop(); |
208 | mqttClient.loop(); |
|
196 | |
209 | |
|
197 | return true; |
210 | return true; |
|
198 | } |
211 | } |
|
199 | |
212 | |
|
200 | void setup() { |
213 | void setup() { |
|
201 | Serial.begin(115200); |
214 | Serial.begin(115200); |
|
- | 215 | #ifdef DEBUG |
||
202 | Serial.println("Booted, setting up Wifi in 10s..."); |
216 | Serial.println("Booted, setting up Wifi in 10s..."); |
|
- | 217 | #endif |
||
203 | delay(10000); |
218 | delay(10000); |
|
204 | |
219 | |
|
205 | WiFi.mode(WIFI_STA); |
220 | WiFi.mode(WIFI_STA); |
|
206 | #if defined(ARDUINO_ARCH_ESP8266) |
221 | #if defined(ARDUINO_ARCH_ESP8266) |
|
207 | WiFi.hostname(HOSTNAME().c_str()); |
222 | WiFi.hostname(HOSTNAME().c_str()); |
|
208 | #elif defined(ARDUINO_ARCH_ESP32) |
223 | #elif defined(ARDUINO_ARCH_ESP32) |
|
209 | WiFi.setHostname(HOSTNAME().c_str()); |
224 | WiFi.setHostname(HOSTNAME().c_str()); |
|
210 | #endif |
225 | #endif |
|
211 | WiFi.begin(sta_ssid, sta_psk); |
226 | WiFi.begin(STA_SSID, STA_PSK); |
|
212 | while (WiFi.waitForConnectResult() != WL_CONNECTED) { |
227 | while (WiFi.waitForConnectResult() != WL_CONNECTED) { |
|
- | 228 | #ifdef DEBUG |
||
213 | Serial.println("Failed to connect to Wifi, rebooting in 5s..."); |
229 | Serial.println("Failed to connect to Wifi, rebooting in 5s..."); |
|
- | 230 | #endif |
||
214 | delay(5000); |
231 | delay(5000); |
|
215 | ESP.restart(); |
232 | ESP.restart(); |
|
216 | } |
233 | } |
|
217 | |
234 | #ifdef DEBUG |
|
218 | Serial.print("Connected to Wifi: "); |
235 | Serial.print("Connected to Wifi: "); |
|
- | 236 | #endif |
||
219 | Serial.println(WiFi.localIP()); |
237 | Serial.println(WiFi.localIP()); |
|
220 | |
238 | #ifdef DEBUG |
|
221 | Serial.println("Setting up OTA in 10s..."); |
239 | Serial.println("Setting up OTA in 10s..."); |
|
- | 240 | #endif |
||
222 | delay(10000); |
241 | delay(10000); |
|
223 | |
242 | |
|
224 | // Port defaults to 8266 |
243 | // Port defaults to 8266 |
|
225 | ArduinoOTA.setPort(ota_port); |
244 | ArduinoOTA.setPort(OTA_PORT); |
|
226 | |
245 | |
|
227 | // Hostname defaults to esp-[ChipID] |
246 | // Hostname defaults to esp-[ChipID] |
|
228 | ArduinoOTA.setHostname(HOSTNAME().c_str()); |
247 | ArduinoOTA.setHostname(HOSTNAME().c_str()); |
|
229 | |
248 | |
|
230 | // Set the OTA password |
249 | // Set the OTA password |
|
231 | ArduinoOTA.setPassword(ota_password); |
250 | ArduinoOTA.setPassword(OTA_PASSWORD); |
|
232 | |
251 | |
|
233 | ArduinoOTA.onStart([]() { |
252 | ArduinoOTA.onStart([]() { |
|
234 | switch (ArduinoOTA.getCommand()) { |
253 | switch (ArduinoOTA.getCommand()) { |
|
235 | case U_FLASH: // Sketch |
254 | case U_FLASH: // Sketch |
|
- | 255 | #ifdef DEBUG |
||
236 | Serial.println("OTA start updating sketch."); |
256 | Serial.println("OTA start updating sketch."); |
|
- | 257 | #endif |
||
237 | break; |
258 | break; |
|
238 | #if defined(ARDUINO_ARCH_ESP8266) |
259 | #if defined(ARDUINO_ARCH_ESP8266) |
|
239 | case U_FS: |
260 | case U_FS: |
|
240 | #elif defined(ARDUINO_ARCH_ESP32) |
261 | #elif defined(ARDUINO_ARCH_ESP32) |
|
241 | case U_SPIFFS: |
262 | case U_SPIFFS: |
|
242 | #endif |
263 | #endif |
|
- | 264 | #ifdef DEBUG |
||
243 | Serial.println("OTA start updating filesystem."); |
265 | Serial.println("OTA start updating filesystem."); |
|
- | 266 | #endif |
||
244 | SPIFFS.end(); |
267 | SPIFFS.end(); |
|
245 | break; |
268 | break; |
|
246 | default: |
269 | default: |
|
- | 270 | #ifdef DEBUG |
||
247 | Serial.println("Unknown OTA update type."); |
271 | Serial.println("Unknown OTA update type."); |
|
- | 272 | #endif |
||
248 | break; |
273 | break; |
|
249 | } |
274 | } |
|
250 | }); |
275 | }); |
|
251 | ArduinoOTA.onEnd([]() { |
276 | ArduinoOTA.onEnd([]() { |
|
- | 277 | #ifdef DEBUG |
||
252 | Serial.println("OTA update complete."); |
278 | Serial.println("OTA update complete."); |
|
- | 279 | #endif |
||
253 | SPIFFS.begin(); |
280 | SPIFFS.begin(); |
|
254 | #if defined(ARDUINO_ARCH_ESP8266) |
281 | #if defined(ARDUINO_ARCH_ESP8266) |
|
255 | // For what it's worth, check the filesystem on ESP8266. |
282 | // For what it's worth, check the filesystem on ESP8266. |
|
256 | SPIFFS.check(); |
283 | SPIFFS.check(); |
|
257 | #endif |
284 | #endif |
|
258 | ESP.restart(); |
285 | ESP.restart(); |
|
259 | }); |
286 | }); |
|
260 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { |
287 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { |
|
- | 288 | #ifdef DEBUG |
||
261 | Serial.printf("OTA update progress: %u%%\r", (progress / (total / 100))); |
289 | Serial.printf("OTA update progress: %u%%\r", (progress / (total / 100))); |
|
- | 290 | #endif |
||
262 | }); |
291 | }); |
|
263 | ArduinoOTA.onError([](ota_error_t error) { |
292 | ArduinoOTA.onError([](ota_error_t error) { |
|
- | 293 | #ifdef DEBUG |
||
264 | Serial.printf("OTA update error [%u]: ", error); |
294 | Serial.printf("OTA update error [%u]: ", error); |
|
- | 295 | #endif |
||
265 | switch (error) { |
296 | switch (error) { |
|
266 | case OTA_AUTH_ERROR: |
297 | case OTA_AUTH_ERROR: |
|
- | 298 | #ifdef DEBUG |
||
267 | Serial.println("OTA authentication failed"); |
299 | Serial.println("OTA authentication failed"); |
|
- | 300 | #endif |
||
268 | break; |
301 | break; |
|
269 | case OTA_BEGIN_ERROR: |
302 | case OTA_BEGIN_ERROR: |
|
- | 303 | #ifdef DEBUG |
||
270 | Serial.println("OTA begin failed"); |
304 | Serial.println("OTA begin failed"); |
|
- | 305 | #endif |
||
271 | break; |
306 | break; |
|
272 | case OTA_CONNECT_ERROR: |
307 | case OTA_CONNECT_ERROR: |
|
- | 308 | #ifdef DEBUG |
||
273 | Serial.println("OTA connect failed"); |
309 | Serial.println("OTA connect failed"); |
|
- | 310 | #endif |
||
274 | break; |
311 | break; |
|
275 | case OTA_RECEIVE_ERROR: |
312 | case OTA_RECEIVE_ERROR: |
|
- | 313 | #ifdef DEBUG |
||
276 | Serial.println("OTA receive failed"); |
314 | Serial.println("OTA receive failed"); |
|
- | 315 | #endif |
||
277 | break; |
316 | break; |
|
278 | case OTA_END_ERROR: |
317 | case OTA_END_ERROR: |
|
- | 318 | #ifdef DEBUG |
||
279 | Serial.println("OTA end failed"); |
319 | Serial.println("OTA end failed"); |
|
- | 320 | #endif |
||
280 | break; |
321 | break; |
|
281 | default: |
322 | default: |
|
- | 323 | #ifdef DEBUG |
||
282 | Serial.println("Unknown OTA failure"); |
324 | Serial.println("Unknown OTA failure"); |
|
- | 325 | #endif |
||
283 | break; |
326 | break; |
|
284 | } |
327 | } |
|
285 | ESP.restart(); |
328 | ESP.restart(); |
|
286 | }); |
329 | }); |
|
287 | ArduinoOTA.begin(); |
330 | ArduinoOTA.begin(); |
|
288 | |
331 | |
|
289 | // Set up MQTT client. |
332 | // Set up MQTT client. |
|
290 | mqttClient.setServer(mqtt_host, mqtt_port); |
333 | mqttClient.setServer(MQTT_HOST, MQTT_PORT); |
|
291 | mqttClient.setCallback(mqttCallback); |
334 | mqttClient.setCallback(mqttCallback); |
|
292 | |
335 | |
|
293 | // Touchdown. |
336 | // Touchdown. |
|
- | 337 | #ifdef DEBUG |
||
294 | Serial.println("Setup complete."); |
338 | Serial.println("Setup complete."); |
|
- | 339 | #endif |
||
295 | } |
340 | } |
|
296 | |
341 | |
|
297 | void loop() { |
342 | void loop() { |
|
298 | // Check the Wifi connection status. |
343 | // Check the Wifi connection status. |
|
299 | int wifiStatus = WiFi.status(); |
344 | int wifiStatus = WiFi.status(); |
|
300 | switch (wifiStatus) { |
345 | switch (wifiStatus) { |
|
301 | case WL_CONNECTED: |
346 | case WL_CONNECTED: |
|
302 | if (!loopWifiConnected()) { |
347 | if (!loopWifiConnected()) { |
|
303 | delay(1000); |
348 | delay(1000); |
|
304 | break; |
349 | break; |
|
305 | } |
350 | } |
|
306 | delay(1); |
351 | delay(1); |
|
307 | break; |
352 | break; |
|
308 | case WL_NO_SHIELD: |
353 | case WL_NO_SHIELD: |
|
- | 354 | #ifdef DEBUG |
||
309 | Serial.println("No Wifi shield present."); |
355 | Serial.println("No Wifi shield present."); |
|
- | 356 | #endif |
||
310 | goto DEFAULT_CASE; |
357 | goto DEFAULT_CASE; |
|
311 | break; |
358 | break; |
|
312 | case WL_NO_SSID_AVAIL: |
359 | case WL_NO_SSID_AVAIL: |
|
- | 360 | #ifdef DEBUG |
||
313 | Serial.println("Configured SSID not found."); |
361 | Serial.println("Configured SSID not found."); |
|
- | 362 | #endif |
||
314 | goto DEFAULT_CASE; |
363 | goto DEFAULT_CASE; |
|
315 | break; |
364 | break; |
|
316 | // Temporary statuses indicating transitional states. |
365 | // Temporary statuses indicating transitional states. |
|
317 | case WL_IDLE_STATUS: |
366 | case WL_IDLE_STATUS: |
|
318 | case WL_SCAN_COMPLETED: |
367 | case WL_SCAN_COMPLETED: |
|
319 | delay(1000); |
368 | delay(1000); |
|
320 | break; |
369 | break; |
|
321 | // Fatal Wifi statuses trigger a delayed ESP restart. |
370 | // Fatal Wifi statuses trigger a delayed ESP restart. |
|
322 | case WL_CONNECT_FAILED: |
371 | case WL_CONNECT_FAILED: |
|
323 | case WL_CONNECTION_LOST: |
372 | case WL_CONNECTION_LOST: |
|
324 | case WL_DISCONNECTED: |
373 | case WL_DISCONNECTED: |
|
325 | default: |
374 | default: |
|
- | 375 | #ifdef DEBUG |
||
326 | Serial.println("Wifi connection failed with status: " + String(wifiStatus)); |
376 | Serial.println("Wifi connection failed with status: " + String(wifiStatus)); |
|
- | 377 | #endif |
||
327 | DEFAULT_CASE: |
378 | DEFAULT_CASE: |
|
328 | delay(10000); |
379 | delay(10000); |
|
329 | ESP.restart(); |
380 | ESP.restart(); |
|
330 | break; |
381 | break; |
|
331 | } |
382 | } |
|
332 | } |
383 | } |
|
333 | |
384 | |
|
334 |
|
385 |
|
|
335 | |
386 | |
|
336 | |
387 | |
|
337 | |
388 | |