forked from wicket1001/IoT-Box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadSensors.py
122 lines (100 loc) · 2.92 KB
/
ReadSensors.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
import json
import random
import smbus
import time
from ServerRequest import *
bus = smbus.SMBus(1)
operational = False
def __init__():
try:
time.sleep(0.5)
bus.write_byte(0x40, 0xF5)
time.sleep(0.5)
bus.write_byte(0x40, 0xF3)
operational = True
except:
pass
def getPressure():
if operational:
try:
data = bus.read_i2c_block_data(0x60, 0x04, 8)
A0 = (data[0] * 256 + data[1]) / 8.0
B1 = (data[2] * 256 + data[3])
if B1 > 32767:
B1 -= 65536
B1 = B1 / 8192.0
B2 = (data[4] * 256 + data[5])
if B2 > 32767:
B2 -= 65535
B2 = B2 / 16384.0
C12 = ((data[6] * 256 + data[7]) / 4) / 4194304.0
bus.write_byte_data(0x60, 0x12, 0x00)
time.sleep(0.5)
data = bus.read_i2c_block_data(0x60, 0x00, 4)
pres = ((data[0] * 256) + (data[1] & 0xC0)) / 64
temp = ((data[2] * 256) + (data[3] & 0xC0)) / 64
presComp = A0 + (B1 + C12 * temp) * pres + B2 * temp
pressure = (65.0 / 1023.0) * presComp + 50
return pressure
except:
return -1
return -1
def getHumidity():
if operational:
try:
time.sleep(0.5)
data0 = bus.read_byte(0x40)
time.sleep(0.5)
data1 = bus.read_byte(0x40)
humidity = ((data0 * 256 + data1) * 125 / 65536.0) - 6
return humidity
except:
return -1
return -1
def getTemperature():
if operational:
try:
time.sleep(0.5)
data0 = bus.read_byte(0x40)
time.sleep(0.5)
data1 = bus.read_byte(0x40)
cTemp1 = ((data0 * 256 + data1) * 175.72 / 65536.0) - 46.85
except:
pass
try:
bus.write_byte_data(0x60, 0x12, 0x00)
time.sleep(0.5)
data = bus.read_i2c_block_data(0x60, 0x00, 4)
temp = ((data[2] * 256) + (data[3] & 0xC0)) / 64
cTemp2 = (temp - 498) / (-5.35) + 25
except:
pass
if cTemp1 is not None and cTemp2 is not None:
return (cTemp1+cTemp2)/2
elif cTemp1 is not None:
return cTemp1
elif cTemp2 is not None:
return cTemp2
else:
return -1
return -1
def handleResponse(response, verbosity=1):
if verbosity==3:
print(response.status_code)
if verbosity==2:
print(response.content)
if verbosity==1:
print(json.dumps(response.json(), indent=4))
# print(response.content)
def readGyro():
return random.random()
setHigherTrustLevel()
r = insertNewValuesHistoricalDataChannel()
handleResponse(r, 3)
r = querySitesBlueprint()
handleResponse(r)
r = queryYoungestRawValues()
handleResponse(r)
handleResponse(r, 3)
# 20180217142556949
# 201501011030