Skip to content

Instantly share code, notes, and snippets.

@mika-koivusaari
Created October 7, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mika-koivusaari/90febd8a101412bf0e5c71d9c736e855 to your computer and use it in GitHub Desktop.
Save mika-koivusaari/90febd8a101412bf0e5c71d9c736e855 to your computer and use it in GitHub Desktop.
Check how much ESP8266 RTC drifts with micropython
import machine
import ntptime
import time
from umqtt.simple import MQTTClient
import sys
import network
def gettimestr():
rtc=machine.RTC()
curtime=rtc.datetime()
_time="%04d" % curtime[0]+"%02d" % curtime[1]+"%02d" % curtime[2]+" "+"%02d" % curtime[4]+"%02d" % curtime[5]
return _time
wifi = network.WLAN(network.STA_IF)
while not wifi.isconnected():
print(".")
time.sleep(1)
#check if gpio4 is pulled down
stoppin = machine.Pin(4,mode=machine.Pin.IN,pull=machine.Pin.PULL_UP)
if stoppin.value()==0:
print("pin down")
else:
print("pin up, send message")
c = MQTTClient("rtc_test", "192.168.0.106")
c.connect()
c.publish("/test/rtc",gettimestr())
c.disconnect()
# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# set RTC.ALARM0 to fire after 60 seconds (waking the device)
rtc.alarm(rtc.ALARM0, 60000)
# put the device to sleep
machine.deepsleep()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment