You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
I need to use ESP32 as a websocket over ethernet without wifi.
The environment cannot use wifi.
I need a websocket connection from my laptop to ESP32 via ethernet.
How can this be possible?
Even if I try to change the example, the server is connected and the web page is loaded, but the websocket cannot be connected.
Thank you in advance.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetWebServer.h>
#include <WebSocketsServer_Generic.h>
EthernetWebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
switch (type)
{
case WStype_DISCONNECTED:
Serial.println( "[" + String(num) + "] Disconnected!");
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// send message to client
webSocket.sendTXT(num, "Connected");
}
break;
case WStype_TEXT:
Serial.println( "[" + String(num) + "] get Text: " + String((char *) payload));
if (payload[0] == '#')
{
// we get RGB data
// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
}
break;
default:
break;
}
}
void setup() {
WiFi.mode(WIFI_AP_STA); //Infinite reboot prevention
Serial.begin(115200);
Ethernet.init (5);
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
Ethernet.begin(mac);
Serial.print("WebSockets Server @ IP address: ");
Serial.println(Ethernet.localIP());
webSocket.begin();
webSocket.onEvent(webSocketEvent);
server.on("/", []()
{
// send index.html
server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']); connection.onopen = function () { connection.send('Connect ' + new Date()); }; connection.onerror = function (error) { console.log('WebSocket Error ', error);};connection.onmessage = function (e) { console.log('Server: ', e.data);};function sendRGB() { var r = parseInt(document.getElementById('r').value).toString(16); var g = parseInt(document.getElementById('g').value).toString(16); var b = parseInt(document.getElementById('b').value).toString(16); if(r.length < 2) { r = '0' + r; } if(g.length < 2) { g = '0' + g; } if(b.length < 2) { b = '0' + b; } var rgb = '#'+r+g+b; console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/></body></html>");
});
server.begin();
}
void loop()
{
unsigned long t = millis();
webSocket.loop();
server.handleClient();
}
The text was updated successfully, but these errors were encountered:
I need to use ESP32 as a websocket over ethernet without wifi.
The environment cannot use wifi.
I need a websocket connection from my laptop to ESP32 via ethernet.
How can this be possible?
Even if I try to change the example, the server is connected and the web page is loaded, but the websocket cannot be connected.
Thank you in advance.
The text was updated successfully, but these errors were encountered: