/
temp-matrix_5x6.py
executable file
·227 lines (217 loc) · 7.91 KB
/
temp-matrix_5x6.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/path-to-working-dir/temp-matrix/paho-mqtt/bin/python
#
# requires:
# paho-mqtt in a venv: https://pypi.org/project/paho-mqtt/#installation
# insert your own sensor addresses (see below line 125+)
# your MQTT connection settings and credentials (line 65+)
#
# recommended:
# ssh to your pi
# screen
# cd /path-to-working-dir/temp-matrix
# source paho-mqtt/bin/activate
# python temp-matrix_5x6.py
#
# nice:
# crontab -e
# */5 * * * * cd /path-to-working-dir/temp-matrix/ && /path-to-working-dir/paho-mqtt/bin/python /path-to-working-dir/temp-matrix/temp-matrix_5x6.py
#
import os
import glob
import time
from time import gmtime, strftime
import json
import paho.mqtt.client as mqtt
# These lines mount the device:
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
# Get all the filenames begin with 28 in the path base_dir.
device_folders = glob.glob(base_dir + '28*')
# print device_folders # DEBUG
def read_temp_raw(device_file):
f = open(device_file, 'r')
lines = f.readlines()
#print(device_file, lines)
f.close()
return lines
def read_temp(device_file):
maxAttempts = 3
while True:
temp = try_temp(device_file)
if temp != -99.0:
return temp
maxAttempts -= 1
if maxAttempts <= 0:
return temp
def try_temp(device_file):
temp_c = -99.0
lines = read_temp_raw(device_file+'/w1_slave')
if len(lines) == 2:
if lines[0].strip()[-3:] == 'YES':
equals_pos = lines[1].find('t=')
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
def send_measurement(EYEShive, matrix):
# The MQTT host
mqtt_host = 'swarm.hiveeyes.org'
mqtt_port = 1883
mqtt_user = 'username'
mqtt_pass = 'some_safe_password'
# The MQTT topic
mqtt_topic = u'{realm}/{network}/{gateway}/{node}/message-json'.format(
realm = 'hiveeyes', # collective
network = 'xxxxxxxx-yyyy-zzzz-aaaa-bbbbbbbbbbbb', # beekepper-ID
gateway = 'some_name', # location
node = some_other_name # in my case: not hive but gateway
)
# Define measurement for 30 sensors in 5x6 matrix
measurement = {
'temp-ir-1-1': matrix[0][0],
'temp-ir-1-2': matrix[0][1],
'temp-ir-1-3': matrix[0][2],
'temp-ir-1-4': matrix[0][3],
'temp-ir-1-5': matrix[0][4],
'temp-ir-2-1': matrix[1][0],
'temp-ir-2-2': matrix[1][1],
'temp-ir-2-3': matrix[1][2],
'temp-ir-2-4': matrix[1][3],
'temp-ir-2-5': matrix[1][4],
'temp-ir-3-1': matrix[2][0],
'temp-ir-3-2': matrix[2][1],
'temp-ir-3-3': matrix[2][2],
'temp-ir-3-4': matrix[2][3],
'temp-ir-3-5': matrix[2][4],
'temp-ir-4-1': matrix[3][0],
'temp-ir-4-2': matrix[3][1],
'temp-ir-4-3': matrix[3][2],
'temp-ir-4-4': matrix[3][3],
'temp-ir-4-5': matrix[3][4],
'temp-ir-5-1': matrix[4][0],
'temp-ir-5-2': matrix[4][1],
'temp-ir-5-3': matrix[4][2],
'temp-ir-5-4': matrix[4][3],
'temp-ir-5-5': matrix[4][4],
'temp-ir-6-1': matrix[5][0],
'temp-ir-6-2': matrix[5][1],
'temp-ir-6-3': matrix[5][2],
'temp-ir-6-4': matrix[5][3],
'temp-ir-6-5': matrix[5][4],
}
# Serialize data as JSON
payload = json.dumps(measurement)
# Publish to MQTT
pid = os.getpid()
client_id = '{}:{}'.format('hiverize', str(pid))
backend = mqtt.Client(client_id=client_id, clean_session=True)
backend.username_pw_set(mqtt_user, mqtt_pass)
backend.connect(mqtt_host, mqtt_port)
backend.publish(mqtt_topic, payload)
backend.disconnect()
# make shure to research your own sensors adresses, for example with
# ls -la /sys/bus/w1/devices/
for folder in device_folders:
tc = read_temp(folder)
label = 'no_known_label'
if folder == '/sys/bus/w1/devices/28-0346d4430b06':
label = 'temp-ir-1-1'
temp_ir_1_1 = tc
if folder == '/sys/bus/w1/devices/28-0cf3d443ba40':
label = 'temp-ir-1-2'
temp_ir_1_2 = tc
if folder == '/sys/bus/w1/devices/28-0e49d44343bd':
label = 'temp-ir-1-3'
temp_ir_1_3 = tc
if folder == '/sys/bus/w1/devices/28-11c1d443f241':
label = 'temp-ir-1-4'
temp_ir_1_4 = tc
if folder == '/sys/bus/w1/devices/28-1937d443bed6':
label = 'temp-ir-1-5'
temp_ir_1_5 = tc
if folder == '/sys/bus/w1/devices/28-2231d443d266':
label = 'temp-ir-2-1'
temp_ir_2_1 = tc
if folder == '/sys/bus/w1/devices/28-282bd4430f5e':
label = 'temp-ir-2-2'
temp_ir_2_2 = tc
if folder == '/sys/bus/w1/devices/28-2846d443e4f2':
label = 'temp-ir-2-3'
temp_ir_2_3 = tc
if folder == '/sys/bus/w1/devices/28-297ad443f622':
label = 'temp-ir-2-4'
temp_ir_2_4 = tc
if folder == '/sys/bus/w1/devices/28-2c6cd443ccf7':
label = 'temp-ir-2-5'
temp_ir_2_5 = tc
if folder == '/sys/bus/w1/devices/28-304ad4436d1a':
label = 'temp-ir-3-1'
temp_ir_3_1 = tc
if folder == '/sys/bus/w1/devices/28-32c9d443b51b':
label = 'temp-ir-3-2'
temp_ir_3_2 = tc
if folder == '/sys/bus/w1/devices/28-3ce1d443d148':
label = 'temp-ir-3-3'
temp_ir_3_3 = tc
if folder == '/sys/bus/w1/devices/28-400bd4439156':
label = 'temp-ir-3-4'
temp_ir_3_4 = tc
if folder == '/sys/bus/w1/devices/28-450ed4430afe':
label = 'temp-ir-3-5'
temp_ir_3_5 = tc
if folder == '/sys/bus/w1/devices/28-4694d44325ca':
label = 'temp-ir-4-1'
temp_ir_4_1 = tc
if folder == '/sys/bus/w1/devices/28-5196d4434d53':
label = 'temp-ir-4-2'
temp_ir_4_2 = tc
if folder == '/sys/bus/w1/devices/28-550cd4434596':
label = 'temp-ir-4-3'
temp_ir_4_3 = tc
if folder == '/sys/bus/w1/devices/28-57b6d44367cb':
label = 'temp-ir-4-4'
temp_ir_4_4 = tc
if folder == '/sys/bus/w1/devices/28-5821d44339c2':
label = 'temp-ir-4-5'
temp_ir_4_5 = tc
if folder == '/sys/bus/w1/devices/28-65f2d4434f51':
label = 'temp-ir-5-1'
temp_ir_5_1 = tc
if folder == '/sys/bus/w1/devices/28-6723d443b9ea':
label = 'temp-ir-5-2'
temp_ir_5_2 = tc
if folder == '/sys/bus/w1/devices/28-6755d443de81':
label = 'temp-ir-5-3'
temp_ir_5_3 = tc
if folder == '/sys/bus/w1/devices/28-6950d443323f':
label = 'temp-ir-5-4'
temp_ir_5_4 = tc
if folder == '/sys/bus/w1/devices/28-6ae6d4434899':
label = 'temp-ir-5-5'
temp_ir_5_5 = tc
if folder == '/sys/bus/w1/devices/28-6b2ad4437e19':
label = 'temp-ir-6-1'
temp_ir_6_1 = tc
if folder == '/sys/bus/w1/devices/28-6f8ad443a557':
label = 'temp-ir-6-2'
temp_ir_6_2 = tc
if folder == '/sys/bus/w1/devices/28-72d1d44362f0':
label = 'temp-ir-6-3'
temp_ir_6_3 = tc
if folder == '/sys/bus/w1/devices/28-72d8d44397f7':
label = 'temp-ir-6-4'
temp_ir_6_4 = tc
if folder == '/sys/bus/w1/devices/28-79d2d4430cf1':
label = 'temp-ir-6-5'
temp_ir_6_5 = tc
# print ('%s %3.1f deg C %s' % (folder, tc, label)) # DEBUG
# print ('%s %3.1f' % (label, tc)) # DEBUG
matrix = [[temp_ir_1_1, temp_ir_1_2, temp_ir_1_3, temp_ir_1_4, temp_ir_1_5], \
[temp_ir_2_1, temp_ir_2_2, temp_ir_2_3, temp_ir_2_4, temp_ir_2_5], \
[temp_ir_3_1, temp_ir_3_2, temp_ir_3_3, temp_ir_3_4, temp_ir_3_5], \
[temp_ir_4_1, temp_ir_4_2, temp_ir_4_3, temp_ir_4_4, temp_ir_4_5], \
[temp_ir_5_1, temp_ir_5_2, temp_ir_5_3, temp_ir_5_4, temp_ir_5_5], \
[temp_ir_6_1, temp_ir_6_2, temp_ir_6_3, temp_ir_6_4, temp_ir_6_5]]
# print (matrix) # DEBUG
send_measurement('noho', matrix)
# print (strftime("%Y-%m-%d %H:%M:%S", time.localtime())," Done sending. Going to sleep for 15min.") # DEBUG