Переглянути джерело

get config via Json and websocket

master
gituser 1 місяць тому
джерело
коміт
28ef908a20
5 змінених файлів з 58 додано та 4 видалено
  1. 11
    1
      Commands.h
  2. 30
    1
      ConfigManager.cpp
  3. 5
    0
      ConfigManager.h
  4. 11
    1
      LEDLamp.ino
  5. 1
    1
      sloeber.ino.cpp

+ 11
- 1
Commands.h Переглянути файл

@@ -15,7 +15,9 @@ enum Commands {
CMD_CHG_COLOR,
CMD_CHG_BRIGHTNESS,
CMD_CHG_CFG_PARAM,
CMD_RESET_WIFI_CFG
CMD_RESET_WIFI_CFG,
CMD_GET_CONFIG,
CMD_ESP_RESTART
};

static const char* cmdToString(Commands cmd) {
@@ -32,6 +34,10 @@ static const char* cmdToString(Commands cmd) {
return "CHG_CFG_PARAM";
case CMD_RESET_WIFI_CFG:
return "RESET_WIFI_CFG";
case CMD_GET_CONFIG:
return "GET_CONFIG";
case CMD_ESP_RESTART:
return "ESP_RESTART";
default:
return "UNKNOWN";
}
@@ -54,6 +60,10 @@ static Commands strToCommand(const char *cmd) {
return CMD_CHG_CFG_PARAM;
if (strcmp(cmd, "RESET_WIFI_CFG") == 0)
return CMD_RESET_WIFI_CFG;
if (strcmp(cmd, "GET_CONFIG") == 0)
return CMD_GET_CONFIG;
if (strcmp(cmd, "ESP_RESTART") == 0)
return CMD_ESP_RESTART;

return CMD_UNKNOWN;
}

+ 30
- 1
ConfigManager.cpp Переглянути файл

@@ -109,7 +109,36 @@ Config& ConfigManager::setToDefaultLEDConfig(Config &config) {
return config;
}


Config& ConfigManager::getConfig() {
return _config;
}

void ConfigManager::getConfig(JsonDocument &doc) {
doc.clear();

doc["version"] = _config.version;
doc["ssid"] = _config.ssid;
doc["pass"] = _config.pass;
doc["wifiValid"] = _config.wifiValid;
doc["mode"] = _config.mode;
doc["brightness"] = _config.brightness;

rgbwToJson(_config.singleColor, doc["singleColor"].to<JsonObject>());
rgbwToJson(_config.segmentColor1, doc["segmentColor1"].to<JsonObject>());
rgbwToJson(_config.segmentColor2, doc["segmentColor2"].to<JsonObject>());
rgbwToJson(_config.segmentColor3, doc["segmentColor3"].to<JsonObject>());

doc["rainbowDelay"] = _config.rainbowDelay;
doc["spectralStepTime"] = _config.spectralStepTime;
doc["fadingHoldTime"] = _config.fadingHoldTime;
doc["fadingFadeTime"] = _config.fadingFadeTime;
doc["flashOnTime"] = _config.flashOnTime;
doc["flashOffTime"] = _config.flashOffTime;
}

void ConfigManager::rgbwToJson(const RGBWColor &c, JsonObject obj) {
obj["r"] = c.r;
obj["g"] = c.g;
obj["b"] = c.b;
obj["w"] = c.w;
}

+ 5
- 0
ConfigManager.h Переглянути файл

@@ -9,6 +9,7 @@
#define CONFIGMANAGER_H_

#include <Arduino.h>
#include <ArduinoJson.h>
#include <EEPROM.h>
#include "LEDModes.h"

@@ -54,9 +55,13 @@ class ConfigManager {
Config& setToDefaultWiFiConfig(Config &config);
Config& setToDefaultLEDConfig(Config &config);
Config& getConfig();
void getConfig(JsonDocument &doc);


private:
Config _config;

void rgbwToJson(const RGBWColor &c, JsonObject obj);
};

#endif /* CONFIGMANAGER_H_ */

+ 11
- 1
LEDLamp.ino Переглянути файл

@@ -120,7 +120,17 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
deserializeJson(doc, msg);
const char *cmd = doc["cmd"];

if (strToCommand(cmd) == CMD_SWITCH_LED) {
if (strToCommand(cmd) == CMD_ESP_RESTART) {
ESP.restart();

} else if (strToCommand(cmd) == CMD_GET_CONFIG) {
JsonDocument doc;
String json;
configManager.getConfig(doc);
serializeJson(doc, json);
webSocket.sendTXT(num, json);

} else if (strToCommand(cmd) == CMD_SWITCH_LED) {

const char *value = doc["val"];
if (strcmp(value, "on") == 0) {

+ 1
- 1
sloeber.ino.cpp Переглянути файл

@@ -2,7 +2,7 @@
//This is a automatic generated file
//Please do not modify this file
//If you touch this file your change will be overwritten during the next build
//This file has been generated on 2026-06-29 15:24:39
//This file has been generated on 2026-06-29 16:12:29

#include "Arduino.h"
#include "Arduino.h"

Завантаження…
Відмінити
Зберегти