shidalgo11
Posts: 3
Joined: Mon Mar 13, 2017 10:51 am

Real-time graphing

Mon Mar 13, 2017 11:00 am

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! :)

davef
Posts: 64
Joined: Wed Feb 20, 2013 8:39 pm
Location: Christchurch, NZ

Re: Real-time graphing

Mon Mar 13, 2017 7:13 pm

Search for rrdtool. There are some RaspberryPi tutorials using it.

User avatar
Gavinmc42
Posts: 5155
Joined: Wed Aug 28, 2013 3:31 am

Re: Real-time graphing

Mon Mar 13, 2017 9:55 pm

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.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

jogl
Posts: 93
Joined: Sun Mar 13, 2016 12:02 pm
Location: SW Ontario Canada

Re: Real-time graphing

Mon Mar 13, 2017 10:55 pm

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/

timrowledge
Posts: 1365
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: Real-time graphing

Mon Mar 13, 2017 11:40 pm

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.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

shidalgo11
Posts: 3
Joined: Mon Mar 13, 2017 10:51 am

Re: Real-time graphing

Wed Mar 15, 2017 3:37 pm

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 :?:

User avatar
scruss
Posts: 3819
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Real-time graphing

Wed Mar 15, 2017 4:37 pm

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.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

shidalgo11
Posts: 3
Joined: Mon Mar 13, 2017 10:51 am

Re: Real-time graphing

Wed Mar 15, 2017 4:50 pm

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?

jogl
Posts: 93
Joined: Sun Mar 13, 2016 12:02 pm
Location: SW Ontario Canada

Re: Real-time graphing

Wed Mar 15, 2017 5:33 pm

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.
Last edited by jogl on Wed Mar 15, 2017 5:36 pm, edited 1 time in total.

MarkTF
Posts: 318
Joined: Tue Mar 03, 2015 4:59 pm

Re: Real-time graphing

Wed Mar 15, 2017 5:35 pm

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.

jogl
Posts: 93
Joined: Sun Mar 13, 2016 12:02 pm
Location: SW Ontario Canada

Re: Real-time graphing

Wed Mar 15, 2017 6:01 pm

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.

Return to “General discussion”