Communication between two boards using wifi

Hello, I am pretty new to Arduino programming and currently, I am writing some codes for two Arduino boards: MKR WiFi 1010 and Nano 33 IOT to send and receive data using wifi. I am using the Nano board as a client to send an array of int containing potentiometer ADC values, and the MKR board as a server receiving the data. Below is the code for each board and the problem is that I cannot receive the data on the server. There might be some problems connecting two boards or using the write() and read() methods but I honestly could not find which part of the codes went wrong. Any help will be greatly appreciated. Thank you.

client side

#include <SPI.h>
#include<WiFiNINA.h>
  
char SSID[] = "....."; 
char PASS[] = ".....";
int status = WL_IDLE_STATUS;
IPAddress mkr (192, 168, 1, 105);
WiFiClient nano;

void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial.print("connecting to wifi: ");
  Serial.println(SSID);

  status = WiFi.begin(SSID, PASS);
  if(status!= WL_CONNECTED){
    Serial.println("connection failed");
    while(true);
  }
  else{
    Serial.print("connected to: ");
    Serial.println(SSID);
    if(nano.connect(mkr, 23)){
      Serial.println("connected");
    }
    else{
      Serial.println("connection failed");
    }
  }
  
}

void loop() {
  int data[] = {analogRead(A0)/10.23, analogRead(A1)/10.23};
  if(nano.connected()){
  nano.write((uint8_t*)&data,sizeof(data));
  }
}

server side

#include <SPI.h>
#include<WiFiNINA.h>
 
char SSID[] = "....."; 
char PASS[] = ".....";
int status = WL_IDLE_STATUS;
WiFiServer mkr(23);

void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial.print("connecting to wifi: ");
  Serial.println(SSID);

  status = WiFi.begin(SSID, PASS);
  if(status!= WL_CONNECTED){
    Serial.println("connection failed");
    while(true);
  }
  else{
    mkr.begin();
    Serial.print("Connected to wifi. My address:");
    IPAddress myAddress = WiFi.localIP();
    Serial.println(myAddress);
  }
}

void loop() {
  WiFiClient client = mkr.available();
  int size=10;
  int*clientData=(int*)malloc(size);
  if(client){
    if(size=client.available()){
      size=client.read((uint8_t*)clientData,size);
    }
  }
  Serial.println(clientData[0]);
  Serial.println(clientData[1]);
  free(clientData);
}
 

Ooops!

Does this help: Link to a very basic tutorial about TCP (evolved to: TCP-socket democode to send / receive data / characters similar to serial - #8 by PerryBebbington ?

I wrote that for the ESP8266, I suspect there are differences between that and the boards you are using, but hopefully there's enough there to help you.

Oh! And well done for posting your code correctly on your first post :smiley:

1 Like

What does your Serial Monitor output tell you?

Hi, it currently shows no value at all

Hi, I have just gone through the link and the setup was a little different but it looked really helpful. Thank you for your comment.

Are you saying that your sketch doesn't get as far as printing "connecting to wifi: "?!?

I thought you were asking about these lines. To answer your question again, both boards connect to the wifi and the client also connects to the server. However, the lines I have quoted do not print any values to the serial monitor.

That is very strange. The loop() function should print those two lines even if there is no client connection. Without the network parts, it is equivalent to:

void loop() 
{
  int size=10;
  int * clientData = (int*) malloc(size);
  Serial.println(clientData[0]);
  Serial.println(clientData[1]);
  free(clientData);
}

I found that strange as well. Do you think there are any parts of the code that are causing this problem?

The sketch must be hanging before getting to loop(). I would add more debug output to setup() to see where it is hanging.

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