AsyncUDP reboots on .listen()

Apple Mac OS
Firefox 124.0.2
Arduino IDE 2.3.3-nightly-20240328   
Arduino Nano ESP32
Arduino C++
#include <AsyncUDP.h>

#include <UniversalTimer.h>

#define LED_BUILTIN 2 // The built-in LED is connected to pin 2

UniversalTimer WiFi_transmit_start(5000, false);
UniversalTimer WiFi_transmit_delay(2000, false);
UniversalTimer blink_on(250, false);
IPAddress ip(192, 168, 1, 173);
uint16_t IPport = 57400;
uint8_t sensorValue;
uint32_t remote_IP;
uint8_t data_in;
uint8_t data_out;
uint8_t packet_size;

void read_incoming();
void write_outgoing();

AsyncUDP udp;
AsyncUDPPacket *packet;

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

  // Set ADC resolution (optional)
  analogReadResolution(8); // 8-bit resolution

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  data_in = 0;

  //listen(const IPAddress addr, uint16_t port)
  udp.listen(ip, IPport);
/*
  udp.onPacket([](AsyncUDPPacket packet) {

    //read_incoming();

    Serial.println("end of lambda");
  }); //udp.onPacket([](AsyncUDPPacket packet)
*/
  Serial.println("end of setup()");

} //void setup()



void loop() {
/*
  Serial.println("start loop()");

    //this ensures somebody starts transmitting
  //if (!WiFi_transmit_start.isRunning()) WiFi_transmit_start.start();

  if (data_in) {
    Serial.println("if (data_in)");

    //.check() indicates timeout has expired
    if (WiFi_transmit_delay.check() | WiFi_transmit_start.check()) {
      Serial.println("pre-write_outgoing()");
      //returns data_out
      write_outgoing();
   
      //size_t write(uint8_t): Writes a single byte to the current packet.
      //udp.write(data_out);  //Writes a single byte to the current packet.

      WiFi_transmit_start.resetTimerValue();

      data_out = 0;

      //udp.flush(); //Stops the UDP connection and cleans up resources.

      

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

  Serial.println("pre-blink()");

  blink();

  data_in = 0;
*/
  Serial.println("end of loop()");

} //void loop()


Everything seems to work correctly until I add udp.listen(ip, IPport);. When I do, I get

Backtrace: 0x40083535:0x3ffb2020 0x4008a645:0x3ffb2040 0x4008f541:0x3ffb2060 0x400df9a6:0x3ffb2190 0x400d227d:0x3ffb21c0 0x400d242e:0x3ffb2200 0x400d1ded:0x3ffb2240 0x400d37ee:0x3ffb2290

ELF file SHA256: 2af11f397ef3bcb1

Rebooting...

ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:1

load:0x3fff0030,len:1344

load:0x40078000,len:13964

load:0x40080400,len:3600

entry 0x400805f0

Ln 43, Col 3

ESP32-WROOM-DA Module

on /dev/cu.usbserial-0001

2

I checked the method .listen() against the file AsyncUDP.h, and the way I'm using it looks good.
When I take .listen() out, udp.onPacket() generates no errors. I think the AsyncUDP.h package I'm using came from the ESP32 board (upgrade?)

What's happening?

Haven't used the AsyncUdp before but don't you need the wifi connected first??
Async UDP Server demo
The demo does anyways..

good luck.. ~q

1 Like

I don't know why I didn't find the example before. While (of course) I'm having new problems now, I'm betting you were right about missing WiFi.mode and WiFi.begin.

Thank you!

You're welcome..
Don't knock yourself, I'm pretty good at finding stuff.. :slight_smile:

What new issues are you having, maybe I can help??

~q

Today I'm having a hard time getting my code to work as well as it did yesterday.

At the moment I am concentrating on getting the example you sent running. I have three versions of WiFi.h scattered around. The one that works best is the one that came from WiFiNINA, which of course was intended for Arduino Uno, not Nano ESP32. The others (Arduino ESP32 and Espressif) error out on failure to find precursor header files. The same thing is happening with AsycUDP.h.

I went to the Arduino Library Manager to look for WiFi. There were many hits, but nothing specifically for WiFi. I'm accustomed to looking for appropriate libraries in Github, but it's hard to say how successful that's been. Is there a way to find a WiFi library specifically for ESP32 through the Arduino IDE?

Another possibility is to find a WiFi.h and an AsyncUDP.h, then copy them, along with all the subsequent precursors, into one custom folder. However, my experience says that no matter how many precursors I copy into my custom folder, there will always be more needed.

Ideas?

These are both included in the esp32 Arduino core, the link earlier is to the core..
They should work straight away..
What core gets used depends on the board selected and what cores you have installed for them..

hope this helps.. ~q

//#include </Users/User/Robot/programming/libraries/WiFi/src/WiFi.h> //ESP32 Grokhotkov
//#include </Users/User/Robot/programming/libraries/esp/esp-idf/components/esp_wifi/include/esp_private/WiFi.h> //SPDX
#include </Users/User/Robot/programming/libraries/arduino-esp32-master/libraries/WiFi/src/WiFi.h> //ESP32 Grokhotkov

//#include </Users/User/Library/Arduino15/packages/arduino/hardware/esp32/2.0.13/libraries/AsyncUDP/src/AsyncUDP.h> //ESP32 me-no-dev
#include </Users/User/Robot/programming/libraries/arduino-esp32-master/libraries/AsyncUDP/src/AsyncUDP.h> //tcpip ESPASYNCUDP_H ??

#include <arduino_secrets.h>




//const char* ssid = "***********";
//const char* password = "***********";

