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/lr_stripes/src | |
| parent | e60bc43eb30ab72ec9fdfae581058bf8acfd8586 (diff) | |
| download | arduino-7b92ff758d09fd6a91a8dcc8859534c88b6f7883.tar.gz arduino-7b92ff758d09fd6a91a8dcc8859534c88b6f7883.tar.bz2 arduino-7b92ff758d09fd6a91a8dcc8859534c88b6f7883.zip | |
Migrate to HTTPUpdater
Diffstat (limited to 'oliver/lr_stripes/src')
| -rw-r--r-- | oliver/lr_stripes/src/lr_stripes.cpp | 85 |
1 files changed, 55 insertions, 30 deletions
diff --git a/oliver/lr_stripes/src/lr_stripes.cpp b/oliver/lr_stripes/src/lr_stripes.cpp index 7059f8b..6ad4282 100644 --- a/oliver/lr_stripes/src/lr_stripes.cpp +++ b/oliver/lr_stripes/src/lr_stripes.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 <Bounce2.h> | 5 | #include <Bounce2.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"; |
| @@ -81,17 +82,20 @@ WiFiClient espClient; | |||
| 81 | PubSubClient client(espClient); | 82 | PubSubClient client(espClient); |
| 82 | char convBuffer[10]; | 83 | char convBuffer[10]; |
| 83 | 84 | ||
| 85 | void callback(char* topic, byte* payload, unsigned int length); | ||
| 86 | void blink(); | ||
| 84 | bool relayRead(struct sensor_t *sensor); | 87 | bool relayRead(struct sensor_t *sensor); |
| 85 | void relayWrite(struct sensor_t *sensor, bool state, bool send_update=false); | 88 | void relayWrite(struct sensor_t *sensor, bool state, bool send_update=false); |
| 86 | void flipRelay(struct sensor_t *sensor, bool send_update=false); | 89 | void flipRelay(struct sensor_t *sensor, bool send_update=false); |
| 87 | void checkButtons(void); | 90 | void checkButtons(void); |
| 88 | inline uint8_t pwmValue(uint8_t level); | 91 | inline uint8_t pwmValue(uint8_t level); |
| 89 | void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update=false); | 92 | void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update=false); |
| 93 | void checkFirmwareUpdate(); | ||
| 90 | 94 | ||
| 91 | void setup() | 95 | void setup() |
| 92 | { | 96 | { |
| 93 | Serial.begin(115200); | 97 | Serial.begin(115200); |
| 94 | pinMode(BUILTIN_LED, OUTPUT); | 98 | pinMode(LED_BUILTIN, OUTPUT); |
| 95 | analogWriteRange(100); | 99 | analogWriteRange(100); |
| 96 | 100 | ||
| 97 | // set relay pins to output mode + restore to last known state | 101 | // set relay pins to output mode + restore to last known state |
| @@ -129,32 +133,22 @@ void setup() | |||
| 129 | } | 133 | } |
| 130 | 134 | ||
| 131 | WiFiManager wifiManager; | 135 | WiFiManager wifiManager; |
| 132 | wifiManager.autoConnect(autoconf_ssid, autoconf_pwd); | 136 | wifiManager.setConfigPortalTimeout(600); |
| 137 | Serial.printf_P(PSTR("Setting up WiFi\n")); | ||
| 138 | if (!wifiManager.autoConnect("ESP8266_LR_STRIPES")) { | ||
| 139 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); | ||
| 140 | delay(5000); | ||
| 141 | ESP.restart(); | ||
| 142 | } | ||
| 133 | 143 | ||
| 134 | setup_ota(); | 144 | yield(); |
| 145 | checkFirmwareUpdate(); | ||
| 146 | yield(); | ||
| 135 | 147 | ||
| 136 | client.setServer(mqtt_server, mqtt_port); | 148 | client.setServer(mqtt_server, mqtt_port); |
| 137 | client.setCallback(callback); | 149 | client.setCallback(callback); |
| 138 | 150 | ||
| 139 | digitalWrite(BUILTIN_LED, HIGH); //Turn off led as default | 151 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default |
| 140 | } | ||
| 141 | |||
| 142 | void setup_ota() | ||
| 143 | { | ||
| 144 | // Set OTA Password, and change it in platformio.ini | ||
| 145 | ArduinoOTA.setPassword(OTA_PASSWORD); | ||
| 146 | ArduinoOTA.onStart([]() {}); | ||
| 147 | ArduinoOTA.onEnd([]() {}); | ||
| 148 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {}); | ||
| 149 | ArduinoOTA.onError([](ota_error_t error) | ||
| 150 | { | ||
| 151 | if (error == OTA_AUTH_ERROR); // Auth failed | ||
| 152 | else if (error == OTA_BEGIN_ERROR); // Begin failed | ||
| 153 | else if (error == OTA_CONNECT_ERROR); // Connect failed | ||
| 154 | else if (error == OTA_RECEIVE_ERROR); // Receive failed | ||
| 155 | else if (error == OTA_END_ERROR); // End failed | ||
| 156 | }); | ||
| 157 | ArduinoOTA.begin(); | ||
| 158 | } | 152 | } |
| 159 | 153 | ||
| 160 | void reconnect() | 154 | void reconnect() |
| @@ -244,9 +238,9 @@ void callback(char* topic, byte* payload, unsigned int length) | |||
| 244 | void blink() | 238 | void blink() |
| 245 | { | 239 | { |
| 246 | //Blink on received MQTT message | 240 | //Blink on received MQTT message |
| 247 | digitalWrite(BUILTIN_LED, LOW); | 241 | digitalWrite(LED_BUILTIN, LOW); |
| 248 | delay(25); | 242 | delay(25); |
| 249 | digitalWrite(BUILTIN_LED, HIGH); | 243 | digitalWrite(LED_BUILTIN, HIGH); |
| 250 | } | 244 | } |
| 251 | 245 | ||
| 252 | void loop() | 246 | void loop() |
| @@ -254,7 +248,6 @@ void loop() | |||
| 254 | if (!client.connected()) | 248 | if (!client.connected()) |
| 255 | reconnect(); | 249 | reconnect(); |
| 256 | client.loop(); | 250 | client.loop(); |
| 257 | ArduinoOTA.handle(); | ||
| 258 | checkButtons(); | 251 | checkButtons(); |
| 259 | } | 252 | } |
| 260 | 253 | ||
| @@ -367,3 +360,35 @@ void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update) | |||
| 367 | client.publish(sensor->dimmer.mqtt_pub, convBuffer, true); | 360 | client.publish(sensor->dimmer.mqtt_pub, convBuffer, true); |
| 368 | } | 361 | } |
| 369 | } | 362 | } |
| 363 | |||
| 364 | void checkFirmwareUpdate() | ||
| 365 | { | ||
| 366 | BearSSL::WiFiClientSecure update_client; | ||
| 367 | update_client.setInsecure(); | ||
| 368 | |||
| 369 | client.publish(mqtt_device_boot, "fwupdate running"); | ||
| 370 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | ||
| 371 | ESPhttpUpdate.rebootOnUpdate(true); | ||
| 372 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | ||
| 373 | switch(ret) | ||
| 374 | { | ||
| 375 | case HTTP_UPDATE_FAILED: | ||
| 376 | { | ||
| 377 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | ||
| 378 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | ||
| 379 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | ||
| 380 | client.publish(mqtt_device_boot, tmp.c_str()); | ||
| 381 | } | ||
| 382 | break; | ||
| 383 | |||
| 384 | case HTTP_UPDATE_NO_UPDATES: | ||
| 385 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | ||
| 386 | client.publish(mqtt_device_boot, "fwupdate noupdates"); | ||
| 387 | break; | ||
| 388 | |||
| 389 | case HTTP_UPDATE_OK: | ||
| 390 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | ||
| 391 | client.publish(mqtt_device_boot, "fwupdate ok"); | ||
| 392 | break; | ||
| 393 | } | ||
| 394 | } | ||
