-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_fw.py
138 lines (112 loc) · 4.69 KB
/
main_fw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright (C) 2017 Aleksey Rogov <alex@arogov.com>. All rights reserved.
import machine
import network
from binascii import hexlify, a2b_base64
from bme280 import BME280
from hashlib import sha1
from json import loads
from os import remove
from time import ticks_us
from urequests import post
from zlib import decompress
SLEEP_TIME = 612000
WD_TIMER = 15000
WIFI_SSID = 'WIFI-AP'
WIFI_BSSID = b'\xAA\xBB\xCC\xDD\xEE\xFF'
WIFI_KEY = 'password'
IFCONFIG_IP = '192.168.1.254'
IFCONFIG_MASK = '255.255.255.0'
IFCONFIG_GW = '192.168.1.1'
IFCONFIG_DNS = '192.168.1.1'
BREAK_PIN = 13
SCL_PIN = 4
SDA_PIN = 5
SERVER_KEY = '1234567890qwertyuiop_asdfghjklzxcvbnm-ASDFGHJKLZ'
SERVER_URL = 'https://example.com/thermo'
MAIN_FILE_NAME = 'main.py'
def deepsleep(period):
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
rtc.alarm(rtc.ALARM0, period)
machine.deepsleep()
def wd_handler(calledvalue):
print(f'[{ticks_us() / 1000000:12.6f}] Diconnecting from network')
wlan.disconnect()
print(f'[{ticks_us() / 1000000:12.6f}] Pulling down interface')
wlan.active(False)
print(f'[{ticks_us() / 1000000:12.6f}] Sleeping.')
deepsleep(SLEEP_TIME)
pin = machine.Pin(BREAK_PIN, machine.Pin.IN, machine.Pin.PULL_UP)
if pin.value() == 1:
print(f'[{ticks_us() / 1000000:12.6f}] Copyright (C) 2017 Aleksey Rogov <alex@arogov.com>. All rights reserved.')
print(f'[{ticks_us() / 1000000:12.6f}] Init watchdog timer...', end='')
wd_scheduler = machine.Timer(-1)
wd_scheduler.init(period=WD_TIMER, mode=machine.Timer.PERIODIC, callback=wd_handler)
print(f'OK\n[{ticks_us() / 1000000:12.6f}] Connecting to network...', end='')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.ifconfig((IFCONFIG_IP, IFCONFIG_MASK, IFCONFIG_GW, IFCONFIG_DNS))
if wlan.config('essid') == WIFI_SSID:
wlan.connect()
else:
wlan.connect(WIFI_SSID, WIFI_KEY, bssid=WIFI_BSSID)
print(f'OK\n[{ticks_us() / 1000000:12.6f}] Calculating SHA1: ', end='')
s = sha1()
with open(MAIN_FILE_NAME, 'rb') as f:
for line in f:
s.update(line)
sha1 = hexlify(s.digest()).decode('utf-8')
print(sha1)
print(f'[{ticks_us() / 1000000:12.6f}] Gathering data from sensor: ', end='')
try:
i2c = machine.I2C(scl=machine.Pin(SCL_PIN), sda=machine.Pin(SDA_PIN))
bme = BME280(i2c=i2c)
cd = bme.read_compensated_data()
except:
cd = [0, 0, 0]
print('Fail')
else:
print(f't = {cd[0] / 100:.1f} C; P = {cd[1] / 256:.3f} Pa; RH = {cd[2] / 1024:.1f}%')
adcv = machine.ADC(0).read()
print(f'[{ticks_us() / 1000000:12.6f}] ADC value = {adcv}')
mac = hexlify(network.WLAN().config('mac')).decode('utf-8')
uniq_id = hexlify(machine.unique_id()).decode('utf-8')
while not wlan.isconnected():
machine.idle()
print(f'[{ticks_us() / 1000000:12.6f}] Connected')
print(f'[{ticks_us() / 1000000:12.6f}] Sending data to server...', end='')
response = post(SERVER_URL, data=f'{{"sid":"{uniq_id}","mac":"{mac}","temp":"{cd[0] / 100}","pres":"{cd[1] / 256}","hum":"{cd[2] / 1024}","adc":"{adcv}","sha1":"{sha1}","update":"true"}}',
headers={"Content-Type": "application/json", "Authorization": f"Bearer {SERVER_KEY}"})
if response.status_code == 201:
print(f'[{ticks_us() / 1000000:12.6f}] Server returned non 2xx code')
wd_handler(None)
print(f'OK\n[{ticks_us() / 1000000:12.6f}] Reading response: ', end='')
response_json = response.json()
response.close()
del(response)
gc.collect()
print(f'status: {response_json["status"]}, reason: {response_json["reason"]}')
if response_json["status"] != True:
print(f'[{ticks_us() / 1000000:12.6f}] Server returned invalid status')
wd_handler(None)
recvd_sha1 = response_json.get('sha1')
if recvd_sha1 and recvd_sha1 != sha1:
print(f'[{ticks_us() / 1000000:12.6f}] New firmware is available with SHA1 {response_json["sha1"]}')
zip_data = a2b_base64(response_json["content"])
del(response_json)
gc.collect()
fw_new = decompress(zip_data)
del(zip_data)
gc.collect()
new_fw_sha1 = hexlify(sha1(fw_new).digest()).decode('utf-8')
if recvd_sha1 == new_fw_sha1:
remove(MAIN_FILE_NAME)
f = open(MAIN_FILE_NAME, 'wb+')
f.write(fw_new)
f.close()
print(f'[{ticks_us() / 1000000:12.6f}] Firmware successfully upgraded with SHA1 {new_fw_sha1}')
else:
print(f'[{ticks_us() / 1000000:12.6f}] SHA1 error: {recvd_sha1} vs {new_fw_sha1}')
wd_handler(None)
else:
print(f'[{ticks_us() / 1000000:12.6f}] Break pin is pulled down.')