Aula JazmatiNajib Kassab
Published © MIT

Raspberry Pi Smart Food Scale with Hexabitz Modules

In this tutorial we are building a Raspberry Pi smart food scale with Hexabitz modules

IntermediateWork in progress24 hours3,270
Raspberry Pi Smart Food Scale with Hexabitz Modules

Things used in this project

Story

Read more

Schematics

Raspberry Pi Weight Scale Schematic

Code

The code

Python
# -*- coding: utf-8 -*-
"""
Created on Fri Sep  3 19:44:22 2021

@author: Aula_J
"""

from tkinter import *
import time
#import numpy as np
import serial
import struct
ser = serial.Serial(        
               port='/dev/ttyS0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
print(ser.name)
def byte_to_float(b):
    x = struct.unpack('f', b)
    return x
def stream():
    global w
    s = ser.read(4) #4byte
    print(s)
    [w,] = byte_to_float(s)
    w = int(w)
    print(w)
    return w
    

root=Tk()
root.title("Hexabitz Scale")
OPTIONS = [
"Apples", "Pears" ,"Raspberries",
"Bread",
"Sugar"
] #etc

def start():
  global w
  w = stream()
  print(w)
  lable= Label (root,width=24,fg="yellow",bg="black")
  lable.config(font=("Courier",20))
  lable.config(text=str(w)+ ' g')
  lable.pack()

button=Button(root,text='Stop',bg ='orange',width=50,command=root.destroy)
button.pack()
Bsample=Button(root,text='Sample weight(g)', bg ='gold',width=50,command=start)
Bsample.pack()
L1 = Label(root, text = " Food Name")
L1.pack()
variable = StringVar(root)
variable.set(OPTIONS[0]) # default value
w = OptionMenu(root, variable, *OPTIONS)
w.pack()
def Calculate_calories():
    q =variable.get()
    global w
    #w = stream()
    w1 =int(w)
    if (q == "Apples"):
        x= 0.48
        z = x*w1
        print(z)
    elif(q == "Pears"):
        x= 0.57
        z = x*w1
        print(z)
    elif(q == "Bread"):
        x= 3.8
        z = x*w1
        print(z)
    elif(q == "Sugar"):
        x= 5.8
        z = x*w1
        print(z)
    else:
        x= 0.85
        z = x*w1
        print(z)
    lable= Label (root,width=24,fg="yellow",bg="black")
    lable.config(font=("Courier",20))
    lable.config(text = str(z) + " calories")
    lable.pack()
buttonc=Button(root,text='Calculate calories', bg ='light green',width=50,command=Calculate_calories)
buttonc.pack()
root.mainloop()

H26R0x-Firmware

HF1R0x-Firmware 0.2.0

Credits

Aula Jazmati

Aula Jazmati

52 projects β€’ 215 followers
πŸ’‘πŸ•ŠοΈ
Najib Kassab

Najib Kassab

11 projects β€’ 15 followers

Comments