arduino-sketches – Diff between revs 9 and 14
?pathlinks?
Rev 9 | Rev 14 | |||
---|---|---|---|---|
Line 52... | Line 52... | |||
52 | #if defined(ARDUINO_ARCH_ESP32) |
52 | #if defined(ARDUINO_ARCH_ESP32) |
|
53 | #include <FS.h> |
53 | #include <FS.h> |
|
54 | #include <SPIFFS.h> |
54 | #include <SPIFFS.h> |
|
55 | #endif |
55 | #endif |
|
Line 56... | Line -... | |||
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 | |
56 | |
|
66 | WiFiClient espClient; |
57 | WiFiClient espClient; |
|
Line 67... | Line 58... | |||
67 | PubSubClient mqttClient(espClient); |
58 | PubSubClient mqttClient(espClient); |
|
68 | |
59 | |
|
69 | // Define GPIO pins for supported architectures. |
60 | // Define GPIO pins for supported architectures. |
|
70 | #if defined(ARDUINO_ARCH_ESP8266) |
61 | #if defined(ARDUINO_ARCH_ESP8266) |
|
71 | int PINS[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8 }; |
62 | int PINS[] = { D0, D1, D2, D3, D4, D5, D6, D7, D8 }; |
|
72 | int ANAL[] = { A0 }; |
63 | int ANAL[] = { A0 }; |
|
73 | #elif defined(ARDUINO_ARCH_ESP32) |
64 | #elif defined(ARDUINO_ARCH_ESP32) |
|
74 | int PINS[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
65 | int PINS[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
|
75 | 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, |
66 | 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
|
76 | 23, 25, 26, 27, 32, 33, 34, 35, 36, 37, |
- | ||
77 | 38, 39 |
67 | 22, 23, 24, 25, 26, 27, 28, 19, 30, 31, |
|
78 | }; |
68 | 32, 33, 34, 35, 36, 37, 38, 39 }; |
|
Line 79... | Line 69... | |||
79 | int ANAL[] = { 36, 39, 34, 35 }; |
69 | int ANAL[] = { 36, 39, 34, 35 }; |
|
80 | #endif |
70 | #endif |
|
Line 172... | Line 162... | |||
172 | return; |
162 | return; |
|
173 | } |
163 | } |
|
174 | } |
164 | } |
|
Line 175... | Line 165... | |||
175 | |
165 | |
|
176 | bool mqttConnect() { |
166 | bool mqttConnect() { |
|
177 | Serial.println("Attempting to connect to MQTT broker: " + String(mqtt_host)); |
167 | Serial.println("Attempting to connect to MQTT broker: " + String(MQTT_HOST)); |
|
Line 178... | Line 168... | |||
178 | mqttClient.setServer(mqtt_host, mqtt_port); |
168 | mqttClient.setServer(MQTT_HOST, MQTT_PORT); |
|
179 | |
169 | |
|
180 | StaticJsonDocument<256> msg; |
170 | StaticJsonDocument<256> msg; |
|
181 | if (mqttClient.connect(MQTT_CLIENT_ID().c_str(), mqtt_username, mqtt_password)) { |
171 | if (mqttClient.connect(MQTT_CLIENT_ID().c_str(), MQTT_USERNAME, MQTT_PASSWORD)) { |
|
182 | Serial.println("Established connection with MQTT broker using client ID: " + MQTT_CLIENT_ID()); |
172 | Serial.println("Established connection with MQTT broker using client ID: " + MQTT_CLIENT_ID()); |
|
183 | mqttClient.setCallback(mqttCallback); |
173 | mqttClient.setCallback(mqttCallback); |
|
184 | msg["action"] = "connected"; |
174 | msg["action"] = "connected"; |
|
Line 225... | Line 215... | |||
225 | #if defined(ARDUINO_ARCH_ESP8266) |
215 | #if defined(ARDUINO_ARCH_ESP8266) |
|
226 | WiFi.hostname(HOSTNAME().c_str()); |
216 | WiFi.hostname(HOSTNAME().c_str()); |
|
227 | #elif defined(ARDUINO_ARCH_ESP32) |
217 | #elif defined(ARDUINO_ARCH_ESP32) |
|
228 | WiFi.setHostname(HOSTNAME().c_str()); |
218 | WiFi.setHostname(HOSTNAME().c_str()); |
|
229 | #endif |
219 | #endif |
|
230 | WiFi.begin(sta_ssid, sta_psk); |
220 | WiFi.begin(STA_SSID, STA_PSK); |
|
231 | while (WiFi.waitForConnectResult() != WL_CONNECTED) { |
221 | while (WiFi.waitForConnectResult() != WL_CONNECTED) { |
|
232 | Serial.println("Failed to connect to Wifi, rebooting in 5s..."); |
222 | Serial.println("Failed to connect to Wifi, rebooting in 5s..."); |
|
233 | delay(5000); |
223 | delay(5000); |
|
234 | ESP.restart(); |
224 | ESP.restart(); |
|
235 | } |
225 | } |
|
Line 239... | Line 229... | |||
239 | |
229 | |
|
240 | Serial.println("Setting up OTA in 10s..."); |
230 | Serial.println("Setting up OTA in 10s..."); |
|
Line 241... | Line 231... | |||
241 | delay(10000); |
231 | delay(10000); |
|
242 | |
232 | |
|
Line 243... | Line 233... | |||
243 | // Port defaults to 8266 |
233 | // Port defaults to 8266 |
|
244 | ArduinoOTA.setPort(ota_port); |
234 | ArduinoOTA.setPort(OTA_PORT); |
|
Line 245... | Line 235... | |||
245 | |
235 | |
|
246 | // Hostname defaults to esp-[ChipID] |
236 | // Hostname defaults to esp-[ChipID] |
|
Line 247... | Line 237... | |||
247 | ArduinoOTA.setHostname(HOSTNAME().c_str()); |
237 | ArduinoOTA.setHostname(HOSTNAME().c_str()); |
|
248 | |
238 | |
|
249 | // Set the OTA password |
239 | // Set the OTA password |
|
250 | ArduinoOTA.setPassword(ota_password); |
240 | ArduinoOTA.setPassword(OTA_PASSWORD); |
|
Line 304... | Line 294... | |||
304 | ESP.restart(); |
294 | ESP.restart(); |
|
305 | }); |
295 | }); |
|
306 | ArduinoOTA.begin(); |
296 | ArduinoOTA.begin(); |
|
Line 307... | Line 297... | |||
307 | |
297 | |
|
308 | // Set up MQTT client. |
298 | // Set up MQTT client. |
|
309 | mqttClient.setServer(mqtt_host, mqtt_port); |
299 | mqttClient.setServer(MQTT_HOST, MQTT_PORT); |
|
Line 310... | Line 300... | |||
310 | mqttClient.setCallback(mqttCallback); |
300 | mqttClient.setCallback(mqttCallback); |
|
311 | |
301 | |
|
312 | // Touchdown. |
302 | // Touchdown. |