-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrobit.py
304 lines (208 loc) · 8.11 KB
/
microbit.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
#
# This is a weeWX driver to enable weeWX to read data from an Arduino weather station.
#
# See here for more details:
#
# https://github.com/wrybread/weewx-ArduinoWeatherStation
#
# by [email protected]
#
# Microbit-weatherbit version by [email protected]
#
#
#
"""Driver for Micro:bit Climate weather station.
See here for more info:
https://github.com/thagorastos/school-weather-station
"""
from __future__ import with_statement
import serial
import syslog
import time
import io
from pprint import pprint
import weewx.drivers
DRIVER_NAME = 'microbit'
DRIVER_VERSION = '0.1'
def loader(config_dict, _):
return MicrobitDriver(**config_dict[DRIVER_NAME])
def confeditor_loader():
return MicrobitConfEditor()
INHG_PER_MBAR = 0.0295333727
METER_PER_FOOT = 0.3048
MILE_PER_KM = 0.621371
DEFAULT_PORT = '/dev/ttyACM0'
DEBUG_READ = 1
def logmsg(level, msg):
syslog.syslog(level, 'Microbit: %s' % msg)
def logdbg(msg):
logmsg(syslog.LOG_DEBUG, msg)
def loginf(msg):
logmsg(syslog.LOG_INFO, msg)
def logerr(msg):
logmsg(syslog.LOG_ERR, msg)
class MicrobitDriver(weewx.drivers.AbstractDevice):
"""weewx driver that communicates with an microbit weather station
port - serial port
[Required. Default is /dev/ttyACM0]
polling_interval - how often to query the serial interface, seconds
[Optional. Default is 1]
max_tries - how often to retry serial communication before giving up
[Optional. Default is 5]
retry_wait - how long to wait, in seconds, before retrying after a failure
[Optional. Default is 10]
"""
def __init__(self, **stn_dict):
self.port = stn_dict.get('port', DEFAULT_PORT)
self.polling_interval = float(stn_dict.get('polling_interval', 2))
self.max_tries = int(stn_dict.get('max_tries', 10))
self.retry_wait = int(stn_dict.get('retry_wait', 10))
self.last_rain = None
loginf('driver version is %s' % DRIVER_VERSION)
loginf('using serial port %s' % self.port)
loginf('polling interval is %s' % self.polling_interval)
global DEBUG_READ
DEBUG_READ = int(stn_dict.get('debug_read', DEBUG_READ))
self.last_read_time = time.time()
self.read_counter = 0
logdbg("Opening the microbit on port %s" % self.port)
self.baudrate = 115200
self.timeout = 10 # changed from 60
self.serial_port = None
try:
self.serial_port = serial.Serial(self.port, self.baudrate,
timeout=self.timeout)
self.serial_port.flush()
logdbg("Successfully opened the microbit.")
except Exception as e:
logerr("Error opening the microbit! %s" % e)
def read_buffer(self):
# read the buffer
new_data = ""
try:
while True:
# sio = io.TextIOWrapper(io.BufferedReader(self.serial_port)) # , newline="\r\n")
# new_data = sio.readline()
# new_data = new_data.replace("\r\n", "")
new_data = self.serial_port.read(self.serial_port.inWaiting()).decode('ascii')
self.serial_port.flush()
return new_data
except Exception:
print("exception: ", self.serial_port.in_waiting)
pass
def parse_readings(self, b):
"""
The microbit script emits data in the format:
[0]outTemp
[1]pressure
[2]outHumidity
[3]windSpeed
[4]windDir
[5]soilMoist1 (not currently used; sensor not connected)
[6]soilTemp1
[7]rain_total
"""
data = {}
try:
if "," in b:
parts = b.split(",")
# print("parsing: ", b, "parts =", parts)
data = dict()
data['outTemp'] = float(parts[0])
data['pressure'] = float(parts[1])
data['outHumidity'] = float(parts[2])
data['windSpeed'] = float(parts[3])
data['windDir'] = float(parts[4])
# data['soilMoist1'] = float(parts[5])
data['soilTemp1'] = float(parts[6])
rain = parts[7].replace('\r\n', '').strip()
data['rain_total'] = float(rain)
# pprint("Microbit driver: ", data)
except Exception as e:
logerr("Error parsing data: %s" % e)
if DEBUG_READ:
logdbg(data)
return data
@property
def hardware_name(self):
return "microbit"
def genLoopPackets(self):
ntries = 0
while ntries < self.max_tries:
ntries += 1
try:
packet = {'dateTime': int(time.time() + 0.5),
'usUnits': weewx.METRIC}
serial_data = self.read_buffer() # read the buffer from the microbit climate
data = self.parse_readings(serial_data) # parse the data
packet.update(data)
self._augment_packet(packet)
ntries = 0
'''
# print the time between reads and the count for debugging for now
self.read_counter += 1
time_since_last_read = time.time() - self.last_read_time
print("%s seconds since last read" % time_since_last_read, self.read_counter)
self.last_read_time = time.time()
'''
yield packet
if self.polling_interval:
time.sleep(self.polling_interval)
except (serial.serialutil.SerialException, weewx.WeeWxIOError) as e:
logerr("Failed attempt %d of %d to get LOOP data: %s" %
(ntries, self.max_tries, e))
time.sleep(self.retry_wait)
else:
msg = "Max retries (%d) exceeded for LOOP data" % self.max_tries
logerr(msg)
raise weewx.RetriesExceeded(msg)
def _augment_packet(self, packet):
# no wind direction when wind speed is zero
if 'windSpeed' in packet and not packet['windSpeed']:
packet['windDir'] = None
# calculate rain from rain_total
packet['rain'] = weewx.wxformulas.calculate_rain(packet.get('rain_total'), self.last_rain)
self.last_rain = packet.get('rain_total')
class MicrobitConfEditor(weewx.drivers.AbstractConfEditor):
@property
def default_stanza(self):
return """
[Microbit]
# This section is for a Microbit Weather Station.
# Serial port such as /dev/ttyACM0, /dev/ttyS0, /dev/ttyUSB0, or /dev/cuaU0
port = /dev/ttyACM0
# The driver to use:
driver = user.microbit
"""
def prompt_for_settings(self):
print("Specify the serial port on which the station is connected, for")
print("example /dev/ttyUSB0 or /dev/ttyACM0.")
port = self._prompt('port', '/dev/ttyACM0')
return {'port': port}
# define a main entry point for basic testing of the station without weewx
# engine and service overhead. invoke this as follows from the weewx root dir:
#
# # To run on a standard apt-get install (unconfirmed, might ened to adjust path
# PYTHONPATH=bin python /home/weewx/bin/user/Microbit.py
# For setup.py instals:
# PYTHONPATH=/home/weewx/bin python /home/weewx/bin/user/Microbit.py
#
if __name__ == '__main__':
import optparse
usage = """%prog [options] [--help]"""
syslog.openlog('micro', syslog.LOG_PID | syslog.LOG_CONS)
syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG))
parser = optparse.OptionParser(usage=usage)
parser.add_option('--version', dest='version', action='store_true',
help='display driver version')
parser.add_option('--port', dest='port', metavar='PORT',
help='serial port to which the station is connected',
default=DEFAULT_PORT)
(options, args) = parser.parse_args()
if options.version:
print("micro driver version %s" % DRIVER_VERSION)
exit(0)
station = MicrobitDriver()
for packet in station.genLoopPackets():
print(time.time(), packet)