-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfj-data-to-html.py
executable file
·55 lines (45 loc) · 1.37 KB
/
vfj-data-to-html.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
import sys
import minimalmodbus
from time import sleep
if len(sys.argv) > 1:
serial_port = sys.argv[1]
else:
serial_port = 'COM3'
wind_speed_sensor = minimalmodbus.Instrument(serial_port, 1) # Serial port, slave address (dec)
wind_speed_sensor.serial.baudrate = 9600
wind_speed_sensor.serial.timeout = 0.5
wind_direction_sensor = minimalmodbus.Instrument(serial_port, 2) # Serial port, slave address (dec)
wind_direction_sensor.serial.baudrate = 9600
wind_direction_sensor.serial.timeout = 0.5
while True:
try:
wind_speed = str(wind_speed_sensor.read_register(0, 2)) + ' m/s'
except OSError:
wind_speed = 'Failed to read from instrument'
try:
wind_direction = str(wind_direction_sensor.read_register(0, 2)) + ' degrees'
except OSError:
wind_direction = 'Failed to read from instrument'
html_code = """\
<html>
<body>
<h1>Vassfjellet weather sensors</h1>
<p>
Wind speed: {wind_speed_s}
<br>
Wind direction: {wind_direction_s}
</p>
</body>
</html>\
""".format(wind_speed_s = wind_speed, wind_direction_s = wind_direction)
for attempt in range(5):
try:
f = open('vfj-weather-sensors.html','w+')
f.write(html_code)
f.close()
except OSError:
sleep(0.5)
continue
else:
break
sleep(1)