Go Down

Topic: how to conect two Arduino UNO Wifi Rev 2 (Read 421 times) previous topic - next topic

aastrog

Hi,


trying to connect two Arduino UNO Wifi Rev 2 boards via Wifi.
One will be the server  creating the Wifi network and sending data. Second one will be a client receiving the data and processing it.

Any link to simple code example?

Thanks

Juraj

#1
Jul 02, 2020, 11:56 am Last Edit: Jul 02, 2020, 11:58 am by Juraj
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 your client board 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

Code: [Select]
   if (client.connect(serverIP, PORT)) {
     client.print("request\n");
     String response = client.readStringUntil('\n');
     Serial.println(response);
     client.stop();
   }

server side

Code: [Select]
   WiFiClient client = server.available();
   if (client && client.connected()) {
     String request = client.readStringUntil('\n');
     Serial.println(request);
     client.print("response\n");
     client.stop();
   }


see the examples of the WiFiNINA library for full server and client examples

Go Up