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!!!!!
- Selected board = ESP8266 Generic
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
ESP8266WebServer server;
WebSocketsServer webSocket = WebSocketsServer(81);
Result: Compiling and running perfect.
- 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.
- 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();
}
}
}