-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHoneyHoney.py
executable file
·88 lines (65 loc) · 1.85 KB
/
HoneyHoney.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
#! /usr/bin/python2
import time
import sys
import paho.mqtt.client as mqtt
import os.path
import json
from datetime import date
from datetime import datetime
EMULATE_HX711=False
HourSec = lambda hour : hour*60*60
referenceUnit = 1
DataTime = HourSec(3*0.0001) # Data taking frequency in hour
today = date.today()
now = datetime.now()
if not EMULATE_HX711:
import RPi.GPIO as GPIO
from hx711 import HX711
else:
from emulated_hx711 import HX711
def cleanAndExit():
print("Cleaning...")
if not EMULATE_HX711:
GPIO.cleanup()
print("Bye!")
sys.exit()
# Method to check whether or not your raspi is connected to the internet
# to use this method add
# import socket
# import requests
# def is_connected():
# try:
# # connect to the host -- tells us if the host is actually
# # reachable
# if socket.create_connection(("1.1.1.1", 53)):
# # requests.get('https://1.1.1.1/').status_code
# return True
# else:
# return False
# except OSError:
# pass
# return False
# waiting for the internet connection to establish
# a more sophisticated way can be obtained by using the is_connected() method at the beginning of this code.
time.sleep(60)
hx = HX711(5, 6)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(22)
# hx.set_reference_unit(referenceUnit)
hx.reset()
hx.tare()
print("Tare done! Add weight now...")
while True:
try:
val = hx.get_weight(5)
print(val)
hx.power_down()
hx.power_up()
myClient = mqtt.Client()
data_set = {"weight":val}
json_dump = json.dumps(data_set)
myClient.connect("<mqtt.hostname>", 1883)
myClient.publish("rpi3_2806549e",json_dump)
time.sleep(DataTime)
except (KeyboardInterrupt, SystemExit):
cleanAndExit()