Page 1 of 1

Trying to record pH

Posted: Tue Apr 27, 2021 1:10 pm
by jgassens
Hello, I'm trying to record pH using the 1130_PH adapter and I don't really know python super well and it isn't working. Wondered if anyone could take a look and let me know where I've gone wrong:

Code: Select all

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


	#Create your Phidget channels
	voltageInput0 = VoltageInput()

	#Set addressing parameters to specify which channel to open (if any)
	voltageInput0.setIsHubPortDevice(True)
	voltageInput0.setHubPort(0)

	#Open your Phidgets and wait for attachment
	voltageInput0.openWaitForAttachment(5000)

	#Do stuff with your Phidgets here or in your event handlers.
	#Set the sensor type to match the analog sensor you are using after opening the Phidget
	voltageInput0.setSensorType(VoltageSensorType.SENSOR_TYPE_1130_PH)
	#Other valid sensor types for this sensor include: SENSOR_TYPE_1130_ORP
while (True):
    #Update user
    print("Logging data...")
    
    #Write data to file in CSV format
    with open ('phidgets_pH.csv','a') as datafile:
        datafile.write(str(voltageInput0.getVoltagee()) + "\n")
    
    #Increment count
    count += 1
    
    #If 10 data points have been recorded, close file and exit program
    if(count == 10):
        print("Logging complete, exiting program")
        exit()
The error I am getting is:

Code: Select all

Traceback (most recent call last):
  File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast .py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "/Users/user/Downloads/example .py", line 7
    voltageInput0 = VoltageInput()
    ^
IndentationError: unexpected indent

Re: Trying to record pH

Posted: Tue Apr 27, 2021 2:37 pm
by mparadis
Python is rather particular about indentation, so it's upset that the first section of your code (everything before the while loop) is indented. It should only be indented if it's part of a function or loop or 'if' statement.

There's also a typo in getVoltage() and you'll need to declare 'count' (e.g. count=0) before using it in an operation.

If you want to log in pH instead of voltage, you should use voltageInput0.getSensorValue() instead of getVoltage().

You may eventually want to use events instead of polling in a loop. Here's a code sample that uses events:

Code: Select all

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

def onSensorChange(self, sensorValue, sensorUnit):
	print("SensorValue: " + str(sensorValue))
	print("SensorUnit: " + str(sensorUnit.symbol))
	print("----------")

def main():
	voltageInput0 = VoltageInput()

	voltageInput0.setIsHubPortDevice(True)
	voltageInput0.setHubPort(0)

	voltageInput0.setOnSensorChangeHandler(onSensorChange)

	voltageInput0.openWaitForAttachment(5000)

	voltageInput0.setSensorType(VoltageSensorType.SENSOR_TYPE_1130_PH)
	#Other valid sensor types for this sensor include: SENSOR_TYPE_1130_ORP

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

	voltageInput0.close()

main()