Browse Source

led on off with WebSocket architecture

master
gituser 1 week ago
parent
commit
6516424d31
7 changed files with 166 additions and 66 deletions
  1. 10
    4
      .cproject
  2. 16
    6
      .project
  3. 1
    1
      .settings/language.settings.xml
  4. 6
    0
      .settings/org.eclipse.cdt.core.prefs
  5. 7
    2
      .sproject
  6. 123
    51
      LEDLamp.ino
  7. 3
    2
      sloeber.ino.cpp

+ 10
- 4
.cproject View File

<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="io.sloeber.compiler.cpp.sketch.option.incpath.1935522056" name="Include Paths (-I)" superClass="io.sloeber.compiler.cpp.sketch.option.incpath" valueType="includePath"> <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="io.sloeber.compiler.cpp.sketch.option.incpath.1935522056" name="Include Paths (-I)" superClass="io.sloeber.compiler.cpp.sketch.option.incpath" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/variant}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/core}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/core}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WebServer/src}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WebServer/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/EEPROM}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/EEPROM}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/DNSServer/src}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/DNSServer/src}&quot;"/>
<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;"/>
</option> </option>
<inputType id="io.sloeber.compiler.cpp.sketch.input.1770649762" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/> <inputType id="io.sloeber.compiler.cpp.sketch.input.1770649762" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
</tool> </tool>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="io.sloeber.compiler.c.sketch.option.incpath.225042047" name="Include Paths (-I)" superClass="io.sloeber.compiler.c.sketch.option.incpath" valueType="includePath"> <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="io.sloeber.compiler.c.sketch.option.incpath.225042047" name="Include Paths (-I)" superClass="io.sloeber.compiler.c.sketch.option.incpath" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/variant}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/core}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/core}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WebServer/src}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WebServer/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/EEPROM}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/EEPROM}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/DNSServer/src}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/DNSServer/src}&quot;"/>
<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;"/>
</option> </option>
<inputType id="io.sloeber.compiler.c.sketch.input.1705705" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/> <inputType id="io.sloeber.compiler.c.sketch.input.1705705" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
</tool> </tool>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="io.sloeber.compiler.asm.sketch.option.incpath.328612697" name="Include Paths (-I)" superClass="io.sloeber.compiler.asm.sketch.option.incpath" valueType="includePath"> <option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="io.sloeber.compiler.asm.sketch.option.incpath.328612697" name="Include Paths (-I)" superClass="io.sloeber.compiler.asm.sketch.option.incpath" valueType="includePath">
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/variant}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/core}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/core/core}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WiFi/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WebServer/src}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/ESP8266WebServer/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/EEPROM}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/EEPROM}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/DNSServer/src}&quot;"/> <listOptionValue builtIn="false" value="&quot;${workspace_loc:/LEDLamp/libraries/DNSServer/src}&quot;"/>
<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;"/>
</option> </option>
<inputType id="io.sloeber.compiler.S.sketch.input.1190005390" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/> <inputType id="io.sloeber.compiler.S.sketch.input.1190005390" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
</tool> </tool>
</toolChain> </toolChain>
</folderInfo> </folderInfo>
<sourceEntries> <sourceEntries>
<entry excluding="libraries/?*/**/?xamples/**|libraries/?*/**/?xtras/**|libraries/?*/**/test*/**|libraries/?*/**/third-party/**|libraries/**/._*|libraries/?*/utility/*/*" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
<entry excluding="libraries/WebSockets/travis|libraries/?*/**/?xamples/**|libraries/?*/**/?xtras/**|libraries/?*/**/test*/**|libraries/?*/**/third-party/**|libraries/**/._*|libraries/?*/utility/*/*" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries> </sourceEntries>
</configuration> </configuration>
</storageModule> </storageModule>

+ 16
- 6
.project View File

