diff options
| -rw-r--r-- | linz/oekofen_thermo/platformio.ini | 1 | ||||
| -rw-r--r-- | linz/oekofen_thermo/src/main.cpp | 158 | ||||
| -rw-r--r-- | oliver/dr_desk/platformio.ini | 1 | ||||
| -rw-r--r-- | oliver/dr_desk/src/main.cpp | 172 | ||||
| -rw-r--r-- | oliver/lr_stripes/platformio.ini | 1 | ||||
| -rw-r--r-- | oliver/lr_stripes/src/main.cpp | 208 |
6 files changed, 349 insertions, 192 deletions
diff --git a/linz/oekofen_thermo/platformio.ini b/linz/oekofen_thermo/platformio.ini index 2c7b00e..625bc4f 100644 --- a/linz/oekofen_thermo/platformio.ini +++ b/linz/oekofen_thermo/platformio.ini | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | [env:d1_mini] | 11 | [env:d1_mini] |
| 12 | platform = espressif8266 | 12 | platform = espressif8266 |
| 13 | board = d1_mini | 13 | board = d1_mini |
| 14 | lib_deps = WiFiManager, PubSubClient | ||
| 14 | framework = arduino | 15 | framework = arduino |
| 15 | monitor_speed = 115200 | 16 | monitor_speed = 115200 |
| 16 | upload_speed = 115200 | 17 | upload_speed = 115200 |
diff --git a/linz/oekofen_thermo/src/main.cpp b/linz/oekofen_thermo/src/main.cpp index 5713a0b..084a305 100644 --- a/linz/oekofen_thermo/src/main.cpp +++ b/linz/oekofen_thermo/src/main.cpp | |||
| @@ -2,14 +2,48 @@ | |||
| 2 | #include <WiFiManager.h> | 2 | #include <WiFiManager.h> |
| 3 | #include <ESP8266WebServer.h> | 3 | #include <ESP8266WebServer.h> |
| 4 | 4 | ||
| 5 | #include "MCP42010.h" | 5 | #include <PubSubClient.h> |
| 6 | #include <ESP8266httpUpdate.h> | ||
| 7 | |||
| 8 | #include "secrets.h" | ||
| 9 | |||
| 10 | #define MQTT_BROKER "192.168.0.6" | ||
| 11 | #define MQTT_BROKER_PORT 1883 | ||
| 12 | //#define MQTT_USERNAME "" | ||
| 13 | //#define MQTT_PASSWORD "" | ||
| 14 | #define MQTT_BASE "home/oekofen" | ||
| 15 | |||
| 16 | // fw update | ||
| 17 | // - increment number + build | ||
| 18 | // - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/oekofen.bin | ||
| 19 | // - reboot device or send "fwupdate" to mqtt_topic_cmd | ||
| 20 | // - fw update state is published in mqtt_topic_state | ||
| 21 | #define FIRMWARE_VERSION 1 | ||
| 22 | //#define FIRMWARE_URL "" | ||
| 23 | |||
| 24 | #define _STR(s) #s | ||
| 25 | #define STR(s) _STR(s) | ||
| 26 | |||
| 27 | const String mqtt_clientId = "OekoFen-" + String(ESP.getChipId()) | ||
| 28 | + "/v" + STR(FIRMWARE_VERSION); | ||
| 29 | const char* mqtt_topic_ping = MQTT_BASE "/ping"; | ||
| 30 | const char* mqtt_topic_cmd = MQTT_BASE "/command"; | ||
| 31 | const String mqtt_topic_state = String(MQTT_BASE) + "/" + String(ESP.getChipId()); | ||
| 32 | |||
| 33 | bool mqttLoop(); | ||
| 34 | void mqttCallback(char *topic, byte *payload, unsigned int length); | ||
| 35 | void checkFirmwareUpdate(); | ||
| 36 | |||
| 37 | WiFiClient wifi_client; | ||
| 38 | PubSubClient mqtt(wifi_client); | ||
| 6 | 39 | ||
| 7 | ESP8266WebServer http_service(80); | 40 | ESP8266WebServer http_service(80); |
| 8 | MCP42010 msp42010(D8); | ||
| 9 | 41 | ||
| 10 | void setup() { | 42 | void setup() { |
| 11 | Serial.begin(115200); | 43 | Serial.begin(115200); |
| 12 | msp42010.setup(); | 44 | analogWriteFreq(8000); |
| 45 | pinMode(D3, OUTPUT); | ||
| 46 | pinMode(LED_BUILTIN, OUTPUT); | ||
| 13 | 47 | ||
| 14 | WiFiManager wifiManager; | 48 | WiFiManager wifiManager; |
| 15 | wifiManager.setConfigPortalTimeout(600); | 49 | wifiManager.setConfigPortalTimeout(600); |
| @@ -22,25 +56,119 @@ void setup() { | |||
| 22 | ESP.restart(); | 56 | ESP.restart(); |
| 23 | } | 57 | } |
| 24 | 58 | ||
| 59 | yield(); | ||
| 60 | checkFirmwareUpdate(); | ||
| 61 | yield(); | ||
| 62 | |||
| 63 | mqtt.setServer(MQTT_BROKER, MQTT_BROKER_PORT); | ||
| 64 | mqtt.setCallback(mqttCallback); | ||
| 65 | |||
| 25 | http_service.on("/", HTTP_GET, []() { | 66 | http_service.on("/", HTTP_GET, []() { |
| 26 | uint8_t val = random(0, 255); | 67 | http_service.send_P(200, PSTR("text/plain"), PSTR("I'm an oekofen fake thermostat")); |
| 27 | //msp42010.setPot(MCP42010::P0, val); | 68 | }); |
| 28 | msp42010.setPot(MCP42010::P1, val); | 69 | http_service.on("/set", HTTP_POST, []() { |
| 29 | http_service.send(200, PSTR("text/plain"), String(val)); | 70 | if (!http_service.hasArg("value")) { |
| 71 | http_service.send_P(400, PSTR("text/plain"), PSTR("Bad Request")); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | int val = http_service.arg("value").toInt(); | ||
| 76 | int pwm_val = constrain(val, 0, 1023); | ||
| 77 | analogWrite(D3, pwm_val); | ||
| 78 | http_service.send(200, "text/plain", String(pwm_val)); | ||
| 30 | }); | 79 | }); |
| 31 | http_service.begin(); | 80 | http_service.begin(); |
| 32 | 81 | ||
| 33 | Serial.println("Setup done"); | 82 | Serial.println("Setup done"); |
| 34 | } | 83 | } |
| 35 | 84 | ||
| 36 | void loop() { | 85 | bool mqttLoop() |
| 37 | //http_service.handleClient(); | 86 | { |
| 38 | if (Serial.available() > 0) | 87 | if (!mqtt.connected()) |
| 39 | { | 88 | { |
| 40 | while (Serial.read() >= 0) {} | 89 | Serial.printf_P(PSTR("Connecting to MQTT\n")); |
| 41 | uint8_t val = random(0, 255); | 90 | if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) |
| 42 | //msp42010.setPot(MCP42010::P0, val); | 91 | return false; |
| 43 | msp42010.setPot(MCP42010::P1, val); | 92 | |
| 44 | Serial.println(val, DEC); | 93 | Serial.printf_P(PSTR("MQTT connected\n")); |
| 94 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); | ||
| 95 | mqtt.subscribe(mqtt_topic_ping); | ||
| 96 | mqtt.subscribe(mqtt_topic_cmd); | ||
| 45 | } | 97 | } |
| 98 | |||
| 99 | yield(); | ||
| 100 | mqtt.loop(); | ||
| 101 | return true; | ||
| 102 | } | ||
| 103 | |||
| 104 | void mqttCallback(char *topic, byte *payload, unsigned int length) | ||
| 105 | { | ||
| 106 | char c_payload[length + 1]; | ||
| 107 | memcpy(c_payload, payload, length); | ||
| 108 | c_payload[length] = '\0'; | ||
| 109 | |||
| 110 | if (!strcmp(topic, mqtt_topic_ping)) | ||
| 111 | { | ||
| 112 | if (!strcmp(c_payload, "ping")) | ||
| 113 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | else if (!strcmp(topic, mqtt_topic_cmd)) | ||
| 117 | { | ||
| 118 | if (!strcmp(c_payload, "TODO")) | ||
| 119 | { | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | else if (!strcmp(c_payload, "fwupdate")) | ||
| 123 | { | ||
| 124 | checkFirmwareUpdate(); | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | else if (!strcmp(c_payload, "reset")) | ||
| 128 | { | ||
| 129 | Serial.printf_P(PSTR("Resetting\n")); | ||
| 130 | ESP.reset(); | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | Serial.printf_P(PSTR("Unhandled MQTT message: [%s] %s\n"), topic, c_payload); | ||
| 136 | } | ||
| 137 | |||
| 138 | void checkFirmwareUpdate() | ||
| 139 | { | ||
| 140 | BearSSL::WiFiClientSecure update_client; | ||
| 141 | update_client.setInsecure(); | ||
| 142 | |||
| 143 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate running"); | ||
| 144 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | ||
| 145 | ESPhttpUpdate.rebootOnUpdate(true); | ||
| 146 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | ||
| 147 | switch(ret) | ||
| 148 | { | ||
| 149 | case HTTP_UPDATE_FAILED: | ||
| 150 | { | ||
| 151 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | ||
| 152 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | ||
| 153 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | ||
| 154 | mqtt.publish(mqtt_topic_state.c_str(), tmp.c_str()); | ||
| 155 | } | ||
| 156 | break; | ||
| 157 | |||
| 158 | case HTTP_UPDATE_NO_UPDATES: | ||
| 159 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | ||
| 160 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate noupdates"); | ||
| 161 | break; | ||
| 162 | |||
| 163 | case HTTP_UPDATE_OK: | ||
| 164 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | ||
| 165 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate ok"); | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | void loop() { | ||
| 171 | http_service.handleClient(); | ||
| 172 | if (!mqttLoop()) | ||
| 173 | delay(5000); | ||
| 46 | } | 174 | } |
diff --git a/oliver/dr_desk/platformio.ini b/oliver/dr_desk/platformio.ini index c6d35d4..e5c54f1 100644 --- a/oliver/dr_desk/platformio.ini +++ b/oliver/dr_desk/platformio.ini | |||
| @@ -13,6 +13,7 @@ default_envs = nodemcuv2 | |||
| 13 | [env:nodemcuv2] | 13 | [env:nodemcuv2] |
| 14 | platform = espressif8266 | 14 | platform = espressif8266 |
| 15 | board = nodemcuv2 | 15 | board = nodemcuv2 |
| 16 | lib_deps = WiFiManager, PubSubClient, FastLED | ||
| 16 | framework = arduino | 17 | framework = arduino |
| 17 | monitor_speed = 115200 | 18 | monitor_speed = 115200 |
| 18 | upload_speed = 115200 | 19 | upload_speed = 115200 |
diff --git a/oliver/dr_desk/src/main.cpp b/oliver/dr_desk/src/main.cpp index 6f31b76..8791c80 100644 --- a/oliver/dr_desk/src/main.cpp +++ b/oliver/dr_desk/src/main.cpp | |||
| @@ -1,22 +1,17 @@ | |||
| 1 | #include <Arduino.h> | 1 | #include <Arduino.h> |
| 2 | #include <ESP8266mDNS.h> | 2 | #include <ESP8266mDNS.h> |
| 3 | #include <PubSubClient.h> | ||
| 4 | #include <WiFiManager.h> | 3 | #include <WiFiManager.h> |
| 5 | #include <FastLED.h> | 4 | #include <FastLED.h> |
| 5 | #include <PubSubClient.h> | ||
| 6 | #include <ESP8266httpUpdate.h> | 6 | #include <ESP8266httpUpdate.h> |
| 7 | #include "secrets.h" | ||
| 8 | 7 | ||
| 9 | #define _STR(s) #s | 8 | #include "secrets.h" |
| 10 | #define STR(s) _STR(s) | ||
| 11 | |||
| 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. | ||
| 14 | |||
| 15 | const char* mqtt_pingall_sub = "home/pingall"; | ||
| 16 | const char* mqtt_pingall_pub = "home/pingall/response"; | ||
| 17 | 9 | ||
| 18 | #define MQTT_BASE "home/diningroom/desk" | 10 | #define MQTT_BROKER "192.168.1.5" |
| 19 | const char* mqtt_device_boot = MQTT_BASE "/device"; | 11 | #define MQTT_BROKER_PORT 1883 |
| 12 | //#define MQTT_USERNAME "" | ||
| 13 | //#define MQTT_PASSWORD "" | ||
| 14 | #define MQTT_BASE "home/diningroom/desk" | ||
| 20 | 15 | ||
| 21 | const char* mqtt_mode_sub = MQTT_BASE; | 16 | const char* mqtt_mode_sub = MQTT_BASE; |
| 22 | const char* mqtt_mode_pub = MQTT_BASE "/status"; | 17 | const char* mqtt_mode_pub = MQTT_BASE "/status"; |
| @@ -33,16 +28,25 @@ const char* mqtt_brightness_pub = MQTT_BASE "/brightness/status"; | |||
| 33 | #define RGB_COLOR_ORDER GRB | 28 | #define RGB_COLOR_ORDER GRB |
| 34 | #define NUM(a) (sizeof(a) / sizeof(*a)) | 29 | #define NUM(a) (sizeof(a) / sizeof(*a)) |
| 35 | 30 | ||
| 36 | #define FIRMWARE_VERSION 1 | 31 | // fw update |
| 37 | //#define FIRMWARE_URL "" | 32 | // - increment number + build |
| 33 | // - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/olidrdesk.bin | ||
| 34 | // - reboot device or send "fwupdate" to mqtt_topic_cmd | ||
| 35 | // - fw update state is published in mqtt_topic_state | ||
| 36 | #define FIRMWARE_VERSION 1 | ||
| 37 | //#define FIRMWARE_URL "" | ||
| 38 | 38 | ||
| 39 | WiFiClient espClient; | 39 | #define _STR(s) #s |
| 40 | PubSubClient client(espClient); | 40 | #define STR(s) _STR(s) |
| 41 | char convBuffer[10]; | ||
| 42 | CRGB leds[RGB_NUM_LEDS]; | ||
| 43 | 41 | ||
| 44 | void callback(char *topic, uint8_t *payload, unsigned int length); | 42 | const String mqtt_clientId = "DrDesk-" + String(ESP.getChipId()) |
| 45 | void blink(); | 43 | + "/v" + STR(FIRMWARE_VERSION); |
| 44 | const char* mqtt_topic_ping = MQTT_BASE "/ping"; | ||
| 45 | const char* mqtt_topic_cmd = MQTT_BASE "/command"; | ||
| 46 | const String mqtt_topic_state = String(MQTT_BASE) + "/" + String(ESP.getChipId()); | ||
| 47 | |||
| 48 | bool mqttLoop(); | ||
| 49 | void mqttCallback(char *topic, byte *payload, unsigned int length); | ||
| 46 | void checkFirmwareUpdate(); | 50 | void checkFirmwareUpdate(); |
| 47 | 51 | ||
| 48 | void switchMode(struct mode *mode); | 52 | void switchMode(struct mode *mode); |
| @@ -52,6 +56,11 @@ void modeRainbow(); | |||
| 52 | void modeRainbowFast(); | 56 | void modeRainbowFast(); |
| 53 | void modeStrobo(); | 57 | void modeStrobo(); |
| 54 | 58 | ||
| 59 | WiFiClient wifi_client; | ||
| 60 | PubSubClient mqtt(wifi_client); | ||
| 61 | char convBuffer[10]; | ||
| 62 | CRGB leds[RGB_NUM_LEDS]; | ||
| 63 | |||
| 55 | struct mode { | 64 | struct mode { |
| 56 | const char *name; | 65 | const char *name; |
| 57 | void (*func)(); | 66 | void (*func)(); |
| @@ -92,7 +101,9 @@ void setup() | |||
| 92 | WiFiManager wifiManager; | 101 | WiFiManager wifiManager; |
| 93 | wifiManager.setConfigPortalTimeout(600); | 102 | wifiManager.setConfigPortalTimeout(600); |
| 94 | Serial.printf_P(PSTR("Setting up WiFi\n")); | 103 | Serial.printf_P(PSTR("Setting up WiFi\n")); |
| 95 | if (!wifiManager.autoConnect("ESP8266_DR_DESK")) { | 104 | WiFi.hostname("drdesk"); |
| 105 | if (!wifiManager.autoConnect("ESP8266_DR_DESK")) | ||
| 106 | { | ||
| 96 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); | 107 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); |
| 97 | delay(5000); | 108 | delay(5000); |
| 98 | ESP.restart(); | 109 | ESP.restart(); |
| @@ -102,63 +113,71 @@ void setup() | |||
| 102 | checkFirmwareUpdate(); | 113 | checkFirmwareUpdate(); |
| 103 | yield(); | 114 | yield(); |
| 104 | 115 | ||
| 105 | client.setServer(mqtt_server, mqtt_port); | 116 | mqtt.setServer(MQTT_BROKER, MQTT_BROKER_PORT); |
| 106 | client.setCallback(callback); | 117 | mqtt.setCallback(mqttCallback); |
| 107 | 118 | ||
| 108 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default | 119 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default |
| 109 | } | 120 | } |
| 110 | 121 | ||
| 111 | void reconnect() | 122 | bool mqttLoop() |
| 112 | { | 123 | { |
| 113 | // Loop until we're reconnected | 124 | if (!mqtt.connected()) |
| 114 | while (!client.connected()) | ||
| 115 | { | 125 | { |
| 116 | // Create a random client ID | 126 | Serial.printf_P(PSTR("Connecting to MQTT\n")); |
| 117 | String clientId = "ESP8266Client-"; | 127 | if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) |
| 118 | clientId += String(random(0xffff), HEX); | 128 | return false; |
| 129 | |||
| 130 | Serial.printf_P(PSTR("MQTT connected\n")); | ||
| 131 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); | ||
| 132 | mqtt.subscribe(mqtt_topic_ping); | ||
| 133 | mqtt.subscribe(mqtt_topic_cmd); | ||
| 134 | |||
| 135 | // publish states | ||
| 136 | Serial.println(mqtt_mode_sub); | ||
| 137 | mqtt.subscribe(mqtt_mode_sub); | ||
| 138 | mqtt.publish(mqtt_mode_pub, set.mode->name, true); | ||
| 139 | |||
| 140 | Serial.println(mqtt_color_sub); | ||
| 141 | mqtt.subscribe(mqtt_color_sub); | ||
| 142 | itoa(set.color, convBuffer, 10); | ||
| 143 | mqtt.publish(mqtt_color_pub, convBuffer, true); | ||
| 119 | 144 | ||
| 120 | // Attempt to connect | 145 | Serial.println(mqtt_brightness_sub); |
| 121 | if (client.connect(clientId.c_str())) | 146 | mqtt.subscribe(mqtt_brightness_sub); |
| 122 | { | 147 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); |
| 123 | // Once connected, publish an announcement... | 148 | mqtt.publish(mqtt_brightness_pub, convBuffer, true); |
| 124 | client.publish(mqtt_device_boot, "connected"); | ||
| 125 | // ... and resubscribe | ||
| 126 | client.subscribe(mqtt_pingall_sub); | ||
| 127 | |||
| 128 | // publish states | ||
| 129 | Serial.println(mqtt_mode_sub); | ||
| 130 | client.subscribe(mqtt_mode_sub); | ||
| 131 | client.publish(mqtt_mode_pub, set.mode->name, true); | ||
| 132 | |||
| 133 | Serial.println(mqtt_color_sub); | ||
| 134 | client.subscribe(mqtt_color_sub); | ||
| 135 | itoa(set.color, convBuffer, 10); | ||
| 136 | client.publish(mqtt_color_pub, convBuffer, true); | ||
| 137 | |||
| 138 | Serial.println(mqtt_brightness_sub); | ||
| 139 | client.subscribe(mqtt_brightness_sub); | ||
| 140 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); | ||
| 141 | client.publish(mqtt_brightness_pub, convBuffer, true); | ||
| 142 | } | ||
| 143 | else | ||
| 144 | { | ||
| 145 | // Wait 5 seconds before retrying | ||
| 146 | delay(5000); | ||
| 147 | } | ||
| 148 | } | 149 | } |
| 150 | |||
| 151 | yield(); | ||
| 152 | mqtt.loop(); | ||
| 153 | return true; | ||
| 149 | } | 154 | } |
| 150 | 155 | ||
| 151 | void callback(char *topic, uint8_t *payload, unsigned int length) | 156 | void mqttCallback(char *topic, byte *payload, unsigned int length) |
| 152 | { | 157 | { |
| 153 | uint8_t c_payload[length]; | 158 | char c_payload[length + 1]; |
| 154 | memcpy(c_payload, payload, length); | 159 | memcpy(c_payload, payload, length); |
| 155 | c_payload[length] = '\0'; | 160 | c_payload[length] = '\0'; |
| 156 | 161 | ||
| 157 | if (strcmp(topic, mqtt_pingall_sub) == 0) | 162 | if (!strcmp(topic, mqtt_topic_ping)) |
| 163 | { | ||
| 164 | if (!strcmp(c_payload, "ping")) | ||
| 165 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | else if (!strcmp(topic, mqtt_topic_cmd)) | ||
| 158 | { | 169 | { |
| 159 | blink(); | 170 | if (!strcmp(c_payload, "fwupdate")) |
| 160 | client.publish(mqtt_pingall_pub, | 171 | { |
| 161 | "{\"diningroom_desk\":\"connected\"}"); | 172 | checkFirmwareUpdate(); |
| 173 | return; | ||
| 174 | } | ||
| 175 | else if (!strcmp(c_payload, "reset")) | ||
| 176 | { | ||
| 177 | Serial.printf_P(PSTR("Resetting\n")); | ||
| 178 | ESP.reset(); | ||
| 179 | return; | ||
| 180 | } | ||
| 162 | } | 181 | } |
| 163 | else if (strcmp(topic, mqtt_mode_sub) == 0) | 182 | else if (strcmp(topic, mqtt_mode_sub) == 0) |
| 164 | { | 183 | { |
| @@ -179,25 +198,17 @@ void callback(char *topic, uint8_t *payload, unsigned int length) | |||
| 179 | set.color = atoi((char *)c_payload); | 198 | set.color = atoi((char *)c_payload); |
| 180 | current.idle = false; | 199 | current.idle = false; |
| 181 | itoa(set.color, convBuffer, 10); | 200 | itoa(set.color, convBuffer, 10); |
| 182 | client.publish(mqtt_color_pub, convBuffer, true); | 201 | mqtt.publish(mqtt_color_pub, convBuffer, true); |
| 183 | } | 202 | } |
| 184 | else if (strcmp(topic, mqtt_brightness_sub) == 0) | 203 | else if (strcmp(topic, mqtt_brightness_sub) == 0) |
| 185 | { | 204 | { |
| 186 | set.brightness = map(atoi((char *)c_payload), 0, 100, 0, 255); | 205 | set.brightness = map(atoi((char *)c_payload), 0, 100, 0, 255); |
| 187 | current.idle = false; | 206 | current.idle = false; |
| 188 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); | 207 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); |
| 189 | client.publish(mqtt_brightness_pub, convBuffer, true); | 208 | mqtt.publish(mqtt_brightness_pub, convBuffer, true); |
| 190 | } | 209 | } |
| 191 | } | 210 | } |
| 192 | 211 | ||
| 193 | void blink() | ||
| 194 | { | ||
| 195 | //Blink on received MQTT message | ||
| 196 | digitalWrite(LED_BUILTIN, LOW); | ||
| 197 | delay(25); | ||
| 198 | digitalWrite(LED_BUILTIN, HIGH); | ||
| 199 | } | ||
| 200 | |||
| 201 | void calcBrightness() | 212 | void calcBrightness() |
| 202 | { | 213 | { |
| 203 | #define FADE_STEP 10 | 214 | #define FADE_STEP 10 |
| @@ -217,7 +228,7 @@ void switchMode(struct mode *mode) | |||
| 217 | Serial.println(mode->name); | 228 | Serial.println(mode->name); |
| 218 | set.mode = mode; | 229 | set.mode = mode; |
| 219 | current.idle = false; | 230 | current.idle = false; |
| 220 | client.publish(mqtt_mode_pub, set.mode->name, true); | 231 | mqtt.publish(mqtt_mode_pub, set.mode->name, true); |
| 221 | } | 232 | } |
| 222 | 233 | ||
| 223 | void modeOff() | 234 | void modeOff() |
| @@ -270,7 +281,7 @@ void checkFirmwareUpdate() | |||
| 270 | BearSSL::WiFiClientSecure update_client; | 281 | BearSSL::WiFiClientSecure update_client; |
| 271 | update_client.setInsecure(); | 282 | update_client.setInsecure(); |
| 272 | 283 | ||
| 273 | client.publish(mqtt_device_boot, "fwupdate running"); | 284 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate running"); |
| 274 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | 285 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); |
| 275 | ESPhttpUpdate.rebootOnUpdate(true); | 286 | ESPhttpUpdate.rebootOnUpdate(true); |
| 276 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | 287 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); |
| @@ -281,27 +292,26 @@ void checkFirmwareUpdate() | |||
| 281 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | 292 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), |
| 282 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | 293 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); |
| 283 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | 294 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); |
| 284 | client.publish(mqtt_device_boot, tmp.c_str()); | 295 | mqtt.publish(mqtt_topic_state.c_str(), tmp.c_str()); |
| 285 | } | 296 | } |
| 286 | break; | 297 | break; |
| 287 | 298 | ||
| 288 | case HTTP_UPDATE_NO_UPDATES: | 299 | case HTTP_UPDATE_NO_UPDATES: |
| 289 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | 300 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); |
| 290 | client.publish(mqtt_device_boot, "fwupdate noupdates"); | 301 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate noupdates"); |
| 291 | break; | 302 | break; |
| 292 | 303 | ||
| 293 | case HTTP_UPDATE_OK: | 304 | case HTTP_UPDATE_OK: |
| 294 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | 305 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); |
| 295 | client.publish(mqtt_device_boot, "fwupdate ok"); | 306 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate ok"); |
| 296 | break; | 307 | break; |
| 297 | } | 308 | } |
| 298 | } | 309 | } |
| 299 | 310 | ||
| 300 | void loop() | 311 | void loop() |
| 301 | { | 312 | { |
| 302 | if (!client.connected()) | 313 | if (!mqttLoop()) |
| 303 | reconnect(); | 314 | delay(5000); |
| 304 | client.loop(); | ||
| 305 | 315 | ||
| 306 | EVERY_N_MILLISECONDS(100) | 316 | EVERY_N_MILLISECONDS(100) |
| 307 | { | 317 | { |
diff --git a/oliver/lr_stripes/platformio.ini b/oliver/lr_stripes/platformio.ini index c6d35d4..06b3430 100644 --- a/oliver/lr_stripes/platformio.ini +++ b/oliver/lr_stripes/platformio.ini | |||
| @@ -13,6 +13,7 @@ default_envs = nodemcuv2 | |||
| 13 | [env:nodemcuv2] | 13 | [env:nodemcuv2] |
| 14 | platform = espressif8266 | 14 | platform = espressif8266 |
| 15 | board = nodemcuv2 | 15 | board = nodemcuv2 |
| 16 | lib_deps = WiFiManager, PubSubClient, Bounce2 | ||
| 16 | framework = arduino | 17 | framework = arduino |
| 17 | monitor_speed = 115200 | 18 | monitor_speed = 115200 |
| 18 | upload_speed = 115200 | 19 | upload_speed = 115200 |
diff --git a/oliver/lr_stripes/src/main.cpp b/oliver/lr_stripes/src/main.cpp index b55a198..3fe2238 100644 --- a/oliver/lr_stripes/src/main.cpp +++ b/oliver/lr_stripes/src/main.cpp | |||
| @@ -1,21 +1,18 @@ | |||
| 1 | #include <Arduino.h> | 1 | #include <Arduino.h> |
| 2 | #include <ESP8266mDNS.h> | 2 | #include <ESP8266mDNS.h> |
| 3 | #include <PubSubClient.h> | ||
| 4 | #include <WiFiManager.h> | 3 | #include <WiFiManager.h> |
| 5 | #include <Bounce2.h> | 4 | #include <Bounce2.h> |
| 5 | #include <PubSubClient.h> | ||
| 6 | #include <ESP8266httpUpdate.h> | 6 | #include <ESP8266httpUpdate.h> |
| 7 | 7 | ||
| 8 | #define _STR(s) #s | 8 | #include "secrets.h" |
| 9 | #define STR(s) _STR(s) | ||
| 10 | 9 | ||
| 11 | const char* mqtt_server = "192.168.1.2"; //MQTT Server IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT | 10 | #define MQTT_BROKER "192.168.1.5" |
| 12 | const int mqtt_port = 1883; //MQTT Server PORT, default is 1883 but can be anything. | 11 | #define MQTT_BROKER_PORT 1883 |
| 12 | //#define MQTT_USERNAME "" | ||
| 13 | //#define MQTT_PASSWORD "" | ||
| 14 | #define MQTT_BASE "home/livingroom/ledstripe" | ||
| 13 | 15 | ||
| 14 | const char* mqtt_pingall_sub = "home/pingall"; | ||
| 15 | const char* mqtt_pingall_pub = "home/pingall/response"; | ||
| 16 | |||
| 17 | #define MQTT_BASE "home/livingroom/ledstripe" | ||
| 18 | const char* mqtt_device_boot = MQTT_BASE "/device"; | ||
| 19 | 16 | ||
| 20 | enum sensor_type : uint8_t | 17 | enum sensor_type : uint8_t |
| 21 | { | 18 | { |
| @@ -77,19 +74,37 @@ struct sensor_t sensors[] = { | |||
| 77 | 74 | ||
| 78 | #define DIMMER_FADE_DELAY 40 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim) | 75 | #define DIMMER_FADE_DELAY 40 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim) |
| 79 | 76 | ||
| 80 | WiFiClient espClient; | 77 | // fw update |
| 81 | PubSubClient client(espClient); | 78 | // - increment number + build |
| 82 | char convBuffer[10]; | 79 | // - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/olilrstripes.bin |
| 80 | // - reboot device or send "fwupdate" to mqtt_topic_cmd | ||
| 81 | // - fw update state is published in mqtt_topic_state | ||
| 82 | #define FIRMWARE_VERSION 1 | ||
| 83 | //#define FIRMWARE_URL "" | ||
| 84 | |||
| 85 | #define _STR(s) #s | ||
| 86 | #define STR(s) _STR(s) | ||
| 87 | |||
| 88 | const String mqtt_clientId = "LrStripes-" + String(ESP.getChipId()) | ||
| 89 | + "/v" + STR(FIRMWARE_VERSION); | ||
| 90 | const char* mqtt_topic_ping = MQTT_BASE "/ping"; | ||
| 91 | const char* mqtt_topic_cmd = MQTT_BASE "/command"; | ||
| 92 | const String mqtt_topic_state = String(MQTT_BASE) + "/" + String(ESP.getChipId()); | ||
| 93 | |||
| 94 | bool mqttLoop(); | ||
| 95 | void mqttCallback(char *topic, byte *payload, unsigned int length); | ||
| 96 | void checkFirmwareUpdate(); | ||
| 83 | 97 | ||
| 84 | void callback(char* topic, byte* payload, unsigned int length); | ||
| 85 | void blink(); | ||
| 86 | bool relayRead(struct sensor_t *sensor); | 98 | bool relayRead(struct sensor_t *sensor); |
| 87 | void relayWrite(struct sensor_t *sensor, bool state, bool send_update=false); | 99 | void relayWrite(struct sensor_t *sensor, bool state, bool send_update=false); |
| 88 | void flipRelay(struct sensor_t *sensor, bool send_update=false); | 100 | void flipRelay(struct sensor_t *sensor, bool send_update=false); |
| 89 | void checkButtons(void); | 101 | void checkButtons(void); |
| 90 | inline uint8_t pwmValue(uint8_t level); | 102 | inline uint8_t pwmValue(uint8_t level); |
| 91 | void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update=false); | 103 | void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update=false); |
| 92 | void checkFirmwareUpdate(); | 104 | |
| 105 | WiFiClient wifi_client; | ||
| 106 | PubSubClient mqtt(wifi_client); | ||
| 107 | char convBuffer[10]; | ||
| 93 | 108 | ||
| 94 | void setup() | 109 | void setup() |
| 95 | { | 110 | { |
| @@ -134,7 +149,9 @@ void setup() | |||
| 134 | WiFiManager wifiManager; | 149 | WiFiManager wifiManager; |
| 135 | wifiManager.setConfigPortalTimeout(600); | 150 | wifiManager.setConfigPortalTimeout(600); |
| 136 | Serial.printf_P(PSTR("Setting up WiFi\n")); | 151 | Serial.printf_P(PSTR("Setting up WiFi\n")); |
| 137 | if (!wifiManager.autoConnect("ESP8266_LR_STRIPES")) { | 152 | WiFi.hostname("lrstripes"); |
| 153 | if (!wifiManager.autoConnect("ESP8266_LR_STRIPES")) | ||
| 154 | { | ||
| 138 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); | 155 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); |
| 139 | delay(5000); | 156 | delay(5000); |
| 140 | ESP.restart(); | 157 | ESP.restart(); |
| @@ -144,110 +161,101 @@ void setup() | |||
| 144 | checkFirmwareUpdate(); | 161 | checkFirmwareUpdate(); |
| 145 | yield(); | 162 | yield(); |
| 146 | 163 | ||
| 147 | client.setServer(mqtt_server, mqtt_port); | 164 | mqtt.setServer(MQTT_BROKER, MQTT_BROKER_PORT); |
| 148 | client.setCallback(callback); | 165 | mqtt.setCallback(mqttCallback); |
| 149 | 166 | ||
| 150 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default | 167 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default |
| 151 | } | 168 | } |
| 152 | 169 | ||
| 153 | void reconnect() | 170 | bool mqttLoop() |
| 154 | { | 171 | { |
| 155 | // Loop until we're reconnected | 172 | if (!mqtt.connected()) |
| 156 | while (!client.connected()) | ||
| 157 | { | 173 | { |
| 158 | // Create a random client ID | 174 | Serial.printf_P(PSTR("Connecting to MQTT\n")); |
| 159 | String clientId = "ESP8266Client-"; | 175 | if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) |
| 160 | clientId += String(random(0xffff), HEX); | 176 | return false; |
| 161 | 177 | ||
| 162 | // Attempt to connect | 178 | Serial.printf_P(PSTR("MQTT connected\n")); |
| 163 | if (client.connect(clientId.c_str())) | 179 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); |
| 180 | mqtt.subscribe(mqtt_topic_ping); | ||
| 181 | mqtt.subscribe(mqtt_topic_cmd); | ||
| 182 | |||
| 183 | // publish states | ||
| 184 | for (uint8_t i = 0; i < NUM(sensors); i++) | ||
| 164 | { | 185 | { |
| 165 | // Once connected, publish an announcement... | 186 | struct sensor_t *sensor = &sensors[i]; |
| 166 | client.publish(mqtt_device_boot, "connected"); | 187 | if (sensor->type & SENSOR_RELAY) |
| 167 | // ... and resubscribe | 188 | { |
| 168 | client.subscribe(mqtt_pingall_sub); | 189 | Serial.println(sensor->relay.mqtt_sub); |
| 190 | mqtt.subscribe(sensor->relay.mqtt_sub); | ||
| 191 | mqtt.publish(sensor->relay.mqtt_pub, relayRead(sensor) ? "1" : "0", | ||
| 192 | true); | ||
| 193 | } | ||
| 169 | 194 | ||
| 170 | // publish states | 195 | if (sensor->type & SENSOR_DIMMER) |
| 171 | for (uint8_t i = 0; i < NUM(sensors); i++) | ||
| 172 | { | 196 | { |
| 173 | struct sensor_t *sensor = &sensors[i]; | 197 | Serial.println(sensor->dimmer.mqtt_sub); |
| 174 | if (sensor->type & SENSOR_RELAY) | 198 | mqtt.subscribe(sensor->dimmer.mqtt_sub); |
| 175 | { | 199 | itoa(sensor->dimmer.level, convBuffer, 10); |
| 176 | Serial.println(sensor->relay.mqtt_sub); | 200 | mqtt.publish(sensor->dimmer.mqtt_pub, convBuffer, true); |
| 177 | client.subscribe(sensor->relay.mqtt_sub); | ||
| 178 | client.publish(sensor->relay.mqtt_pub, relayRead(sensor) ? "1" : "0", | ||
| 179 | true); | ||
| 180 | } | ||
| 181 | |||
| 182 | if (sensor->type & SENSOR_DIMMER) | ||
| 183 | { | ||
| 184 | Serial.println(sensor->dimmer.mqtt_sub); | ||
| 185 | client.subscribe(sensor->dimmer.mqtt_sub); | ||
| 186 | itoa(sensor->dimmer.level, convBuffer, 10); | ||
| 187 | client.publish(sensor->dimmer.mqtt_pub, convBuffer, true); | ||
| 188 | } | ||
| 189 | } | 201 | } |
| 190 | } | 202 | } |
| 191 | else | ||
| 192 | { | ||
| 193 | // Wait 5 seconds before retrying | ||
| 194 | delay(5000); | ||
| 195 | } | ||
| 196 | } | 203 | } |
| 204 | |||
| 205 | yield(); | ||
| 206 | mqtt.loop(); | ||
| 207 | return true; | ||
| 197 | } | 208 | } |
| 198 | 209 | ||
| 199 | void callback(char* topic, byte* payload, unsigned int length) | 210 | void mqttCallback(char *topic, byte *payload, unsigned int length) |
| 200 | { | 211 | { |
| 201 | char c_payload[length]; | 212 | char c_payload[length + 1]; |
| 202 | memcpy(c_payload, payload, length); | 213 | memcpy(c_payload, payload, length); |
| 203 | c_payload[length] = '\0'; | 214 | c_payload[length] = '\0'; |
| 204 | 215 | ||
| 205 | if (strcmp(topic, mqtt_pingall_sub) == 0) | 216 | if (!strcmp(topic, mqtt_topic_ping)) |
| 206 | { | 217 | { |
| 207 | blink(); | 218 | if (!strcmp(c_payload, "ping")) |
| 208 | client.publish(mqtt_pingall_pub, | 219 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); |
| 209 | "{\"livingroom_ledstrip\":\"connected\"}"); | ||
| 210 | return; | 220 | return; |
| 211 | } | 221 | } |
| 212 | 222 | else if (!strcmp(topic, mqtt_topic_cmd)) | |
| 213 | for (uint8_t i = 0; i < NUM(sensors); i++) | ||
| 214 | { | 223 | { |
| 215 | struct sensor_t *sensor = &sensors[i]; | 224 | if (!strcmp(c_payload, "fwupdate")) |
| 216 | if (sensor->type & SENSOR_RELAY | ||
| 217 | && length > 0 | ||
| 218 | && strcmp(topic, sensor->relay.mqtt_sub) == 0) | ||
| 219 | { | 225 | { |
| 220 | blink(); | 226 | checkFirmwareUpdate(); |
| 221 | relayWrite(sensor, payload[0] == '1', true); | ||
| 222 | return; | 227 | return; |
| 223 | } | 228 | } |
| 224 | 229 | else if (!strcmp(c_payload, "reset")) | |
| 225 | if (sensor->type & SENSOR_DIMMER | ||
| 226 | && length > 0 | ||
| 227 | && strcmp(topic, sensor->dimmer.mqtt_sub) == 0) | ||
| 228 | { | 230 | { |
| 229 | blink(); | 231 | Serial.printf_P(PSTR("Resetting\n")); |
| 230 | uint8_t level = atoi((char *)payload); | 232 | ESP.reset(); |
| 231 | fadeDimmer(sensor, (level > 100) ? 100 : level, true); | ||
| 232 | return; | 233 | return; |
| 233 | } | 234 | } |
| 234 | } | 235 | } |
| 235 | } | 236 | else |
| 236 | 237 | { | |
| 237 | void blink() | 238 | for (uint8_t i = 0; i < NUM(sensors); i++) |
| 238 | { | 239 | { |
| 239 | //Blink on received MQTT message | 240 | struct sensor_t *sensor = &sensors[i]; |
| 240 | digitalWrite(LED_BUILTIN, LOW); | 241 | if (sensor->type & SENSOR_RELAY |
| 241 | delay(25); | 242 | && length > 0 |
| 242 | digitalWrite(LED_BUILTIN, HIGH); | 243 | && strcmp(topic, sensor->relay.mqtt_sub) == 0) |
| 243 | } | 244 | { |
| 245 | relayWrite(sensor, payload[0] == '1', true); | ||
| 246 | return; | ||
| 247 | } | ||
| 244 | 248 | ||
| 245 | void loop() | 249 | if (sensor->type & SENSOR_DIMMER |
| 246 | { | 250 | && length > 0 |
| 247 | if (!client.connected()) | 251 | && strcmp(topic, sensor->dimmer.mqtt_sub) == 0) |
| 248 | reconnect(); | 252 | { |
| 249 | client.loop(); | 253 | uint8_t level = atoi((char *)payload); |
| 250 | checkButtons(); | 254 | fadeDimmer(sensor, (level > 100) ? 100 : level, true); |
| 255 | return; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 251 | } | 259 | } |
| 252 | 260 | ||
| 253 | bool relayRead(struct sensor_t *sensor) | 261 | bool relayRead(struct sensor_t *sensor) |
| @@ -277,7 +285,7 @@ void relayWrite(struct sensor_t *sensor, bool state, bool send_update) | |||
| 277 | #endif | 285 | #endif |
| 278 | 286 | ||
| 279 | if (send_update) | 287 | if (send_update) |
| 280 | client.publish(sensor->relay.mqtt_pub, state ? "1" : "0", true); | 288 | mqtt.publish(sensor->relay.mqtt_pub, state ? "1" : "0", true); |
| 281 | } | 289 | } |
| 282 | 290 | ||
| 283 | void flipRelay(struct sensor_t *sensor, bool send_update) | 291 | void flipRelay(struct sensor_t *sensor, bool send_update) |
| @@ -356,7 +364,7 @@ void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update) | |||
| 356 | if (send_update) | 364 | if (send_update) |
| 357 | { | 365 | { |
| 358 | itoa(level, convBuffer, 10); | 366 | itoa(level, convBuffer, 10); |
| 359 | client.publish(sensor->dimmer.mqtt_pub, convBuffer, true); | 367 | mqtt.publish(sensor->dimmer.mqtt_pub, convBuffer, true); |
| 360 | } | 368 | } |
| 361 | } | 369 | } |
| 362 | 370 | ||
| @@ -365,7 +373,7 @@ void checkFirmwareUpdate() | |||
| 365 | BearSSL::WiFiClientSecure update_client; | 373 | BearSSL::WiFiClientSecure update_client; |
| 366 | update_client.setInsecure(); | 374 | update_client.setInsecure(); |
| 367 | 375 | ||
| 368 | client.publish(mqtt_device_boot, "fwupdate running"); | 376 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate running"); |
| 369 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | 377 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); |
| 370 | ESPhttpUpdate.rebootOnUpdate(true); | 378 | ESPhttpUpdate.rebootOnUpdate(true); |
| 371 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | 379 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); |
| @@ -376,18 +384,26 @@ void checkFirmwareUpdate() | |||
| 376 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | 384 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), |
| 377 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | 385 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); |
| 378 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | 386 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); |
| 379 | client.publish(mqtt_device_boot, tmp.c_str()); | 387 | mqtt.publish(mqtt_topic_state.c_str(), tmp.c_str()); |
| 380 | } | 388 | } |
| 381 | break; | 389 | break; |
| 382 | 390 | ||
| 383 | case HTTP_UPDATE_NO_UPDATES: | 391 | case HTTP_UPDATE_NO_UPDATES: |
| 384 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | 392 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); |
| 385 | client.publish(mqtt_device_boot, "fwupdate noupdates"); | 393 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate noupdates"); |
| 386 | break; | 394 | break; |
| 387 | 395 | ||
| 388 | case HTTP_UPDATE_OK: | 396 | case HTTP_UPDATE_OK: |
| 389 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | 397 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); |
| 390 | client.publish(mqtt_device_boot, "fwupdate ok"); | 398 | mqtt.publish(mqtt_topic_state.c_str(), "fwupdate ok"); |
| 391 | break; | 399 | break; |
| 392 | } | 400 | } |
| 393 | } | 401 | } |
| 402 | |||
| 403 | void loop() | ||
| 404 | { | ||
| 405 | if (!mqttLoop()) | ||
| 406 | delay(5000); | ||
| 407 | |||
| 408 | checkButtons(); | ||
| 409 | } | ||
