Add periodic NTP resync
This commit is contained in:
		| @@ -29,11 +29,17 @@ Photos: | ||||
| Boot ESP8266 with program pin held low and flash MicroPython: | ||||
|  | ||||
| ```bash | ||||
| wget http://micropython.org/resources/firmware/esp8266-20170612-v1.9.1.bin | ||||
| esptool.py --port /dev/ttyUSB0 --baud 460800 erase_flash | ||||
| esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170612-v1.9.1.bin  | ||||
| ``` | ||||
|  | ||||
| Upload main.py and boot.py and adjust network configuration in boot.py accordingly. | ||||
| Upload main.py and boot.py and adjust network configuration in boot.py accordingly: | ||||
|  | ||||
| ```bash | ||||
| ampy -p /dev/ttyUSB0 put boot.py | ||||
| ampy -p /dev/ttyUSB0 put main.py | ||||
| ``` | ||||
|  | ||||
| ## Assembly tips | ||||
|  | ||||
|   | ||||
							
								
								
									
										17
									
								
								boot.py
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								boot.py
									
									
									
									
									
								
							| @@ -1,17 +1,16 @@ | ||||
| # Just in case prevent boot loops | ||||
| from time import sleep | ||||
| print("Press Ctrl-C to stop boot script...") | ||||
| sleep(1) | ||||
|  | ||||
| # Disable AP | ||||
| import network | ||||
| ap_if = network.WLAN(network.AP_IF) | ||||
| ap_if.active(False) | ||||
| print("Access point disabled") | ||||
|  | ||||
| # Connect to wireless network as client | ||||
| import network | ||||
| sta_if = network.WLAN(network.STA_IF) | ||||
| sta_if.active(True) | ||||
| sta_if.connect("Robootikaklubi", "u4HNj3sgYK") | ||||
|  | ||||
| # Synchronize clock | ||||
| import ntptime | ||||
| ntptime.settime() | ||||
| while not sta_if.isconnected(): | ||||
|     pass | ||||
|  | ||||
| # Clean up | ||||
| import gc | ||||
|   | ||||
							
								
								
									
										25
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								main.py
									
									
									
									
									
								
							| @@ -1,11 +1,25 @@ | ||||
|  | ||||
| import ntptime | ||||
| import esp | ||||
| from time import sleep, localtime | ||||
| from machine import Pin, Timer | ||||
|  | ||||
| TIMEZONE = 3 | ||||
| RESYNC = 7200 # Resync once in two hours | ||||
|  | ||||
| print("Press Ctrl-C now abort main.py execution and to retain keyboard input") | ||||
| sleep(1) | ||||
|  | ||||
| # Configure powersave | ||||
| esp.sleep_type(esp.SLEEP_LIGHT) | ||||
|  | ||||
| # Note that keyboard input is lost beyond this point! | ||||
| clock = Pin(3, mode=Pin.OUT) | ||||
| latch = Pin(0, mode=Pin.OUT) | ||||
| data = Pin(2, mode=Pin.OUT) | ||||
| blink = 0 | ||||
| lookup = 11, 9, 12, 8, 0, 4, 1, 3, 2, 10 | ||||
| countdown = 0 | ||||
|  | ||||
| def bitbang_bit(value): | ||||
|     if value & 1: | ||||
| @@ -17,7 +31,7 @@ def bitbang_bit(value): | ||||
|  | ||||
| def bitbang_digit(digit): | ||||
|     bitbang_bit(blink) | ||||
|     for i in range(0,width): | ||||
|     for i in range(0,4): | ||||
|         bitbang_bit(lookup[digit] << i >> 3) | ||||
|     bitbang_bit(blink) | ||||
|     bitbang_bit(blink) | ||||
| @@ -26,12 +40,19 @@ def bitbang_digit(digit): | ||||
| timer = Timer(-1) | ||||
|  | ||||
| def schedule(delay=0): | ||||
|     global countdown | ||||
|     if countdown <= 0: | ||||
|         ntptime.settime() | ||||
|         countdown = RESYNC | ||||
|         print("Resync") | ||||
|     countdown -= 1 | ||||
|     if delay: | ||||
|         timer.init(period=1000, mode=Timer.ONE_SHOT, callback=dump) | ||||
|     else: | ||||
|         dump() | ||||
|  | ||||
| def dump_time(hour, minute, second): | ||||
|     print("Time is %02d:%02d:%02d" % (hour, minute, second)) | ||||
|     bitbang_digit(hour // 10) | ||||
|     bitbang_digit(hour % 10) | ||||
|     bitbang_digit(minute // 10) | ||||
| @@ -42,7 +63,7 @@ def dump_time(hour, minute, second): | ||||
| def dump(t=None): | ||||
|     global blink | ||||
|     year, month, day, hour, minute, second, _, millis = localtime() | ||||
|     dump_time((hour + 3) % 24, minute, second) | ||||
|     dump_time((hour + TIMEZONE) % 24, minute, second) | ||||
|     latch.on() | ||||
|     latch.off() | ||||
|     blink = 1-blink | ||||
|   | ||||
		Reference in New Issue
	
	Block a user