Page 1 of 1

Real-time graphing

Posted: Mon Mar 13, 2017 11:00 am
by shidalgo11
Firstly I will start by saying I am very new to this little micro-processor, so I am only starting to figure out how to go about doing little things with it.
I currently have an ADS1115 analogue-to-digital converter plugged into my raspberry pi 2 to read in input signal from a strain-gauge sensor. I have managed to get the readings from the "channel 0" of my ADC no problem, but what I would like to do is to plot a real time graph with strain against time. I have been looking at gnuplot and some tutorials online but I am struggling to wrap my head round using this tool. If there is anyone out there who has done this before and could give me a hand that would be greatly appreciated! :)

Re: Real-time graphing

Posted: Mon Mar 13, 2017 7:13 pm
by davef
Search for rrdtool. There are some RaspberryPi tutorials using it.

Re: Real-time graphing

Posted: Mon Mar 13, 2017 9:55 pm
by Gavinmc42
gnuplot works on Pi's but is standard Linux so you don't need to google Pi specific stuff.
I got it going by using Linux tuts.
Many ways to plot mostly with massive dependencies.

Mathematica plots too.
I have used pygame to make my own plotter. there is a gnuplot.py interface too
Web server = dygraphs to plot csv

These days I use baremetal on Ultibo and diy plotting in a graphic console, basic stuff mostly for testing.

Re: Real-time graphing

Posted: Mon Mar 13, 2017 10:55 pm
by jogl
I'm currently experimenting with Matplotlib and Python. If you know some python that would be fairly easy to set up.

I like this series of tutorials, this is chapter 16, the live graph tutorial:

https://pythonprogramming.net/live-grap ... -tutorial/

Re: Real-time graphing

Posted: Mon Mar 13, 2017 11:40 pm
by timrowledge
Or you could use Squeak with PlotMorph; that's what I'm using along with my MQTT client to read weather data and indoor environment data and then display it.

Re: Real-time graphing

Posted: Wed Mar 15, 2017 3:37 pm
by shidalgo11
jogl wrote:I'm currently experimenting with Matplotlib and Python. If you know some python that would be fairly easy to set up.

I like this series of tutorials, this is chapter 16, the live graph tutorial:

https://pythonprogramming.net/live-grap ... -tutorial/
Sorry for the late reply!
Thank you for this post, I had to look elsewhere how to install matplot, but I managed to get a graph with pre-recorded data. However, I am trying to modify the data I am graphing as the graph is plotted, but once again I have hit a wall!
What i am trying is the following:

while (1):
if GPIO.input(21):
file = open("channel0.txt","a")
channel1 = adc.read_adc(0, gain = GAIN)
time.sleep (0.3)
file.write(str(channel1)), file.write(","), file.write(str(tme))
# this is just the code to record my values from the ADC i am using when an external pushbutton is pressed of course. "tme" and "gain" have been set before the while loop, and then:
def animate(i):
graph_data = open("channel0.txt", "r").read()
lines = graph_data.split('\n')
xs = [ ]
ys = [ ]
for line in lines:
if len(line) > 1:
y, x = line.split (",")
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval = 500)
plt.show()

But I am not getting any luck :?:

Re: Real-time graphing

Posted: Wed Mar 15, 2017 4:37 pm
by scruss
please post Python code in a Code block: all indentation and code sense is lost otherwise.

I've seen Electron used as very effective realtime graphing and control UI, but it introduces a whole different technology stack.

Re: Real-time graphing

Posted: Wed Mar 15, 2017 4:50 pm
by shidalgo11

Code: Select all

while (1):
 if GPIO.input(21):
  file = open("channel0.txt","a")
  channel1 = adc.read_adc(0, gain = GAIN)
  time.sleep (0.3)
  file.write(str(channel1)), file.write(","), file.write(str(tme))
# this is just the code to record my values from the ADC i am using when an external pushbutton is pressed of course. "tme" and "gain" have been set before the while loop, and then:
 def animate(i):
  graph_data = open("channel0.txt", "r").read()
  lines = graph_data.split('\n')
  xs = [ ]
  ys = [ ]
  for line in lines:
    if len(line) > 1:
       y, x = line.split (",")
       xs.append(x)
       ys.append(y)
   ax1.clear()
   ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval = 500)
plt.show()
My bad, is this any better?

Re: Real-time graphing

Posted: Wed Mar 15, 2017 5:33 pm
by jogl
Do you have fig defined? This is working for me:

Code: Select all

# sqlgraph2.py
import MySQLdb
import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.animation as animation
from matplotlib import style
style.use('ggplot')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)


def read_from_db():
     bla bla bla 

def animate(i):
        
    read_from_db()
    plt.plot(dtg,b)
    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d   %H:%M'))
    plt.gcf().autofmt_xdate()
    plt.xlabel('Date and/or Time')
    plt.ylabel('Temp (*F)')
    plt.title('Shack Temperature')
    plt.legend()
   
ani = animation.FuncAnimation(fig, animate, interval=10000)
plt.show()
I erased all of the (poor and lengthy) coding that reads the db and converts the data into dtg and b.

You may want to try splitting your data collecting out from your graphing like I did. I remember doing it, and there was a reason, but I have forgotten the reason.

Re: Real-time graphing

Posted: Wed Mar 15, 2017 5:35 pm
by MarkTF
For the code you've shown, "plot.show()" and "ani = animation.FuncAnimation(fig, animate, interval = 500)" are outside the infinite "while" loop and will never be executed.

Re: Real-time graphing

Posted: Wed Mar 15, 2017 6:01 pm
by jogl
For the code you've shown, "plot.show()" and "ani = animation.FuncAnimation(fig, animate, interval = 500)" are outside the infinite "while" loop and will never be executed.
Yes, I didn't notice that. Further if you are cutting and pasting the Sentdex code (like I did) you don't need the while loop. Do your data reading in your animate function and the matplotlib.animation will do the looping.