Python Forum
Guizero HX711 Load Cell and python3 stdout?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guizero HX711 Load Cell and python3 stdout?
#1
Hi, im after a bit of help, ive been playing around with a couple of bits of code and im really struggling getting the two to work together, i have made a gui using guizero and i am wanting to launch a load cell example file i have from inside the gui when i open a separate window ive tried many things and i dont know what else to try, i would really appropriate if someone could help me out and show me how to combine the two to get meaningful results.


The two work fine independently but the second bit of code which is for a load cell displays the output in the command/terminal window and i need it to show inside the gui. (This is being used on a raspberry pi) the code for the gui is this:

from guizero import App
import tkinter as tk
import time
import serial

#Items to be defined

def open_main():
    main.show()
def open_window():
    window.show(wait=True)
def open_window2():
    window2.show(wait=True)
def close_window():
    window.hide()
def close_window2():    
    window2.hide()



app = App(bg="#09920B", title="Test")  #App Start
from guizero import App, Text, PushButton, ButtonGroup, Picture, Window # 
Import Objects from GUIZERO


welcome_message = Text(app,color="white" , text="Welcome", size=40, 
font="Times New Roman") #Main Title

Logo = Picture(app, image="logo.gif") # GIF

#Pull Test Window Star
window = Window(app, title="Pull Test",height=300, width=500) #Window 
size,name, etc
window.hide()
logo = Picture(window,image="logo.png",)
window.hide()
welcome_message = Text(window,color="black" , text="Kilograms:(units)", 
size=40, font="Times New Roman")
window.hide()


open_button = PushButton(app, text="Pull Test", command=open_window, 
width=150) # Pull Test Button in main screen

close_button = PushButton(window, text="Close", command=close_window) 
#Close button in pull test window

#Push Test Window Start


window2 = Window(app, title="Push Test",height=300, width=500) #Push Test 
Window
window2.hide()
logo = Picture(window2,image="logo.png")                
welcome_message = Text(window2,color="black" , text="Kilograms:(units)", 
size=40, font="Times New Roman") #Units to be measured

window2.hide()

open_button = PushButton(app, text="Push Test", command=open_window2, 
width=200)#Open window for push test
close_button = PushButton(window2, text="Close", command=close_window2)   
#Close button in Push test window

welcome_message = Text(app,color="white" , text="Calibration", size=40, 
font="Times New Roman")  #text displayed at bottom of main page

Logo = Picture(app, image="rds1.png") #RDS logo on bottom of main page

app.display()
and the code for the load cell that i want to launch inside the window "push test" and "pull test" is here:

#!/usr/bin/env python3
import RPi.GPIO as GPIO  # import GPIO
from hx711 import HX711  # import the class HX711

try:
    GPIO.setmode(GPIO.BCM)  # set GPIO pin mode to BCM numbering
    # Create an object hx which represents your real hx711 chip
    # Required input parameters are only 'dout_pin' and 'pd_sck_pin'
    hx = HX711(dout_pin=5, pd_sck_pin=6)
    # measure tare and save the value as offset for current channel
    # and gain selected. That means channel A and gain 128
    err = hx.zero()
    # check if successful
    if err:
        raise ValueError('Tare is unsuccessful.')

    reading = hx.get_raw_data_mean()
    if reading:  # always check if you get correct value or only False
    # now the value is close to 0
    print('Data subtracted by offset but still not converted to units:',
          reading)
    else:
        print('invalid data', reading)

# In order to calculate the conversion ratio to some units, in my case I want grams,
# you must have known weight.
    input('Put known weight on the scale and then press Enter')
    reading = hx.get_data_mean()
    if reading:
        print('Mean value from HX711 subtracted by offset:', reading)
        known_weight_grams = input(
            'Write how many grams it was and press Enter: ')
        try:
            value = float(known_weight_grams)
            print(value, 'grams')
        except ValueError:
            print('Expected integer or float and I have got:',
              known_weight_grams)

    # set scale ratio for particular channel and gain which is
    # used to calculate the conversion to units. Required argument is only
    # scale ratio. Without arguments 'channel' and 'gain_A' it sets
    # the ratio for current channel and gain.
        ratio = reading / value  # calculate the ratio for channel A and gain 128
        hx.set_scale_ratio(ratio)  # set ratio for current channel
        print('Ratio is set.')
    else:
        raise ValueError('Cannot calculate mean value. Try debug mode. Variable reading:', reading)

# Read data several times and return mean value
# subtracted by offset and converted by scale ratio to
# desired units. In my case in grams.
    print("Now, I will read data in infinite loop. To exit press 'CTRL + C'")
    input('Press Enter to begin reading')
    print('Current weight on the scale in grams is: ')
    while True:
    print(hx.get_weight_mean(20), 'g')

except (KeyboardInterrupt, SystemExit):
    print('Bye :)')

finally:
GPIO.cleanup()
i have been playing around for ages and any help would be greatly appreciated Big Grin Big Grin Big Grin
Reply
#2
guizero is just a wrapper around tkinter.
Looking at the code I don't think it adds much to original tkinter
You may be better off using tkinter directly and avoiding the overhead.
when using two modules together, at minimum, code should be written as functions,
classes make interface cleaner and easier.
I don't have a pi zero running at the moment, so can't run the code or I'd give it a go.
Suggest you read up on functions, classes, and tkinter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [subprocess] Why stdout sent to stderr? Winfried 3 468 Jan-26-2024, 07:26 PM
Last Post: snippsat
  Performance options for sys.stdout.writelines dgrunwal 11 3,110 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  changing stdout and stderr Skaperen 4 2,683 Dec-02-2020, 08:58 PM
Last Post: Skaperen
  Normalizing a value from HX711 duckredbeard 4 2,353 Sep-10-2020, 12:00 AM
Last Post: deanhystad
  Issue with HX711 reading duckredbeard 0 2,182 Aug-28-2020, 10:00 AM
Last Post: duckredbeard
  Get stdout of a running process yok0 0 3,020 Aug-20-2020, 10:12 AM
Last Post: yok0
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,896 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  will with sys.stdout as f: close sys.stdout? Skaperen 9 4,599 Nov-03-2019, 07:57 AM
Last Post: Gribouillis
  HX711&matplotlib problems olego 0 1,934 Jul-12-2019, 12:22 PM
Last Post: olego
  Add stdout to text file maxtimbo 3 3,149 Feb-05-2019, 12:53 AM
Last Post: maxtimbo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020