summaryrefslogtreecommitdiffstats
path: root/oliver/dr_desk
diff options
context:
space:
mode:
Diffstat (limited to 'oliver/dr_desk')
-rw-r--r--oliver/dr_desk/.gitignore5
-rw-r--r--oliver/dr_desk/platformio.ini19
-rw-r--r--oliver/dr_desk/src/main.cpp326
3 files changed, 0 insertions, 350 deletions
diff --git a/oliver/dr_desk/.gitignore b/oliver/dr_desk/.gitignore
deleted file mode 100644
index 89cc49c..0000000
--- a/oliver/dr_desk/.gitignore
+++ /dev/null
@@ -1,5 +0,0 @@
1.pio
2.vscode/.browse.c_cpp.db*
3.vscode/c_cpp_properties.json
4.vscode/launch.json
5.vscode/ipch
diff --git a/oliver/dr_desk/platformio.ini b/oliver/dr_desk/platformio.ini
deleted file mode 100644
index e5c54f1..0000000
--- a/oliver/dr_desk/platformio.ini
+++ /dev/null
@@ -1,19 +0,0 @@
1; PlatformIO Project Configuration File
2;
3; Build options: build flags, source filter, extra scripting
4; Upload options: custom port, speed and extra flags
5; Library options: dependencies, extra library storages
6;
7; Please visit documentation for the other options and examples
8; http://docs.platformio.org/en/stable/projectconf.html
9
10[platformio]
11default_envs = nodemcuv2
12
13[env:nodemcuv2]
14platform = espressif8266
15board = nodemcuv2
16lib_deps = WiFiManager, PubSubClient, FastLED
17framework = arduino
18monitor_speed = 115200
19upload_speed = 115200
diff --git a/oliver/dr_desk/src/main.cpp b/oliver/dr_desk/src/main.cpp
deleted file mode 100644
index 8791c80..0000000
--- a/oliver/dr_desk/src/main.cpp
+++ /dev/null
@@ -1,326 +0,0 @@
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/desk"
15
16const char* mqtt_mode_sub = MQTT_BASE;
17const char* mqtt_mode_pub = MQTT_BASE "/status";
18
19const char* mqtt_color_sub = MQTT_BASE "/color";
20const char* mqtt_color_pub = MQTT_BASE "/color/status";
21
22const char* mqtt_brightness_sub = MQTT_BASE "/brightness";
23const 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/olidrdesk.bin
34// - reboot device or send "fwupdate" to mqtt_topic_cmd
35// - fw update state is published in mqtt_topic_state
36#define FIRMWARE_VERSION 1
37//#define FIRMWARE_URL ""
38
39#define _STR(s) #s
40#define STR(s) _STR(s)
41
42const String mqtt_clientId = "DrDesk-" + String(ESP.getChipId())
43 + "/v" + STR(FIRMWARE_VERSION);
44const char* mqtt_topic_ping = MQTT_BASE "/ping";
45const char* mqtt_topic_cmd = MQTT_BASE "/command";
46const String mqtt_topic_state = String(MQTT_BASE) + "/" + String(ESP.getChipId());
47
48bool mqttLoop();
49void mqttCallback(char *topic, byte *payload, unsigned int length);
50void checkFirmwareUpdate();
51
52void switchMode(struct mode *mode);
53void modeOff();
54void modeSolid();
55void modeRainbow();
56void modeRainbowFast();
57void modeStrobo();
58
59WiFiClient wifi_client;
60PubSubClient mqtt(wifi_client);
61char convBuffer[10];
62CRGB leds[RGB_NUM_LEDS];
63
64struct mode {
65 const char *name;
66 void (*func)();
67};
68
69struct mode modes[] = {
70 { "off", modeOff },
71 { "solid", modeSolid },
72 { "rainbow", modeRainbow },
73 { "rainbowfast", modeRainbowFast },
74 { "strobo", modeStrobo },
75};
76
77struct
78{
79 struct mode *mode = &modes[0];
80 uint32_t color = CRGB::Red;
81 uint8_t brightness = 204; // 80%
82} set;
83
84struct
85{
86 uint8_t brightness;
87 bool idle = false;
88} current;
89
90void 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
122bool 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 return false;
129
130 Serial.printf_P(PSTR("MQTT connected\n"));
131 mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str());
132 mqtt.subscribe(mqtt_topic_ping);
133 mqtt.subscribe(mqtt_topic_cmd);
134
135 // publish states
136 Serial.println(mqtt_mode_sub);
137 mqtt.subscribe(mqtt_mode_sub);
138 mqtt.publish(mqtt_mode_pub, set.mode->name, true);
139
140 Serial.println(mqtt_color_sub);
141 mqtt.subscribe(mqtt_color_sub);
142 itoa(set.color, convBuffer, 10);
143 mqtt.publish(mqtt_color_pub, convBuffer, true);
144
145 Serial.println(mqtt_brightness_sub);
146 mqtt.subscribe(mqtt_brightness_sub);
147 itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10);
148 mqtt.publish(mqtt_brightness_pub, convBuffer, true);
149 }
150
151 yield();
152 mqtt.loop();
153 return true;
154}
155
156void mqttCallback(char *topic, byte *payload, unsigned int length)
157{
158 char c_payload[length + 1];
159 memcpy(c_payload, payload, length);
160 c_payload[length] = '\0';
161
162 if (!strcmp(topic, mqtt_topic_ping))
163 {
164 if (!strcmp(c_payload, "ping"))
165 mqtt.publish(mqtt_topic_ping, mqtt_clientId.c_str());
166 return;
167 }
168 else if (!strcmp(topic, mqtt_topic_cmd))
169 {
170 if (!strcmp(c_payload, "fwupdate"))
171 {
172 checkFirmwareUpdate();
173 return;
174 }
175 else if (!strcmp(c_payload, "reset"))
176 {
177 Serial.printf_P(PSTR("Resetting\n"));
178 ESP.reset();
179 return;
180 }
181 }
182 else if (strcmp(topic, mqtt_mode_sub) == 0)
183 {
184 for (uint8_t i = 0; i < NUM(modes); ++i)
185 {
186 if (strcmp(modes[i].name, (char *)c_payload) != 0)
187 continue;
188 switchMode(&modes[i]);
189 break;
190 }
191 }
192 else if (strcmp(topic, mqtt_color_sub) == 0)
193 {
194 // switch from OFF to SOLID
195 if (set.mode == &modes[0])
196 switchMode(&modes[1]);
197
198 set.color = atoi((char *)c_payload);
199 current.idle = false;
200 itoa(set.color, convBuffer, 10);
201 mqtt.publish(mqtt_color_pub, convBuffer, true);
202 }
203 else if (strcmp(topic, mqtt_brightness_sub) == 0)
204 {
205 set.brightness = map(atoi((char *)c_payload), 0, 100, 0, 255);
206 current.idle = false;
207 itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10);
208 mqtt.publish(mqtt_brightness_pub, convBuffer, true);
209 }
210}
211
212void calcBrightness()
213{
214 #define FADE_STEP 10
215 if (current.brightness == set.brightness)
216 return;
217 int fadeAmount = set.brightness - current.brightness;
218 if (abs(fadeAmount) > FADE_STEP)
219 fadeAmount = (fadeAmount > 0) ? FADE_STEP : -FADE_STEP;
220 current.brightness += fadeAmount;
221}
222
223void switchMode(struct mode *mode)
224{
225 if (set.mode == mode)
226 return;
227 Serial.print("Switching mode to ");
228 Serial.println(mode->name);
229 set.mode = mode;
230 current.idle = false;
231 mqtt.publish(mqtt_mode_pub, set.mode->name, true);
232}
233
234void modeOff()
235{
236 fill_solid(leds, RGB_NUM_LEDS, CRGB::Black);
237 current.idle = true;
238}
239
240void modeSolid()
241{
242 fill_solid(leds, RGB_NUM_LEDS, set.color);
243 calcBrightness();
244 nscale8_video(leds, RGB_NUM_LEDS, current.brightness);
245 current.idle = (current.brightness == set.brightness);
246}
247
248void modeRainbow()
249{
250 static CRGBPalette16 palette = RainbowColors_p;
251 static uint8_t hue = 0;
252 calcBrightness();
253 for (int i = 0; i < RGB_NUM_LEDS; ++i)
254 leds[i] = ColorFromPalette(palette, hue, current.brightness);
255 hue++;
256}
257
258void modeRainbowFast()
259{
260 static CRGBPalette16 palette = RainbowColors_p;
261 static uint8_t hue = 0;
262 calcBrightness();
263 for (int i = 0; i < RGB_NUM_LEDS; ++i)
264 leds[i] = ColorFromPalette(palette, hue, current.brightness);
265 hue+=5;
266}
267
268void modeStrobo()
269{
270 static bool state = 0;
271 if (set.color == CRGB::Black)
272 return;
273 fill_solid(leds, RGB_NUM_LEDS, (state) ? set.color : CRGB::Black);
274 calcBrightness();
275 nscale8_video(leds, RGB_NUM_LEDS, current.brightness);
276 state = !state;
277}
278
279void checkFirmwareUpdate()
280{
281 BearSSL::WiFiClientSecure update_client;
282 update_client.setInsecure();
283
284 mqtt.publish(mqtt_topic_state.c_str(), "fwupdate running");
285 ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH);
286 ESPhttpUpdate.rebootOnUpdate(true);
287 t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION));
288 switch(ret)
289 {
290 case HTTP_UPDATE_FAILED:
291 {
292 Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"),
293 ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
294 String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString();
295 mqtt.publish(mqtt_topic_state.c_str(), tmp.c_str());
296 }
297 break;
298
299 case HTTP_UPDATE_NO_UPDATES:
300 Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n"));
301 mqtt.publish(mqtt_topic_state.c_str(), "fwupdate noupdates");
302 break;
303
304 case HTTP_UPDATE_OK:
305 Serial.printf_P(PSTR("HTTP_UPDATE_OK\n"));
306 mqtt.publish(mqtt_topic_state.c_str(), "fwupdate ok");
307 break;
308 }
309}
310
311void loop()
312{
313 if (!mqttLoop())
314 delay(5000);
315
316 EVERY_N_MILLISECONDS(100)
317 {
318 if (!current.idle)
319 {
320 set.mode->func();
321 FastLED.show();
322 }
323 if (!current.idle && current.brightness == 0)
324 current.idle = true;
325 }
326}