From 9c1aa4e125d434fa451d660d16c0ce0286d9ace1 Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 12 Oct 2023 22:16:54 +0200 Subject: Rename oliver/dr_table --- oliver/dr_table.old/.gitignore | 5 + oliver/dr_table.old/platformio.ini | 19 +++ oliver/dr_table.old/src/main.cpp | 328 +++++++++++++++++++++++++++++++++++++ oliver/dr_table/.gitignore | 5 - oliver/dr_table/platformio.ini | 19 --- oliver/dr_table/src/main.cpp | 328 ------------------------------------- 6 files changed, 352 insertions(+), 352 deletions(-) create mode 100644 oliver/dr_table.old/.gitignore create mode 100644 oliver/dr_table.old/platformio.ini create mode 100644 oliver/dr_table.old/src/main.cpp delete mode 100644 oliver/dr_table/.gitignore delete mode 100644 oliver/dr_table/platformio.ini delete mode 100644 oliver/dr_table/src/main.cpp diff --git a/oliver/dr_table.old/.gitignore b/oliver/dr_table.old/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/oliver/dr_table.old/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/oliver/dr_table.old/platformio.ini b/oliver/dr_table.old/platformio.ini new file mode 100644 index 0000000..e5c54f1 --- /dev/null +++ b/oliver/dr_table.old/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter, extra scripting +; Upload options: custom port, speed and extra flags +; Library options: dependencies, extra library storages +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/en/stable/projectconf.html + +[platformio] +default_envs = nodemcuv2 + +[env:nodemcuv2] +platform = espressif8266 +board = nodemcuv2 +lib_deps = WiFiManager, PubSubClient, FastLED +framework = arduino +monitor_speed = 115200 +upload_speed = 115200 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 @@ +#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; + } +} diff --git a/oliver/dr_table/.gitignore b/oliver/dr_table/.gitignore deleted file mode 100644 index 89cc49c..0000000 --- a/oliver/dr_table/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.pio -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch diff --git a/oliver/dr_table/platformio.ini b/oliver/dr_table/platformio.ini deleted file mode 100644 index e5c54f1..0000000 --- a/oliver/dr_table/platformio.ini +++ /dev/null @@ -1,19 +0,0 @@ -; PlatformIO Project Configuration File -; -; Build options: build flags, source filter, extra scripting -; Upload options: custom port, speed and extra flags -; Library options: dependencies, extra library storages -; -; Please visit documentation for the other options and examples -; http://docs.platformio.org/en/stable/projectconf.html - -[platformio] -default_envs = nodemcuv2 - -[env:nodemcuv2] -platform = espressif8266 -board = nodemcuv2 -lib_deps = WiFiManager, PubSubClient, FastLED -framework = arduino -monitor_speed = 115200 -upload_speed = 115200 diff --git a/oliver/dr_table/src/main.cpp b/oliver/dr_table/src/main.cpp deleted file mode 100644 index 309c4a1..0000000 --- a/oliver/dr_table/src/main.cpp +++ /dev/null @@ -1,328 +0,0 @@ -#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