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);
}