diff options
Diffstat (limited to 'oliver/dr_desk/src/dr_desk.cpp')
| -rw-r--r-- | oliver/dr_desk/src/dr_desk.cpp | 316 |
1 files changed, 0 insertions, 316 deletions
diff --git a/oliver/dr_desk/src/dr_desk.cpp b/oliver/dr_desk/src/dr_desk.cpp deleted file mode 100644 index 6f31b76..0000000 --- a/oliver/dr_desk/src/dr_desk.cpp +++ /dev/null | |||
| @@ -1,316 +0,0 @@ | |||
| 1 | #include <Arduino.h> | ||
| 2 | #include <ESP8266mDNS.h> | ||
| 3 | #include <PubSubClient.h> | ||
| 4 | #include <WiFiManager.h> | ||
| 5 | #include <FastLED.h> | ||
| 6 | #include <ESP8266httpUpdate.h> | ||
| 7 | #include "secrets.h" | ||
| 8 | |||
| 9 | #define _STR(s) #s | ||
| 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 | |||
| 18 | #define MQTT_BASE "home/diningroom/desk" | ||
| 19 | const char* mqtt_device_boot = MQTT_BASE "/device"; | ||
| 20 | |||
| 21 | const char* mqtt_mode_sub = MQTT_BASE; | ||
| 22 | const char* mqtt_mode_pub = MQTT_BASE "/status"; | ||
| 23 | |||
| 24 | const char* mqtt_color_sub = MQTT_BASE "/color"; | ||
| 25 | const char* mqtt_color_pub = MQTT_BASE "/color/status"; | ||
| 26 | |||
| 27 | const char* mqtt_brightness_sub = MQTT_BASE "/brightness"; | ||
| 28 | const char* mqtt_brightness_pub = MQTT_BASE "/brightness/status"; | ||
| 29 | |||
| 30 | #define RGB_PIN 2 | ||
| 31 | #define RGB_NUM_LEDS 12 | ||
| 32 | #define RGB_CHIPSET WS2812B | ||
| 33 | #define RGB_COLOR_ORDER GRB | ||
| 34 | #define NUM(a) (sizeof(a) / sizeof(*a)) | ||
| 35 | |||
| 36 | #define FIRMWARE_VERSION 1 | ||
| 37 | //#define FIRMWARE_URL "" | ||
| 38 | |||
| 39 | WiFiClient espClient; | ||
| 40 | PubSubClient client(espClient); | ||
| 41 | char convBuffer[10]; | ||
| 42 | CRGB leds[RGB_NUM_LEDS]; | ||
| 43 | |||
| 44 | void callback(char *topic, uint8_t *payload, unsigned int length); | ||
| 45 | void blink(); | ||
| 46 | void checkFirmwareUpdate(); | ||
| 47 | |||
| 48 | void switchMode(struct mode *mode); | ||
| 49 | void modeOff(); | ||
| 50 | void modeSolid(); | ||
| 51 | void modeRainbow(); | ||
| 52 | void modeRainbowFast(); | ||
| 53 | void modeStrobo(); | ||
| 54 | |||
| 55 | struct mode { | ||
| 56 | const char *name; | ||
| 57 | void (*func)(); | ||
| 58 | }; | ||
| 59 | |||
| 60 | struct mode modes[] = { | ||
| 61 | { "off", modeOff }, | ||
| 62 | { "solid", modeSolid }, | ||
| 63 | { "rainbow", modeRainbow }, | ||
| 64 | { "rainbowfast", modeRainbowFast }, | ||
| 65 | { "strobo", modeStrobo }, | ||
| 66 | }; | ||
| 67 | |||
| 68 | struct | ||
| 69 | { | ||
| 70 | struct mode *mode = &modes[0]; | ||
| 71 | uint32_t color = CRGB::Red; | ||
| 72 | uint8_t brightness = 204; // 80% | ||
| 73 | } set; | ||
| 74 | |||
| 75 | struct | ||
| 76 | { | ||
| 77 | uint8_t brightness; | ||
| 78 | bool idle = false; | ||
| 79 | } current; | ||
| 80 | |||
| 81 | void setup() | ||
| 82 | { | ||
| 83 | Serial.begin(115200); | ||
| 84 | pinMode(LED_BUILTIN, OUTPUT); | ||
| 85 | |||
| 86 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, RGB_NUM_LEDS); | ||
| 87 | current.brightness = set.brightness; | ||
| 88 | // update leds | ||
| 89 | set.mode->func(); | ||
| 90 | FastLED.show(); | ||
| 91 | |||
| 92 | WiFiManager wifiManager; | ||
| 93 | wifiManager.setConfigPortalTimeout(600); | ||
| 94 | Serial.printf_P(PSTR("Setting up WiFi\n")); | ||
| 95 | if (!wifiManager.autoConnect("ESP8266_DR_DESK")) { | ||
| 96 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); | ||
| 97 | delay(5000); | ||
| 98 | ESP.restart(); | ||
| 99 | } | ||
| 100 | |||
| 101 | yield(); | ||
| 102 | checkFirmwareUpdate(); | ||
| 103 | yield(); | ||
| 104 | |||
| 105 | client.setServer(mqtt_server, mqtt_port); | ||
| 106 | client.setCallback(callback); | ||
| 107 | |||
| 108 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default | ||
| 109 | } | ||
| 110 | |||
| 111 | void reconnect() | ||
| 112 | { | ||
| 113 | // Loop until we're reconnected | ||
| 114 | while (!client.connected()) | ||
| 115 | { | ||
| 116 | // Create a random client ID | ||
| 117 | String clientId = "ESP8266Client-"; | ||
| 118 | clientId += String(random(0xffff), HEX); | ||
| 119 | |||
| 120 | // Attempt to connect | ||
| 121 | if (client.connect(clientId.c_str())) | ||
| 122 | { | ||
| 123 | // Once connected, publish an announcement... | ||
| 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 | void callback(char *topic, uint8_t *payload, unsigned int length) | ||
| 152 | { | ||
| 153 | uint8_t c_payload[length]; | ||
| 154 | memcpy(c_payload, payload, length); | ||
| 155 | c_payload[length] = '\0'; | ||
| 156 | |||
| 157 | if (strcmp(topic, mqtt_pingall_sub) == 0) | ||
| 158 | { | ||
| 159 | blink(); | ||
| 160 | client.publish(mqtt_pingall_pub, | ||
| 161 | "{\"diningroom_desk\":\"connected\"}"); | ||
| 162 | } | ||
| 163 | else if (strcmp(topic, mqtt_mode_sub) == 0) | ||
| 164 | { | ||
| 165 | for (uint8_t i = 0; i < NUM(modes); ++i) | ||
| 166 | { | ||
| 167 | if (strcmp(modes[i].name, (char *)c_payload) != 0) | ||
| 168 | continue; | ||
| 169 | switchMode(&modes[i]); | ||
| 170 | break; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | else if (strcmp(topic, mqtt_color_sub) == 0) | ||
| 174 | { | ||
| 175 | // switch from OFF to SOLID | ||
| 176 | if (set.mode == &modes[0]) | ||
| 177 | switchMode(&modes[1]); | ||
| 178 | |||
| 179 | set.color = atoi((char *)c_payload); | ||
| 180 | current.idle = false; | ||
| 181 | itoa(set.color, convBuffer, 10); | ||
| 182 | client.publish(mqtt_color_pub, convBuffer, true); | ||
| 183 | } | ||
| 184 | else if (strcmp(topic, mqtt_brightness_sub) == 0) | ||
| 185 | { | ||
| 186 | set.brightness = map(atoi((char *)c_payload), 0, 100, 0, 255); | ||
| 187 | current.idle = false; | ||
| 188 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); | ||
| 189 | client.publish(mqtt_brightness_pub, convBuffer, true); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 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() | ||
| 202 | { | ||
| 203 | #define FADE_STEP 10 | ||
| 204 | if (current.brightness == set.brightness) | ||
| 205 | return; | ||
| 206 | int fadeAmount = set.brightness - current.brightness; | ||
| 207 | if (abs(fadeAmount) > FADE_STEP) | ||
| 208 | fadeAmount = (fadeAmount > 0) ? FADE_STEP : -FADE_STEP; | ||
| 209 | current.brightness += fadeAmount; | ||
| 210 | } | ||
| 211 | |||
| 212 | void switchMode(struct mode *mode) | ||
| 213 | { | ||
| 214 | if (set.mode == mode) | ||
| 215 | return; | ||
| 216 | Serial.print("Switching mode to "); | ||
| 217 | Serial.println(mode->name); | ||
| 218 | set.mode = mode; | ||
| 219 | current.idle = false; | ||
| 220 | client.publish(mqtt_mode_pub, set.mode->name, true); | ||
| 221 | } | ||
| 222 | |||
| 223 | void modeOff() | ||
| 224 | { | ||
| 225 | fill_solid(leds, RGB_NUM_LEDS, CRGB::Black); | ||
| 226 | current.idle = true; | ||
| 227 | } | ||
| 228 | |||
| 229 | void modeSolid() | ||
| 230 | { | ||
| 231 | fill_solid(leds, RGB_NUM_LEDS, set.color); | ||
| 232 | calcBrightness(); | ||
| 233 | nscale8_video(leds, RGB_NUM_LEDS, current.brightness); | ||
| 234 | current.idle = (current.brightness == set.brightness); | ||
| 235 | } | ||
| 236 | |||
| 237 | void modeRainbow() | ||
| 238 | { | ||
| 239 | static CRGBPalette16 palette = RainbowColors_p; | ||
| 240 | static uint8_t hue = 0; | ||
| 241 | calcBrightness(); | ||
| 242 | for (int i = 0; i < RGB_NUM_LEDS; ++i) | ||
| 243 | leds[i] = ColorFromPalette(palette, hue, current.brightness); | ||
| 244 | hue++; | ||
| 245 | } | ||
| 246 | |||
| 247 | void modeRainbowFast() | ||
| 248 | { | ||
| 249 | static CRGBPalette16 palette = RainbowColors_p; | ||
| 250 | static uint8_t hue = 0; | ||
| 251 | calcBrightness(); | ||
| 252 | for (int i = 0; i < RGB_NUM_LEDS; ++i) | ||
| 253 | leds[i] = ColorFromPalette(palette, hue, current.brightness); | ||
| 254 | hue+=5; | ||
| 255 | } | ||
| 256 | |||
| 257 | void modeStrobo() | ||
| 258 | { | ||
| 259 | static bool state = 0; | ||
| 260 | if (set.color == CRGB::Black) | ||
| 261 | return; | ||
| 262 | fill_solid(leds, RGB_NUM_LEDS, (state) ? set.color : CRGB::Black); | ||
| 263 | calcBrightness(); | ||
| 264 | nscale8_video(leds, RGB_NUM_LEDS, current.brightness); | ||
| 265 | state = !state; | ||
| 266 | } | ||
| 267 | |||
| 268 | void checkFirmwareUpdate() | ||
| 269 | { | ||
| 270 | BearSSL::WiFiClientSecure update_client; | ||
| 271 | update_client.setInsecure(); | ||
| 272 | |||
| 273 | client.publish(mqtt_device_boot, "fwupdate running"); | ||
| 274 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | ||
| 275 | ESPhttpUpdate.rebootOnUpdate(true); | ||
| 276 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | ||
| 277 | switch(ret) | ||
| 278 | { | ||
| 279 | case HTTP_UPDATE_FAILED: | ||
| 280 | { | ||
| 281 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | ||
| 282 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | ||
| 283 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | ||
| 284 | client.publish(mqtt_device_boot, tmp.c_str()); | ||
| 285 | } | ||
| 286 | break; | ||
| 287 | |||
| 288 | case HTTP_UPDATE_NO_UPDATES: | ||
| 289 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | ||
| 290 | client.publish(mqtt_device_boot, "fwupdate noupdates"); | ||
| 291 | break; | ||
| 292 | |||
| 293 | case HTTP_UPDATE_OK: | ||
| 294 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | ||
| 295 | client.publish(mqtt_device_boot, "fwupdate ok"); | ||
| 296 | break; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | void loop() | ||
| 301 | { | ||
| 302 | if (!client.connected()) | ||
| 303 | reconnect(); | ||
| 304 | client.loop(); | ||
| 305 | |||
| 306 | EVERY_N_MILLISECONDS(100) | ||
| 307 | { | ||
| 308 | if (!current.idle) | ||
| 309 | { | ||
| 310 | set.mode->func(); | ||
| 311 | FastLED.show(); | ||
| 312 | } | ||
| 313 | if (!current.idle && current.brightness == 0) | ||
| 314 | current.idle = true; | ||
| 315 | } | ||
| 316 | } | ||
