logo elektroda
logo elektroda
X
logo elektroda

ESP8266 - how to detect lack of internet when downloading NTP time?

maximus22_kr 3528 8
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 15660759
    maximus22_kr
    Level 18  
    Hello
    While testing the download of NTP time through the ESP8266 module, I noticed that in the absence of Internet, when trying to connect, the program will stop ( this is how I have constructed the reception program - the program "turns" in the loop To .... Loop until ).
    At first, I was thinking to create a second condition triggered by a Timer flag, but maybe it would be possible e.g. by connecting to a known website ( e.g. google.pl - 216.58.209.32 ).

    That is to say:
    Code: Text
    Log in, to see the code

    "listen" for the text "Linked", admittedly if there is no internet the program will also hang and an additional condition is needed.

    Maybe one of the Colleagues has coped differently and could give guidance ?
  • ADVERTISEMENT
  • #2 15660784
    michalko12
    MCUs specialist
    To test your internet connection, you can use the AT+PING command by sending a ping to some external server such as google or even this NTP server.

    Quote:
    AT+PING="192.168.1.1"
    AT+PING="www.baidu.com"
  • ADVERTISEMENT
  • #3 15660830
    maximus22_kr
    Level 18  
    Well, it couldn't be simpler
    Code: Text
    Log in, to see the code

    i.e. I get the time value in milliseconds and an OK response, which I listen for.

    I even already have a solution for not blocking the program - even without using the flag from Timer:
    1. send AT+PING="216.58.209.32"
    2. Return from function
    3. Do other job and periodically check response
    4. If received string starts with "+PING", parse number and call user event function.
  • #4 15660854
    tmf
    VIP Meritorious for electroda.pl
    @maximus22_kr IMHO the method with ping changes nothing. In the end, you will devour the ping, which does not mean that the communication with the time server will be ok. If you have a loop that waits indefinitely for a response then that is the part of the program that should be changed. If there is no response within a certain time then you break the loop and handle the emergency.
  • ADVERTISEMENT
  • #5 15660919
    maximus22_kr
    Level 18  
    So you can't do without the Timer after all? I thought so too.
    I.e. before sending the PING I reset the flag and counter, and after the set time it will exit the loop. But letting the PING go before calling the ntp server is probably a good idea ?
  • #6 15660939
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #7 15661158
    maximus22_kr
    Level 18  
    tmf wrote:
    @maximus22_kr If you have a loop that waits indefinitely for a response then this is the part of the program that should be changed. If there is no response within the specified time then you break the loop and handle the contingency.


    That's right, I have the receipt of the time ( actually waiting for the string ) in the loop:
    Code: VB.net
    Log in, to see the code


    i was thinking of adding a small subroutine
    Code: VB.net
    Log in, to see the code


    The flag Flag_net_connect is an interrupt flag from Timer, it sets state 1 after 3 seconds.

    Only, I don't know if I'm combining correctly
    In the main loop, I call the NTP time reading, which I precede with a connection check.
    Code: VB.net
    Log in, to see the code
  • #8 16278116
    Anonymous
    Level 1  
  • #9 16282247
    dondu
    VIP Meritorious for electroda.pl
    maximus22_kr wrote:
    So you can't do without the Timer after all ? I thought so too.
    I.e. before sending the PING I reset the flag and counter, and after the set time it will exit the loop. But letting the PING go before calling the ntp server is probably a good idea ?

    A timer is obviously advisable, but you might as well write your own delay function to wait for one of two events:
    - receipt of a specified number of bytes,
    - or the expiry of a set time.

    Use Polish: ntp1.tp.pl or ntp2.tp.pl
    and ping to them.
ADVERTISEMENT