UDP processing in loop

I am attempting to write a UDP application to control the low voltage lights on my back patio. I have it working well enough but I don't think how the incoming udp packets are handled correctly. I took the code from the udp server example and in setup:

 setup(){
  if(udp.listen(1234)) {
     Serial.print("UDP Listening on IP: ");
     Serial.println(WiFi.localIP());
     udp.onPacket([](AsyncUDPPacket packet) {
       process(packet);
     }
 }

I am assuming then that this creates an ISR which responds to incoming udp requests from the client and processes them entirely within the ISR. I also assume that the AsyncUDPPacket is something like a volatile.

Is there a technique that will allow the process to occur in the normal loop(). The issue for me is the response, which will be sent back to the sender when the process is complete, requires the AsyncUDPPacket to send. I imagine I can create a queue of the requests that come in from the listen and process them in turn, but the packet needs to still be available.

Am I trying to turn this into a tcp application?

what communications are you using WiFi or Ethernet?
if an ESP32 have a look at File>Examples>ESP32 Async UDP

I guess I should have tried this before asking the question! I change setup to just this:

  if(udp.listen(1234)) {
    Serial.print("UDP Listening on IP: ");
    Serial.println(WiFi.localIP());

and in the loop I just call onPacket like:

  udp.onPacket([](AsyncUDPPacket packet) {
    process(packet);
  });

Is there anything wrong with this? Why doesn't the example do it if it is ok?

Application seems to run fine.

That's what I used to start the application with. I have an ESP 32 board.

I say No. An ISR should be short and fast.
Referring to a space communication project once made I suggest the ISR will set a flag, or two, that will be checked in loop.
Suppose each turn in loop check which task, function, has the highest priority and executes that one. Any time consuming task must execute its task in chunks, not any long time evaluations.

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