From e60bc43eb30ab72ec9fdfae581058bf8acfd8586 Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 5 Feb 2020 13:01:01 +0100 Subject: update to platform IO --- oliver/dr_desk/src/dr_desk.cpp | 287 +++++++++++++++++++++++++++++++++++++++++ oliver/dr_desk/src/dr_desk.ino | 1 - 2 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 oliver/dr_desk/src/dr_desk.cpp delete mode 120000 oliver/dr_desk/src/dr_desk.ino (limited to 'oliver/dr_desk/src') diff --git a/oliver/dr_desk/src/dr_desk.cpp b/oliver/dr_desk/src/dr_desk.cpp new file mode 100644 index 0000000..46bf8eb --- /dev/null +++ b/oliver/dr_desk/src/dr_desk.cpp @@ -0,0 +1,287 @@ +#include +#include +#include +#include +#include +#include +#include "secrets.h" + +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 +const char* autoconf_pwd = AUTOCONF_PASSWORD; // AP password so noone else can connect to the ESP in case your router fails +const char* mqtt_server = "192.168.1.2"; //MQTT Server IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT +const int mqtt_port = 1883; //MQTT Server PORT, default is 1883 but can be anything. + +const char* mqtt_pingall_sub = "home/pingall"; +const char* mqtt_pingall_pub = "home/pingall/response"; + +#define MQTT_BASE "home/diningroom/desk" +const char* mqtt_device_boot = MQTT_BASE "/device"; + +const char* mqtt_mode_sub = MQTT_BASE; +const char* mqtt_mode_pub = MQTT_BASE "/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)) + +WiFiClient espClient; +PubSubClient client(espClient); +char convBuffer[10]; +CRGB leds[RGB_NUM_LEDS]; + +void switchMode(struct mode *mode); +void modeOff(); +void modeSolid(); +void modeRainbow(); +void modeRainbowFast(); +void modeStrobo(); + +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(BUILTIN_LED, OUTPUT); + + FastLED.addLeds(leds, RGB_NUM_LEDS); + current.brightness = set.brightness; + // update leds + set.mode->func(); + FastLED.show(); + + WiFiManager wifiManager; + wifiManager.autoConnect(autoconf_ssid, autoconf_pwd); + + setup_ota(); + + client.setServer(mqtt_server, mqtt_port); + client.setCallback(callback); + + digitalWrite(BUILTIN_LED, HIGH); //Turn off led as default +} + +void setup_ota() +{ + // Set OTA Password, and change it in platformio.ini + ArduinoOTA.setPassword(OTA_PASSWORD); + ArduinoOTA.onStart([]() {}); + ArduinoOTA.onEnd([]() {}); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {}); + ArduinoOTA.onError([](ota_error_t error) + { + if (error == OTA_AUTH_ERROR); // Auth failed + else if (error == OTA_BEGIN_ERROR); // Begin failed + else if (error == OTA_CONNECT_ERROR); // Connect failed + else if (error == OTA_RECEIVE_ERROR); // Receive failed + else if (error == OTA_END_ERROR); // End failed + }); + ArduinoOTA.begin(); +} + +void reconnect() +{ + // Loop until we're reconnected + while (!client.connected()) + { + // Create a random client ID + String clientId = "ESP8266Client-"; + clientId += String(random(0xffff), HEX); + + // Attempt to connect + if (client.connect(clientId.c_str())) + { + // Once connected, publish an announcement... + client.publish(mqtt_device_boot, "connected"); + // ... and resubscribe + client.subscribe(mqtt_pingall_sub); + + // publish states + Serial.println(mqtt_mode_sub); + client.subscribe(mqtt_mode_sub); + client.publish(mqtt_mode_pub, set.mode->name, true); + + Serial.println(mqtt_color_sub); + client.subscribe(mqtt_color_sub); + itoa(set.color, convBuffer, 10); + client.publish(mqtt_color_pub, convBuffer, true); + + Serial.println(mqtt_brightness_sub); + client.subscribe(mqtt_brightness_sub); + itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); + client.publish(mqtt_brightness_pub, convBuffer, true); + } + else + { + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +void callback(char *topic, uint8_t *payload, unsigned int length) +{ + uint8_t c_payload[length]; + memcpy(c_payload, payload, length); + c_payload[length] = '\0'; + + if (strcmp(topic, mqtt_pingall_sub) == 0) + { + blink(); + client.publish(mqtt_pingall_pub, + "{\"diningroom_desk\":\"connected\"}"); + } + 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); + client.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); + client.publish(mqtt_brightness_pub, convBuffer, true); + } +} + +void blink() +{ + //Blink on received MQTT message + digitalWrite(BUILTIN_LED, LOW); + delay(25); + digitalWrite(BUILTIN_LED, HIGH); +} + +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; + client.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 loop() +{ + if (!client.connected()) + reconnect(); + client.loop(); + ArduinoOTA.handle(); + + 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_desk/src/dr_desk.ino b/oliver/dr_desk/src/dr_desk.ino deleted file mode 120000 index 0cdf13b..0000000 --- a/oliver/dr_desk/src/dr_desk.ino +++ /dev/null @@ -1 +0,0 @@ -../dr_desk.ino \ No newline at end of file -- cgit v1.2.3