Make two Arduinos wifi rev2 communicate

hello everyone,
i start from the assumption that i am new to arduino programming.
I need to make two arduino wifi rev2 communicate with each other, as I understand I need to make one be the master and one the slave, using the examples in the Wifinina libraries I managed to connect the two Arduino and I managed to turn on and off a led reading from the master what the slave writes, now I can't make the slave read what the master writes on the client so that it is possible to activate outputs or functions on the slave based on what the master writes.
I hope I have explained myself well.
thanks
Riccardo

Take a look at https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterWriter

@Dozie that is not about WiFi

@pelliz1995 in TCP there are servers and clients not masters and slaves. is your master a client or a server? show as some code

Sorry, I didn't write that the two arduino should communicate wirelessly via the wifinina board intergrated in arduino wifi rev2

lama_wireless_master.ino (2,8 KB)
lama_wireless_slave.ino (1,6 KB)
hi,
for now the program is like this, momentarily the server (mistakenly called master by me) reads on the client and based on what it reads it turns on or off the integrated led on the board, however, I don't know what functions to use to allow the client to read what the server writes and based on that make functions.
thank you very much
Riccardo

so if then client is master and server is slave as server serves.

after you get client from server, the two clients are peers. what you write in one of them you can read from the other no matter which one initiated the connection.

no, the file "lama wireless master" would be the server while "lama wireless slave" would be the client, in the server I create an access point with ssid and password and wait for the connection, when they are connected, the server through the function client.read I read what the client writes through the function client.println and based on what it reads I turn on and off a led, and up to here already works.
But now I have to make the server write to the client and on the basis of what it writes the client will have to open or close an output, I can not understand what functions to use.
Reading on the instructions of the wifinina it would seem that I have to use "server.print" to write from the server information for the client but I do not know how to read from the client what is written from the server.
in a nutshell, what i need to do is to wirelessly connect the two arduinos, the communication between the two arduinos must be bilateral, arduino number one must send information to arduino number two and arduino number two must send information to arduino number one.
i hope i have explained myself correctly.
i apologise for any spelling mistakes but i have to translate from italian to english.
thank you
Riccardo

then start a server on the other Arduino too

good evening everyone,
i managed to get the two arduinos to communicate correctly, the server communicates either 0 or 1 to the client and vice versa.
the problem arises when i simulate a problem on the server (to simulate the problem i disconnect the power supply), in a few words when there is no communication the client sends WiFi.begin() every 60 seconds and when it sends it there is a slowdown/locking of the arduino for a few seconds and then it delays the part of the program that should work even without a connection (in this case just a led that blinks).
below is the programme I wrote on the server:

#include <SPI.h>
#include <WiFiNINA.h>

    
int keyIndex = 1;                
int led =  8;
unsigned long t1, dt1 = 0;
WiFiServer server(80);
IPAddress ip(192, 168, 4, 1);

void setup() {

  Serial.begin(9600);
  pinMode(led, OUTPUT);
  WiFi.config(ip);
  WiFi.beginAP("Prova1234", "Prova4444");
  server.begin();
}

void loop() {

  WiFiClient client = server.available();

  if (client.available()) {           

   int c = client.read();            

   if (c == 48) {

    digitalWrite(led, HIGH);  
    client.println("0");          

   }

   if (c == 49) {
 
    digitalWrite(led, LOW);   
    client.println("1");            
   }
  }
}

below is the programme I wrote on the client:

#include <SPI.h>
#include <WiFiNINA.h>
 
int led = 8;  
int led2 = 7;                       
unsigned long t1, dt1 = 0; 
byte secondi = 0;
byte ritardowifi = 0;
bool timer = false;   
bool lamp = false;          
WiFiClient client;   
IPAddress ip(192, 168, 4, 2);
IPAddress server(192,168,4,1);

void setup() {

 Serial.begin(9600); 
 pinMode (led, OUTPUT);
 pinMode (led2, OUTPUT);
 WiFi.config(ip); 

}

void loop() {
  
if (secondi - ritardowifi >= 60){
   ritardowifi = secondi;
   lamp = 0;
 }
 
 if (lamp == 0 && WiFi.status() != WL_CONNECTED) {     
   lamp = 1;
   WiFi.begin("Prova1234", 1, "Prova4444");
   if (WiFi.status() == WL_CONNECTED){   
   client.connect(server, 80);
   }
  } 
  
 dt1 = millis() - t1; 
 if (dt1 >= 1000){
   t1= millis();
   timer =!timer; 
   secondi = secondi + 1; 
   if (timer == false){
     client.write("1");
     digitalWrite(led2, HIGH);
   }
   if (timer == true){
     client.write("0");
     digitalWrite(led2, LOW);
   }
 }
 
 if (client.available()) {
  int c = client.read();
  if (c == 49){
    digitalWrite(led, HIGH);
  }
  if (c == 48){
    digitalWrite(led, LOW);
  }
 }
}

thanks to all

hi,
i've already tried but i still have to set the minimum timeout to 1000ms (lower it can't connect) and anyway a "delay" in the program creates it.
I've already tried to reread all the various functions on "wifinina" to see if I can solve it but I haven't found anything, even doing a scan of the networks so that I send WiFi.begin() only when there's the wifi network I'm interested in I have slowdowns because even the "wifi.scannetwork" function creates slowdowns in the program.

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