-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
211 lines (156 loc) · 5.5 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
## MicroPython Imports
import time
import machine
import network
import gc
import ujson as json
from _thread import start_new_thread
## Local Imports
import adafruit_max31865 as max31865
def check_wifi(nic):
if 'wifi-station' not in settings:
return
if nic.isconnected():
return
nic.connect(settings['wifi-station']['name'], settings['wifi-station']['pass'])
while not nic.isconnected():
time.sleep(0.2)
def wait_ap(ap):
if ap.active() and ap.isconnected():
print("AP Already Started")
return
while not ap.active() and not ap.isconnected():
print('***')
time.sleep(0.2)
print("AP Now Started")
def mean(data):
return sum(data)/float(len(data))
def _ss(data):
c = mean(data)
ss = sum((x-c)**2 for x in data)
return ss
def stddev(data, ddof=0):
ss = _ss(data)
pvar = ss/(len(data)-ddof)
return pvar**0.5
def poll_sensors(extra_info=False):
meta = []
samples = []
for idx, sensor in sensors:
value = sensor.temperature*settings['device']['scale']
if settings['device']['to_int']:
value = int(value)
data = {'id':idx, 'value':value}
if extra_info:
data['name'] = sensor_info[idx-1]['name']
meta.append(data)
samples.append(value)
meta.append({'id':'agg','mean':mean(samples),'stddev':stddev(samples)})
return meta
def mqtt_loop():
## Create MQTT Connection
c = mqtt.MQTTClient(settings['device']['name'], settings['mqtt']['ip'], 1883)
c.connect()
c.publish(settings['device']['name'], json.dumps(sensor_info))
while True:
check_wifi(nic)
c.publish(settings['device']['name'], json.dumps(poll_sensors()))
time.sleep(settings['mqtt']['sleep'])
settings = json.load(open("settings.json", 'r'))
RTD_NOMINAL = 1000.0 ## Resistance of RTD at 0C
RTD_REFERENCE = 4300.0 ## Value of reference resistor on PCB
HTTP_HEADERS = {"Access-Control-Allow-Origin": "*"}
## Create Software SPI controller. MAX31865 requires polarity of 0 and phase of 1.
## Currently, the micropython on the ESP32 does not support hardware SPI
sck = machine.Pin( 5, machine.Pin.OUT)
mosi = machine.Pin(18, machine.Pin.IN)
miso = machine.Pin(19, machine.Pin.OUT)
spi = machine.SPI(baudrate=50000, sck=sck, mosi=mosi, miso=miso, polarity=0, phase=1)
## Create SPI Chip Select pins
cs1 = machine.Pin(33, machine.Pin.OUT, value=1)
cs2 = machine.Pin(15, machine.Pin.OUT, value=1)
cs3 = machine.Pin(32, machine.Pin.OUT, value=1)
cs4 = machine.Pin(14, machine.Pin.OUT, value=1)
css = [cs1, cs2, cs3, cs4]
sensors = []
sensor_info = []
idx = 0
## Create array of active RTD sensors and information about them
for name in settings['device']['sensors']:
idx += 1
if name.upper() == "DISABLE":
continue
sensors.append([
idx,
max31865.MAX31865(
spi, css[idx-1],
wires = 4,
rtd_nominal = RTD_NOMINAL,
ref_resistor = RTD_REFERENCE)
])
sensor_info.append({
"id" : idx,
"scale" : settings['device']['scale'],
"type" : "temperature",
"sensor" : "RTD1000 -> MAX31865",
"name" : name
})
## Setup WIFI Networking as a client
if 'wifi-station' in settings:
nic = network.WLAN(network.STA_IF)
nic.active(True)
check_wifi(nic)
## Setup WIFI Networking as an access point
if 'wifi-ap' in settings:
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=settings['wifi-ap']['name'])
wait_ap(ap)
## Clean out allocated memory so far
gc.collect()
gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
## Become a MQTT client, if configured so
if 'mqtt' in settings:
import mqtt
# start_new_thread(mqtt_thread, ())
mqtt_loop()
def _acceptWebSocketCallback(webSocket, httpClient) :
print("WS ACCEPT")
webSocket.RecvTextCallback = _recvTextCallback
webSocket.RecvBinaryCallback = _recvBinaryCallback
webSocket.ClosedCallback = _closedCallback
def _recvTextCallback(webSocket, msg):
print("WS RECV TEXT : %s" % msg)
webSocket.SendText("Reply for %s" % msg)
def _recvBinaryCallback(webSocket, data):
print("WS RECV DATA : %s" % data)
def _closedCallback(webSocket):
print("WS CLOSED")
def _handle_datajson_get(httpClient, httpResponse, routeArgs=None):
data = poll_sensors(extra_info=True)
## For convenience, add in:
## each sensor's name (from the settings file)
## and sensor global configuration (scale & int conversion)
# for entry in data:
# if entry['id'] == 'agg':
# entry['scale'] = settings['device']['scale']
# entry['to_int'] = settings['device']['to_int']
# else:
# entry['name'] = sensor_info[entry['id']-1]['name']
httpResponse.WriteResponseJSONOk(obj=data, headers=HTTP_HEADERS)
if 'http' in settings:
from microWebSrv import MicroWebSrv
handlers = [
( "/data.json", "GET", _handle_datajson_get ),
]
mws = MicroWebSrv(routeHandlers=handlers, webPath='/www')
mws.MaxWebSocketRecvLen = 64 # Default is set to 1024
mws.WebSocketThreaded = False # WebSockets without new threads
mws.AcceptWebSocketCallback = _acceptWebSocketCallback
## Does not work, silently fails
## Might be due to attempting to start 10 threads
# mws.Start()
## Works, but blocks the REPL
mws.Start(threaded = False)
## Sometimes works
# start_new_thread(mws._serverProcess, ())