-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduinoenv.py
73 lines (60 loc) · 1.97 KB
/
arduinoenv.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from visa_probestation_dev import VisaProbestationDev
from math import log
import logging
class ArduinoEnvSensor ( VisaProbestationDev ) :
def __init__ ( self, resource_name ) :
super ( ArduinoEnvSensor, self ) .__init__ ( resource_name, True )
self._res.encoding = "utf-8"
def get_error ( self ) :
error = self._query ( "error" ) . strip ( )
if error == "None":
return None
else:
return error
def identify ( self ) :
return self._query ( u"*IDN?" ) .strip ( )
def get_reading ( self, sensor = None ) :
if sensor is None:
ret = self._query ( "measureall" ) .strip ( )
elif sensor == 0x76:
ret = self._query ( "measure76" ) .strip ( )
elif sensor == 0x77:
ret = self._query ( "measure77" ) .strip ( )
else:
raise ValueError ( "sensor must be either None, 0x76 or 0x77." )
if ret.startswith("Error:"):
raise RuntimeError("Arduino responded {}".format(ret))
return ret
@staticmethod
def parse_tphr ( line, devname ) :
temperature = pressure = humidity = resistance = None
for field in line.split ( "," ) :
field = field.strip ( )
if field[-2:] == "°C":
temperature = float ( field[:-2] )
elif field[-2:] == "Pa":
pressure = float ( field[:-2] )
elif field[-1:] == "%":
humidity = float ( field[:-1] )
elif field[-2:] == "kΩ":
resistance = float ( field[:-2] )
return { "{}_temperature".format ( devname ) : temperature,
"{}_pressure" .format ( devname ) : pressure,
"{}_humidity" .format ( devname ) : humidity,
"{}_resistance" .format ( devname ) : resistance }
@staticmethod
def get_dewpoint (t, rh):
"""
t Temperature in degrees Celcius
rh Relative humidity in percent
"""
a = 6.1121
b = 17.62
c = 243.12
logphi = log(rh) - log(100)
return c * ((b * t) / (c + t) + logphi) / ((b * c) / (c + t) - logphi)
if __name__ == "__main__":
dev = ArduinoEnvSensor("ASRLCOM10::INSTR")
print(dev.get_reading())