Strange behavior of time on ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Strange behavior of time on ESP8266

Post by lazarvgd » Wed Oct 31, 2018 8:59 am

Hi, I am using LoLin board in order to turn on/off my boiler, so that I can heat the water between 0000h and 0800h.

The board is connected to wifi router.
I am using ntptime library to obtain correct time and switching off and on pin is going ok when I want to control on minute base, for sake of testing. It is slow to wait 8hrs before pin will be turned off or 16h to be turned on again. So, everything works fine, my ESP is switching relay on and off.

The problem shows when I changed from minutes to hours and everything connected to boiler.
The first thing that I have noticed is that esp did not turned on boiler heater at 0000h, I had to restart the board.
The second thing is that this morning at 0800 my heater did not turned off. I have connected pc and esp with usb cable and found out that my board is printing on serial port time by UTC and instead of 0700h I have got 0636h.

What I think that cause the problem is the delay of 1 sec that I added in while loop and that has created the offset in time.

Can you please share your opinion?

U can find my code here:

NTP lib:

Code: Select all

try:
    import usocket as socket
except:
    import socket
try:
    import ustruct as struct
except:
    import struct

# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600

host = "192.168.1.20"

def time():
    NTP_QUERY = bytearray(48)
    NTP_QUERY[0] = 0x1b
    addr = socket.getaddrinfo(host, 123)[0][-1]
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.settimeout(1)
    res = s.sendto(NTP_QUERY, addr)
    msg = s.recv(48)
    s.close()
    val = struct.unpack("!I", msg[40:44])[0]
    return val - NTP_DELTA

# There's currently no timezone support in MicroPython, so
# utime.localtime() will return UTC time (as if it was .gmtime())
def settime():
    t = time()
    import machine
    import utime
    tm = utime.localtime(t)
    tm = tm[0:3] + (0,) + tm[3:6] + (0,)
    machine.RTC().datetime(tm)
    print(utime.localtime())
my code:

Code: Select all

import machine
import network
import time
import ntptime
import utime


led = machine.Pin(2, machine.Pin.OUT)
led.on()

#setup network
def connect_on_network():
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        sta_if.active(True)
        sta_if.connect('net', 'passwd')
        while not sta_if.isconnected():
            time.sleep(30)
            pass
        print('net cfg:',sta_if.ifconfig())
        time.sleep(30)
        print(ntptime.settime())

def check_time(t):
    print(t)
    if t[3]>=23 or t[3]<7:
        led.off()
    else:
        led.on()

#application
connect_on_network()
time.sleep(2)
while True:
    check_time(utime.localtime())
    time.sleep(1)

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Strange behavior of time on ESP8266

Post by kevinkk525 » Wed Oct 31, 2018 9:29 am

Your code looks ok. I can imagine that after one day the ntptime is too far off from the real time. The esp8266 has a really bad time accuracy. Try syncing to ntptime every few hours. Maybe that solves the problem.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Re: Strange behavior of time on ESP8266

Post by lazarvgd » Wed Oct 31, 2018 9:46 am

I just added the if statement to polling time from NTP every hour, probably this will solve the problem.

Thanks for quick response. :)

lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Re: Strange behavior of time on ESP8266

Post by lazarvgd » Fri Nov 02, 2018 5:50 pm

Here is the updated code, but still it doesn't work as expected. Please take a look at the code and tell me what am I doing wrong?

Code: Select all

import machine
import network
import time
import ntptime
import utime

sta_if = network.WLAN(network.STA_IF)
led = machine.Pin(2, machine.Pin.OUT)
led.on()
isupdated = False
#setup network
def connect_on_network():
    # sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        sta_if.active(True)
        sta_if.connect('SSID', 'PASSWD')
        while not sta_if.isconnected():
            pass
        print('net cfg:',sta_if.ifconfig())
        time.sleep(30)
        print(ntptime.settime())

def check_time(t):
    print(t)
    if t[3]>=23 or t[3]<7:
    # if t[3] % 2 == 0:
        led.off()
    else:
        led.on()
