Getting UDP.h to Work

Apple Mac OS 14.4.1
Firefox 125.0.1
Arduino IDE 2.3.2
Arduino Nano ESP32
Arduino C++
I am trying to get a minimal program going to prove UDP works, unsuccessfully. So far I've tried AsyncUDP and WiFiUDP.
Now I'm working with UDP.h. (I have not yet found an example to use.) While it appears I'm transmitting, I haven't been able to receive anything. The program is written so one processor transmits to the other. You swap the local (ip) and remoteIPs, then upload the same program to the second processor.

What am I getting wrong?

//client4/main

#include <WiFi.h>
#include <UDP.h>
#include <UniversalTimer.h>
#include <arduino_secrets.h>

//const char ssid[] = "ABCD";
//const char password[] = "123456";

UniversalTimer WiFi_transmit(2000, true);
//IPAddress ip(192, 168, 1, 54);
//IPAddress remoteIP(192, 168, 1, 143);
IPAddress ip(192, 168, 1, 143);
IPAddress remoteIP(192, 168, 1, 54);
uint16_t IPport = 57400;
uint32_t remote_IP;

//WiFiClass WiFi;
NetworkUDP udp;

void setup() {
  Serial.begin(115200);
  delay(1000);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  udp.begin(ip, IPport);

  WiFi_transmit.start();

  Serial.println("end of setup()");
  delay(100);

} //void setup()



void loop() {
    
  //.check() indicates timeout has expired
  if (WiFi_transmit.check()) {

    //returns data_out
    write_outgoing();

  } //if (WiFi_transmit_delay.check() | WiFi_transmit_start.check()) 

  read_incoming();

} //void loop()




//client4/read

void read_incoming() {
  
  //Stores the IP address of the remote endpoint of the UDP connection.
  IPAddress remote_IP; 
  //Read a byte of data from the packet.
  uint8_t buffer_in[255];

  //available(): Returns the number of bytes remaining in the current 
  //packet.
  if(udp.available()) {

  //read(char *buffer, size_t len): Reads up to len characters from the 
  //current packet and places them into the buffer. Returns the number 
  //of characters read, or 0 if none are available.
  udp.read(buffer_in, udp.available());

  Serial.print("remoteIP ");
  Serial.print(remote_IP);
  Serial.print(" = ");
  Serial.println(buffer_in[0]);
  } //if(udp.available())

  return;
} //void read_incoming()



//client4/write_outgoing

void write_outgoing() {

  uint8_t data_out = rand() / 100;

  Serial.print("data_out = ");
  Serial.println(data_out);

  //int beginPacket(IPAddress ip, uint16_t port): Begins a packet to a specific IP and port.
  udp.beginPacket(remoteIP,IPport);

  //size_t write(uint8_t): Writes a single byte to the current packet.
  udp.write(data_out);

  //end and transmit packet
  udp.endPacket();

  return;
} //void write_outgoing();



change the above too..

  if (udp.parsePacket()) {

available doesn't really do anything, parsePacket does all the work..

good luck.. ~q

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