From 1783df2792956e63de1bbae3ed7b036bdefec9a1 Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 28 Dec 2020 10:17:18 +0100 Subject: Rename dr_desk to dr_table --- oliver/dr_table/src/main.cpp | 328 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100644 oliver/dr_table/src/main.cpp (limited to 'oliver/dr_table/src/main.cpp') diff --git a/oliver/dr_table/src/main.cpp b/oliver/dr_table/src/main.cpp new file mode 100644 index 0000000..309c4a1 --- /dev/null +++ b/oliver/dr_table/src/main.cpp @@ -0,0 +1,328 @@ +#include +#include +#include +#include +#include +#include + +#include "secrets.h" + +#define MQTT_BROKER "192.168.1.5" +#define MQTT_BROKER_PORT 1883 +//#define MQTT_USERNAME "" +//#define MQTT_PASSWORD "" +#define MQTT_BASE "home/diningroom/table" + +const char* mqtt_mode_sub = MQTT_BASE "/mode"; +const char* mqtt_mode_pub = MQTT_BASE "/mode/status"; + +const char* mqtt_color_sub = MQTT_BASE "/color"; +const char* mqtt_color_pub = MQTT_BASE "/color/status"; + +const char* mqtt_brightness_sub = MQTT_BASE "/brightness"; +const char* mqtt_brightness_pub = MQTT_BASE "/brightness/status"; + +#define RGB_PIN 2 +#define RGB_NUM_LEDS 12 +#define RGB_CHIPSET WS2812B +#define RGB_COLOR_ORDER GRB +#define NUM(a) (sizeof(a) / sizeof(*a)) + +// fw update +// - increment number + build +// - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/olidrtable.bin +// - reboot device or send "fwupdate" to mqtt_topic_cmd +// - fw update state is published in mqtt_topic_status +#define FIRMWARE_VERSION 1 +//#define FIRMWARE_URL "" + +#define _STR(s) #s +#define STR(s) _STR(s) + +const String mqtt_clientId = "DrDesk-" + String(ESP.getChipId()) + + "/v" + STR(FIRMWARE_VERSION); +const char* mqtt_topic_ping = MQTT_BASE "/ping"; +const char* mqtt_topic_status = MQTT_BASE "/status"; +const char* mqtt_topic_cmd = MQTT_BASE "/command"; + +bool mqttLoop(); +void mqttCallback(char *topic, byte *payload, unsigned int length); +void checkFirmwareUpdate(); + +void switchMode(struct mode *mode); +void modeOff(); +void modeSolid(); +void modeRainbow(); +void modeRainbowFast(); +void modeStrobo(); + +WiFiClient wifi_client; +PubSubClient mqtt(wifi_client); +char convBuffer[10]; +CRGB leds[RGB_NUM_LEDS]; + +struct mode { + const char *name; + void (*func)(); +}; + +struct mode modes[] = { + { "off", modeOff }, + { "solid", modeSolid }, + { "rainbow", modeRainbow }, + { "rainbowfast", modeRainbowFast }, + { "strobo", modeStrobo }, +}; + +struct +{ + struct mode *mode = &modes[0]; + uint32_t color = CRGB::Red; + uint8_t brightness = 204; // 80% +} set; + +struct +{ + uint8_t brightness; + bool idle = false; +} current; + +void setup() +{ + Serial.begin(115200); + pinMode(LED_BUILTIN, OUTPUT); + + FastLED.addLeds(leds, RGB_NUM_LEDS); + current.brightness = set.brightness; + // update leds + set.mode->func(); + FastLED.show(); + + WiFiManager wifiManager; + wifiManager.setConfigPortalTimeout(600); + Serial.printf_P(PSTR("Setting up WiFi\n")); + WiFi.hostname("drdesk"); + if (!wifiManager.autoConnect("ESP8266_DR_DESK")) + { + Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); + delay(5000); + ESP.restart(); + } + + yield(); + checkFirmwareUpdate(); + yield(); + + mqtt.setServer(MQTT_BROKER, MQTT_BROKER_PORT); + mqtt.setCallback(mqttCallback); + + digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default +} + +bool mqttLoop() +{ + if (!mqtt.connected()) + { + Serial.printf_P(PSTR("Connecting to MQTT\n")); + if (!mqtt.connect(mqtt_clientId.c_str(), MQTT_USERNAME, MQTT_PASSWORD, + mqtt_topic_status, 0, true, "offline")) + return false; + + Serial.printf_P(PSTR("MQTT connected\n")); + mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); + mqtt.publish(mqtt_topic_status, "online", true); + mqtt.subscribe(mqtt_topic_ping); + mqtt.subscribe(mqtt_topic_cmd); + + // publish states + Serial.println(mqtt_mode_sub); + mqtt.subscribe(mqtt_mode_sub); + mqtt.publish(mqtt_mode_pub, set.mode->name, true); + + Serial.println(mqtt_color_sub); + mqtt.subscribe(mqtt_color_sub); + itoa(set.color, convBuffer, 10); + mqtt.publish(mqtt_color_pub, convBuffer, true); + + Serial.println(mqtt_brightness_sub); + mqtt.subscribe(mqtt_brightness_sub); + itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); + mqtt.publish(mqtt_brightness_pub, convBuffer, true); + } + + yield(); + mqtt.loop(); + return true; +} + +void mqttCallback(char *topic, byte *payload, unsigned int length) +{ + char c_payload[length + 1]; + memcpy(c_payload, payload, length); + c_payload[length] = '\0'; + + if (!strcmp(topic, mqtt_topic_ping)) + { + if (!strcmp(c_payload, "ping")) + mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str()); + return; + } + else if (!strcmp(topic, mqtt_topic_cmd)) + { + if (!strcmp(c_payload, "fwupdate")) + { + checkFirmwareUpdate(); + return; + } + else if (!strcmp(c_payload, "reset")) + { + Serial.printf_P(PSTR("Resetting\n")); + ESP.reset(); + return; + } + } + else if (strcmp(topic, mqtt_mode_sub) == 0) + { + for (uint8_t i = 0; i < NUM(modes); ++i) + { + if (strcmp(modes[i].name, (char *)c_payload) != 0) + continue; + switchMode(&modes[i]); + break; + } + } + else if (strcmp(topic, mqtt_color_sub) == 0) + { + // switch from OFF to SOLID + if (set.mode == &modes[0]) + switchMode(&modes[1]); + + set.color = atoi((char *)c_payload); + current.idle = false; + itoa(set.color, convBuffer, 10); + mqtt.publish(mqtt_color_pub, convBuffer, true); + } + else if (strcmp(topic, mqtt_brightness_sub) == 0) + { + set.brightness = map(atoi((char *)c_payload), 0, 100, 0, 255); + current.idle = false; + itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); + mqtt.publish(mqtt_brightness_pub, convBuffer, true); + } +} + +void calcBrightness() +{ + #define FADE_STEP 10 + if (current.brightness == set.brightness) + return; + int fadeAmount = set.brightness - current.brightness; + if (abs(fadeAmount) > FADE_STEP) + fadeAmount = (fadeAmount > 0) ? FADE_STEP : -FADE_STEP; + current.brightness += fadeAmount; +} + +void switchMode(struct mode *mode) +{ + if (set.mode == mode) + return; + Serial.print("Switching mode to "); + Serial.println(mode->name); + set.mode = mode; + current.idle = false; + mqtt.publish(mqtt_mode_pub, set.mode->name, true); +} + +void modeOff() +{ + fill_solid(leds, RGB_NUM_LEDS, CRGB::Black); + current.idle = true; +} + +void modeSolid() +{ + fill_solid(leds, RGB_NUM_LEDS, set.color); + calcBrightness(); + nscale8_video(leds, RGB_NUM_LEDS, current.brightness); + current.idle = (current.brightness == set.brightness); +} + +void modeRainbow() +{ + static CRGBPalette16 palette = RainbowColors_p; + static uint8_t hue = 0; + calcBrightness(); + for (int i = 0; i < RGB_NUM_LEDS; ++i) + leds[i] = ColorFromPalette(palette, hue, current.brightness); + hue++; +} + +void modeRainbowFast() +{ + static CRGBPalette16 palette = RainbowColors_p; + static uint8_t hue = 0; + calcBrightness(); + for (int i = 0; i < RGB_NUM_LEDS; ++i) + leds[i] = ColorFromPalette(palette, hue, current.brightness); + hue+=5; +} + +void modeStrobo() +{ + static bool state = 0; + if (set.color == CRGB::Black) + return; + fill_solid(leds, RGB_NUM_LEDS, (state) ? set.color : CRGB::Black); + calcBrightness(); + nscale8_video(leds, RGB_NUM_LEDS, current.brightness); + state = !state; +} + +void checkFirmwareUpdate() +{ + BearSSL::WiFiClientSecure update_client; + update_client.setInsecure(); + + mqtt.publish(mqtt_topic_status, "fwupdate running"); + ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); + ESPhttpUpdate.rebootOnUpdate(true); + t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); + switch(ret) + { + case HTTP_UPDATE_FAILED: + { + Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), + ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); + String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); + mqtt.publish(mqtt_topic_status, tmp.c_str()); + } + break; + + case HTTP_UPDATE_NO_UPDATES: + Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); + mqtt.publish(mqtt_topic_status, "fwupdate noupdates"); + break; + + case HTTP_UPDATE_OK: + Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); + mqtt.publish(mqtt_topic_status, "fwupdate ok"); + break; + } +} + +void loop() +{ + if (!mqttLoop()) + delay(5000); + + EVERY_N_MILLISECONDS(100) + { + if (!current.idle) + { + set.mode->func(); + FastLED.show(); + } + if (!current.idle && current.brightness == 0) + current.idle = true; + } +} -- cgit v1.2.3