I've seen some interesting explanations on using websocket comms on mkr 1010 but it's always handling the mkr 1010 as the client and the server being always a computer or some other "non-arduino" server . I'm working on a system with several mkr 1010's in a master/slave setup and bidi comms i.e. i want to use one of the mkr's as a server (does the calculations etc.) and the other as clients (get result from calculations ans send back sensor info). I did not found any library or example of websocket server for mkr1010. I found it for ESP systems. Please help as i tried my bidi comms with webclient and webserver using http but not good enough for my applicaton (not realy bidi). thanks a lot !
1 Answer
Most websocket libraries work over a Client. Client is base class for all EthernetClient and WiFiClient classes.
But why do you want to use a Websocket for communication of two MCU in the same LAN? Websockets are intended for communication over HTTP infrastructure (firewalls, proxies) to a web server in Internet or from browser to a web server.
Use a basic TCP socket over Arduino Client.
WiFiClient object wraps a TCP socket. A normal TCP socket is connected to IP address and port. WiFiServer starts a listening socket on a port. If server on listening socket is contacted by a remote client socket, it creates a local socket connected with the remote client socket on a free port and returns a WiFiClient object wrapping the socket. Everything you write or print to a WiFiClient is send to that one remote socket.
If one of your client boards creates a WiFiClient and connects it to IP address and port of the WiFiServer on your 'server' board, then you get there a WiFiClient from server.available() and this two WiFiClient objects are connected. What you write/print on one side you read only from the WiFiClient object on the other side.
client socket
if (client.connect(serverIP, PORT)) {
client.print("request\n");
String response = client.readStringUntil('\n');
Serial.println(response);
client.stop();
}
server side
WiFiClient client = server.available();
if (client && client.connected()) {
String request = client.readStringUntil('\n');
Serial.println(request);
client.print("response\n");
client.stop();
}
-
thanks a lot ! I have also used the chatserver example to work it out !– MajidCommented Apr 10, 2020 at 20:26