-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
143 lines (110 loc) · 3.38 KB
/
main.py
File metadata and controls
143 lines (110 loc) · 3.38 KB
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
139
140
141
142
143
import network
import dht12
import time
import machine
import esp
import mqtt
import ujson
from influxdb import Measurement
config = ujson.loads(open('config.json').read())
from machine import I2C, Pin, Signal
i2c = I2C(scl=Pin(5), sda=Pin(4))
sensor = dht12.DHT12(i2c)
ledPin = machine.Pin(2, machine.Pin.OUT)
led = Signal(ledPin, invert=True)
sta_if = network.WLAN(network.STA_IF)
client = mqtt.Sensor(name=config['meta']['id'],
server=config['mqtt']['server'],
port=config['mqtt']['port'],
keyfile=config['mqtt']['key_file'],
certfile=config['mqtt']['cert_file'],
root_topic=config['mqtt']['root_topic'])
def togleLed():
led.value(not led.value())
def scan():
scan = sta_if.scan()
for scanned in scan:
for config_network in config['networks']:
if scanned[0].decode() == config_network[0]:
return (config_network[0], config_network[1])
return None, None
def do_conncect():
issd, password = scan()
if not issd:
print('No networks found')
else:
print('Connecting to:', issd)
sta_if.connect(issd, password)
def connect_network():
if not sta_if.isconnected():
sta_if.active(True)
do_conncect()
count = 0;
while not sta_if.isconnected():
togleLed()
time.sleep_ms(100)
count += 1
if count >= 100:
count = 0
do_conncect()
led.off()
print('network config:', sta_if.ifconfig())
def connect_mqtt():
led.on()
client.connect()
print('Connected to MQTT broker')
if client.repl :
import webrepl
import gc
webrepl.start()
gc.collect()
else:
print('REPL disabled')
client.on_repl_disabled = on_repl_disabled
client.publish('network', bytearray((sta_if.ifconfig()[0])).decode())
led.off()
def send_measurment():
measurement = Measurement('climate')
measurement.tags.update(config['meta'])
measurement.fields = {'temperature' : sensor.temperature(),
'humidity': sensor.humidity()}
client.publish_measurment(measurement)
def on_repl_disabled():
print('REPL disabled via MQTT')
sleep()
def sleep():
if config['deepsleep'] and not client.repl:
print('Entering deepsleep')
client.disconnect()
# give us some time to reprogram
if(machine.reset_cause() != machine.DEEPSLEEP_RESET):
for x in range(10):
togleLed()
time.sleep_ms(100)
togleLed()
time.sleep_ms(500)
micro = 1000000
esp.deepsleep(config['interval_minutes'] * 60 * micro)
time.sleep(1)
else:
for x in range(config['interval_minutes'] * 60):
client.update()
time.sleep(1)
def main():
try:
network.WLAN(network.AP_IF).active(False)
sensor.measure()
connect_network()
connect_mqtt()
while True:
send_measurment()
sleep()
except Exception as e:
from sys import print_exception
print_exception(e)
for x in range(10):
togleLed()
time.sleep(1)
machine.reset()
if __name__ == '__main__':
main()