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())
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)