<link> <link>
<name>core/core</name> <name>core/core</name>
<type>2</type> <type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.6.0/cores/esp8266</locationURI>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/cores/esp8266</locationURI>
</link> </link>
<link> <link>
<name>core/variant</name> <name>core/variant</name>
<type>2</type> <type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.6.0/variants/d1</locationURI>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/variants/d1</locationURI>
</link> </link>
<link> <link>
<name>libraries/DNSServer</name> <name>libraries/DNSServer</name>
<type>2</type> <type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.6.0/libraries/DNSServer</locationURI>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/libraries/DNSServer</locationURI>
</link> </link>
<link> <link>
<name>libraries/EEPROM</name> <name>libraries/EEPROM</name>
<type>2</type> <type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.6.0/libraries/EEPROM</locationURI>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/libraries/EEPROM</locationURI>
</link> </link>
<link> <link>
<name>libraries/ESP8266WebServer</name> <name>libraries/ESP8266WebServer</name>
<type>2</type> <type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.6.0/libraries/ESP8266WebServer</locationURI>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/libraries/ESP8266WebServer</locationURI>
</link> </link>
<link> <link>
<name>libraries/ESP8266WiFi</name> <name>libraries/ESP8266WiFi</name>
<type>2</type> <type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/2.6.0/libraries/ESP8266WiFi</locationURI>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/libraries/ESP8266WiFi</locationURI>
</link>
<link>
<name>libraries/Hash</name>
<type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/libraries/Hash</locationURI>
</link>
<link>
<name>libraries/WebSockets</name>
<type>2</type>
<locationURI>ECLIPSE_HOME/arduinoPlugin/libraries/WebSockets/2.7.2</locationURI>
</link> </link>
</linkedResources> </linkedResources>
</projectDescription> </projectDescription>

+ 1
- 1
.settings/language.settings.xml View File

<project> <project>
<configuration id="io.sloeber.core.sketch.0" name="Release"> <configuration id="io.sloeber.core.sketch.0" name="Release">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider"> <extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider class="io.sloeber.core.toolchain.ArduinoLanguageProvider" console="false" env-hash="-201397279775688891" id="io.sloeber.languageSettingsProvider" keep-relative-paths="false" name="Arduino Compiler Settings" parameter="${COMMAND} -E -P -v -dD -D__IN_ECLIPSE__ &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="io.sloeber.core.toolchain.ArduinoLanguageProvider" console="false" env-hash="-1642491514133985484" id="io.sloeber.languageSettingsProvider" keep-relative-paths="false" name="Arduino Compiler Settings" parameter="${COMMAND} -E -P -v -dD -D__IN_ECLIPSE__ &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/> <language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/> <language-scope id="org.eclipse.cdt.core.g++"/>
</provider> </provider>

+ 6
- 0
.settings/org.eclipse.cdt.core.prefs View File

doxygen/doxygen_new_line_after_brief=true
doxygen/doxygen_use_brief_tag=false
doxygen/doxygen_use_javadoc_tags=true
doxygen/doxygen_use_pre_tag=false
doxygen/doxygen_use_structural_commands=false
eclipse.preferences.version=1

+ 7
- 2
.sproject View File

