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

led on off with WebSocket architecture

master
gituser 1 неделю назад
Родитель
Сommit
6516424d31
7 измененных файлов: 166 добавлений и 66 удалений
  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 Просмотреть файл

@@ -21,10 +21,12 @@
<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/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/EEPROM}&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>
<inputType id="io.sloeber.compiler.cpp.sketch.input.1770649762" name="CPP source files" superClass="io.sloeber.compiler.cpp.sketch.input"/>
</tool>
@@ -32,10 +34,12 @@
<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/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/EEPROM}&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>
<inputType id="io.sloeber.compiler.c.sketch.input.1705705" name="C Source Files" superClass="io.sloeber.compiler.c.sketch.input"/>
</tool>
@@ -43,10 +47,12 @@
<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/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/EEPROM}&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>
<inputType id="io.sloeber.compiler.S.sketch.input.1190005390" name="Assembly source files" superClass="io.sloeber.compiler.S.sketch.input"/>
</tool>
@@ -87,7 +93,7 @@
</toolChain>
</folderInfo>
<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>
</configuration>
</storageModule>

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

@@ -34,32 +34,42 @@
<link>
<name>core/core</name>
<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>
<name>core/variant</name>
<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>
<name>libraries/DNSServer</name>
<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>
<name>libraries/EEPROM</name>
<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>
<name>libraries/ESP8266WebServer</name>
<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>
<name>libraries/ESP8266WiFi</name>
<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>
</linkedResources>
</projectDescription>

+ 1
- 1
.settings/language.settings.xml Просмотреть файл

@@ -2,7 +2,7 @@
<project>
<configuration id="io.sloeber.core.sketch.0" name="Release">
<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.g++"/>
</provider>

+ 6
- 0
.settings/org.eclipse.cdt.core.prefs Просмотреть файл

@@ -0,0 +1,6 @@
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 Просмотреть файл

@@ -1,15 +1,20 @@
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.dbg=Disabled
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.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.stacksmash=disabled
Config.Release.board.BOARD.MENU.vt=flash
Config.Release.board.BOARD.MENU.wipe=all
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.UPLOAD.PORT=COM3
Config.Release.compile.sloeber.extra.all=

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

@@ -1,13 +1,13 @@
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <EEPROM.h>
#include <DNSServer.h>

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

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

const byte DNS_PORT = 53;
@@ -191,6 +191,7 @@ void handleRoot() {
<body>

<div class="card">

<h2>Wemos Setup</h2>

<div class="mac">Device: )rawliteral"
@@ -198,19 +199,74 @@ void handleRoot() {
+ R"rawliteral(</div>

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

<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>

<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>

<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>
</html>
)rawliteral";
@@ -287,19 +343,71 @@ bool connectSTA() {
Serial.print("IP: ");
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 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 ----------
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // LED zunächst AUS

loadConfig();

@@ -317,50 +425,14 @@ void setup() {
void loop() {

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

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

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

WiFiClient client = server.available();

if (!client) {
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 Просмотреть файл

@@ -2,12 +2,12 @@
//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-07 15:50:46
//This file has been generated on 2026-06-07 22:43:11

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

@@ -18,6 +18,7 @@ void handleSave() ;
void handleNotFound() ;
void startAP() ;
bool connectSTA() ;
void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) ;
void setup() ;
void loop() ;


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