-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·120 lines (94 loc) · 3.82 KB
/
main.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
import lcddriver
import time
import RPi.GPIO as GPIO
import requests
import json
import time
from hx711 import HX711
from urllib.request import urlopen
URL = "http://restservices496.herokuapp.com/containers"
id = 1
PARAMS = {'container_id':id}
r = requests.get(url = URL, params = PARAMS)
data = r.json()
print("Existing containers:")
print(data)
print(" ")
print(" ")
print(" ")
headers={'Content-type':'application/json', 'Accept':'application/json'}
display = lcddriver.lcd()
#this func will be used if needed
def long_string(display, text = '', num_line = 1, num_cols = 20):
if(len(text) > num_cols):
display.lcd_display_string(text[:num_cols],num_line)
time.sleep(1)
for i in range(len(text) - num_cols + 5):
text_to_print = text[i:i+num_cols]
display.lcd_display_string(text_to_print,num_line)
time.sleep(0.2)
else:
display.lcd_display_string(text,num_line)
display = lcddriver.lcd()
try:
long_string(display, "Welcome to ZoologicalFooding Project", 1)
display.lcd_display_string(":)", 2)
GPIO.setmode(GPIO.BCM)
hx = HX711(dout_pin=5, pd_sck_pin=6)
error = hx.zero()
if error:
raise ValueError('Cannot tare the scale')
reading = hx.get_raw_data_mean()
if not reading:
print('incorrect data', reading)
input('Place a known weighted object and press <enter>')
reading = hx.get_data_mean()
if reading:
known_weight_grams = input('Weight of the object: ')
try:
value = float(known_weight_grams)
except ValueError:
print('Incorrect type of value')
ratio = reading / value
hx.set_scale_ratio(ratio)
print('Scaling done')
long_string(display, "Scaling done", 1)
time.sleep(1)
display.lcd_display_string(" ", 1)
display.lcd_display_string(" ", 2)
else:
raise ValueError('Cannot calculate the incoming value')
print("Starting to measure...")
input('Ready? (<Enter>)')
URL3 = "https://restservices496.herokuapp.com/editContainer/761"
last_time_measured = time.time()
while True:
wght = hx.get_weight_mean(20)
print("%.2f" % wght, 'gr')
display.lcd_display_string("Weight:", 1)
display.lcd_display_string(" ", 2)
display.lcd_display_string("%5.2f gr"%wght, 2)
weight = float("{0:.2f}".format(wght))
if int(time.time() - last_time_measured) > 15:
html = urlopen("http://ipinfo.io/json").read()
data = json.loads(html.decode('utf-8'))
IP=data['ip']
org=data['org']
city = data['city']
country=data['country']
region=data['region']
loc=data['loc']
long = loc[:loc.index(',')]
lat = loc[loc.index(',')+1:]
headers={'Content-type':'application/json', 'Accept':'application/json'}
URL3 = "https://restservices496.herokuapp.com/editContainer/761"
#data = {'name':'real container','type':'dosdsdsg','longitude':1,'latitude':1,'address':'a','weight':0, 'ip':'a','city':'a','region':'a','country':'a'}
data = {'name':'real container','type':'dosdsdsg','longitude':long,'latitude':lat,'address':'adrsssss','weight':weight, 'ip':IP,'city':city,'region':region,'country':country}
r3 = requests.put(url = URL3, data=json.dumps(data),headers=headers)
w = str(weight)
print("Weight : " + w + " gr , Location: " + long + ", " + lat + ", " + city + ", " + country + ", IP: " + IP + " sent to database")
last_time_measured = time.time()
except (KeyboardInterrupt, SystemExit):
print('The program ended')
finally:
GPIO.cleanup()