Config.Release.board.BOARD.ID=d1 Config.Release.board.BOARD.ID=d1
Config.Release.board.BOARD.MENU.FlashFreq=getMenuItemIDFromMenuItemName did not find menu item with name "40MHz" for menu ID "FlashFreq" and for boardID "d1"!
Config.Release.board.BOARD.MENU.FlashMode=getMenuItemIDFromMenuItemName did not find menu item with name "DOUT (compatible)" for menu ID "FlashMode" and for boardID "d1"!
Config.Release.board.BOARD.MENU.baud=921600 Config.Release.board.BOARD.MENU.baud=921600
Config.Release.board.BOARD.MENU.dbg=Disabled Config.Release.board.BOARD.MENU.dbg=Disabled
Config.Release.board.BOARD.MENU.eesz=4M2M Config.Release.board.BOARD.MENU.eesz=4M2M
Config.Release.board.BOARD.MENU.exception=legacy
Config.Release.board.BOARD.MENU.exception=disabled
Config.Release.board.BOARD.MENU.ip=lm2f Config.Release.board.BOARD.MENU.ip=lm2f
Config.Release.board.BOARD.MENU.lvl=None____ Config.Release.board.BOARD.MENU.lvl=None____
Config.Release.board.BOARD.MENU.mmu=3232
Config.Release.board.BOARD.MENU.non32xfer=fast
Config.Release.board.BOARD.MENU.ssl=all Config.Release.board.BOARD.MENU.ssl=all
Config.Release.board.BOARD.MENU.stacksmash=disabled
Config.Release.board.BOARD.MENU.vt=flash Config.Release.board.BOARD.MENU.vt=flash
Config.Release.board.BOARD.MENU.wipe=all Config.Release.board.BOARD.MENU.wipe=all
Config.Release.board.BOARD.MENU.xtal=80 Config.Release.board.BOARD.MENU.xtal=80
Config.Release.board.BOARD.TXT=${SLOEBER_HOME}/arduinoPlugin/packages/esp8266/hardware/esp8266/2.6.0/boards.txt
Config.Release.board.BOARD.TXT=${SLOEBER_HOME}/arduinoPlugin/packages/esp8266/hardware/esp8266/3.1.2/boards.txt
Config.Release.board.PROGRAMMER.NAME=Default Config.Release.board.PROGRAMMER.NAME=Default
Config.Release.board.UPLOAD.PORT=COM3 Config.Release.board.UPLOAD.PORT=COM3
Config.Release.compile.sloeber.extra.all= Config.Release.compile.sloeber.extra.all=

+ 123
- 51
LEDLamp.ino View File

#include "Arduino.h" #include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <DNSServer.h> #include <DNSServer.h>


const int ledPin = LED_BUILTIN; // The built-in LED on the Wemos D1 Mini is on GPIO2 (D4) const int ledPin = LED_BUILTIN; // The built-in LED on the Wemos D1 Mini is on GPIO2 (D4)


ESP8266WebServer webServer(80); ESP8266WebServer webServer(80);
WiFiServer server(1234);
WebSocketsServer webSocket(81);
DNSServer dnsServer; DNSServer dnsServer;


const byte DNS_PORT = 53; const byte DNS_PORT = 53;
<body> <body>


<div class="card"> <div class="card">

<h2>Wemos Setup</h2> <h2>Wemos Setup</h2>


<div class="mac">Device: )rawliteral" <div class="mac">Device: )rawliteral"
+ R"rawliteral(</div> + R"rawliteral(</div>


<form action="/save" method="POST"> <form action="/save" method="POST">

<input name="ssid" placeholder="WLAN Name (SSID)"> <input name="ssid" placeholder="WLAN Name (SSID)">
<div class="pw-wrapper">
<input id="pass" name="pass" type="password" placeholder="Passwort">
<span class="toggle-eye" onclick="togglePassword()">👁</span>
</div>
<button type="submit">Verbinden</button>

<div class="pw-wrapper">
<input id="pass" name="pass" type="password" placeholder="Passwort">
<span class="toggle-eye" onclick="togglePassword()">👁</span>
</div>

<button type="submit">Speichern & Verbinden</button>
</form> </form>


<div class="info">
Verbinde deinen Wemos mit einem WLAN Netzwerk
<div class="btn-row">
<button type="button" onclick="ledOn()">LED EIN</button>
<button type="button" onclick="ledOff()">LED AUS</button>
</div> </div>

</div> </div>


<script>

let ws;

function connectWS() {
ws = new WebSocket("ws://" + location.hostname + ":81/");

ws.onopen = () => {
console.log("WebSocket connected");
};

ws.onmessage = (e) => {
console.log("ESP:", e.data);
};

ws.onclose = () => {
console.log("WS disconnected → reconnect");
setTimeout(connectWS, 1000);
};
}

connectWS();

function ledOn() {
if (ws && ws.readyState === 1) {
ws.send("/ledOn");
}
}

function ledOff() {
if (ws && ws.readyState === 1) {
ws.send("/ledOff");
}
}

function togglePassword() {
const pass = document.getElementById("pass");
const eye = document.querySelector(".toggle-eye");

if (pass.type === "password") {
pass.type = "text";
eye.textContent = "🚫";
} else {
pass.type = "password";
eye.textContent = "👁";
}
}

</script>

</body> </body>
</html> </html>
)rawliteral"; )rawliteral";
Serial.print("IP: "); Serial.print("IP: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());


server.begin();
Serial.println("TCP Server gestartet auf Port 1234");
webServer.on("/", handleRoot);
webServer.on("/save", HTTP_POST, handleSave);
webServer.begin();

webSocket.begin();
webSocket.onEvent(webSocketEvent);
Serial.println("WebSocket gestartet auf Port 81");


return true; return true;
} }



