-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacquire.py
160 lines (127 loc) · 5.11 KB
/
acquire.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
import smbus
import time
import ConfigParser
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
# Define adresses of the sensors
HTS221 = 0x5F
LPS25H = 0x5D #X-NUCLEO-IKS01A1
X_NUCLEO_IKS01A1 = 0xBD
LPS22HB = 0x5D #X-NUCLEO-IKS01A2
X_NUCLEO_IKS01A2 = 0xB1
####### Read temperature and humidity #######
def getTemperatureAndHumidity(bus, HTS221):
# Wake up the device (0x84 does not work)
bus.write_byte_data(HTS221, 0x20, 0x85)
# Humidity Calibration values
H0 = bus.read_byte_data(HTS221, 0x30) / 2
H1 = bus.read_byte_data(HTS221, 0x31) / 2
val0 = bus.read_byte_data(HTS221, 0x36)
val1 = bus.read_byte_data(HTS221, 0x37)
H2 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)
val0 = bus.read_byte_data(HTS221, 0x3A)
val1 = bus.read_byte_data(HTS221, 0x3B)
H3 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)
# Temperature Calibration values
T0 = bus.read_byte_data(HTS221, 0x32)
T0 = (T0 & 0xFF)
T1 = bus.read_byte_data(HTS221, 0x33)
T1 = (T1 & 0xFF)
raw = bus.read_byte_data(HTS221, 0x35)
raw = (raw & 0x0F)
# Convert the temperature Calibration values to 10-bits
T0 = ((raw & 0x03) * 256) + T0
T1 = ((raw & 0x0C) * 64) + T1
val0 = bus.read_byte_data(HTS221, 0x3C)
val1 = bus.read_byte_data(HTS221, 0x3D)
T2 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)
val0 = bus.read_byte_data(HTS221, 0x3E)
val1 = bus.read_byte_data(HTS221, 0x3F)
T3 = ((val1 & 0xFF) * 256) + (val0 & 0xFF)
# Select control register2, 0x21
# 0x01 One shot conversion
# Useless with 0x85 in 0x20
#bus.write_byte_data(HTS221, 0x21, 0x01)
#While data is not ready, wait
while ((bus.read_byte_data(HTS221, 0x27) & 0x03) != 3):
time.sleep(0.1)
# Read data back from 0x28 with command register 0x80, 4 bytes
# humidity msb, humidity lsb, temp msb, temp lsb
data = bus.read_i2c_block_data(HTS221, 0x28 | 0x80, 4)
# Put the device back in sleep mode
bus.write_byte_data(HTS221, 0x20, 0x00)
# Convert the data
humidity = (data[1] * 256) + data[0]
humidity = (((1.0 * H1) - (1.0 * H0)) * (1.0 * humidity - 1.0 * H2) / (1.0 * H3 - 1.0 * H2)) + (1.0 * H0)
if humidity>100:
humidity=100
temp = (data[3] * 256) + data[2]
if temp > 32767 :
temp -= 65536
temperature = ((T1 - T0) / 8.0) * (temp - T2) / (T3 - T2) + (T0 / 8.0)
print "Relative Humidity : %.1f %%" %humidity
print "Temperature in Celsius : %.1f C" %temperature
return temperature, humidity
####### Read pressure LPS22HB #######
def getPressure(bus, LPS22HB):
device = bus.read_byte_data(LPS22HB, 0x0F)
if (device == X_NUCLEO_IKS01A1) :
print "X_NUCLEO_IKS01A1"
# Wake up the device
bus.write_byte_data(LPS22HB, 0x20, 0x95)
elif (device == X_NUCLEO_IKS01A2):
print "X_NUCLEO_IKS01A2"
# Wake up the device
bus.write_byte_data(LPS22HB, 0x10, 0x00)
# One shot measurement
bus.write_byte_data(LPS22HB, 0x11, 0x01)
while ((bus.read_byte_data(LPS22HB, 0x27) & 0x03) != 3):
time.sleep(0.1)
# Read data back from 0x28 with Command register, 0x80
data = bus.read_i2c_block_data(LPS22HB, 0x28 | 0x80, 3)
# Convert the data to hPa
pressure = twos_comp((data[2] * 65536 + data[1] * 256 + data[0])) / 4096.0
print "Barometric Pressure is : %.1f hPa" %pressure
return pressure
####### Write data on the DB #######
def writeToDB(temperature,humidity,pressure):
_time=time.strftime('%Y-%m-%d %H:%M:%S')
# Output data to screen
print(_time)
config = ConfigParser.ConfigParser()
config.readfp(open(r'/home/pi/RaspiMeteogram/db.conf'))
_username = config.get('db-meteogram', 'username')
_password = config.get('db-meteogram', 'password')
_database = config.get('db-meteogram', 'database')
_table = config.get('db-meteogram', 'table')
try:
connection = mysql.connector.connect(host='localhost',
database=_database,
user=_username,
password=_password)
cursor = connection.cursor()
sql = "INSERT INTO "+_table+" (tdatetime, temperature, pressure, humidity) VALUES (%s, %s, %s, %s)"
val = (_time,temperature,pressure,humidity)
result = cursor.execute(sql, val)
connection.commit()
print ("Record inserted successfully into table")
except mysql.connector.Error as error :
connection.rollback() #rollback if any exception occured
print("Failed inserting record into table {}".format(error))
finally:
#closing database connection.
if(connection.is_connected()):
cursor.close()
connection.close()
def twos_comp(val):
if (val & (1 << (24 - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
val = val - (1 << 24) # compute negative value
return val
def main():
# Get I2C bus
bus = smbus.SMBus(1)
temperature, humidity = getTemperatureAndHumidity(bus, HTS221)
pressure = getPressure(bus, LPS22HB)
writeToDB(temperature,humidity,pressure)
main()