Sending sensor data from one microcontroller to another using wifi

Hi, I am stuck at my project and need some help.

For this project I am using two raspberry pi pico w modules.
The one is reading the light intensity from pin A0 using a photoresistor coupled with a 12kohm resistor (everything is working as it should, no problems whatsoever at this point) I am getting values on Serial Monitor max at 1023 and min at 0.
The second pico w is in another room and should turn a build in LED on when a certain threshold of light intensity is reached (in my case it is 550) direct reading no conversion for the simplicity purpose. I have no idea whatsoever how to send and receive this event.
I found some code online for Server and Client but I do not understand most of it. Any help is much appreciated thanks

int photoresistor;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(photoresistor > 550)
  {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, LOW);
  }

  photoresistor = analogRead(A0);
  Serial.println(photoresistor);
  delay(125);
}

my answer there applies to any Arduino WiFi or Ethernet library

1 Like

Can you be more precise? I read your answer, but still cannot understand how to send for example a bool lightOn {true} to the other microcontroller so it lights up the LED.

I saw this video a couple of times I know how to connect Client and Server to my WiFi network, but I still did not figure out the communication part.
(109) Pi Pico W with the Arduino IDE | Using WiFi - YouTube
In the video he is using only one controller and web interface. I have a slightly different case

Maybe try a UDP broadcast instead of TCP client / server..
Every so seconds broadcast the temp to the local broadcast address..
Any system listening will get it..

UDP is not guaranteed, so possible to miss a packet and some consideration can be taken if needed..
But in this case, no big deal, get it on the next shot..

UDP Send

The above is an example, squawks out the datagram structure every 10 seconds..

have fun.. ~q

1 Like

Thank you I am going to take a look at the code right now.

I have looked at the link you sent me and still can not implement it. In the link there is only client part. I am confused right now. Any other advices how to make it?

Well, there is both parts, just not a receiver on esp32..
Here's the other end done on esp32, receiver..

//UDP Receive..


#include <WiFi.h>
#include <WiFiUdp.h>

// network SSID (network name). and network password.
char ssid[] = "****";
char pass[] = "****";



// network key Index number (needed only for WEP).
int keyIndex = 0;

// UDP information
//local broadcast address..
IPAddress udpServer(192, 168, 0, 255);
#define UDP_PORT 3000
WiFiUDP udp;

// UDP Datagram
struct __attribute__((packed)) UDPDatagram {
  uint16_t seq;                          // sequence number
  int16_t sdata;                         // 16 bit integer data
  int32_t idata;                         // 32 bit integer data
  float fdata;                           // float data
  uint16_t crc;                          // crc check
} udpDatagram = { 0, 0, 0, 0.0, 0 };  // initial values


uint8_t buff[sizeof(UDPDatagram)];

void setup() {
  Serial.begin(115200);
  // Wait for serial port to connect. Needed for Leonardo only
  while (!Serial)
    ;
  delay(1000);
  Serial.println();
  Serial.println("ESP32 WiFi UDP listener - receives UDP datagrams from sender");
  WiFi.mode(WIFI_STA);  // Connect to Wifi network.
  WiFi.begin(ssid, pass);
  Serial.println("");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.print("\nlistener IP address: ");
  Serial.println(udpServer);
  udp.begin(UDP_PORT);
  Serial.print("listening on udp port: ");
  Serial.println(UDP_PORT);
}


void loop() {

  if (udp.parsePacket()) {
    //new packet..
    int recv = udp.read(buff, sizeof(buff));
    if (recv == sizeof(UDPDatagram)) {
      memcpy(&udpDatagram,&buff, sizeof(UDPDatagram));
      if (crcVerify()) {
        Serial.println("Packet received..");
      } else{
        Serial.println("Ignoring packet..");
      }
    }
  }
}

bool crcVerify() {
  uint16_t crc = 0;
  Serial.print("Datagram data ");
  for (int i = 0; i < sizeof(udpDatagram) - 2; i++) {
    Serial.print((uint8_t)((uint8_t *)&udpDatagram)[i], HEX);
    Serial.print(' ');
    crc += ((uint8_t *)&udpDatagram)[i];
  }
  return (crc == udpDatagram.crc);
}

UDP Receive

good luck.. ~q

1 Like

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