Using websockets with a UNO R4 Wifi

Hello, I'm trying to try to use websockets with my new UNO R4 WiFi. The example sketches I have found that demonstrate the use of websockets have not compiled. They reference the library WebServer.h which does not not appear when I search for it while I have the UNO R4 board selected in the IDE. The websocket examples make calls to server.on() and server.handleClient() methods which are not part of the server object located in WiFiS3.h which is used for the UNO R4 WiFi. I have been successful using the examples to establish a simple webserver with the UNO R4 WiFi, but they do not use or demonstrate websockets. . I'm looking for a simple example of a websocket sketch for the UNO R4 WiFi so I can learn how to do that. Thanks for any help :slight_smile:

what error message did you get - upload as text
what WebServer.h library are you using? e.g. it looks like WebServer.h is an ESP32 library
although the UNO WiFi R4 has an onboard ESP32-S3 you don't appear to have direct access to run code on it - see web-server-on-uno-r4-wifi
have a look at uno-r4-library-compatibility

I guess you found mentions about this library
https://github.com/khoih-prog/WiFiWebServer/tree/master/src/WiFi_HTTPClient

for some reason it has not only the WebServer but a httpclient and a websocket client too.
It doesn't support the WiFiS3 library, but maybe it can be adopted.

I don't know if there are some generic libraries for WebSocket which can run on any WiFi library or already support the new WiFiS3 library

Thank you for the reply. To answer your question, I'm using the WiFiServer.h library that is part of the UNO R4 toolset. I've called the server object in this library with success for the simple server sketches that listen for the web client to request something. When I try to use this server object with the Websocket examples I have found online, I get the following compiler errors:
C:\Users\Watervale\Documents\Arduino\Oct2023\WebSocket_3\WebSocket_3.ino: In function 'void setup()':
C:\Users\Watervale\Documents\Arduino\Oct2023\WebSocket_3\WebSocket_3.ino:50:10: error: 'class WiFiServer' has no member named 'on'
server.on("/", { // define here wat the webserver needs to do
^~
C:\Users\Watervale\Documents\Arduino\Oct2023\WebSocket_3\WebSocket_3.ino: In lambda function:
C:\Users\Watervale\Documents\Arduino\Oct2023\WebSocket_3\WebSocket_3.ino:51:12: error: 'class WiFiServer' has no member named 'send'; did you mean 'end'?
server.send(200, "text/html", website); // -> it needs to send out the HTML string "webpage" to the client
^~~~
end
C:\Users\Watervale\Documents\Arduino\Oct2023\WebSocket_3\WebSocket_3.ino: In function 'void loop()':
C:\Users\Watervale\Documents\Arduino\Oct2023\WebSocket_3\WebSocket_3.ino:60:10: error: 'class WiFiServer' has no member named 'handleClient'
server.handleClient(); // Needed for the webserver to handle all clients
^~~~~~~~~~~~

exit status 1

Compilation error: 'class WiFiServer' has no member named 'on'

WiFiServer is a basic TCP server. WebServer is a HTTP server. There is a big difference in complexity

Juraj, thanks for pointing that out about the difference in servers. I was not aware. I'm very new to understanding servers, clients, etc I'm going to try some examples in the WiFi_HTTPClient library you suggested earlier.

seems to me that the simplest approach is for you to buy a ESP32
approximate cost in UK about £5 against £30 for UNO R4 WiFi

Horace, yeah, i will consider experimenting with the ESP32. I'm a little surprised that the UNO R4 libraries can't support it yet. I'm also going to keep poking around and learning the difference between TCP servers and HTTP servers. Thanks

it seems that UNO R4 doesn't have support for web sockets, correct?

For Arduino Uno R4, You can use mWebSockets library. The library enables Ethernet Shield be default, To change to WiFi, A little modification is needed in the config.h file.

You can see the detail instruction in Arduino Uno R4 WebSocket Tutorial

1 Like

Thanks. I’ll give it a try.

1 Like

Please give the feedback here if it works for you. It will be useful for others, who is searching for a solution

Hello, what was the results after using the mWebSocket.h?

For me, it works very well!

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

Compile error:

HTML2.ino.ino:5:10: fatal error: WebServer.h: No such file or directory
#include <WebServer.h>
^~~~~~~~~~~~~
compilation terminated.
exit status 1

Compilation error: WebServer.h: No such file or directory

What library is WebServer.h in?