logo elektroda
logo elektroda
X
logo elektroda

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

misiekhardcore 816 9
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18609231
    misiekhardcore
    Level 7  
    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
    khoam
    Level 42  
    misiekhardcore wrote:
    It seems to work somehow but often both modes interfere.
    .
    What specifically is this interference?

    I would not put any Serial.print() between udp.beginPacket() and udp.endPacket().
    I would also check what value udp.beginPacket() and udp.endPacket() return, in order to detect a possible error, according to:
    https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/udp-class.html
  • #3 18609801
    misiekhardcore
    Level 7  
    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
  • ADVERTISEMENT
  • #4 18609821
    khoam
    Level 42  
    misiekhardcore wrote:
    I don't know how to separate this so that both methods work independently so that the parsing can't get mixed up
    .
    If you are using the same local UDP port for both functions, this will be quite a difficult task.
  • #5 18609850
    misiekhardcore
    Level 7  
    Can you be more specific? I'm not that familiar with the subject yet
  • Helpful post
    #6 18609889
    khoam
    Level 42  
    You probably have Udp.begin(localPort) in setup(), so there are packets coming into one local port on the ESP from two different "remote" ports, so they are allowed to "mix".
    You should only do packet parsing in one place. Check which remote port it came from and only call the function used (NTP or other UDP) to read the packet based on that.
  • ADVERTISEMENT
  • #7 18609969
    misiekhardcore
    Level 7  
    So should I do a separate udp.begin() with a separate port each time?
  • Helpful post
    #8 18610063
    khoam
    Level 42  
    misiekhardcore wrote:
    Is that I should do a separate udp.begin() with a separate port each time?
    .
    You mean e.g. udp1 and udp2, two WiFiUDP class objects for two different local ports? You could try it like that, from the code of this class it seems that it could work, but I haven't done such tests :) .
  • ADVERTISEMENT
  • #9 18610074
    misiekhardcore
    Level 7  
    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  
    @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

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.
Summary generated by the language model.
ADVERTISEMENT