diff options
Diffstat (limited to 'oliver/dr_table.old/src/main.cpp')
| -rw-r--r-- | oliver/dr_table.old/src/main.cpp | 328 |
1 files changed, 328 insertions, 0 deletions
diff --git a/oliver/dr_table.old/src/main.cpp b/oliver/dr_table.old/src/main.cpp new file mode 100644 index 0000000..309c4a1 --- /dev/null +++ b/oliver/dr_table.old/src/main.cpp | |||
| @@ -0,0 +1,328 @@ | |||
| 1 | #include <Arduino.h> | ||
| 2 | #include <ESP8266mDNS.h> | ||
| 3 | #include <WiFiManager.h> | ||
| 4 | #include <FastLED.h> | ||
| 5 | #include <PubSubClient.h> | ||
| 6 | #include <ESP8266httpUpdate.h> | ||
| 7 | |||
| 8 | #include "secrets.h" | ||
| 9 | |||
| 10 | #define MQTT_BROKER "192.168.1.5" | ||
| 11 | #define MQTT_BROKER_PORT 1883 | ||
| 12 | //#define MQTT_USERNAME "" | ||
| 13 | //#define MQTT_PASSWORD "" | ||
| 14 | #define MQTT_BASE "home/diningroom/table" | ||
| 15 | |||
| 16 | const char* mqtt_mode_sub = MQTT_BASE "/mode"; | ||
| 17 | const char* mqtt_mode_pub = MQTT_BASE "/mode/status"; | ||
| 18 | |||
| 19 | const char* mqtt_color_sub = MQTT_BASE "/color"; | ||
| 20 | const char* mqtt_color_pub = MQTT_BASE "/color/status"; | ||
| 21 | |||
| 22 | const char* mqtt_brightness_sub = MQTT_BASE "/brightness"; | ||
| 23 | const char* mqtt_brightness_pub = MQTT_BASE "/brightness/status"; | ||
| 24 | |||
| 25 | #define RGB_PIN 2 | ||
| 26 | #define RGB_NUM_LEDS 12 | ||
| 27 | #define RGB_CHIPSET WS2812B | ||
| 28 | #define RGB_COLOR_ORDER GRB | ||
| 29 | #define NUM(a) (sizeof(a) / sizeof(*a)) | ||
| 30 | |||
| 31 | // fw update | ||
| 32 | // - increment number + build | ||
| 33 | // - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/olidrtable.bin | ||
| 34 | // - reboot device or send "fwupdate" to mqtt_topic_cmd | ||
| 35 | // - fw update state is published in mqtt_topic_status | ||
| 36 | #define FIRMWARE_VERSION 1 | ||
| 37 | //#define FIRMWARE_URL "" | ||
| 38 | |||
| 39 | #define _STR(s) #s | ||
| 40 | #define STR(s) _STR(s) | ||
| 41 | |||
| 42 | const String mqtt_clientId = "DrDesk-" + String(ESP.getChipId()) | ||
| 43 | + "/v" + STR(FIRMWARE_VERSION); | ||
| 44 | const char* mqtt_topic_ping = MQTT_BASE "/ping"; | ||
| 45 | const char* mqtt_topic_status = MQTT_BASE "/status"; | ||
| 46 | const char* mqtt_topic_cmd = MQTT_BASE "/command"; | ||
| 47 | |||
| 48 | bool mqttLoop(); | ||
| 49 | void mqttCallback(char *topic, byte *payload, unsigned int length); | ||
| 50 | void checkFirmwareUpdate(); | ||
| 51 | |||
| 52 | void switchMode(struct mode *mode); | ||
| 53 | void modeOff(); | ||
| 54 | void modeSolid(); | ||
| 55 | void modeRainbow(); | ||
| 56 | void modeRainbowFast(); | ||
| 57 | void modeStrobo(); | ||
| 58 | |||
| 59 | WiFiClient wifi_client; | ||
| 60 | PubSubClient mqtt(wifi_client); | ||
| 61 | char convBuffer[10]; | ||
| 62 | CRGB leds[RGB_NUM_LEDS]; | ||
| 63 | |||
| 64 | struct mode { | ||
| 65 | const char *name; | ||
| 66 | void (*func)(); | ||
| 67 | }; | ||
| 68 | |||
| 69 | struct mode modes[] = { | ||
| 70 | { "off", modeOff }, | ||
| 71 | { "solid", modeSolid }, | ||
| 72 | { "rainbow", modeRainbow }, | ||
| 73 | { "rainbowfast", modeRainbowFast }, | ||
| 74 | { "strobo", modeStrobo }, | ||
| 75 | }; | ||
| 76 | |||
| 77 | struct | ||
| 78 | { | ||
| 79 | struct mode *mode = &modes[0]; | ||
| 80 | uint32_t color = CRGB::Red; | ||
| 81 | uint8_t brightness = 204; // 80% | ||
| 82 | } set; | ||
| 83 | |||
| 84 | struct | ||
| 85 | { | ||
| 86 | uint8_t brightness; | ||
| 87 | bool idle = false; | ||
| 88 | } current; | ||
| 89 | |||
| 90 | void setup() | ||
| 91 | { | ||
| 92 | Serial.begin(115200); | ||
| 93 | pinMode(LED_BUILTIN, OUTPUT); | ||
| 94 | |||
| 95 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, RGB_NUM_LEDS); | ||
| 96 | current.brightness = set.brightness; | ||
| 97 | // update leds | ||
| 98 | set.mode->func(); | ||
| 99 | FastLED.show(); | ||
| 100 | |||
| 101 | WiFiManager wifiManager; | ||
| 102 | wifiManager.setConfigPortalTimeout(600); | ||
| 103 | Serial.printf_P(PSTR("Setting up WiFi\n")); | ||
| 104 | WiFi.hostname("drdesk"); | ||
| 105 | if (!wifiManager.autoConnect("ESP8266_DR_DESK")) | ||
| 106 | { | ||
| 107 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); | ||
| 108 | delay(5000); | ||
| 109 | ESP.restart(); | ||
| 110 | } | ||
| 111 | |||
| 112 | yield(); | ||
| 113 | checkFirmwareUpdate(); | ||
| 114 | yield(); | ||
| 115 | |||
| 116 | mqtt.setServer(MQTT_BROKER, MQTT_BROKER_PORT); | ||
| 117 | mqtt.setCallback(mqttCallback); | ||
| 118 | |||
| 119 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default | ||
| 120 | } | ||
| 121 | |||
| 122 | bool mqttLoop() | ||
| 123 | { | ||
| 124 | if (!mqtt.connected()) | ||
| 125 | { | ||
| 126 | Serial.printf_P(PSTR("Connecting to MQTT\n")); | ||
| 127 | if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD, | ||
| 128 | mqtt_topic_status, 0, true, "offline")) | ||
| 129 | return false; | ||
| 130 | |||
| 131 | Serial.printf_P(PSTR("MQTT connected\n")); | ||
| 132 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); | ||
| 133 | mqtt.publish(mqtt_topic_status, "online", true); | ||
| 134 | mqtt.subscribe(mqtt_topic_ping); | ||
| 135 | mqtt.subscribe(mqtt_topic_cmd); | ||
| 136 | |||
| 137 | // publish states | ||
| 138 | Serial.println(mqtt_mode_sub); | ||
| 139 | mqtt.subscribe(mqtt_mode_sub); | ||
| 140 | mqtt.publish(mqtt_mode_pub, set.mode->name, true); | ||
| 141 | |||
| 142 | Serial.println(mqtt_color_sub); | ||
| 143 | mqtt.subscribe(mqtt_color_sub); | ||
| 144 | itoa(set.color, convBuffer, 10); | ||
| 145 | mqtt.publish(mqtt_color_pub, convBuffer, true); | ||
| 146 | |||
| 147 | Serial.println(mqtt_brightness_sub); | ||
| 148 | mqtt.subscribe(mqtt_brightness_sub); | ||
| 149 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); | ||
| 150 | mqtt.publish(mqtt_brightness_pub, convBuffer, true); | ||
| 151 | } | ||
| 152 | |||
| 153 | yield(); | ||
| 154 | mqtt.loop(); | ||
| 155 | return true; | ||
| 156 | } | ||
| 157 | |||
| 158 | void mqttCallback(char *topic, byte *payload, unsigned int length) | ||
| 159 | { | ||
| 160 | char c_payload[length + 1]; | ||
| 161 | memcpy(c_payload, payload, length); | ||
| 162 | c_payload[length] = '\0'; | ||
| 163 | |||
| 164 | if (!strcmp(topic, mqtt_topic_ping)) | ||
| 165 | { | ||
| 166 | if (!strcmp(c_payload, "ping")) | ||
| 167 | mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | else if (!strcmp(topic, mqtt_topic_cmd)) | ||
| 171 | { | ||
| 172 | if (!strcmp(c_payload, "fwupdate")) | ||
| 173 | { | ||
| 174 | checkFirmwareUpdate(); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | else if (!strcmp(c_payload, "reset")) | ||
| 178 | { | ||
| 179 | Serial.printf_P(PSTR("Resetting\n")); | ||
| 180 | ESP.reset(); | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | else if (strcmp(topic, mqtt_mode_sub) == 0) | ||
| 185 | { | ||
| 186 | for (uint8_t i = 0; i < NUM(modes); ++i) | ||
| 187 | { | ||
| 188 | if (strcmp(modes[i].name, (char *)c_payload) != 0) | ||
| 189 | continue; | ||
| 190 | switchMode(&modes[i]); | ||
| 191 | break; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | else if (strcmp(topic, mqtt_color_sub) == 0) | ||
| 195 | { | ||
| 196 | // switch from OFF to SOLID | ||
| 197 | if (set.mode == &modes[0]) | ||
| 198 | switchMode(&modes[1]); | ||
| 199 | |||
| 200 | set.color = atoi((char *)c_payload); | ||
| 201 | current.idle = false; | ||
| 202 | itoa(set.color, convBuffer, 10); | ||
| 203 | mqtt.publish(mqtt_color_pub, convBuffer, true); | ||
| 204 | } | ||
| 205 | else if (strcmp(topic, mqtt_brightness_sub) == 0) | ||
| 206 | { | ||
| 207 | set.brightness = map(atoi((char *)c_payload), 0, 100, 0, 255); | ||
| 208 | current.idle = false; | ||
| 209 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); | ||
| 210 | mqtt.publish(mqtt_brightness_pub, convBuffer, true); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | void calcBrightness() | ||
| 215 | { | ||
| 216 | #define FADE_STEP 10 | ||
| 217 | if (current.brightness == set.brightness) | ||
| 218 | return; | ||
| 219 | int fadeAmount = set.brightness - current.brightness; | ||
| 220 | if (abs(fadeAmount) > FADE_STEP) | ||
| 221 | fadeAmount = (fadeAmount > 0) ? FADE_STEP : -FADE_STEP; | ||
| 222 | current.brightness += fadeAmount; | ||
| 223 | } | ||
| 224 | |||
| 225 | void switchMode(struct mode *mode) | ||
| 226 | { | ||
| 227 | if (set.mode == mode) | ||
| 228 | return; | ||
| 229 | Serial.print("Switching mode to "); | ||
| 230 | Serial.println(mode->name); | ||
| 231 | set.mode = mode; | ||
| 232 | current.idle = false; | ||
| 233 | mqtt.publish(mqtt_mode_pub, set.mode->name, true); | ||
| 234 | } | ||
| 235 | |||
| 236 | void modeOff() | ||
| 237 | { | ||
| 238 | fill_solid(leds, RGB_NUM_LEDS, CRGB::Black); | ||
| 239 | current.idle = true; | ||
| 240 | } | ||
| 241 | |||
| 242 | void modeSolid() | ||
| 243 | { | ||
| 244 | fill_solid(leds, RGB_NUM_LEDS, set.color); | ||
| 245 | calcBrightness(); | ||
| 246 | nscale8_video(leds, RGB_NUM_LEDS, current.brightness); | ||
| 247 | current.idle = (current.brightness == set.brightness); | ||
| 248 | } | ||
| 249 | |||
| 250 | void modeRainbow() | ||
| 251 | { | ||
| 252 | static CRGBPalette16 palette = RainbowColors_p; | ||
| 253 | static uint8_t hue = 0; | ||
| 254 | calcBrightness(); | ||
| 255 | for (int i = 0; i < RGB_NUM_LEDS; ++i) | ||
| 256 | leds[i] = ColorFromPalette(palette, hue, current.brightness); | ||
| 257 | hue++; | ||
| 258 | } | ||
| 259 | |||
| 260 | void modeRainbowFast() | ||
| 261 | { | ||
| 262 | static CRGBPalette16 palette = RainbowColors_p; | ||
| 263 | static uint8_t hue = 0; | ||
| 264 | calcBrightness(); | ||
| 265 | for (int i = 0; i < RGB_NUM_LEDS; ++i) | ||
| 266 | leds[i] = ColorFromPalette(palette, hue, current.brightness); | ||
| 267 | hue+=5; | ||
| 268 | } | ||
| 269 | |||
| 270 | void modeStrobo() | ||
| 271 | { | ||
| 272 | static bool state = 0; | ||
| 273 | if (set.color == CRGB::Black) | ||
| 274 | return; | ||
| 275 | fill_solid(leds, RGB_NUM_LEDS, (state) ? set.color : CRGB::Black); | ||
| 276 | calcBrightness(); | ||
| 277 | nscale8_video(leds, RGB_NUM_LEDS, current.brightness); | ||
| 278 | state = !state; | ||
| 279 | } | ||
| 280 | |||
| 281 | void checkFirmwareUpdate() | ||
| 282 | { | ||
| 283 | BearSSL::WiFiClientSecure update_client; | ||
| 284 | update_client.setInsecure(); | ||
| 285 | |||
| 286 | mqtt.publish(mqtt_topic_status, "fwupdate running"); | ||
| 287 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | ||
| 288 | ESPhttpUpdate.rebootOnUpdate(true); | ||
| 289 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | ||
| 290 | switch(ret) | ||
| 291 | { | ||
| 292 | case HTTP_UPDATE_FAILED: | ||
| 293 | { | ||
| 294 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | ||
| 295 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | ||
| 296 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | ||
| 297 | mqtt.publish(mqtt_topic_status, tmp.c_str()); | ||
| 298 | } | ||
| 299 | break; | ||
| 300 | |||
| 301 | case HTTP_UPDATE_NO_UPDATES: | ||
| 302 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | ||
| 303 | mqtt.publish(mqtt_topic_status, "fwupdate noupdates"); | ||
| 304 | break; | ||
| 305 | |||
| 306 | case HTTP_UPDATE_OK: | ||
| 307 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | ||
| 308 | mqtt.publish(mqtt_topic_status, "fwupdate ok"); | ||
| 309 | break; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | void loop() | ||
| 314 | { | ||
| 315 | if (!mqttLoop()) | ||
| 316 | delay(5000); | ||
| 317 | |||
| 318 | EVERY_N_MILLISECONDS(100) | ||
| 319 | { | ||
| 320 | if (!current.idle) | ||
| 321 | { | ||
| 322 | set.mode->func(); | ||
| 323 | FastLED.show(); | ||
| 324 | } | ||
| 325 | if (!current.idle && current.brightness == 0) | ||
| 326 | current.idle = true; | ||
| 327 | } | ||
| 328 | } | ||
