Просмотр исходного кода

Messages as JSON's with ArduinoJson lib

master
gituser 1 неделю назад
Родитель
Сommit
8ccd76dd62
6 измененных файлов: 96 добавлений и 17 удалений
  1. 3
    0
      .cproject
  2. 5
    0
      .project
  3. 56
    0
      Commands.h
  4. 3
    3
      HtmlPages.h
  5. 26
    13
      LEDLamp.ino
  6. 3
    1
      sloeber.ino.cpp

+ 3
- 0
.cproject Просмотреть файл

@@ -27,6 +27,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/WebSockets/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/Hash/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ArduinoJson/src}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.cpp.sketch.input.1770649762" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
</tool>
@@ -40,6 +41,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/WebSockets/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/Hash/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ArduinoJson/src}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.c.sketch.input.1705705" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
</tool>
@@ -53,6 +55,7 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/WebSockets/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/Hash/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ArduinoJson/src}&quot;"/>
</option>
<inputType id="io.sloeber.compiler.S.sketch.input.1190005390" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
</tool>

+ 5
- 0
.project Просмотреть файл

@@ -41,6 +41,11 @@
<type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/variants/d1</locationURI>
</link>
<link>
<name>libraries/ArduinoJson</name>
<type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/libraries/ArduinoJson/7.0.2</locationURI>
</link>
<link>
<name>libraries/DNSServer</name>
<type>2</type>

+ 56
- 0
Commands.h Просмотреть файл

@@ -0,0 +1,56 @@
/*
* Commands.h
*
* Created on: 09.06.2026
* Author: FSmilari
*/

#ifndef COMMANDS_H_
#define COMMANDS_H_

enum Commands {
CMD_UNKNOWN = -1,
CMD_CHG_MODE,
CMD_SWITCH_LED,
CMD_CHG_COLOR,
CMD_CHG_BRIGHTNESS,
CMD_RESET_WIFI_CFG
};

const char* cmdToString(Commands cmd) {
switch (cmd) {
case CMD_CHG_MODE:
return "CHG_MODE";
case CMD_SWITCH_LED:
return "SWITCH_LED";
case CMD_CHG_COLOR:
return "CHG_COLOR";
case CMD_CHG_BRIGHTNESS:
return "CHG_BRIGHTNESS";
case CMD_RESET_WIFI_CFG:
return "RESET_WIFI_CFG";
default:
return "UNKNOWN";
}
}

Commands strToCommand(const char *cmd) {
if (cmd == nullptr) {
return CMD_UNKNOWN;
}

if (strcmp(cmd, "CHG_MODE") == 0)
return CMD_CHG_MODE;
if (strcmp(cmd, "SWITCH_LED") == 0)
return CMD_SWITCH_LED;
if (strcmp(cmd, "CHG_COLOR") == 0)
return CMD_CHG_COLOR;
if (strcmp(cmd, "CHG_BRIGHTNESS") == 0)
return CMD_CHG_BRIGHTNESS;
if (strcmp(cmd, "RESET_WIFI_CFG") == 0)
return CMD_RESET_WIFI_CFG;

return CMD_UNKNOWN;
}

#endif /* COMMANDS_H_ */

+ 3
- 3
HtmlPages.h Просмотреть файл

@@ -413,19 +413,19 @@ const String getSTAControlPage(String macAddress) {
function ledOn() {
if (ws && ws.readyState === 1) {
ws.send("/ledOn");
ws.send("{\"cmd\":\"SWITCH_LED\",\"val\":\"on\"}");
}
}
function ledOff() {
if (ws && ws.readyState === 1) {
ws.send("/ledOff");
ws.send("{\"cmd\":\"SWITCH_LED\",\"val\":\"off\"}");
}
}

function resetWifiCfg() {
if (ws && ws.readyState === 1) {
ws.send("/resetWifiCfg");
ws.send("{\"cmd\":\"RESET_WIFI_CFG\",\"val\":\"\"}");
}
}

+ 26
- 13
LEDLamp.ino Просмотреть файл

@@ -4,7 +4,9 @@
#include <EEPROM.h>
#include <DNSServer.h>
#include "States.h"
#include "Commands.h"
#include "HtmlPages.h"
#include <ArduinoJson.h>

AppState state = STATE_BOOT;
unsigned long connectStart = 0;
@@ -125,22 +127,33 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
Serial.print("WS empfangen: ");
Serial.println(msg);

if (msg == "/ledOn") {
digitalWrite(ledPin, LOW);
delay(10);
if (digitalRead(ledPin) == LOW) {
webSocket.sendTXT(num, "LED ON");
JsonDocument doc;
deserializeJson(doc, msg);
const char *cmd = doc["cmd"];

if (strToCommand(cmd) == CMD_SWITCH_LED) {

const char *value = doc["val"];
if (strcmp(value, "on") == 0) {
digitalWrite(ledPin, LOW);
delay(10);
if (digitalRead(ledPin) == LOW) {
webSocket.sendTXT(num, "LED ON");
}
} else if (strcmp(value, "off") == 0) {
digitalWrite(ledPin, HIGH);
delay(10);
if (digitalRead(ledPin) == HIGH) {
webSocket.sendTXT(num, "LED OFF");
}
}
} else if (msg == "/ledOff") {
digitalWrite(ledPin, HIGH);
delay(10);
if (digitalRead(ledPin) == HIGH) {
webSocket.sendTXT(num, "LED OFF");
}
} else if (msg == "/resetWifiCfg") {

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

resetWifiConfiguration(num);
} else {
String s = "ACK: " + msg;

String s = "ACK UNKNOWN: " + msg;
webSocket.sendTXT(num, s);
}


+ 3
- 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-09 21:27:43
//This file has been generated on 2026-06-09 22:24:35

#include "Arduino.h"
#include "Arduino.h"
@@ -11,7 +11,9 @@
#include <EEPROM.h>
#include <DNSServer.h>
#include "States.h"
#include "Commands.h"
#include "HtmlPages.h"
#include <ArduinoJson.h>

void saveConfig() ;
void loadConfig() ;

Загрузка…
Отмена
Сохранить