#application
connect_on_network()
time.sleep(2)
while True:
    check_time(utime.localtime())
    time.sleep(1)
    
    if not sta_if.isconnected():
        connect_on_network()
    
    if utime.localtime()[3]%2==0 and not isupdated:
        ntptime.settime()
        isupdated = True
        time.sleep(0.5)
        print("time update...")
    elif utime.localtime()[3]%2 != 0 and isupdated:
        isupdated = False
Thanks :)
Last edited by lazarvgd on Sat Nov 03, 2018 10:51 am, edited 1 time in total.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Strange behavior of time on ESP8266

Post by kevinkk525 » Fri Nov 02, 2018 7:13 pm

Maybe you can debug it a bit more to see what is really wrong like if the time is correct or way off at the time it should be switching.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

deonis
Posts: 11
Joined: Wed Aug 08, 2018 3:02 am

Re: Strange behavior of time on ESP8266

Post by deonis » Fri Nov 02, 2018 9:34 pm

Your problem might not be with software but with hardware. Which version of Micropython do you use?
Are you running esp8266 in the dual mode or station mode only? Do you know the uptime of your board? Does it correlate with the real time? Also what voltage regulator LoLin board uses? Does it dip below 3.2 volts on the esp8266 at any time during the operations? How do you connect the heater? Relay? How do you connect it to your esp8266?

lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Re: Strange behavior of time on ESP8266

Post by lazarvgd » Sat Nov 03, 2018 10:48 am

@kevinkk525 I will add more prints in order to figure out how it behaves.

@deonis I have used file named esp8266-20180511-v1.9.4.bin, downloaded from http://micropython.org/download.
I cannot tell the uptime of the board since I did not observed that, but I have rebooted manually 2 times a day. So no more than 12h.
I am using regular mobile phone charger, 5V output connected to pins GND and VU pin, see on the left side of the board, between G pin and S3:

Image
So the mobile phone charger has output of 5.34V, which powers esp and small relay(the blue one).
It is hard to tell does the voltage dip below 3.2V, because I am using mobile charger as power supply for eps. Also, the charger is powering the small relay which drives big relay.

The heater is connected over big relay.

Currently I am observing the output, will update you if something strange occurs.
What is suspicious to me is the thing that I shouldn't print every sec. time to serial. Probably because of that it gets jammed or something.
Esp is connected to home network, it is adsl router with wifi integrated.

Not sure did I answered to all questions.

EDIT:
I do not have oscilloscope to tell you what kind of signal is at the output of the mobile phone charger. I assume it is pretty smooth and straight line.
Attachments
photo_2018-11-03_10-32-35.jpg
photo_2018-11-03_10-32-35.jpg (134.01 KiB) Viewed 5114 times

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Strange behavior of time on ESP8266

Post by kevinkk525 » Sat Nov 03, 2018 10:52 am

sounds good so far. Printing to serial every second is not a problem.
You could also log to a file when you switch the relay with time and the switch. Then you could see if the logic itself works and if it's a problem with the relay (doubt it but at least you would see if the logic switches when the interal time is correct).
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Re: Strange behavior of time on ESP8266

Post by lazarvgd » Sat Nov 03, 2018 12:29 pm

@kevinkk525, thanks for your time and effort, I really appreciate that.

I have observed the logs, and found that time is not accurate and has to be updated every 15 minutes. :?

Here is a screen shot, I have started the board at 1044h by UTC, and after 2 or 3 hrs I have took the screen shot to compare the times.(my local time is GTM+1, Belgrade, Serbia)

https://imgur.com/a/0Ow7ZMU
kevinkk525 wrote:
Sat Nov 03, 2018 10:52 am
sounds good so far. Printing to serial every second is not a problem.
You could also log to a file when you switch the relay with time and the switch. Then you could see if the logic itself works and if it's a problem with the relay (doubt it but at least you would see if the logic switches when the interal time is correct).

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Strange behavior of time on ESP8266

Post by kevinkk525 » Sat Nov 03, 2018 12:54 pm

In your screenshot the time is 11:49 and your local time seems to be 12:52, so that's only 3 minutes off as the timezone offset is likely missing on your esp8266? That would not be much of a difference. Even after one day that would be "only" ~30 minutes.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply