Jul-12-2019, 12:22 PM
Hi!
I'm making a project in python on rpi 3b+ for a machine I've build in my laboratory and have some problems with the code. :cry:
The aim is to collect data from hx711 wich is connected to load cell in a csv file and to show real time plot on rpi screen.
I'm going to connect 4 hx711 to save and plot data from 4 load cells.
Now i'm trying to make work only 1 cell.
Here is the code:
as examples i took this code to check cpu that works perfectly:
I'm a scientist and more mechanic than a programmer.
I'm making a project in python on rpi 3b+ for a machine I've build in my laboratory and have some problems with the code. :cry:
The aim is to collect data from hx711 wich is connected to load cell in a csv file and to show real time plot on rpi screen.
I'm going to connect 4 hx711 to save and plot data from 4 load cells.
Now i'm trying to make work only 1 cell.
Here is the code:
import sys import csv import time from datetime import datetime import RPi.GPIO as GPIO from hx711 import HX711 import matplotlib.pyplot as plt csvfile = datetime.now().strftime('Base-%Y-%m-%d-%H-%M.csv') fieldnames = ["time1","force1"] with open(csvfile, "a")as output: writer = csv.DictWriter(output, fieldnames=fieldnames) writer.writeheader() # choose pins on rpi (BCM5 and BCM6) hx = HX711(dout=5, pd_sck=6) # HOW TO CALCULATE THE REFFERENCE UNIT ######################################### # To set the reference unit to 1. # Call get_weight before and after putting 1000g weight on your sensor. # Divide difference with grams (1000g) and use it as refference unit. hx.setReferenceUnit(1) hx.reset() hx.tare() fig = plt.figure() ax = fig.add_subplot(111) fig.show() i = 0 x, y = [], [] while True: try: force1 = "{0: 4.1f}".format(hx.getWeight()) time1 = datetime.now().strftime('%H:%M:%S.%f') print(force1, time1) x.append(i) ax.plot(x, force1, color='b') fig.canvas.draw() ax.set_xlim(left=max(0, i-10), right=i+10) ax.set_ylim([-100,100]) i += 1 data = [force1, time1] with open(csvfile, "a")as output: writer = csv.DictWriter(output, fieldnames=fieldnames) info = { "time1": time1, "force1": force1 } writer.writerow(info) writer = csv.writer(output, delimiter=",", lineterminator = '\n') time.sleep(0.01) # update script every 60 seconds except (KeyboardInterrupt, SystemExit): GPIO.cleanup() sys.exit()here is a problem that it plots a straght line wich only goes up, but
print(force1, time1)prints right values of force1
as examples i took this code to check cpu that works perfectly:
import time import matplotlib.pyplot as plt import psutil fig = plt.figure() ax = fig.add_subplot(111) fig.show() i = 0 x, y = [], [] while True: x.append(i) y.append(psutil.cpu_percent()) ax.plot(x, y, color='b') fig.canvas.draw() ax.set_xlim(left=max(0, i-10), right=i+10) ax.set_ylim([-100,100]) print(psutil.cpu_percent()) time.sleep(0.1) i += 1 plt.close()Could you please help me to solve the problem?
I'm a scientist and more mechanic than a programmer.