diff options
Diffstat (limited to 'oliver')
| -rw-r--r-- | oliver/dr_desk/platformio.ini | 7 | ||||
| -rw-r--r-- | oliver/dr_desk/src/dr_desk.cpp | 89 | ||||
| -rw-r--r-- | oliver/lr_stripes/platformio.ini | 7 | ||||
| -rw-r--r-- | oliver/lr_stripes/src/lr_stripes.cpp | 85 |
4 files changed, 118 insertions, 70 deletions
diff --git a/oliver/dr_desk/platformio.ini b/oliver/dr_desk/platformio.ini index 0f31fed..9ad2883 100644 --- a/oliver/dr_desk/platformio.ini +++ b/oliver/dr_desk/platformio.ini | |||
| @@ -14,8 +14,5 @@ env_default = nodemcuv2 | |||
| 14 | platform = espressif8266 | 14 | platform = espressif8266 |
| 15 | board = nodemcuv2 | 15 | board = nodemcuv2 |
| 16 | framework = arduino | 16 | framework = arduino |
| 17 | ;upload_flags = --port=8266 --auth=ESP8266_PASSWORD | 17 | monitor_speed = 115200 |
| 18 | ;upload_port = IP (Set fixed IP in your router first!!!) | 18 | upload_speed = 115200 |
| 19 | |||
| 20 | [platformio] | ||
| 21 | lib_dir=/home/manuel/coding/Arduino/libraries | ||
diff --git a/oliver/dr_desk/src/dr_desk.cpp b/oliver/dr_desk/src/dr_desk.cpp index 46bf8eb..6f31b76 100644 --- a/oliver/dr_desk/src/dr_desk.cpp +++ b/oliver/dr_desk/src/dr_desk.cpp | |||
| @@ -1,15 +1,16 @@ | |||
| 1 | #include <Arduino.h> | 1 | #include <Arduino.h> |
| 2 | #include <ESP8266mDNS.h> | 2 | #include <ESP8266mDNS.h> |
| 3 | #include <ArduinoOTA.h> | ||
| 4 | #include <PubSubClient.h> | 3 | #include <PubSubClient.h> |
| 5 | #include <WiFiManager.h> | 4 | #include <WiFiManager.h> |
| 6 | #include <FastLED.h> | 5 | #include <FastLED.h> |
| 6 | #include <ESP8266httpUpdate.h> | ||
| 7 | #include "secrets.h" | 7 | #include "secrets.h" |
| 8 | 8 | ||
| 9 | 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 | #define _STR(s) #s |
| 10 | const char* autoconf_pwd = AUTOCONF_PASSWORD; // AP password so noone else can connect to the ESP in case your router fails | 10 | #define STR(s) _STR(s) |
| 11 | const char* mqtt_server = "192.168.1.2"; //MQTT Server IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT | 11 | |
| 12 | const int mqtt_port = 1883; //MQTT Server PORT, default is 1883 but can be anything. | 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. | ||
| 13 | 14 | ||
| 14 | const char* mqtt_pingall_sub = "home/pingall"; | 15 | const char* mqtt_pingall_sub = "home/pingall"; |
| 15 | const char* mqtt_pingall_pub = "home/pingall/response"; | 16 | const char* mqtt_pingall_pub = "home/pingall/response"; |
| @@ -32,11 +33,18 @@ const char* mqtt_brightness_pub = MQTT_BASE "/brightness/status"; | |||
| 32 | #define RGB_COLOR_ORDER GRB | 33 | #define RGB_COLOR_ORDER GRB |
| 33 | #define NUM(a) (sizeof(a) / sizeof(*a)) | 34 | #define NUM(a) (sizeof(a) / sizeof(*a)) |
| 34 | 35 | ||
| 36 | #define FIRMWARE_VERSION 1 | ||
| 37 | //#define FIRMWARE_URL "" | ||
| 38 | |||
| 35 | WiFiClient espClient; | 39 | WiFiClient espClient; |
| 36 | PubSubClient client(espClient); | 40 | PubSubClient client(espClient); |
| 37 | char convBuffer[10]; | 41 | char convBuffer[10]; |
| 38 | CRGB leds[RGB_NUM_LEDS]; | 42 | CRGB leds[RGB_NUM_LEDS]; |
| 39 | 43 | ||
| 44 | void callback(char *topic, uint8_t *payload, unsigned int length); | ||
| 45 | void blink(); | ||
| 46 | void checkFirmwareUpdate(); | ||
| 47 | |||
| 40 | void switchMode(struct mode *mode); | 48 | void switchMode(struct mode *mode); |
| 41 | void modeOff(); | 49 | void modeOff(); |
| 42 | void modeSolid(); | 50 | void modeSolid(); |
| @@ -73,7 +81,7 @@ struct | |||
| 73 | void setup() | 81 | void setup() |
| 74 | { | 82 | { |
| 75 | Serial.begin(115200); | 83 | Serial.begin(115200); |
| 76 | pinMode(BUILTIN_LED, OUTPUT); | 84 | pinMode(LED_BUILTIN, OUTPUT); |
| 77 | 85 | ||
| 78 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, RGB_NUM_LEDS); | 86 | FastLED.addLeds<RGB_CHIPSET, RGB_PIN, RGB_COLOR_ORDER>(leds, RGB_NUM_LEDS); |
| 79 | current.brightness = set.brightness; | 87 | current.brightness = set.brightness; |
| @@ -82,32 +90,22 @@ void setup() | |||
| 82 | FastLED.show(); | 90 | FastLED.show(); |
| 83 | 91 | ||
| 84 | WiFiManager wifiManager; | 92 | WiFiManager wifiManager; |
| 85 | wifiManager.autoConnect(autoconf_ssid, autoconf_pwd); | 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 | } | ||
| 86 | 100 | ||
| 87 | setup_ota(); | 101 | yield(); |
| 102 | checkFirmwareUpdate(); | ||
| 103 | yield(); | ||
| 88 | 104 | ||
| 89 | client.setServer(mqtt_server, mqtt_port); | 105 | client.setServer(mqtt_server, mqtt_port); |
| 90 | client.setCallback(callback); | 106 | client.setCallback(callback); |
| 91 | 107 | ||
| 92 | digitalWrite(BUILTIN_LED, HIGH); //Turn off led as default | 108 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default |
| 93 | } | ||
| 94 | |||
| 95 | void setup_ota() | ||
| 96 | { | ||
| 97 | // Set OTA Password, and change it in platformio.ini | ||
| 98 | ArduinoOTA.setPassword(OTA_PASSWORD); | ||
| 99 | ArduinoOTA.onStart([]() {}); | ||
| 100 | ArduinoOTA.onEnd([]() {}); | ||
| 101 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {}); | ||
| 102 | ArduinoOTA.onError([](ota_error_t error) | ||
| 103 | { | ||
| 104 | if (error == OTA_AUTH_ERROR); // Auth failed | ||
| 105 | else if (error == OTA_BEGIN_ERROR); // Begin failed | ||
| 106 | else if (error == OTA_CONNECT_ERROR); // Connect failed | ||
| 107 | else if (error == OTA_RECEIVE_ERROR); // Receive failed | ||
| 108 | else if (error == OTA_END_ERROR); // End failed | ||
| 109 | }); | ||
| 110 | ArduinoOTA.begin(); | ||
| 111 | } | 109 | } |
| 112 | 110 | ||
| 113 | void reconnect() | 111 | void reconnect() |
| @@ -195,9 +193,9 @@ void callback(char *topic, uint8_t *payload, unsigned int length) | |||
| 195 | void blink() | 193 | void blink() |
| 196 | { | 194 | { |
| 197 | //Blink on received MQTT message | 195 | //Blink on received MQTT message |
| 198 | digitalWrite(BUILTIN_LED, LOW); | 196 | digitalWrite(LED_BUILTIN, LOW); |
| 199 | delay(25); | 197 | delay(25); |
| 200 | digitalWrite(BUILTIN_LED, HIGH); | 198 | digitalWrite(LED_BUILTIN, HIGH); |
| 201 | } | 199 | } |
| 202 | 200 | ||
| 203 | void calcBrightness() | 201 | void calcBrightness() |
| @@ -267,12 +265,43 @@ void modeStrobo() | |||
| 267 | state = !state; | 265 | state = !state; |
| 268 | } | 266 | } |
| 269 | 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 | |||
| 270 | void loop() | 300 | void loop() |
| 271 | { | 301 | { |
| 272 | if (!client.connected()) | 302 | if (!client.connected()) |
| 273 | reconnect(); | 303 | reconnect(); |
| 274 | client.loop(); | 304 | client.loop(); |
| 275 | ArduinoOTA.handle(); | ||
| 276 | 305 | ||
| 277 | EVERY_N_MILLISECONDS(100) | 306 | EVERY_N_MILLISECONDS(100) |
| 278 | { | 307 | { |
diff --git a/oliver/lr_stripes/platformio.ini b/oliver/lr_stripes/platformio.ini index 0f31fed..9ad2883 100644 --- a/oliver/lr_stripes/platformio.ini +++ b/oliver/lr_stripes/platformio.ini | |||
| @@ -14,8 +14,5 @@ env_default = nodemcuv2 | |||
| 14 | platform = espressif8266 | 14 | platform = espressif8266 |
| 15 | board = nodemcuv2 | 15 | board = nodemcuv2 |
| 16 | framework = arduino | 16 | framework = arduino |
| 17 | ;upload_flags = --port=8266 --auth=ESP8266_PASSWORD | 17 | monitor_speed = 115200 |
| 18 | ;upload_port = IP (Set fixed IP in your router first!!!) | 18 | upload_speed = 115200 |
| 19 | |||
| 20 | [platformio] | ||
| 21 | lib_dir=/home/manuel/coding/Arduino/libraries | ||
diff --git a/oliver/lr_stripes/src/lr_stripes.cpp b/oliver/lr_stripes/src/lr_stripes.cpp index 7059f8b..6ad4282 100644 --- a/oliver/lr_stripes/src/lr_stripes.cpp +++ b/oliver/lr_stripes/src/lr_stripes.cpp | |||
| @@ -1,15 +1,16 @@ | |||
| 1 | #include <Arduino.h> | 1 | #include <Arduino.h> |
| 2 | #include <ESP8266mDNS.h> | 2 | #include <ESP8266mDNS.h> |
| 3 | #include <ArduinoOTA.h> | ||
| 4 | #include <PubSubClient.h> | 3 | #include <PubSubClient.h> |
| 5 | #include <WiFiManager.h> | 4 | #include <WiFiManager.h> |
| 6 | #include <Bounce2.h> | 5 | #include <Bounce2.h> |
| 6 | #include <ESP8266httpUpdate.h> | ||
| 7 | #include "secrets.h" | 7 | #include "secrets.h" |
| 8 | 8 | ||
| 9 | 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 | #define _STR(s) #s |
| 10 | const char* autoconf_pwd = AUTOCONF_PASSWORD; // AP password so noone else can connect to the ESP in case your router fails | 10 | #define STR(s) _STR(s) |
| 11 | const char* mqtt_server = "192.168.1.2"; //MQTT Server IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT | 11 | |
| 12 | const int mqtt_port = 1883; //MQTT Server PORT, default is 1883 but can be anything. | 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. | ||
| 13 | 14 | ||
| 14 | const char* mqtt_pingall_sub = "home/pingall"; | 15 | const char* mqtt_pingall_sub = "home/pingall"; |
| 15 | const char* mqtt_pingall_pub = "home/pingall/response"; | 16 | const char* mqtt_pingall_pub = "home/pingall/response"; |
| @@ -81,17 +82,20 @@ WiFiClient espClient; | |||
| 81 | PubSubClient client(espClient); | 82 | PubSubClient client(espClient); |
| 82 | char convBuffer[10]; | 83 | char convBuffer[10]; |
| 83 | 84 | ||
| 85 | void callback(char* topic, byte* payload, unsigned int length); | ||
| 86 | void blink(); | ||
| 84 | bool relayRead(struct sensor_t *sensor); | 87 | bool relayRead(struct sensor_t *sensor); |
| 85 | void relayWrite(struct sensor_t *sensor, bool state, bool send_update=false); | 88 | void relayWrite(struct sensor_t *sensor, bool state, bool send_update=false); |
| 86 | void flipRelay(struct sensor_t *sensor, bool send_update=false); | 89 | void flipRelay(struct sensor_t *sensor, bool send_update=false); |
| 87 | void checkButtons(void); | 90 | void checkButtons(void); |
| 88 | inline uint8_t pwmValue(uint8_t level); | 91 | inline uint8_t pwmValue(uint8_t level); |
| 89 | void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update=false); | 92 | void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update=false); |
| 93 | void checkFirmwareUpdate(); | ||
| 90 | 94 | ||
| 91 | void setup() | 95 | void setup() |
| 92 | { | 96 | { |
| 93 | Serial.begin(115200); | 97 | Serial.begin(115200); |
| 94 | pinMode(BUILTIN_LED, OUTPUT); | 98 | pinMode(LED_BUILTIN, OUTPUT); |
| 95 | analogWriteRange(100); | 99 | analogWriteRange(100); |
| 96 | 100 | ||
| 97 | // set relay pins to output mode + restore to last known state | 101 | // set relay pins to output mode + restore to last known state |
| @@ -129,32 +133,22 @@ void setup() | |||
| 129 | } | 133 | } |
| 130 | 134 | ||
| 131 | WiFiManager wifiManager; | 135 | WiFiManager wifiManager; |
| 132 | wifiManager.autoConnect(autoconf_ssid, autoconf_pwd); | 136 | wifiManager.setConfigPortalTimeout(600); |
| 137 | Serial.printf_P(PSTR("Setting up WiFi\n")); | ||
| 138 | if (!wifiManager.autoConnect("ESP8266_LR_STRIPES")) { | ||
| 139 | Serial.printf_P(PSTR("Failed to connect and hit timeout\n")); | ||
| 140 | delay(5000); | ||
| 141 | ESP.restart(); | ||
| 142 | } | ||
| 133 | 143 | ||
| 134 | setup_ota(); | 144 | yield(); |
| 145 | checkFirmwareUpdate(); | ||
| 146 | yield(); | ||
| 135 | 147 | ||
| 136 | client.setServer(mqtt_server, mqtt_port); | 148 | client.setServer(mqtt_server, mqtt_port); |
| 137 | client.setCallback(callback); | 149 | client.setCallback(callback); |
| 138 | 150 | ||
| 139 | digitalWrite(BUILTIN_LED, HIGH); //Turn off led as default | 151 | digitalWrite(LED_BUILTIN, HIGH); //Turn off led as default |
| 140 | } | ||
| 141 | |||
| 142 | void setup_ota() | ||
| 143 | { | ||
| 144 | // Set OTA Password, and change it in platformio.ini | ||
| 145 | ArduinoOTA.setPassword(OTA_PASSWORD); | ||
| 146 | ArduinoOTA.onStart([]() {}); | ||
| 147 | ArduinoOTA.onEnd([]() {}); | ||
| 148 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {}); | ||
| 149 | ArduinoOTA.onError([](ota_error_t error) | ||
| 150 | { | ||
| 151 | if (error == OTA_AUTH_ERROR); // Auth failed | ||
| 152 | else if (error == OTA_BEGIN_ERROR); // Begin failed | ||
| 153 | else if (error == OTA_CONNECT_ERROR); // Connect failed | ||
| 154 | else if (error == OTA_RECEIVE_ERROR); // Receive failed | ||
| 155 | else if (error == OTA_END_ERROR); // End failed | ||
| 156 | }); | ||
| 157 | ArduinoOTA.begin(); | ||
| 158 | } | 152 | } |
| 159 | 153 | ||
| 160 | void reconnect() | 154 | void reconnect() |
| @@ -244,9 +238,9 @@ void callback(char* topic, byte* payload, unsigned int length) | |||
| 244 | void blink() | 238 | void blink() |
| 245 | { | 239 | { |
| 246 | //Blink on received MQTT message | 240 | //Blink on received MQTT message |
| 247 | digitalWrite(BUILTIN_LED, LOW); | 241 | digitalWrite(LED_BUILTIN, LOW); |
| 248 | delay(25); | 242 | delay(25); |
| 249 | digitalWrite(BUILTIN_LED, HIGH); | 243 | digitalWrite(LED_BUILTIN, HIGH); |
| 250 | } | 244 | } |
| 251 | 245 | ||
| 252 | void loop() | 246 | void loop() |
| @@ -254,7 +248,6 @@ void loop() | |||
| 254 | if (!client.connected()) | 248 | if (!client.connected()) |
| 255 | reconnect(); | 249 | reconnect(); |
| 256 | client.loop(); | 250 | client.loop(); |
| 257 | ArduinoOTA.handle(); | ||
| 258 | checkButtons(); | 251 | checkButtons(); |
| 259 | } | 252 | } |
| 260 | 253 | ||
| @@ -367,3 +360,35 @@ void fadeDimmer(struct sensor_t *sensor, uint8_t level, bool send_update) | |||
| 367 | client.publish(sensor->dimmer.mqtt_pub, convBuffer, true); | 360 | client.publish(sensor->dimmer.mqtt_pub, convBuffer, true); |
| 368 | } | 361 | } |
| 369 | } | 362 | } |
| 363 | |||
| 364 | void checkFirmwareUpdate() | ||
| 365 | { | ||
| 366 | BearSSL::WiFiClientSecure update_client; | ||
| 367 | update_client.setInsecure(); | ||
| 368 | |||
| 369 | client.publish(mqtt_device_boot, "fwupdate running"); | ||
| 370 | ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH); | ||
| 371 | ESPhttpUpdate.rebootOnUpdate(true); | ||
| 372 | t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION)); | ||
| 373 | switch(ret) | ||
| 374 | { | ||
| 375 | case HTTP_UPDATE_FAILED: | ||
| 376 | { | ||
| 377 | Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"), | ||
| 378 | ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | ||
| 379 | String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString(); | ||
| 380 | client.publish(mqtt_device_boot, tmp.c_str()); | ||
| 381 | } | ||
| 382 | break; | ||
| 383 | |||
| 384 | case HTTP_UPDATE_NO_UPDATES: | ||
| 385 | Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n")); | ||
| 386 | client.publish(mqtt_device_boot, "fwupdate noupdates"); | ||
| 387 | break; | ||
| 388 | |||
| 389 | case HTTP_UPDATE_OK: | ||
| 390 | Serial.printf_P(PSTR("HTTP_UPDATE_OK\n")); | ||
| 391 | client.publish(mqtt_device_boot, "fwupdate ok"); | ||
| 392 | break; | ||
| 393 | } | ||
| 394 | } | ||
