-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.py
58 lines (50 loc) · 1.74 KB
/
collector.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
import requests
from temperature import getTemperature
# This classes should send sensors data via socket
class Collector:
def __init__(self, server_url='http://127.0.0.1', port="3000"):
self.temperature = 0.0
self.volume = 0 # in ml, int
self.machine_status = "true"
self.sent_status = False
# Rails API is running in localhost:3000
self.server_url = server_url + ':' + str(port) + '/setsensors'
# Collect all sensors data
def collect(self):
self.machine_status = "true"
try:
self.temperature = getTemperature()
except:
self.machine_status = "false"
try:
f = open("/home/pi/autochopp-machine/embedded_electronics/tmp/volume.vol", "r")
self.volume = int(f.read())
f.close()
except:
self.machine_status = "false"
# Send the HTTP POST request
def send_to_server(self):
data = {
'temperature': self.temperature,
'volume': self.volume,
'machine_status': self.machine_status
}
r = requests.post(self.server_url, data)
if r.status_code == 204:
self.sent_status = True
else:
self.sent_status = False
####################### TEST ###################
if __name__=="__main__":
test = Collector("https://fast-retreat-18030.herokuapp.com", '')
import time
while(1):
try:
test.collect()
print "\nTemperature: ", test.temperature
print "Volume: ", test.volume
test.send_to_server()
print "Sent status: ", test.sent_status
time.sleep(60)
except KeyboardInterrupt:
break