return false; return false;
} }


void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {

switch (type) {

case WStype_CONNECTED: {

IPAddress ip = webSocket.remoteIP(num);

Serial.printf(
"WS Client %u verbunden von %d.%d.%d.%d\n",
num,
ip[0], ip[1], ip[2], ip[3]);

webSocket.sendTXT(num, "Connected");
break;
}

case WStype_DISCONNECTED:
Serial.printf("WS Client %u getrennt\n", num);
break;

case WStype_TEXT: {

String msg = String((char*) payload);

Serial.print("WS empfangen: ");
Serial.println(msg);

if (msg == "/ledOn") {
digitalWrite(ledPin, LOW);
webSocket.sendTXT(num, "LED ON");
} else if (msg == "/ledOff") {
digitalWrite(ledPin, HIGH);
webSocket.sendTXT(num, "LED OFF");
} else {
// webSocket.sendTXT(num, "ACK: " + msg);
}

break;
}

default:
break;
}
}

// ---------- SETUP ---------- // ---------- SETUP ----------
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // LED zunächst AUS


loadConfig(); loadConfig();


void loop() { void loop() {


if (WiFi.getMode() == WIFI_AP) { if (WiFi.getMode() == WIFI_AP) {

dnsServer.processNextRequest(); dnsServer.processNextRequest();
webServer.handleClient(); webServer.handleClient();
return;
}


webServer.handleClient(); // optional auch im STA Mode

WiFiClient client = server.available();

if (!client) {
return; return;
} }


Serial.println("Client verbunden");
client.println("Connected");

while (client.connected()) {

if (!client.available()) {
delay(1);
continue;
}

String msg = client.readStringUntil('\n');
msg.trim();

Serial.print("Empfangen: ");
Serial.println(msg);

client.println("ACK: " + msg);

if (msg.equalsIgnoreCase("/ledOn")) {
digitalWrite(ledPin, LOW);
}

if (msg.equalsIgnoreCase("/ledOff")) {
digitalWrite(ledPin, HIGH);
}

if (msg.equalsIgnoreCase("quit")) {
break;
}
}
webServer.handleClient();


client.stop();
Serial.println("Client getrennt");
webSocket.loop();
} }

+ 3
- 2
sloeber.ino.cpp View File

//This is a automatic generated file //This is a automatic generated file
//Please do not modify this file //Please do not modify this file
//If you touch this file your change will be overwritten during the next build //If you touch this file your change will be overwritten during the next build
//This file has been generated on 2026-06-07 15:50:46
//This file has been generated on 2026-06-07 22:43:11


#include "Arduino.h" #include "Arduino.h"
#include "Arduino.h" #include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <DNSServer.h> #include <DNSServer.h>


void handleNotFound() ; void handleNotFound() ;
void startAP() ; void startAP() ;
bool connectSTA() ; bool connectSTA() ;
void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) ;
void setup() ; void setup() ;
void loop() ; void loop() ;



Loading…
Cancel
Save