How to plot an external device output into a graph?

105 Views Asked by At

I have a Phidgets differential pressure sensor device connected to Python and used a template code to output the pressure. I've got it to work and it's outputting pressure values into the console. However, I'm looking to graph the output values and make a linear plot vs. time. Does anyone know how to do this? I've attached the code I'm using.

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time

def onSensorChange(self, sensorValue, sensorUnit):
    print("SensorValue: " + str(sensorValue))

def main():
    voltageRatioInput4 = VoltageRatioInput()

    voltageRatioInput4.setChannel(4)

    voltageRatioInput4.setOnSensorChangeHandler(onSensorChange)

    voltageRatioInput4.openWaitForAttachment(5000)

    voltageRatioInput4.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1139)

    try:
        input("Press Enter to Stop\n")
    except (Exception, KeyboardInterrupt):
        pass

    voltageRatioInput4.close()

main()

It's outputting sensorValue!

SensorValue: 0.223

That's what I want. However, it's not saving it into some form of variable so that I can plot it against time. Any attempts to get the value results in

NameError: name 'sensorValue' is not defined

Does anyone know how to get the values from sensorValue into an array variable?

Always lurked around stackoverflow when I had MATLAB homework. Found my way back here needing help again for Python homework, hehe. Any help is appreciated!

1

There are 1 best solutions below

2
Mariusz Olszowski On BEST ANSWER

You should use some variable to keep the history. Since variables created inside function exsists only inside the function - you can use variable from outside, using global.

...

history = []

def onSensorChange(self, sensorValue, sensorUnit):
    global history
    history.append( sensorValue )
    print("SensorValue: " + str(sensorValue))
    print("History of values: ", history)

...

Look how history changes when onSensorChange function is called.