diff options
| author | manuel <manuel@mausz.at> | 2020-03-01 23:58:12 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-03-01 23:58:12 +0100 |
| commit | 7b92ff758d09fd6a91a8dcc8859534c88b6f7883 (patch) | |
| tree | 13e13b51c154db0c38431aa528d9d326c38595fa /oliver/dr_desk/src/dr_desk.cpp | |
| parent | e60bc43eb30ab72ec9fdfae581058bf8acfd8586 (diff) | |
| download | arduino-7b92ff758d09fd6a91a8dcc8859534c88b6f7883.tar.gz arduino-7b92ff758d09fd6a91a8dcc8859534c88b6f7883.tar.bz2 arduino-7b92ff758d09fd6a91a8dcc8859534c88b6f7883.zip | |
Migrate to HTTPUpdater
Diffstat (limited to 'oliver/dr_desk/src/dr_desk.cpp')
| -rw-r--r-- | oliver/dr_desk/src/dr_desk.cpp | 89 |
1 files changed, 59 insertions, 30 deletions
diff --git a/oliver/dr_desk/src/dr_desk.cpp b/oliver/dr_desk/src/dr_desk.cpp index 46bf8eb..6f31b76 100644 --- a/oliver/dr_desk/src/dr_desk.cpp +++ b/oliver/dr_desk/src/dr_desk.cpp | |||
| @@ -1,15 +1,16 @@ | |||
| 1 | #include <Arduino.h> | 1 | #include <Arduino.h> |
| 2 | #include <ESP8266mDNS.h> | 2 | #include <ESP8266mDNS.h> |
| 3 | #include <ArduinoOTA.h> | ||
| 4 | #include <PubSubClient.h> | 3 | #include <PubSubClient.h> |
| 5 | #include <WiFiManager.h> | 4 | #include <WiFiManager.h> |
| 6 | #include <FastLED.h> | 5 | #include <FastLED.h> |
| 6 | #include <ESP8266httpUpdate.h> | ||
| 7 | #include "secrets.h" | 7 | #include "secrets.h" |
| 8 | 8 | ||
| 9 | const char* autoconf_ssid = AUTOCONF_SSID; //AP name for WiFi setup AP which your ESP will open when not able to connect to other WiFi | 9 | #define _STR(s) #s |
| 10 | const char* autoconf_pwd = AUTOCONF_PASSWORD; // AP password so noone else can connect to the ESP in case your router fails | 10 | #define STR(s) _STR(s) |
| 11 | const char* mqtt_server = "192.168.1.2"; //MQTT Server IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT | 11 | |
| 12 | const int mqtt_port = 1883; //MQTT Server PORT, default is 1883 but can be anything. | 12 | const char* mqtt_server = "192.168.1.2"; //MQTT Server IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT |
| 13 | const int mqtt_port = 1883; //MQTT Server PORT, default is 1883 but can be anything. | ||
| 13 | 14 | ||
| 14 | const char* mqtt_pingall_sub = "home/pingall"; | 15 | const char* mqtt_pingall_sub = "home/pingall"; |
| 15 | const char* mqtt_pingall_pub = "home/pingall/response"; | 16 | const char* mqtt_pingall_pub = "home/pingall/response"; |
| @@ -32,11 +33,18 @@ const char* mqtt_brightness_pub = MQTT_BASE "/brightness/status"; | |||
| 32 | #define RGB_COLOR_ORDER GRB | 33 | #define RGB_COLOR_ORDER GRB |
| 33 | #define NUM(a) (sizeof(a) / sizeof(*a)) | 34 | #define NUM(a) (sizeof(a) / sizeof(*a)) |
| 34 | 35 | ||
| 36 | #define FIRMWARE_VERSION 1 | ||
| 37 | //#define FIRMWARE_URL "" | ||
| 38 | |||
| 35 | WiFiClient espClient; | 39 | WiFiClient espClient; |
| 36 | PubSubClient client(espClient); | 40 | PubSubClient client(espClient); |
| 37 | char convBuffer[10]; | 41 | char convBuffer[10]; |
| 38 | CRGB leds[RGB_NUM_LEDS]; | 42 | CRGB leds[RGB_NUM_LEDS]; |
| 39 | 43 | ||
| 44 | void callback(char *topic, uint8_t *payload, unsigned int length); | ||
| 45 | void blink(); | ||
| 46 | void checkFirmwareUpdate(); | ||
| 47 | |||
| 40 | void switchMode(struct mode *mode); | 48 | void switchMode(struct mode *mode); |
| 41 | void modeOff(); | 49 | void modeOff(); |
| 42 | void modeSolid(); | 50 | void modeSolid(); |
| @@ -73,7 +81,7 @@ struct | |||
| 73 | void setup() | 81 | void setup() |
| 74 | { | 82 | { |
| 75 | Serial.begin(115200); | 83 | Serial.begin(115200); |
| 76 | pinMode(BUILTIN_LED, OUTPUT); | 84 | pinMode(LED_BUILTIN, OUTPUT); |
| 77 | 85 | ||
| 78 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, RGB_NUM_LEDS); | 86 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, RGB_NUM_LEDS); |
| 79 | current.brightness = set.brightness; | 87 | current.brightness = set.brightness; |
| @@ -82,32 +90,22 @@ void setup() | |||
| 82 | FastLED.show(); | 90 | FastLED.show(); |
| 83 | 91 | ||
| 84 | WiFiManager wifiManager; | 92 | WiFiManager wifiManager; |
| 85 | wifiManager.autoConnect(autoconf_ssid, autoconf_pwd); | 93 | wifiManager.setConfigPortalTimeout(600); |
| 94 | Serial.printf_P(PSTR("Setting up WiFi\n")); | ||
| 95 | if (!wifiManager.autoConnect("ESP8266_DR_DESK")) { | ||
| 96 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); | ||
| 97 | delay(5000); | ||
| 98 | ESP.restart(); | ||
| 99 | } | ||
| 86 | 100 | ||
| 87 | setup_ota(); | 101 | yield(); |
| 102 | checkFirmwareUpdate(); | ||
| 103 | yield(); | ||
| 88 | 104 | ||
| 89 | client.setServer(mqtt_server, mqtt_port); | 105 | client.setServer(mqtt_server, mqtt_port); |
| 90 | client.setCallback(callback); | 106 | client.setCallback(callback); |
| 91 | 107 | ||
| 92 | digitalWrite(BUILTIN_LED, HIGH); //Turn off led as default | 108 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default |
| 93 | } | ||
| 94 | |||
| 95 | void setup_ota() | ||
| 96 | { | ||
| 97 | // Set OTA Password, and change it in platformio.ini | ||
| 98 | ArduinoOTA.setPassword(OTA_PASSWORD); | ||
| 99 | ArduinoOTA.onStart([]() {}); | ||
| 100 | ArduinoOTA.onEnd([]() {}); | ||
| 101 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {}); | ||
| 102 | ArduinoOTA.onError([](ota_error_t error) | ||
| 103 | { | ||
| 104 | if (error == OTA_AUTH_ERROR); // Auth failed | ||
| 105 | else if (error == OTA_BEGIN_ERROR); // Begin failed | ||
| 106 | else if (error == OTA_CONNECT_ERROR); // Connect failed | ||
| 107 | else if (error == OTA_RECEIVE_ERROR); // Receive failed | ||
| 108 | else if (error == OTA_END_ERROR); // End failed | ||
| 109 | }); | ||
| 110 | ArduinoOTA.begin(); | ||
| 111 | } | 109 | } |
| 112 | 110 | ||
| 113 | void reconnect() | 111 | void reconnect() |
| @@ -195,9 +193,9 @@ void callback(char *topic, uint8_t *payload, unsigned int length) | |||
| 195 | void blink() | 193 | void blink() |
| 196 | { | 194 | { |
| 197 | //Blink on received MQTT message | 195 | //Blink on received MQTT message |
| 198 | digitalWrite(BUILTIN_LED, LOW); | 196 | digitalWrite(LED_BUILTIN, LOW); |
| 199 | delay(25); | 197 | delay(25); |
| 200 | digitalWrite(BUILTIN_LED, HIGH); | 198 | digitalWrite(LED_BUILTIN, HIGH); |
| 201 | } | 199 | } |
| 202 | 200 | ||
| 203 | void calcBrightness() | 201 | void calcBrightness() |
| @@ -267,12 +265,43 @@ void modeStrobo() | |||
| 267 | state = !state; | 265 | state = !state; |
| 268 | } | 266 | } |
| 269 | 267 | ||
| 268 | void checkFirmwareUpdate() | ||
| 269 | { | ||
| 270 | BearSSL::WiFiClientSecure update_client; | ||
| 271 | update_client.setInsecure(); | ||
| 272 | |||
| 273 | client.publish(mqtt_device_boot, "fwupdate running"); | ||
| 274 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | ||
| 275 | ESPhttpUpdate.rebootOnUpdate(true); | ||
| 276 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | ||
| 277 | switch(ret) | ||
| 278 | { | ||
| 279 | case HTTP_UPDATE_FAILED: | ||
| 280 | { | ||
| 281 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | ||
| 282 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | ||
| 283 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | ||
| 284 | client.publish(mqtt_device_boot, tmp.c_str()); | ||
| 285 | } | ||
| 286 | break; | ||
| 287 | |||
| 288 | case HTTP_UPDATE_NO_UPDATES: | ||
| 289 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | ||
| 290 | client.publish(mqtt_device_boot, "fwupdate noupdates"); | ||
| 291 | break; | ||
| 292 | |||
| 293 | case HTTP_UPDATE_OK: | ||
| 294 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | ||
| 295 | client.publish(mqtt_device_boot, "fwupdate ok"); | ||
| 296 | break; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 270 | void loop() | 300 | void loop() |
| 271 | { | 301 | { |
| 272 | if (!client.connected()) | 302 | if (!client.connected()) |
| 273 | reconnect(); | 303 | reconnect(); |
| 274 | client.loop(); | 304 | client.loop(); |
| 275 | ArduinoOTA.handle(); | ||
| 276 | 305 | ||
| 277 | EVERY_N_MILLISECONDS(100) | 306 | EVERY_N_MILLISECONDS(100) |
| 278 | { | 307 | { |
