-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
63 lines (54 loc) · 1.23 KB
/
main.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
import network
import time
import gc
import machine
import config
from machine import UART
from machine import Pin
from umqtt.simple import MQTTClient
uart = UART(1, baudrate=115200, invert=UART.INV_RX, rx=Pin(5), timeout=11000)
led = machine.Pin("LED", machine.Pin.OUT)
led.on()
network.hostname('HANmeter')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(config.SSID, config.PSK)
led.off()
waitcount = 0
while wlan.status() != 3:
waitcount+=1
time.sleep(0.5)
led.toggle()
if waitcount > 120:
led.off()
machine.reset()
#led on when connected to wlan
led.on()
print("WIFI CONNECTED")
mqc = MQTTClient("HANMeter", config.MQTTHost, 1883)
try:
mqc.connect()
except:
machine.soft_reset()
print("MQTT CONNECTED")
workbuffert = bytearray(1024)
mv = memoryview(workbuffert)
while True:
mvpos=0
s = uart.readline()
while s[0] != ord('/'):
s = uart.readline()
#print("NEW HAN PACKAGE")
mv[mvpos : mvpos+len(s)] = s
mvpos += len(s)
led.off()
while s[0]!= ord('!'):
s = uart.readline()
mv[mvpos : mvpos+len(s)] = s
mvpos += len(s)
try:
mqc.publish(config.MQTTTopic,workbuffert[0:mvpos])
except:
machine.soft_reset()
gc.collect()
led.on()