AsyncUDP udp;

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("WiFi Failed");
    while (1) {
      delay(1000);
    }
  }
  if (udp.listen(1234)) {
    Serial.print("UDP Listening on IP: ");
    Serial.println(WiFi.localIP());
    udp.onPacket([](AsyncUDPPacket packet) {
      Serial.print("UDP Packet Type: ");
      Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast"
                                                                             : "Unicast");
      Serial.print(", From: ");
      Serial.print(packet.remoteIP());
      Serial.print(":");
      Serial.print(packet.remotePort());
      Serial.print(", To: ");
      Serial.print(packet.localIP());
      Serial.print(":");
      Serial.print(packet.localPort());
      Serial.print(", Length: ");
      Serial.print(packet.length());
      Serial.print(", Data: ");
      Serial.write(packet.data(), packet.length());
      Serial.println();
      //reply to the client
      packet.printf("Got %u bytes of data", packet.length());
    });
  }
}

void loop() {
  delay(1000);
  //Send broadcast
  udp.broadcast("Anyone here?");
}

AsyncUDPServer.ino:17:3: error: 'WiFi' was not declared in this scope
WiFi.mode(WIFI_STA);

Sorry for the wait..
Should not need those long includes..
When the core is installed you just need <WiFi.h> and same for the <AsyncUDP.h>..
Should compile..
esp32 Async UDP simmed..
Above is a link to a WoKi simulator with code you posted only changed the includes and it compiles..

What version of esp32 is installed into Arduino??

~q

Should not need those long includes..
Agreed, but when it doesn't work on the first half dozen tries, you look for why. There are only a few versions of either, but they get repackaged a lot. Using the fully specified paths allows me to be sure what I'm testing. The notes at the end help me remember which version of the file I'm testing.

version: Arduino_ESP32_OTA

I've never heard of Wokwi before. I'll have to look into it. I tried running it, and this is what I got...

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:2

load:0x3fff0030,len:1156

load:0x40078000,len:11456

ho 0 tail 12 room 4

load:0x40080400,len:2972

entry 0x400805dc

WiFi Failed

Which version of WiFi.h and AsyncUDP.h are you using?

well, if you edit the ssid and password, might connect..
but it does compile and try to spin up..

that's not exactly what I was asking..
latest stable release is 2.0.15..

Arduino ESP32 Core

In Arduino, got the latest developer release 3 something..

But if you can't compile by using <WiFi.h> then something not right with your core install..

WoKi is nice, use it quite a bit especially around here helping people..
Just got a free account..

~q

here's a screen shot of my Arduino..
Got the board manager open and you can see esp32 version installed..

You are seriously useful.

Wokwi can't find my arduino_secrets file, even after I gave a complete path. I gave up and entered everything in the plain. The program took much longer to run, so you were right, it wanted a host and password, but still failed.

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:2

load:0x3fff0030,len:1156

load:0x40078000,len:11456

ho 0 tail 12 room 4

load:0x40080400,len:2972

entry 0x400805dc

WiFi Failed

that's not exactly what I was asking..
latest stable release is 2.0.15..
I looked up ESP32 in Library Manager, and Arduino_ESP32_OTA is what I got. The latest stable release is: 2.0.15. Great. What is the file name?

In Arduino, got the latest developer release 3 something..
Being a newb, I am not in the habit of jumping into developers versions, but I'm more than willing to try them. :slightly_smiling_face: Where do I download dev rel 3?

You'd have to add the file, took me a while to figure that one out..
hit the little arrow next to library manager, select new file, give it a name, copy the dope into it and use a local include "secrets.h"..
Don't really use it for network stuff but looks like it should work..

Did a some demo's of using udp for esp32 a while back..
UDP Receiver..
Show how to send/receive structures..
There's a sender in my git too..

~q

Check the Board Manager, not the lib manager..

~q

You probably gonna see and install instead of update like my screen shot..
I hope so anyways, easy fix..

~q

What you can install depends on the url you put into the "Additional boards manager urls" in file preferences..

here's another screen shot..

~q

this may help..

Arduino esp32 using Arduino IDE

Has both urls, use only one of them..
Really don't need the latest dev, usually a bit broken.. :slight_smile:

~q

Now I'm guessing you are asking for the Arduino IDE version I'm using.
Version: 2.3.3-nightly-20240328

I now have;
https://espressif.github.io/arduino-esp32/package_esp32_index.json
https://espressif.github.io/arduino-esp32/package_esp32_index.json
(rebooted Arduino IDE)

Arduino ESP32 boards remains at 2.0.13.
ESP32 2.0.14 has been updated to 3.0.0-rc1.

I ran New ESP32 Project - Wokwi Simulator and it worked. As of now I can't prove what copy of WiFi.h wokwi was using. I copied the file over to a new sketch on IDE and used the new copy (of an existing) WiFi.h (3.0.0-rc1), and got hung up on one of the precursors (WiFiUDP.h).

Do you have this url in the "additional boards manager URL's" twice??
That might confuse things, take one out, only need one..
That url is for the stable release, which is 2.0.15..

No not really, but I do see you're using the nightly release..
I'm on a slightly older, you can see version in the pics i posted..
I wouldn't expect the nightly build to be the problem though..

This doesn't sound right, this should be the same??
The esp32 core for Arduino gets installed here on my box..
C:\Users\user\AppData\Local\Arduino15\packages
My user name is user..
I have one folder in there named esp32..
Arduino manages these folders for you, so you don't normally have to poke around in there..
I would try removing the esp32 url from file->preferences..
Shutdown Arduino..
Then delete the esp32 folder inside packages..
Then launch Arduino add url back into file->preferences board manager urls..
Then install latest stable release..
Should re-install the esp32 core..

~q

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