-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendAirpiData.py
92 lines (79 loc) · 2.64 KB
/
sendAirpiData.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Authors:
Michalis Tsiolkas
Lucian Nikolaos Xaxiris
Marios Balamatsias
Vasileios Karavasilis
Gerasimos Chamalis
Savvopoylos Petros
Krommydas Konstantinos
"""
### Can combined with the airpi program provided by
### https://github.com/haydnw/AirPi
### It reads the output json file, prints the lines
### and send to a server.
import sys
import json
import time
import urllib2
def printData(dataDict):
a = u'Ώρα:', dataDict['Date and time'];
print ''.join(a).encode('utf-8');
a = u'Θερμοκρασία (BMP):', dataDict['Temperature-BMP'];
print ''.join(a).encode('utf-8');
a = u'Πίεση:', dataDict['Pressure'];
print ''.join(a).encode('utf-8');
a = u'Υγρασία:', dataDict['Relative_Humidity'];
print ''.join(a).encode('utf-8');
a = u'Θερμοκρασία (DHT):', dataDict['Temperature-DHT'];
print ''.join(a).encode('utf-8');
a = u'Επίπεδο Φωτός:', dataDict['Light_Level'];
print ''.join(a).encode('utf-8');
a = u'Μονοξείδιο του Άνθρακα:', dataDict['Carbon_Monoxide'];
print ''.join(a).encode('utf-8');
a = u'Θόρυβος:', dataDict['Volume'];
print ''.join(a).encode('utf-8');
a = "";
print ''.join(a).encode('utf-8');
def sendData(dataDict, rasId, rasPass):
# rpiid = sys.argv[2];
urlstr = 'http://met-ioamaellak.rhcloud.com/insert.php?id=' + rasId + \
'&pass=' + rasPass + '&when=' + \
dataDict['Date and time'].replace (" ", "T") + '&temp-bmp=' + \
dataDict['Temperature-BMP'] + '&pressure=' + \
dataDict['Pressure'] +'&humidity=' + \
dataDict['Relative_Humidity'] +'&temp-dht=' + \
dataDict['Temperature-DHT'] + '&light=' + \
dataDict['Light_Level'] +'&CO=' + dataDict['Carbon_Monoxide'] + \
'&volume=' + dataDict['Volume'];
print urlstr;
try:
urllib2.urlopen(urlstr).read();
except:
pass
#Get the name of the file with the json output.
def main(fileName, rasId, rasPass):
#Open the file and read every line.
file = open(fileName, 'r');
for line in file:
jl = json.loads(line);
printData(jl);
sendData(jl, rasId, rasPass);
#Sleep and try to read more lines.
while True:
line = file.readline();
if not line:
time.sleep(0.5);
continue;
jl = json.loads(line);
printData(jl);
sendData(jl, rasId, rasPass);
file.close();
if __name__ == "__main__":
# Print usage and exit if less or more than 4 arguments are given.
if len(sys.argv) != 4:
print 'Usage: ', sys.argv[0], '<file_with_json>', '<AirPi ID>', '<Pass>';
sys.exit(1);
main(sys.argv[1], sys.argv[2], sys.argv[3])