-
Notifications
You must be signed in to change notification settings - Fork 1
/
yocto_env_sensors.py
91 lines (73 loc) · 3.11 KB
/
yocto_env_sensors.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, sys
import csv
from datetime import datetime, tzinfo, timedelta
import ntplib
from yoctopuce.yocto_api import *
from yoctopuce.yocto_temperature import *
from yoctopuce.yocto_humidity import *
from yoctopuce.yocto_pressure import *
# Setup Timezone data
class simple_utc(tzinfo):
def tzname(self, **kwargs):
return "UTC"
def utcoffset(self, dt):
return timedelta(0)
# Check that system time is reasonable
ntpclient = ntplib.NTPClient()
response = ntpclient.request('europe.pool.ntp.org', version=3)
print("System time is offset from NTP by %s seconds" % response.offset)
if abs(response.offset) > 1:
sys.exit("System time is offset from NTP by %s seconds" % response.offset)
# Setup the API to use local USB devices
errmsg = YRefParam()
if YAPI.RegisterHub("usb", errmsg) != YAPI.SUCCESS:
sys.exit("init error" + errmsg.value)
# Connect to Maxi Thermistor and Meteo boards
thermistor_sn = "THRMSTR2-F266D"
thermistor_module = YModule.FindModule(thermistor_sn)
if not thermistor_module.isOnline():
sys.exit('Maxi Thermistor module not connected')
meteo_sn = "METEOMK2-FFF4B"
meteo_module = YModule.FindModule(meteo_sn)
if not meteo_module.isOnline():
sys.exit('Meteo device not connected')
# Connect to the sensors
tempSensor1 = YTemperature.FindTemperature(thermistor_sn + '.temperature1')
tempSensor2 = YTemperature.FindTemperature(thermistor_sn + '.temperature2')
tempSensor3 = YTemperature.FindTemperature(thermistor_sn + '.temperature3')
roomTempSensor = YTemperature.FindTemperature(meteo_sn + '.temperature')
roomPressureSensor = YPressure.FindPressure(meteo_sn + '.pressure')
roomHumiditySensor = YHumidity.FindHumidity(meteo_sn + '.humidity')
# Read current values as a sanity check
print("Current sensor values:")
if thermistor_module.isOnline():
print('%2.1f°C %2.1f°C %2.1f°C' % (
tempSensor1.get_currentValue(),
tempSensor2.get_currentValue(),
tempSensor3.get_currentValue()
))
if meteo_module.isOnline():
print('%2.1f°C %4.0fmB° %2.1f%%' % (
roomTempSensor.get_currentValue(),
roomPressureSensor.get_currentValue(),
roomHumiditySensor.get_currentValue()
))
timestamp = datetime.datetime.utcnow().replace(tzinfo=simple_utc()).isoformat()
with open("%s_data.csv" % timestamp, mode='w') as data_file:
fieldNames = ['datetime', 'humidity', 'pressure', 'roomTemp', 'temp1', 'temp2', 'temp3']
data_writer = csv.DictWriter(data_file, fieldNames, delimiter=',', quoting=csv.QUOTE_MINIMAL)
data_writer.writeheader()
timestamp = datetime.datetime.utcnow().replace(tzinfo=simple_utc()).isoformat()
measurement = {
'datetime': timestamp,
'humidity': roomHumiditySensor.get_currentValue(),
'pressure': roomPressureSensor.get_currentValue(),
'roomTemp': roomTempSensor.get_currentValue(),
'temp1': tempSensor1.get_currentValue(),
'temp2': tempSensor2.get_currentValue(),
'temp3': tempSensor3.get_currentValue()
}
data_writer.writerow(measurement)
YAPI.FreeAPI()