logo elektroda
logo elektroda
X
logo elektroda

[Solved] ESP8266 simultaneous NTP client and UDP server - how to avoid interference?

misiekhardcore 1266 9
Best answers LABEL_AI_GENERATED

How can I prevent an ESP8266 NTP client and UDP server from mixing up incoming packets when both use UDP?

Use separate UDP handling paths—preferably separate WiFiUDP objects bound to different local ports for the NTP client and the other UDP communication—so packets are not parsed by the wrong code [#18609889][#18610063] If both functions share one local UDP port, packets from different remote ports can arrive on that same port and get mixed up, so parse packets in one place and dispatch them based on the remote port [#18609889] In the thread, creating two UDP objects with different ports solved the interference, and the original poster confirmed that it worked correctly [#18610063][#18612913]
AI summary based on the discussion. May contain errors.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18609231
    misiekhardcore
    Level 7  
    Posts: 8
    Rate: 2
    Hi, I have a problem because I am trying to write a program on the ESP8266. I need such a program in which I simultaneously handle the downloading of time from an NTP server and at the same time I also want to communicate with another device via UDP protocol. Everything would be fine if it wasn't for the fact that when downloading time, we first send data to the NTP server and then parse the packet (working as a client), while communication with the other device starts with parsing the packet first and only then a reply is sent (working as a server). This seems to work somehow, but the two modes often interfere with each other.

    Here is a code snippet with both functions (both work separately without problems):
    Code: C / C++
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #2 18609783
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #3 18609801
    misiekhardcore
    Level 7  
    Posts: 8
    Rate: 2
    The interference is that both methods parse the package so it happens that one parses the package that is intended for the other and vice versa. I don't know how to separate this so that both methods work independently so that the parsing can't get mixed up
  • #4 18609821
    Anonymous
    Level 1  
  • #5 18609850
    misiekhardcore
    Level 7  
    Posts: 8
    Rate: 2
    Can you be more specific? I'm not that familiar with the subject yet
  • ADVERTISEMENT
  • Helpful post
    #6 18609889
    Anonymous
    Level 1  
  • #7 18609969
    misiekhardcore
    Level 7  
    Posts: 8
    Rate: 2
    So should I do a separate udp.begin() with a separate port each time?
  • ADVERTISEMENT
  • Helpful post
    #8 18610063
    Anonymous
    Level 1  
  • #9 18610074
    misiekhardcore
    Level 7  
    Posts: 8
    Rate: 2
    I honestly didn't come across it, but it has a chance of working. When I do the tests I'll be sure to let you know how it went. I'll create two objects with different ports and see how it goes.
  • #10 18612913
    misiekhardcore
    Level 7  
    Posts: 8
    Rate: 2
    @khoam everything worked as you wrote. Creating two separate UDP class objects and assigning separate ports helped. Everything is running smoothly.
    I consider the topic closed

Topic summary

LABEL_AI_GENERATED
The discussion revolves around the challenge of simultaneously implementing an NTP client and a UDP server on the ESP8266 without interference. The main issue arises from both functions parsing packets, leading to confusion between the NTP and UDP communications. A solution proposed involves using separate UDP class objects with distinct local ports for each function, which successfully resolves the interference problem. The user confirmed that this approach worked effectively, allowing both functionalities to operate independently without packet mixing.
AI summary based on the discussion. May contain errors.
ADVERTISEMENT