-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.py
executable file
·180 lines (165 loc) · 7.07 KB
/
get_data.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import serial, time, os.path, urllib, re
from urllib.request import urlopen
debug = True
path_to_files = '/tmp/'
serial_port = "/dev/ttyUSB0"
serial_rate = 115200
arduino = serial.Serial(serial_port, serial_rate)
#arduino = serial.Serial(port = None)
#arduino.port = serial_port
#arduino.baudrate = serial_rate
arduino.bytesize = serial.EIGHTBITS
arduino.parity = serial.PARITY_NONE
arduino.stopbits = serial.STOPBITS_ONE
arduino.timeout = 2
arduino.xonxoff = False
arduino.rtscts = False
arduino.dsrdtr = False
arduino.writeTimeout = 0
time.sleep(2.5)
#arduino.open()
arduino.flushInput()
arduino.flushOutput()
arduino.write("t".encode())
#arduino.close()
time.sleep(2.5)
arduino_temp_values = [None] * 2
main_temp_values = [None] * 2
humidity_values = [None] * 2
voltage_values = [None] * 2
temps_values = [None] * 3
dallas_address = [
"28-0416b35d80ff", # PC stul
"28-0516c110d6ff", # Skrinka
"28-0316b5e979ff", # Detsky pokoj
]
# Print debug function
def debug_print(text):
if debug:
print(text)
while 1:
debug_print("DEBUG: Reading data")
if arduino.isOpen():
debug_print("DEBUG: Arduino connected")
arduino.flushInput()
arduino.flushOutput()
arduino.write("t".encode())
for x in range(0, 1): # Can be deleted after transitioning to BME (or other external temp)
output = arduino.readline().rstrip("\r\n".encode())
debug_print("DEBUG: Arduino temperature " + str(x) + ": " + output.decode())
if float(output) <= 70 and float(output) >= -50:
arduino_temp_values[x] = round(float(output), 1)
else:
arduino_temp_values[x] = u"---"
arduino.readline()
arduino.write("h".encode())
for x in range(0, 1): # Can be deleted after transitioning to BME (or other external humidity)
output = arduino.readline().rstrip("\r\n".encode())
debug_print("DEBUG: Arduino humidity " + str(x) + ": " + output.decode())
if float(output) <= 100 and float(output) >= 0:
humidity_values[x] = round(float(output), 1)
else:
humidity_values[x] = u"---"
humidity_file = open(path_to_files + 'local_humidity', 'w')
humidity_file.write("%s\n" % humidity_values[x])
humidity_file.close()
arduino.readline()
# Dallas teplotni cidla
for i in range(0, len(dallas_address)): # Smycka pro cteni podle zadanych adres
try:
temp_file = open('/sys/bus/w1/devices/' + dallas_address[i] + '/w1_slave', 'r') # Otevrit soubor s daty
file_lines = temp_file.read().splitlines()
crc = re.compile('crc=.. (.*)')
crc_value = crc.search(file_lines[0])
debug_print(crc_value.group(1))
if crc_value.group(1) == "YES": # Overit kontrolni soucet
temp = re.compile('t=(.*)')
temp_value = temp.search(file_lines[1])
debug_print(temp_value.group(1))
if temp_value.group(1) != "85000": # Pokud neukazuje 85 stupnu, coz je chybovy stav
temps_values[i] = float(temp_value.group(1)) / 1000 # Ulozit teplotu
else:
temps_values[i] = u"---" # Kdyby ukazoval 85, tak ulozit ---
else:
temps_values[i] = u"---" # Kdyz neni kontrolni souce v poradku, zapsat ---
except:
temps_values[i] = u"---" # Kdyz se nepovede otevrit soubor s daty, zapsat ---
debug_print("DEBUG: " + "/sys/bus/w1/devices/" + dallas_address[i] + "/w1_slave" + " Temp read failed")
dallas_file = open(path_to_files + 'dallas', 'w') # Otevrit soubor pro zapsani teplot
for i in range(0, len(temps_values) + 1):
if i < len(temps_values) and temps_values[i] == u"---": # Kdyz je misto teploty ulozeno ---, nejde zaokrouhlit (aby nespadl program)
dallas_file.write("%s\n" % temps_values[i]) # Ulozit rovnou ---
elif i < len(temps_values):
dallas_file.write("%s\n" % round(temps_values[i], 1)) # Ulozit zaokrouhlenou teplotu
elif arduino_temp_values[0] == u"---" or (u"---" in temps_values and i == len(temps_values)): # Vyjimka, pokud je nekde ulozeno ---, nepocitat prumer (aby nespadl program)
#dallas_file.write("%s\n" % u"---")
main_temp_values[0] = u"---"
elif i == len(temps_values): # DO posledniho radku ulozit prumer teplot
main_temp_values[0] = round((temps_values[0] + temps_values[1] + temps_values[2] + arduino_temp_values[0]) / 4, 1)
#dallas_file.write("%s\n" % round((temps_values[0] + temps_values[1] + temps_values[2] + main_temp_values[0]) / 4, 1)) # Prumer se pocita z teplot v obyvaku, pokoji a "hlavni teploty" (na vypinaci v kuchyni)
dallas_file.close()
# Udaje z bazenu/meteobudky
meteo_file = open(path_to_files + 'meteo', 'w')
try:
data = urlopen("http://meteo.pavoukovo.cz/meteo", data=None, timeout=3).read().decode() # Otevrit soubor
if data.find("html") == -1:
meteo_file.write(data)
lines = data.splitlines()
if lines[2] != u"---":
main_temp_values[1] = round(float(lines[2]), 1)
else:
main_temp_values[1] = u"---"
debug_print("DEBUG: HTTP meteo read ok")
else:
for i in range(0, 7):
meteo_file.write("%s\n" % u"---") # Kdyz se nepodari otevrit soubor, zapsat ---
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP meteo read failed, remote file not found")
except:
for i in range(0, 7):
meteo_file.write("%s\n" % u"---") # Kdyz se nepodari otevrit soubor, zapsat ---
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP meteo read failed")
meteo_file.close()
main_temps_file = open(path_to_files + 'main_temps', 'w')
main_temps_file.write("%s\n" % main_temp_values[0])
main_temps_file.write("%s\n" % main_temp_values[1])
main_temps_file.close()
# Teploty z kotelny
temps_file = open(path_to_files + 'temps', 'w')
try:
data = urlopen("http://control.pavoukovo.cz/temps", data=None, timeout=3).read().decode() # Otevrit soubor
if data.find("html") == -1:
temps_file.write(data)
debug_print("DEBUG: HTTP temps read ok")
else:
for i in range(0, 8):
temps_file.write("%s\n" % u"---") # Kdyz se nepodari otevrit soubor, zapsat ---
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP temps read failed, remote file not found")
except:
for i in range(0, 8):
temps_file.write("%s\n" % u"---") # Kdyz se nepodari otevrit soubor, zapsat ---
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP temps read failed")
temps_file.close()
# Rychlost vetru
wind_file = open(path_to_files + 'wind', 'w')
try:
debug_print("DEBUG: Wind start")
data = urlopen("http://wind.pavoukovo.cz/wind", data=None, timeout=3).read().decode() # Otevrit soubor
debug_print("DEBUG: Wind opened")
if data.find("html") == -1:
debug_print("DEBUG: Wind HTML found")
wind_file.write(data)
debug_print("DEBUG: HTTP wind read ok")
else:
debug_print("DEBUG: Wind bad reply")
for i in range(0, 5):
wind_file.write("%s\n" % u"---") # Kdyz se nepodari otevrit soubor, zapsat ---
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP wind read failed, remote file not found")
except:
debug_print("DEBUG: Wind open failed")
for i in range(0, 5):
wind_file.write("%s\n" % u"---") # Kdyz se nepodari otevrit soubor, zapsat ---
debug_print("DEBUG: " + time.strftime("%H:%M:%S") + " HTTP wind read failed")
wind_file.close()
time.sleep(5)