Web Server and Web Sockets

I have tried to run the same software (Web Server program
with Web Sockets) on on 3 different platforms.
The only differences are the included libraries.

The Arduino Nano_33_IOT is the only one giving problems.

Are there any known library issues or is it me who are
doing something wrong!!!!!


  1. Selected board = ESP8266 Generic

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>

ESP8266WebServer server;
WebSocketsServer webSocket = WebSocketsServer(81);

Result: Compiling and running perfect.


  1. Selected board = TTGO T1 (Esp32 type)

#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer_Generic.h>

WebServer server;
WebSocketsServer webSocket = WebSocketsServer(81);

Result: Compiling and running perfect.


  1. Selected board = Arduino Nano_33_IOT

#include <WiFiNINA_Generic.h>
#include <WiFiWebServer.h>
#include <WebSocketsServer_Generic.h>

WiFiWebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);

Result: Compiling and running with the following error
message from the Chrome Browser.

WebSocket connection to 'ws://192.168.68.114:81/' failed:

Copy of the software:

#include <WiFiNINA_Generic.h>
#include <WiFiWebServer.h>
#include <WebSocketsServer_Generic.h>

WiFiWebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);

char* ssid = "xxxxx";
char* password = "yyyyy";

char webpage[] PROGMEM = R"=====(
<html>
<head>
  <script>
    var Socket;
    function init() {
      Socket = new WebSocket('ws://' + window.location.hostname + ':81');
      Socket.onmessage = function(event) {
        document.getElementById("rxConsole").value += event.data;
      }
    }
    function sendText() {
      Socket.send(document.getElementById("txBar").value);
      document.getElementById("txBar").value = "";
    }
    function sendBrightness(){
      Socket.send("#"+document.getElementById("brightness").value);
    }    
  </script>
</head>
<body onload = "javascript:init()">
  <div>
    <textarea id = "rxConsole"> </textarea>
  </div>
    <hr/>
  <div>
    <input type = "text" id = "txBar" onkeydown = "if(event.keyCode == 13) sendText();" />
  </div>
    <hr/>
  <div>
    <input type="range" min="0" max="1023" value="512" id="brightness" oninput="sendBrightness()" />
  </div>  

</body>
</html>
)=====";

void setup() {
  Serial.begin(115200);
  String fv = WiFi.firmwareVersion();
  Serial.print("WiFi Firmware Version: ");
  Serial.println(fv);

  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.print("\nIP Address: ");
  Serial.println(WiFi.localIP());

  server.on("/",[](){server.send_P(200,"text/html", webpage);});
  server.begin();
  webSocket.begin();
  webSocket.onEvent(webSocketEvent);
  Serial.println("In browser type:  192.168.68.xxx");
}

void loop() {
  webSocket.loop();
  server.handleClient();
  if(Serial.available() > 0) {
    char c[] = {(char)Serial.read()};
    webSocket.broadcastTXT(c, sizeof(c));
  }
}

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  if(type == WStype_TEXT) {
    if(payload[0] == '#'){
      uint16_t brightness = (uint16_t) strtol((const char *) &payload[1], NULL, 10);
      Serial.print("brightness= ");
      Serial.println(brightness);
    }
    else {
      for(int i = 0; i < length; i++)
      Serial.print((char)payload[i]);
      Serial.println();
    }  
  }
}

Arduino Nano 33 IoT: https://store.arduino.cc/arduino-nano-33-iot.

Could you please add a line with three backslash-single-quotes before and after the sketch ?

```
Your sketch
```

The NINA module might have a older firmware version. Do you know what is inside the NINA block ? A ESP32 !

Thank you for the hint - three backslash-single-quotes.

I added a couple of lines - WiFi Firmware Version: 1.3.0

Regards Carsten

Forgot your question regarding the NINA block - according to the product info - it should be a NINA-W10 with an ESP32.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.