Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 HX711&matplotlib problems
#1
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:

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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Matplotlib : linewidth parameters Zopost 0 149 Aug-16-2019, 07:07 PM
Last Post: Zopost
  hatching not working properly with matplotlib Staph 3 100 Jul-28-2019, 07:17 AM
Last Post: ThomasL
  Having problems using 'or' in a 'if' statement? umut3806 2 224 Jul-21-2019, 11:33 PM
Last Post: umut3806
  Matplotlib problem ift38375 0 111 Jul-02-2019, 02:55 PM
Last Post: ift38375
  Matplotlib contour no data coordinates b4rtt 1 212 Jun-18-2019, 09:36 AM
Last Post: b4rtt
  Matplotlib ImportError robertinoc 4 317 Jun-14-2019, 09:11 AM
Last Post: snippsat
  Matplotlib animation problem Potatoez 1 346 May-21-2019, 03:58 PM
Last Post: Potatoez
  MatplotLib Sliders punksnotdead 4 302 May-18-2019, 08:45 PM
Last Post: punksnotdead
  Problems with "re.fullmatch" AlanT 8 519 May-18-2019, 04:45 PM
Last Post: AlanT
  Animation using matplotlib query JohnDoe 0 159 May-09-2019, 08:07 PM
Last Post: JohnDoe

Forum Jump:


Users browsing this thread: 1 Guest(s)