update to 2.1.2

This commit is contained in:
BPoH_Voodoo
2018-07-08 21:10:39 +02:00
parent 00f6ab5b58
commit 1bbff1eb4a
8 changed files with 157 additions and 133 deletions
+51 -28
View File
@@ -70,8 +70,8 @@ ESP8266WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81); WebSocketsServer webSocket = WebSocketsServer(81);
#ifdef HTTP_OTA #ifdef HTTP_OTA
#include <ESP8266HTTPUpdateServer.h> #include <ESP8266HTTPUpdateServer.h>
ESP8266HTTPUpdateServer httpUpdater; ESP8266HTTPUpdateServer httpUpdater;
#endif #endif
#ifdef USE_NEOANIMATIONFX #ifdef USE_NEOANIMATIONFX
@@ -100,11 +100,11 @@ NeoAnimationFX<NEOMETHOD> strip(neoStrip);
#endif #endif
#ifdef USE_WS2812FX #ifdef USE_WS2812FX
// *************************************************************************** // ***************************************************************************
// Load libraries / Instanciate WS2812FX library // Load libraries / Instanciate WS2812FX library
// *************************************************************************** // ***************************************************************************
// https://github.com/kitesurfer1404/WS2812FX // https://github.com/kitesurfer1404/WS2812FX
#include "WS2812FX.h" #include "WS2812FX.h"
#ifdef RGBW #ifdef RGBW
WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRBW + NEO_KHZ800); WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRBW + NEO_KHZ800);
@@ -112,13 +112,13 @@ NeoAnimationFX<NEOMETHOD> strip(neoStrip);
WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800); WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800);
#endif #endif
// Parameter 1 = number of pixels in strip // Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid) // Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed: // Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
@@ -348,6 +348,10 @@ DBG_OUTPUT_PORT.println("Starting....");
WiFi.setSleepMode(WIFI_NONE_SLEEP); WiFi.setSleepMode(WIFI_NONE_SLEEP);
// Uncomment if you want to set static IP
// Order is: IP, Gateway and Subnet
//wifiManager.setSTAStaticIPConfig(IPAddress(192,168,0,128), IPAddress(192,168,0,1), IPAddress(255,255,255,0));
//fetches ssid and pass and tries to connect //fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name //if it does not connect it starts an access point with the specified name
//here "AutoConnectAP" //here "AutoConnectAP"
@@ -516,18 +520,21 @@ DBG_OUTPUT_PORT.println("Starting....");
}, handleFileUpload); }, handleFileUpload);
//get heap status, analog input value and all GPIO statuses in one json call //get heap status, analog input value and all GPIO statuses in one json call
server.on("/esp_status", HTTP_GET, []() { server.on("/esp_status", HTTP_GET, []() {
//String json = "{"; DynamicJsonDocument jsonBuffer;
//json += "\"heap\":" + String(ESP.getFreeHeap()); JsonObject& json = jsonBuffer.to<JsonObject>();
//// json += ", \"analog\":" + String(analogRead(A0));
//// json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
//json += "}";
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(9));
JsonObject& json = jsonBuffer.createObject();
json["HOSTNAME"] = HOSTNAME; json["HOSTNAME"] = HOSTNAME;
json["version"] = SKETCH_VERSION; json["version"] = SKETCH_VERSION;
json["heap"] = ESP.getFreeHeap(); json["heap"] = ESP.getFreeHeap();
json["sketch_size"] = ESP.getSketchSize();
json["free_sketch_space"] = ESP.getFreeSketchSpace();
json["flash_chip_size"] = ESP.getFlashChipSize();
json["flash_chip_real_size"] = ESP.getFlashChipRealSize();
json["flash_chip_speed"] = ESP.getFlashChipSpeed();
json["sdk_version"] = ESP.getSdkVersion();
json["core_version"] = ESP.getCoreVersion();
json["cpu_freq"] = ESP.getCpuFreqMHz();
json["chip_id"] = ESP.getFlashChipId();
#ifndef USE_NEOANIMATIONFX #ifndef USE_NEOANIMATIONFX
json["animation_lib"] = "WS2812FX"; json["animation_lib"] = "WS2812FX";
json["pin"] = PIN; json["pin"] = PIN;
@@ -542,22 +549,38 @@ DBG_OUTPUT_PORT.println("Starting....");
json["button_mode"] = "OFF"; json["button_mode"] = "OFF";
#endif #endif
#ifdef ENABLE_AMQTT #ifdef ENABLE_AMQTT
json["amqtt"] = "ON";
#endif
#ifdef ENABLE_MQTT
json["mqtt"] = "ON"; json["mqtt"] = "ON";
#else
json["mqtt"] = "OFF";
#endif #endif
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
json["home_assistant"] = "ON"; json["home_assistant"] = "ON";
#else #else
json["home_assistant"] = "OFF"; json["home_assistant"] = "OFF";
#endif #endif
//char buffer[json.measureLength() + 1]; #ifdef ENABLE_LEGACY_ANIMATIONS
//json.printTo(buffer, sizeof(buffer)); json["legacy_animations"] = "ON";
#else
json["legacy_animations"] = "OFF";
#endif
#ifdef HTTP_OTA
json["esp8266_http_updateserver"] = "ON";
#endif
#ifdef ENABLE_OTA
json["arduino_ota"] = "ON";
#endif
#ifdef ENABLE_STATE_SAVE_SPIFFS
json["state_save"] = "SPIFFS";
#endif
#ifdef ENABLE_STATE_SAVE_EEPROM
json["state_save"] = "EEPROM";
#endif
String json_str; String json_str;
json.printTo(json_str); serializeJson(json, json_str);
server.sendHeader("Access-Control-Allow-Origin", "*"); server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "application/json", json_str); server.send(200, "application/json", json_str);
//json_str = String();
}); });
+69 -87
View File
@@ -97,10 +97,6 @@ void handleSetAllMode(uint8_t * mypayload) {
main_color.green = ((rgb >> 8) & 0xFF); main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb ) & 0xFF); main_color.blue = ((rgb ) & 0xFF);
// for (int i = 0; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue, main_color.white);
// }
// strip.show();
DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: W: [%u] R: [%u] G: [%u] B: [%u]\n", main_color.white, main_color.red, main_color.green, main_color.blue); DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: W: [%u] R: [%u] G: [%u] B: [%u]\n", main_color.white, main_color.red, main_color.green, main_color.blue);
#ifdef ENABLE_LEGACY_ANIMATIONS #ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true; exit_func = true;
@@ -296,18 +292,11 @@ void handleSetWS2812FXMode(uint8_t * mypayload) {
} }
String listStatusJSON(void) { String listStatusJSON(void) {
//char json[255];
//char modeName[30];
uint8_t tmp_mode = (mode == SET_MODE) ? (uint8_t) ws2812fx_mode : strip.getMode(); uint8_t tmp_mode = (mode == SET_MODE) ? (uint8_t) ws2812fx_mode : strip.getMode();
//strncpy_P(modeName, (PGM_P)strip.getModeName(tmp_mode), sizeof(modeName)); // copy from progmem
//snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}",
// mode, tmp_mode, modeName, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue);
const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(6); const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(6);
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonDocument jsonBuffer(bufferSize);
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.to<JsonObject>();
root["mode"] = (uint8_t) mode; root["mode"] = (uint8_t) mode;
root["ws2812fx_mode"] = tmp_mode; root["ws2812fx_mode"] = tmp_mode;
root["ws2812fx_mode_name"] = strip.getModeName(tmp_mode); root["ws2812fx_mode_name"] = strip.getModeName(tmp_mode);
@@ -319,11 +308,8 @@ String listStatusJSON(void) {
color.add(main_color.green); color.add(main_color.green);
color.add(main_color.blue); color.add(main_color.blue);
// char* json = (char*) malloc(root.measureLength() + 1);
// root.printTo(json, sizeof(json));
String json; String json;
root.printTo(json); serializeJson(root, json);
return json; return json;
} }
@@ -334,20 +320,9 @@ void getStatusJSON() {
} }
String listModesJSON(void) { String listModesJSON(void) {
// String modes = "[";
// for (uint8_t i = 0; i < strip.getModeCount(); i++) {
// modes += "{\"mode\":";
// modes += i;
// modes += ", \"name\":\"";
// modes += strip.getModeName(i);
// modes += "\"},";
// }
// modes += "{}]";
// return modes;
const size_t bufferSize = JSON_ARRAY_SIZE(strip.getModeCount()+1) + strip.getModeCount()*JSON_OBJECT_SIZE(2); const size_t bufferSize = JSON_ARRAY_SIZE(strip.getModeCount()+1) + strip.getModeCount()*JSON_OBJECT_SIZE(2);
DynamicJsonBuffer jsonBuffer(bufferSize); DynamicJsonDocument jsonBuffer(bufferSize);
JsonArray& json = jsonBuffer.createArray(); JsonArray& json = jsonBuffer.to<JsonArray>();
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
JsonObject& object = json.createNestedObject(); JsonObject& object = json.createNestedObject();
object["mode"] = i; object["mode"] = i;
@@ -356,7 +331,7 @@ String listModesJSON(void) {
JsonObject& object = json.createNestedObject(); JsonObject& object = json.createNestedObject();
String json_str; String json_str;
json.printTo(json_str); serializeJson(json, json_str);
return json_str; return json_str;
} }
@@ -470,7 +445,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
if (payload[0] == '?') { if (payload[0] == '?') {
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
ws2812fx_speed = constrain(d, 0, 255); ws2812fx_speed = constrain(d, 0, 255);
strip.setSpeed(convertSpeed(ws2812fx_speed)); mode = SETSPEED;
Dbg_Prefix(mqtt, num); Dbg_Prefix(mqtt, num);
DBG_OUTPUT_PORT.printf("Set speed to: [%u]\n", ws2812fx_speed); DBG_OUTPUT_PORT.printf("Set speed to: [%u]\n", ws2812fx_speed);
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
@@ -488,7 +463,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
if (payload[0] == '%') { if (payload[0] == '%') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10); uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
brightness = constrain(b, 0, 255); brightness = constrain(b, 0, 255);
strip.setBrightness(brightness); mode = BRIGHTNESS;
Dbg_Prefix(mqtt, num); Dbg_Prefix(mqtt, num);
DBG_OUTPUT_PORT.printf("Set brightness to: [%u]\n", brightness); DBG_OUTPUT_PORT.printf("Set brightness to: [%u]\n", brightness);
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
@@ -593,25 +568,30 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
// $ ==> Get status Info. // $ ==> Get status Info.
if (payload[0] == '$') { if (payload[0] == '$') {
String json = listStatusJSON(); String json = listStatusJSON();
Dbg_Prefix(mqtt, num); if (mqtt == true) {
DBG_OUTPUT_PORT.println("Get status info: " + json); DBG_OUTPUT_PORT.print("MQTT: ");
webSocket.sendTXT(num, json);
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, listStatusJSON()); mqtt_client.publish(mqtt_outtopic, json.c_str());
#endif #endif
#ifdef ENABLE_AMQTT #ifdef ENABLE_AMQTT
String liststat = (String) listStatusJSON(); amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, json.c_str());
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, liststat.c_str());
#endif #endif
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
webSocket.sendTXT(num, json);
}
DBG_OUTPUT_PORT.println("Get status info: " + json);
} }
// ~ ==> Get WS2812 modes. // ~ ==> Get WS2812 modes.
if (payload[0] == '~') { if (payload[0] == '~') {
String json = listModesJSON(); String json = listModesJSON();
Dbg_Prefix(mqtt, num); if (mqtt == true) {
DBG_OUTPUT_PORT.println("Get WS2812 modes."); DBG_OUTPUT_PORT.print("MQTT: ");
DBG_OUTPUT_PORT.println(json);
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
// TODO: Fix this, doesn't return anything. Too long?
// Hint: https://github.com/knolleary/pubsubclient/issues/110
DBG_OUTPUT_PORT.printf("Error: Not implemented. Message too large for pubsubclient."); DBG_OUTPUT_PORT.printf("Error: Not implemented. Message too large for pubsubclient.");
mqtt_client.publish(mqtt_outtopic, "ERROR: Not implemented. Message too large for pubsubclient."); mqtt_client.publish(mqtt_outtopic, "ERROR: Not implemented. Message too large for pubsubclient.");
//String json_modes = listModesJSON(); //String json_modes = listModesJSON();
@@ -623,11 +603,16 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
#ifdef ENABLE_AMQTT #ifdef ENABLE_AMQTT
amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, json.c_str()); amqttClient.publish(mqtt_outtopic.c_str(), qospub, false, json.c_str());
#endif #endif
} else {
DBG_OUTPUT_PORT.print("WS: ");
webSocket.sendTXT(num, "OK");
webSocket.sendTXT(num, json);
}
DBG_OUTPUT_PORT.println("Get WS2812 modes.");
DBG_OUTPUT_PORT.println(json);
} }
// / ==> Set WS2812 mode. // / ==> Set WS2812 mode.
// TODO: Fix this, doesn't return anything. Too long?
// Hint: https://github.com/knolleary/pubsubclient/issues/110
if (payload[0] == '/') { if (payload[0] == '/') {
handleSetWS2812FXMode(payload); handleSetWS2812FXMode(payload);
Dbg_Prefix(mqtt, num); Dbg_Prefix(mqtt, num);
@@ -648,7 +633,6 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) {
} }
} }
// *************************************************************************** // ***************************************************************************
// WS request handlers // WS request handlers
// *************************************************************************** // ***************************************************************************
@@ -764,9 +748,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
void sendState() { void sendState() {
const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6); const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6);
//StaticJsonBuffer<bufferSize> jsonBuffer; DynamicJsonDocument jsonBuffer(bufferSize);
DynamicJsonBuffer jsonBuffer(bufferSize); JsonObject& root = jsonBuffer.to<JsonObject>();
JsonObject& root = jsonBuffer.createObject();
root["state"] = (stateOn) ? on_cmd : off_cmd; root["state"] = (stateOn) ? on_cmd : off_cmd;
JsonObject& color = root.createNestedObject("color"); JsonObject& color = root.createNestedObject("color");
@@ -784,8 +767,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
strncpy_P(modeName, (PGM_P)strip.getModeName(strip.getMode()), sizeof(modeName)); // copy from progmem strncpy_P(modeName, (PGM_P)strip.getModeName(strip.getMode()), sizeof(modeName)); // copy from progmem
root["effect"] = modeName; root["effect"] = modeName;
char buffer[root.measureLength() + 1]; char buffer[measureJson(root) + 1];
root.printTo(buffer, sizeof(buffer)); serializeJson(root, buffer, sizeof(buffer));
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_ha_state_out.c_str(), buffer, true); mqtt_client.publish(mqtt_ha_state_out.c_str(), buffer, true);
@@ -802,15 +785,15 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
bool processJson(char* message) { bool processJson(char* message) {
const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + 150; const size_t bufferSize = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + 150;
//StaticJsonBuffer<bufferSize> jsonBuffer; DynamicJsonDocument jsonBuffer(bufferSize);
DynamicJsonBuffer jsonBuffer(bufferSize); DeserializationError error = deserializeJson(jsonBuffer, message);
JsonObject& root = jsonBuffer.parseObject(message); if (error) {
DBG_OUTPUT_PORT.print("parseObject() failed: ");
if (!root.success()) { DBG_OUTPUT_PORT.println(error.c_str());
DBG_OUTPUT_PORT.println("parseObject() failed");
return false; return false;
} }
//DBG_OUTPUT_PORT.println("JSON ParseObject() done!"); //DBG_OUTPUT_PORT.println("JSON ParseObject() done!");
JsonObject& root = jsonBuffer.as<JsonObject>();
if (root.containsKey("state")) { if (root.containsKey("state")) {
const char* state_in = root["state"]; const char* state_in = root["state"];
@@ -860,7 +843,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
if (root.containsKey("effect")) { if (root.containsKey("effect")) {
animation_on = true; animation_on = true;
String effectString = root["effect"].asString(); String effectString = root["effect"].as<String>();
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
if(String(strip.getModeName(i)) == effectString) { if(String(strip.getModeName(i)) == effectString) {
@@ -937,8 +920,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
ha_send_data.detach(); ha_send_data.detach();
mqtt_client.subscribe(mqtt_ha_state_in.c_str(), qossub); mqtt_client.subscribe(mqtt_ha_state_in.c_str(), qossub);
#ifdef MQTT_HOME_ASSISTANT_SUPPORT #ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11)); DynamicJsonDocument jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject(); JsonObject& json = jsonBuffer.to<JsonObject>();
json["name"] = HOSTNAME; json["name"] = HOSTNAME;
json["platform"] = "mqtt_json"; json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out; json["state_topic"] = mqtt_ha_state_out;
@@ -953,8 +936,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
effect_list.add(strip.getModeName(i)); effect_list.add(strip.getModeName(i));
} }
char buffer[json.measureLength() + 1]; char buffer[json.measureJson() + 1];
json.printTo(buffer, sizeof(buffer)); serializeJson(json, buffer, sizeof(buffer));
mqtt_client.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), buffer, true); mqtt_client.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), buffer, true);
#endif #endif
#endif #endif
@@ -1017,8 +1000,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
uint16_t packetIdSub2 = amqttClient.subscribe((char *)mqtt_ha_state_in.c_str(), qossub); uint16_t packetIdSub2 = amqttClient.subscribe((char *)mqtt_ha_state_in.c_str(), qossub);
DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub2); DBG_OUTPUT_PORT.printf("Subscribing at QoS %d, packetId: ", qossub); DBG_OUTPUT_PORT.println(packetIdSub2);
#ifdef MQTT_HOME_ASSISTANT_SUPPORT #ifdef MQTT_HOME_ASSISTANT_SUPPORT
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11)); DynamicJsonDocument jsonBuffer(JSON_ARRAY_SIZE(strip.getModeCount()) + JSON_OBJECT_SIZE(11));
JsonObject& json = jsonBuffer.createObject(); JsonObject& json = jsonBuffer.to<JsonObject>();
json["name"] = HOSTNAME; json["name"] = HOSTNAME;
json["platform"] = "mqtt_json"; json["platform"] = "mqtt_json";
json["state_topic"] = mqtt_ha_state_out; json["state_topic"] = mqtt_ha_state_out;
@@ -1033,8 +1016,8 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
for (uint8_t i = 0; i < strip.getModeCount(); i++) { for (uint8_t i = 0; i < strip.getModeCount(); i++) {
effect_list.add(strip.getModeName(i)); effect_list.add(strip.getModeName(i));
} }
char buffer[json.measureLength() + 1]; char buffer[json.measureJson() + 1];
json.printTo(buffer, sizeof(buffer)); serializeJson(json, buffer, sizeof(buffer));
DBG_OUTPUT_PORT.println(buffer); DBG_OUTPUT_PORT.println(buffer);
amqttClient.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), qospub, true, buffer); amqttClient.publish(String("homeassistant/light/" + String(HOSTNAME) + "/config").c_str(), qospub, true, buffer);
#endif #endif
@@ -1258,9 +1241,8 @@ bool writeConfigFS(bool saveConfig){
//FS save //FS save
updateFS = true; updateFS = true;
DBG_OUTPUT_PORT.print("Saving config: "); DBG_OUTPUT_PORT.print("Saving config: ");
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(4)); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(4));
// StaticJsonBuffer<JSON_OBJECT_SIZE(4)> jsonBuffer; JsonObject& json = jsonBuffer.to<JsonObject>();
JsonObject& json = jsonBuffer.createObject();
json["mqtt_host"] = mqtt_host; json["mqtt_host"] = mqtt_host;
json["mqtt_port"] = mqtt_port; json["mqtt_port"] = mqtt_port;
json["mqtt_user"] = mqtt_user; json["mqtt_user"] = mqtt_user;
@@ -1270,8 +1252,8 @@ bool writeConfigFS(bool saveConfig){
File configFile = SPIFFS.open("/config.json", "w"); File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) DBG_OUTPUT_PORT.println("failed to open config file for writing"); if (!configFile) DBG_OUTPUT_PORT.println("failed to open config file for writing");
json.printTo(DBG_OUTPUT_PORT); serializeJson(json, DBG_OUTPUT_PORT);
json.printTo(configFile); serializeJson(json, configFile);
configFile.close(); configFile.close();
updateFS = false; updateFS = false;
return true; return true;
@@ -1295,13 +1277,13 @@ bool readConfigFS() {
size_t size = configFile.size(); size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]); std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size); configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(4)+300); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(4)+300);
// StaticJsonBuffer<JSON_OBJECT_SIZE(4)+300> jsonBuffer; DeserializationError error = deserializeJson(jsonBuffer, buf.get());
JsonObject& json = jsonBuffer.parseObject(buf.get());
DBG_OUTPUT_PORT.print("Config: "); DBG_OUTPUT_PORT.print("Config: ");
json.printTo(DBG_OUTPUT_PORT); if (!error) {
if (json.success()) {
DBG_OUTPUT_PORT.println(" Parsed!"); DBG_OUTPUT_PORT.println(" Parsed!");
JsonObject& json = jsonBuffer.as<JsonObject>();
serializeJson(json, DBG_OUTPUT_PORT);
strcpy(mqtt_host, json["mqtt_host"]); strcpy(mqtt_host, json["mqtt_host"]);
strcpy(mqtt_port, json["mqtt_port"]); strcpy(mqtt_port, json["mqtt_port"]);
strcpy(mqtt_user, json["mqtt_user"]); strcpy(mqtt_user, json["mqtt_user"]);
@@ -1309,7 +1291,8 @@ bool readConfigFS() {
updateFS = false; updateFS = false;
return true; return true;
} else { } else {
DBG_OUTPUT_PORT.println("Failed to load json config"); DBG_OUTPUT_PORT.print("Failed to load json config: ");
DBG_OUTPUT_PORT.println(error.c_str());
} }
} else { } else {
DBG_OUTPUT_PORT.println("Failed to open /config.json"); DBG_OUTPUT_PORT.println("Failed to open /config.json");
@@ -1327,9 +1310,8 @@ bool writeStateFS(){
updateFS = true; updateFS = true;
//save the strip state to FS JSON //save the strip state to FS JSON
DBG_OUTPUT_PORT.print("Saving cfg: "); DBG_OUTPUT_PORT.print("Saving cfg: ");
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(7)); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(7));
// StaticJsonBuffer<JSON_OBJECT_SIZE(7)> jsonBuffer; JsonObject& json = jsonBuffer.to<JsonObject>();
JsonObject& json = jsonBuffer.createObject();
json["mode"] = static_cast<int>(mode); json["mode"] = static_cast<int>(mode);
json["strip_mode"] = (int) strip.getMode(); json["strip_mode"] = (int) strip.getMode();
json["brightness"] = brightness; json["brightness"] = brightness;
@@ -1348,8 +1330,8 @@ bool writeStateFS(){
updateStateFS = false; updateStateFS = false;
return false; return false;
} }
json.printTo(DBG_OUTPUT_PORT); serializeJson(json, DBG_OUTPUT_PORT);
json.printTo(configFile); serializeJson(json, configFile);
configFile.close(); configFile.close();
updateFS = false; updateFS = false;
spiffs_save_state.detach(); spiffs_save_state.detach();
@@ -1371,11 +1353,11 @@ bool readStateFS() {
// Allocate a buffer to store contents of the file. // Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]); std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size); configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(7)+200); DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(7)+200);
// StaticJsonBuffer<JSON_OBJECT_SIZE(7)+200> jsonBuffer; DeserializationError error = deserializeJson(jsonBuffer, buf.get());
JsonObject& json = jsonBuffer.parseObject(buf.get()); if (!error) {
json.printTo(DBG_OUTPUT_PORT); JsonObject& json = jsonBuffer.as<JsonObject>();
if (json.success()) { serializeJson(json, DBG_OUTPUT_PORT);
mode = static_cast<MODE>((int) json["mode"]); mode = static_cast<MODE>((int) json["mode"]);
ws2812fx_mode = json["strip_mode"]; ws2812fx_mode = json["strip_mode"];
brightness = json["brightness"]; brightness = json["brightness"];
@@ -1399,7 +1381,7 @@ bool readStateFS() {
DBG_OUTPUT_PORT.println("Failed to open \"/stripstate.json\""); DBG_OUTPUT_PORT.println("Failed to open \"/stripstate.json\"");
} }
} else { } else {
DBG_OUTPUT_PORT.println("Coudnt find \"/stripstate.json\""); DBG_OUTPUT_PORT.println("Couldn't find \"/stripstate.json\"");
} }
//end read //end read
updateFS = false; updateFS = false;
+1 -1
View File
@@ -1 +1 @@
#define SKETCH_VERSION "2.1.1" #define SKETCH_VERSION "2.1.2"
+5
View File
@@ -6,4 +6,9 @@
* 11 May 2018 v 2.1.1 * 11 May 2018 v 2.1.1
* - Use ArduinoJSON to send JSON replies * - Use ArduinoJSON to send JSON replies
* - Add strip.trigger() * - Add strip.trigger()
*
* 17 Jun 2018 v 2.1.2
* - Upgrade to ArduinoJSON 6.xx from ArduinoJSON 5.xx
* - Added example for static IP
* - Added more internal variables to /esp_status
*/ */
+5 -1
View File
@@ -11,6 +11,9 @@
[![Demo video Apple Homekit integration](https://j.gifs.com/gJP2o6.gif)](https://youtu.be/4JnGXZaPnrw) [![Demo video Apple Homekit integration](https://j.gifs.com/gJP2o6.gif)](https://youtu.be/4JnGXZaPnrw)
___ ___
Update 13.05.2018:
I've worked on a new web UI for the last weeks. It's now available as an early preview. There is a [video](https://youtu.be/lryDPMA2qpY) that shows the new features. [Try it out](https://github.com/toblum/McLightingUI) if you want and leave some feedback.
Update 07.04.2018: Update 07.04.2018:
And even more changes to McLighting! Most of them were contributed by user @debsahu. Thank you! And even more changes to McLighting! Most of them were contributed by user @debsahu. Thank you!
- Update arduino-esp8266 to latest, at least version 2.4.1 - Update arduino-esp8266 to latest, at least version 2.4.1
@@ -130,7 +133,7 @@ I hope I didn't miss any sources and mentioned every author. In case I forgot so
## Todos ## Todos
- [x] MQTT support - [x] MQTT support
- [ ] Support multiple strips and control them separately or together [Issue](https://github.com/toblum/McLighting/issues/118) - [ ] Support multiple strips and control them separately or together [Issue](https://github.com/toblum/McLighting/issues/118)
- [ ] Save favourite effects? [Issue](https://github.com/toblum/McLighting/issues/35) - [x] Save favourite effects? [Issue](https://github.com/toblum/McLighting/issues/35)
- [ ] Make number of pixels, MQTT and PIN configurable via front end [Issue](https://github.com/toblum/McLighting/issues/93) and [Issue](https://github.com/toblum/McLighting/issues/101) - [ ] Make number of pixels, MQTT and PIN configurable via front end [Issue](https://github.com/toblum/McLighting/issues/93) and [Issue](https://github.com/toblum/McLighting/issues/101)
- [x] OTA update [Issue](https://github.com/toblum/McLighting/issues/93) - [x] OTA update [Issue](https://github.com/toblum/McLighting/issues/93)
- [ ] Bundle webpages instead of SPIFFS [Issue](https://github.com/toblum/McLighting/issues/93) - [ ] Bundle webpages instead of SPIFFS [Issue](https://github.com/toblum/McLighting/issues/93)
@@ -149,6 +152,7 @@ I hope I didn't miss any sources and mentioned every author. In case I forgot so
- [ ] If no wifi, at least enable button mode. - [ ] If no wifi, at least enable button mode.
- [ ] Also enable McLighting in Wifi AP mode. - [ ] Also enable McLighting in Wifi AP mode.
- [x] Make a set of NodeRed nodes. - [x] Make a set of NodeRed nodes.
- [ ] IR remote support [issue](https://github.com/toblum/McLightingUI/issues/3)
- [ ] Multiple buttons/GPIO Inputs. [Issue](https://github.com/toblum/McLighting/issues/119) - [ ] Multiple buttons/GPIO Inputs. [Issue](https://github.com/toblum/McLighting/issues/119)
- [ ] Music visualizer / Bring back ArtNet [Issue](https://github.com/toblum/McLighting/issues/111) - [ ] Music visualizer / Bring back ArtNet [Issue](https://github.com/toblum/McLighting/issues/111)
- [ ] Display version and parameters (Number of LEDs, definition settings, ..) in the web UI [Issue](https://github.com/toblum/McLighting/issues/150) - [ ] Display version and parameters (Number of LEDs, definition settings, ..) in the web UI [Issue](https://github.com/toblum/McLighting/issues/150)
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 119 KiB