-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread2_post2.py
72 lines (53 loc) · 1.66 KB
/
read2_post2.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
import serial
import datetime
import time
import requests
ser=serial.Serial('/dev/ttyACM0')
filename="data.csv"
# credentials for belfast.pvos.org (for this particular sensor feed)
#public_key = "i9p7tgvmbxhg"
#private_key = "69cqt4v4hq99"
public_key = "myvqc22uibva"
private_key = "iyxuuqac8gj3"
# these will stay fixed:
base_url = "http://bayou.pvos.org/data/"
full_url = base_url+public_key
counter = 0
counter_write = 100
while True:
out=ser.readline()
dec=out.decode("utf-8")
decs = dec.strip().split(",")
#time=datetime.datetime.now()
#stamp=datetime.datetime.timestamp(time)*1000
stamp=int(time.time())
print(counter, stamp,decs[0],decs[1],decs[2],decs[3])
#counter = counter + 1
temp_0=float(decs[0])*9./5.+32.
temp_1=float(decs[1])*9./5.+32.
batt = decs[2]
rssi = decs[3]
#write to file
#if (counter > counter_write):
print("writing ...")
out_str=str(stamp)+","+str(decs[0])+","+str(decs[1])+","+str(decs[2]+","+str(decs[3])+"\n")
f = open(filename,"a")
f.write(out_str)
f.close()
counter = 0
# post to bayou
try:
#post to bayou
#post node_0
myobj = {"private_key":private_key, "node_id":0,"temperature_c":temp_0,"battery_volts":batt,"rssi":rssi}
x = requests.post(full_url, data = myobj)
print(myobj)
print(x.text)
time.sleep(1)
#post node_1
myobj = {"private_key":private_key, "node_id":1,"temperature_c":temp_1,"battery_volts":batt,"rssi":rssi}
x = requests.post(full_url, data = myobj)
print(myobj)
print(x.text)
except:
print("an exception occurred when posting")