Skip to content

Instantly share code, notes, and snippets.

@tudorelu
Last active July 7, 2020 09:11
Show Gist options
  • Select an option

  • Save tudorelu/b1402f372c797a2013963eff374d1976 to your computer and use it in GitHub Desktop.

Select an option

Save tudorelu/b1402f372c797a2013963eff374d1976 to your computer and use it in GitHub Desktop.
import RPi.GPIO as GPIO
import time
import math
import sys
from hx711 import HX711
from pijuice import PiJuice
import os
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate('./iot-med-firebase-adminsdk-269bo-7c3624a3c4.json')
default_app = firebase_admin.initialize_app(cred)
db = firestore.client()
pijuice = PiJuice(1, 0x14)
time.sleep(20)
if sys.version_info[0] != 3:
raise Exception("Python 3 is required.")
hx = HX711(5, 6, gain=128)
global first
global flag
def cleanAndExit():
print("Cleaning up...")
GPIO.cleanup()
print("Bye!")
sys.exit()
def setup():
"""
code run once
"""
print("Initializing.")
scale_ready = False
while not scale_ready:
if (GPIO.input(hx.DOUT) == 0):
scale_ready = False
if (GPIO.input(hx.DOUT) == 1):
print("Initialization complete!")
scale_ready = True
def calibrate():
hx.set_offset(8396071.0)
hx.set_scale(-103.03333333333333)
"""
def calibrate():
readyCheck = input("Remove any items from scale. Press any key when ready.")
offset = hx.read_average()
print("Value at zero (offset): {}".format(offset))
hx.set_offset(offset)
print("Please place an item of known weight on the scale.")
readyCheck = input("Press any key to continue when ready.")
measured_weight = (hx.read_average()-hx.get_offset())
item_weight = input("Please enter the item's weight in grams.\n>")
scale = int(measured_weight)/int(item_weight)
hx.set_scale(scale)
print("Scale adjusted for grams: {}".format(scale))
"""
flag = 0
first = 0
# check the sampling rate at the enf of the code
def loop():
global first
global flag
try:
prompt_handled = False
while not prompt_handled:
val = hx.get_grams()
if flag==0:
first = val
#hx.power_down()
#hx.power_up()
#print("Item weighs {} grams.\n".format(val))
diff = first-val
difference = "%.1f" % round(diff,2)
d = float(difference)
charge = pijuice.status.GetChargeLevel()
#ch=charge['data']
percent = int((val/first)*100)
if(percent > 0):
if(percent<=100):
print("Percent left ->", percent, "%")
timestamp = round(time.time())
ch = 29;
med_ref = db.collection(u'devices').document(u'dev_0001')\
.update({u'readings.'+u'{}'.format(timestamp):percent})
med_ref = db.collection(u'devices').document(u'dev_0001')\
.update({u'readings_battery.'+u'{}'.format(timestamp):ch})
med_ref = db.collection(u'devices').document(u'dev_0001')\
.update({u'lastEntryValue':u'{}'.format(percent)})
med_ref = db.collection(u'devices').document(u'dev_0001')\
.update({u'lastEntryTime':u'{}'.format(timestamp)})
print("uploaded")
elif percentage<0:
print("error value")
flag = flag+1
#change the time here to alter sampling rate
time.sleep(120)
except (KeyboardInterrupt, SystemExit):
print("wrong value")
cleanAndExit()
if __name__ == "__main__":
setup()
calibrate()
while flag <= 0 :
loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment