This repository has been archived by the owner on Dec 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meteo_otio.py
executable file
·182 lines (166 loc) · 6.4 KB
/
meteo_otio.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
'''
Ce programme récupère les données d'une station météo OTIO WH5100 au moyen
d'un dongle DVB-T de type RTL-2838
'''
__docformat__ = 'restructuredtext en'
from datetime import datetime
from time import sleep
import subprocess
import urllib.request
import json
import logging
logging.basicConfig(
level=logging.DEBUG,
format='[METEO] - %(asctime)s - %(message)s',
datefmt='[%d/%m/%Y %H:%M:%S]')
API_URL = "http://127.0.0.1/api/meteo"
RTL_SDR_FREQ = 868304625
'''
Il semble que l'état des piles modifié l'offset de la température
'''
REMPLACEMENT = {
'6d': '65',
'6c': '64',
'6b': '63',
'6a': '62',
'25': '65',
'24': '64',
'23': '63',
'22': '62'
}
def getweather():
'''
Lit les données depuis le rtl_sdr
La fonction pour la température est y = 0.05x-1268.8
La fonction pour l'humidité est y = x/2
Pour la vitesse du vent, la formule a était trouvée avec une regression
linéaire sur une vingtaine de mesure.
Pour le pluviomètre, il s'agit d'un compteur relatif ; la valeur est
stockée pour pouvoir effectuer une différence avec la précédente.
Ensuite, pour afficher les mêmes valeurs que sur la station méto "officielle",
il faut utiliser la formule suivante :
0.3*(((np.sum(pluie)/2)*10)//3)
avec np.sum la somme des enregistrements sur une période donnée (1h, 24h, ...)
'''
timestamp = datetime.now().timestamp()
def temperature(raw):
return round((0.05*raw-1268.8), 1)
def humidity(raw):
return int(round(raw/2))
def wind(raw):
return round((0.61264343715451175*raw-0.018142655636458116), 1)
def rain(raw):
current = int(raw)
try:
with open("/tmp/old_rain", "r") as tmp:
old_rain = int(tmp.read())
except FileNotFoundError:
old_rain = None
except Exception as err:
logging.error("Error : " + str(err))
return int(0)
if old_rain is None:
with open("/tmp/old_rain", "w") as tmp:
tmp.write(str(current))
return int(0)
if current > old_rain:
res = round(float(current-old_rain)*0.3, 1)
with open("/tmp/old_rain", "w") as tmp:
tmp.write(str(current))
else:
if (current+255)-old_rain < 254:
res = round(float((current+255)-old_rain)*0.3, 1)
with open("/tmp/old_rain", "w") as tmp:
tmp.write(str(current))
else:
res = 0
return float(res)
p = subprocess.Popen(
["sudo", "rtl_433", "-f", str(RTL_SDR_FREQ), "-q", "-A", "-T", "50"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
try:
frame = None
for line in stderr.decode().splitlines():
if '[00]' in line and '{79}' in line:
frame = line.split()
if frame[4] in REMPLACEMENT.keys():
frame[4] = REMPLACEMENT[frame[4]]
t = temperature(int(str(frame[4]) + str(frame[5]), 16))
h = humidity(int(str(frame[6]), 16))
w_median = wind(int(frame[7], 16))
w_gust = wind(int(frame[8], 16))
r = rain(int(frame[10], 16))
break
elif "Time expired" in line:
logging.error("Aucune donnée reçu !")
return None
elif "Signal caught" in line:
### Specifique à FreeBSD
### TODO : adapter à GNU/Linux
logging.error("Erreur de communication !")
# p = subprocess.Popen(
# ["sudo", "usbconfig"],
# stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# stdout, stderr = p.communicate()
# ports = stdout.decode().splitlines()
## for port in ports:
# if "Realtek" in port:
# port_meteo = port.split()[0].split(':')[0]
# logging.info("Redémarrage de port USB... ({})".format(port_meteo))
# p = subprocess.Popen(
# ["sudo", "usbconfig", "-d", port_meteo, "reset"],
# stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# stdout, stderr = p.communicate()
sleep(10)
return None
if frame:
with open("/home/antoine/station_meteo.log", "a") as logfile:
logfile.write(str(frame) + "\n")
if -20 <= t <= 60 and 0 <= h <= 100 and r < 10:
donnees_meteo = {
'timestamp': float(timestamp),
'temp': float(t),
'hum': int(h),
'wind': float(w_median),
'gust': float(w_gust),
'rain': float(r),
}
return donnees_meteo, int(frame[10], 16)
else:
logging.error("Donnée illogique :/")
logging.error("T="+str(t)+" H="+str(h)+" R="+str(r))
logging.error(str(frame))
return None
else:
return None
except Exception as err:
logging.error("Impossible de décoder les données : " + str(err))
for line in stderr.decode().splitlines():
if '[00]' in line:
logging.debug(line)
else:
logging.debug("Aucunes données valides reçues : « "
+ line + " »")
return None
def main(): # pragma: no cover
''' Renvoie les données sur une API si exécuté directement '''
req = urllib.request.Request(API_URL)
req.add_header('Content-Type', 'application/json; charset=utf-8')
data = getweather()
if data is not None:
logging.info("Données : " + str(data[0]))
data = json.dumps(data[0]).encode()
req.add_header('Content-Length', len(data))
try:
reponse = urllib.request.urlopen(req, data, 2)
logging.info("Données envoyées, le serveur a répondu « "
+ str(reponse.code) + " »")
except Exception as err:
logging.error("Aucunes données envoyées : " + str(err))
if __name__ == '__main__': # pragma: no cover
while True:
main()