diff options
| -rw-r--r-- | oliver/dr_desk/.gitignore | 4 | ||||
| -rw-r--r-- | oliver/dr_desk/dr_desk.ino | 286 | ||||
| -rw-r--r-- | oliver/dr_desk/lib/readme.txt | 36 | ||||
| -rw-r--r-- | oliver/dr_desk/platformio.ini | 21 | ||||
| l--------- | oliver/dr_desk/src/dr_desk.ino | 1 |
5 files changed, 348 insertions, 0 deletions
diff --git a/oliver/dr_desk/.gitignore b/oliver/dr_desk/.gitignore new file mode 100644 index 0000000..5dac9f5 --- /dev/null +++ b/oliver/dr_desk/.gitignore | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | .pioenvs | ||
| 2 | .piolibdeps | ||
| 3 | .clang_complete | ||
| 4 | .gcc-flags.json | ||
diff --git a/oliver/dr_desk/dr_desk.ino b/oliver/dr_desk/dr_desk.ino new file mode 100644 index 0000000..0ff13e2 --- /dev/null +++ b/oliver/dr_desk/dr_desk.ino | |||
| @@ -0,0 +1,286 @@ | |||
| 1 | #include <ESP8266mDNS.h> | ||
| 2 | #include <ArduinoOTA.h> | ||
| 3 | #include <PubSubClient.h> | ||
| 4 | #include <WiFiManager.h> | ||
| 5 | #include <FastLED.h> | ||
| 6 | #include "secrets.h" | ||
| 7 | |||
| 8 | 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 | ||
| 9 | const char* autoconf_pwd = AUTOCONF_PASSWORD; // AP password so noone else can connect to the ESP in case your router fails | ||
| 10 | const char* mqtt_server = "192.168.1.2"; //MQTT Server IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT | ||
| 11 | const int mqtt_port = 1883; //MQTT Server PORT, default is 1883 but can be anything. | ||
| 12 | |||
| 13 | const char* mqtt_pingall_sub = "home/pingall"; | ||
| 14 | const char* mqtt_pingall_pub = "home/pingall/response"; | ||
| 15 | |||
| 16 | #define MQTT_BASE "home/diningroom/desk" | ||
| 17 | const char* mqtt_device_boot = MQTT_BASE "/device"; | ||
| 18 | |||
| 19 | const char* mqtt_mode_sub = MQTT_BASE; | ||
| 20 | const char* mqtt_mode_pub = MQTT_BASE "/status"; | ||
| 21 | |||
| 22 | const char* mqtt_color_sub = MQTT_BASE "/color"; | ||
| 23 | const char* mqtt_color_pub = MQTT_BASE "/color/status"; | ||
| 24 | |||
| 25 | const char* mqtt_brightness_sub = MQTT_BASE "/brightness"; | ||
| 26 | const char* mqtt_brightness_pub = MQTT_BASE "/brightness/status"; | ||
| 27 | |||
| 28 | #define RGB_PIN 2 | ||
| 29 | #define RGB_NUM_LEDS 12 | ||
| 30 | #define RGB_CHIPSET WS2812B | ||
| 31 | #define RGB_COLOR_ORDER GRB | ||
| 32 | #define NUM(a) (sizeof(a) / sizeof(*a)) | ||
| 33 | |||
| 34 | WiFiClient espClient; | ||
| 35 | PubSubClient client(espClient); | ||
| 36 | char convBuffer[10]; | ||
| 37 | CRGB leds[RGB_NUM_LEDS]; | ||
| 38 | |||
| 39 | void switchMode(struct mode *mode); | ||
| 40 | void modeOff(); | ||
| 41 | void modeSolid(); | ||
| 42 | void modeRainbow(); | ||
| 43 | void modeRainbowFast(); | ||
| 44 | void modeStrobo(); | ||
| 45 | |||
| 46 | struct mode { | ||
| 47 | const char *name; | ||
| 48 | void (*func)(); | ||
| 49 | }; | ||
| 50 | |||
| 51 | struct mode modes[] = { | ||
| 52 | { "off", modeOff }, | ||
| 53 | { "solid", modeSolid }, | ||
| 54 | { "rainbow", modeRainbow }, | ||
| 55 | { "rainbowfast", modeRainbowFast }, | ||
| 56 | { "strobo", modeStrobo }, | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct | ||
| 60 | { | ||
| 61 | struct mode *mode = &modes[0]; | ||
| 62 | uint32_t color = CRGB::Red; | ||
| 63 | uint8_t brightness = 204; // 80% | ||
| 64 | } set; | ||
| 65 | |||
| 66 | struct | ||
| 67 | { | ||
| 68 | uint8_t brightness; | ||
| 69 | bool idle = false; | ||
| 70 | } current; | ||
| 71 | |||
| 72 | void setup() | ||
| 73 | { | ||
| 74 | Serial.begin(115200); | ||
| 75 | pinMode(BUILTIN_LED, OUTPUT); | ||
| 76 | |||
| 77 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, RGB_NUM_LEDS); | ||
| 78 | current.brightness = set.brightness; | ||
| 79 | // update leds | ||
| 80 | set.mode->func(); | ||
| 81 | FastLED.show(); | ||
| 82 | |||
| 83 | WiFiManager wifiManager; | ||
| 84 | wifiManager.autoConnect(autoconf_ssid, autoconf_pwd); | ||
| 85 | |||
| 86 | setup_ota(); | ||
| 87 | |||
| 88 | client.setServer(mqtt_server, mqtt_port); | ||
| 89 | client.setCallback(callback); | ||
| 90 | |||
| 91 | digitalWrite(BUILTIN_LED, HIGH); //Turn off led as default | ||
| 92 | } | ||
| 93 | |||
| 94 | void setup_ota() | ||
| 95 | { | ||
| 96 | // Set OTA Password, and change it in platformio.ini | ||
| 97 | ArduinoOTA.setPassword(OTA_PASSWORD); | ||
| 98 | ArduinoOTA.onStart([]() {}); | ||
| 99 | ArduinoOTA.onEnd([]() {}); | ||
| 100 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {}); | ||
| 101 | ArduinoOTA.onError([](ota_error_t error) | ||
| 102 | { | ||
| 103 | if (error == OTA_AUTH_ERROR); // Auth failed | ||
| 104 | else if (error == OTA_BEGIN_ERROR); // Begin failed | ||
| 105 | else if (error == OTA_CONNECT_ERROR); // Connect failed | ||
| 106 | else if (error == OTA_RECEIVE_ERROR); // Receive failed | ||
| 107 | else if (error == OTA_END_ERROR); // End failed | ||
| 108 | }); | ||
| 109 | ArduinoOTA.begin(); | ||
| 110 | } | ||
| 111 | |||
| 112 | void reconnect() | ||
| 113 | { | ||
| 114 | // Loop until we're reconnected | ||
| 115 | while (!client.connected()) | ||
| 116 | { | ||
| 117 | // Create a random client ID | ||
| 118 | String clientId = "ESP8266Client-"; | ||
| 119 | clientId += String(random(0xffff), HEX); | ||
| 120 | |||
| 121 | // Attempt to connect | ||
| 122 | if (client.connect(clientId.c_str())) | ||
| 123 | { | ||
| 124 | // Once connected, publish an announcement... | ||
| 125 | client.publish(mqtt_device_boot, "connected"); | ||
| 126 | // ... and resubscribe | ||
| 127 | client.subscribe(mqtt_pingall_sub); | ||
| 128 | |||
| 129 | // publish states | ||
| 130 | Serial.println(mqtt_mode_sub); | ||
| 131 | client.subscribe(mqtt_mode_sub); | ||
| 132 | client.publish(mqtt_mode_pub, set.mode->name, true); | ||
| 133 | |||
| 134 | Serial.println(mqtt_color_sub); | ||
| 135 | client.subscribe(mqtt_color_sub); | ||
| 136 | itoa(set.color, convBuffer, 10); | ||
| 137 | client.publish(mqtt_color_pub, convBuffer, true); | ||
| 138 | |||
| 139 | Serial.println(mqtt_brightness_sub); | ||
| 140 | client.subscribe(mqtt_brightness_sub); | ||
| 141 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); | ||
| 142 | client.publish(mqtt_brightness_pub, convBuffer, true); | ||
| 143 | } | ||
| 144 | else | ||
| 145 | { | ||
| 146 | // Wait 5 seconds before retrying | ||
| 147 | delay(5000); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | void callback(char *topic, uint8_t *payload, unsigned int length) | ||
| 153 | { | ||
| 154 | uint8_t c_payload[length]; | ||
| 155 | memcpy(c_payload, payload, length); | ||
| 156 | c_payload[length] = '\0'; | ||
| 157 | |||
| 158 | if (strcmp(topic, mqtt_pingall_sub) == 0) | ||
| 159 | { | ||
| 160 | blink(); | ||
| 161 | client.publish(mqtt_pingall_pub, | ||
| 162 | "{\"diningroom_desk\":\"connected\"}"); | ||
| 163 | } | ||
| 164 | else if (strcmp(topic, mqtt_mode_sub) == 0) | ||
| 165 | { | ||
| 166 | for (uint8_t i = 0; i < NUM(modes); ++i) | ||
| 167 | { | ||
| 168 | if (strcmp(modes[i].name, (char *)c_payload) != 0) | ||
| 169 | continue; | ||
| 170 | switchMode(&modes[i]); | ||
| 171 | break; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | else if (strcmp(topic, mqtt_color_sub) == 0) | ||
| 175 | { | ||
| 176 | // switch from OFF to SOLID | ||
| 177 | if (set.mode == &modes[0]) | ||
| 178 | switchMode(&modes[1]); | ||
| 179 | |||
| 180 | set.color = atoi((char *)c_payload); | ||
| 181 | current.idle = false; | ||
| 182 | itoa(set.color, convBuffer, 10); | ||
| 183 | client.publish(mqtt_color_pub, convBuffer, true); | ||
| 184 | } | ||
| 185 | else if (strcmp(topic, mqtt_brightness_sub) == 0) | ||
| 186 | { | ||
| 187 | set.brightness = map(atoi((char *)c_payload), 0, 100, 0, 255); | ||
| 188 | current.idle = false; | ||
| 189 | itoa(map(set.brightness, 0, 255, 0, 100), convBuffer, 10); | ||
| 190 | client.publish(mqtt_brightness_pub, convBuffer, true); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | void blink() | ||
| 195 | { | ||
| 196 | //Blink on received MQTT message | ||
| 197 | digitalWrite(BUILTIN_LED, LOW); | ||
| 198 | delay(25); | ||
| 199 | digitalWrite(BUILTIN_LED, HIGH); | ||
| 200 | } | ||
| 201 | |||
| 202 | void calcBrightness() | ||
| 203 | { | ||
| 204 | #define FADE_STEP 10 | ||
| 205 | if (current.brightness == set.brightness) | ||
| 206 | return; | ||
| 207 | int fadeAmount = set.brightness - current.brightness; | ||
| 208 | if (abs(fadeAmount) > FADE_STEP) | ||
| 209 | fadeAmount = (fadeAmount > 0) ? FADE_STEP : -FADE_STEP; | ||
| 210 | current.brightness += fadeAmount; | ||
| 211 | } | ||
| 212 | |||
| 213 | void switchMode(struct mode *mode) | ||
| 214 | { | ||
| 215 | if (set.mode == mode) | ||
| 216 | return; | ||
| 217 | Serial.print("Switching mode to "); | ||
| 218 | Serial.println(mode->name); | ||
| 219 | set.mode = mode; | ||
| 220 | current.idle = false; | ||
| 221 | client.publish(mqtt_mode_pub, set.mode->name, true); | ||
| 222 | } | ||
| 223 | |||
| 224 | void modeOff() | ||
| 225 | { | ||
| 226 | fill_solid(leds, RGB_NUM_LEDS, CRGB::Black); | ||
| 227 | current.idle = true; | ||
| 228 | } | ||
| 229 | |||
| 230 | void modeSolid() | ||
| 231 | { | ||
| 232 | fill_solid(leds, RGB_NUM_LEDS, set.color); | ||
| 233 | calcBrightness(); | ||
| 234 | nscale8_video(leds, RGB_NUM_LEDS, current.brightness); | ||
| 235 | current.idle = (current.brightness == set.brightness); | ||
| 236 | } | ||
| 237 | |||
| 238 | void modeRainbow() | ||
| 239 | { | ||
| 240 | static CRGBPalette16 palette = RainbowColors_p; | ||
| 241 | static uint8_t hue = 0; | ||
| 242 | calcBrightness(); | ||
| 243 | for (int i = 0; i < RGB_NUM_LEDS; ++i) | ||
| 244 | leds[i] = ColorFromPalette(palette, hue, current.brightness); | ||
| 245 | hue++; | ||
| 246 | } | ||
| 247 | |||
| 248 | void modeRainbowFast() | ||
| 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+=5; | ||
| 256 | } | ||
| 257 | |||
| 258 | void modeStrobo() | ||
| 259 | { | ||
| 260 | static bool state = 0; | ||
| 261 | if (set.color == CRGB::Black) | ||
| 262 | return; | ||
| 263 | fill_solid(leds, RGB_NUM_LEDS, (state) ? set.color : CRGB::Black); | ||
| 264 | calcBrightness(); | ||
| 265 | nscale8_video(leds, RGB_NUM_LEDS, current.brightness); | ||
| 266 | state = !state; | ||
| 267 | } | ||
| 268 | |||
| 269 | void loop() | ||
| 270 | { | ||
| 271 | if (!client.connected()) | ||
| 272 | reconnect(); | ||
| 273 | client.loop(); | ||
| 274 | ArduinoOTA.handle(); | ||
| 275 | |||
| 276 | EVERY_N_MILLISECONDS(100) | ||
| 277 | { | ||
| 278 | if (!current.idle) | ||
| 279 | { | ||
| 280 | set.mode->func(); | ||
| 281 | FastLED.show(); | ||
| 282 | } | ||
| 283 | if (!current.idle && current.brightness == 0) | ||
| 284 | current.idle = true; | ||
| 285 | } | ||
| 286 | } | ||
diff --git a/oliver/dr_desk/lib/readme.txt b/oliver/dr_desk/lib/readme.txt new file mode 100644 index 0000000..dbadc3d --- /dev/null +++ b/oliver/dr_desk/lib/readme.txt | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | |||
| 2 | This directory is intended for the project specific (private) libraries. | ||
| 3 | PlatformIO will compile them to static libraries and link to executable file. | ||
| 4 | |||
| 5 | The source code of each library should be placed in separate directory, like | ||
| 6 | "lib/private_lib/[here are source files]". | ||
| 7 | |||
| 8 | For example, see how can be organized `Foo` and `Bar` libraries: | ||
| 9 | |||
| 10 | |--lib | ||
| 11 | | |--Bar | ||
| 12 | | | |--docs | ||
| 13 | | | |--examples | ||
| 14 | | | |--src | ||
| 15 | | | |- Bar.c | ||
| 16 | | | |- Bar.h | ||
| 17 | | |--Foo | ||
| 18 | | | |- Foo.c | ||
| 19 | | | |- Foo.h | ||
| 20 | | |- readme.txt --> THIS FILE | ||
| 21 | |- platformio.ini | ||
| 22 | |--src | ||
| 23 | |- main.c | ||
| 24 | |||
| 25 | Then in `src/main.c` you should use: | ||
| 26 | |||
| 27 | #include <Foo.h> | ||
| 28 | #include <Bar.h> | ||
| 29 | |||
| 30 | // rest H/C/CPP code | ||
| 31 | |||
| 32 | PlatformIO will find your libraries automatically, configure preprocessor's | ||
| 33 | include paths and build them. | ||
| 34 | |||
| 35 | More information about PlatformIO Library Dependency Finder | ||
| 36 | - http://docs.platformio.org/page/librarymanager/ldf.html | ||
diff --git a/oliver/dr_desk/platformio.ini b/oliver/dr_desk/platformio.ini new file mode 100644 index 0000000..0f31fed --- /dev/null +++ b/oliver/dr_desk/platformio.ini | |||
| @@ -0,0 +1,21 @@ | |||
| 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] | ||
| 11 | env_default = nodemcuv2 | ||
| 12 | |||
| 13 | [env:nodemcuv2] | ||
| 14 | platform = espressif8266 | ||
| 15 | board = nodemcuv2 | ||
| 16 | framework = arduino | ||
| 17 | ;upload_flags = --port=8266 --auth=ESP8266_PASSWORD | ||
| 18 | ;upload_port = IP (Set fixed IP in your router first!!!) | ||
| 19 | |||
| 20 | [platformio] | ||
| 21 | lib_dir=/home/manuel/coding/Arduino/libraries | ||
diff --git a/oliver/dr_desk/src/dr_desk.ino b/oliver/dr_desk/src/dr_desk.ino new file mode 120000 index 0000000..0cdf13b --- /dev/null +++ b/oliver/dr_desk/src/dr_desk.ino | |||
| @@ -0,0 +1 @@ | |||
| ../dr_desk.ino \ No newline at end of file | |||
