Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

[Question] #9

Closed
qlalfdu opened this issue May 7, 2021 · 1 comment
Closed

[Question] #9

qlalfdu opened this issue May 7, 2021 · 1 comment
Labels
Support Library support wontfix This will not be worked on

Comments

@qlalfdu
Copy link

qlalfdu commented May 7, 2021

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();
}

PN20-1357

@khoih-prog
Copy link
Owner

This WebSockets library only works for ESP826/EP32 using native WiiFi. Check Important Notes. I suggest you try the WebSockets2_Generic Library.


Important Notes

  1. Currently, the WebSocketServer feature is usable only for ESP8266/ESP32 using native WiFi.
  2. The timeline when to fix and reintroduce the WebSocketServer feature to other boards is not determined yet.
  3. Please use the new WebSockets2_Generic Library if WebSocketServer is necessary. See Issue 2, Issue 3 and Issue 4

@khoih-prog khoih-prog added Support Library support wontfix This will not be worked on labels May 7, 2021
Repository owner deleted a comment from Shivdoke39 Nov 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Support Library support wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants