Python code for handling Events from Digital input

Supporting 2.7 and 3.2+
Post Reply
drc
Phidgetly
Posts: 16
Joined: Sat Nov 15, 2014 8:33 am
Location: Ottawa
Contact:

Python code for handling Events from Digital input

Post by drc »

I'm hoping someone here can share a sample of Python code showing how to handle events from a Digital Input on an interface kit. My application is pretty basic - I want to log time/date when a digital input changes state. I've looked at the sample code ("Interface kit-simple.py") but my Python skills are not sufficient to figure out how to make it work.

Thanks for your help.
pmpaq
Fresh meat
Posts: 4
Joined: Thu Nov 13, 2014 12:51 pm
Contact:

Re: Python code for handling Events from Digital input

Post by pmpaq »

I'm a bit new to both phidgets and python, but have recently thrown myself into it so I'll try at an answer....

The key process handling the change events of the input in the sample code is with the function interfaceKitInputChanged. That function was setup as an event handler in the main program code with:

Code: Select all

    interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged)
So if you setup the pyhton logging module by adding to the module imports near the top of the program:

Code: Select all

import logging
and near the beginning of the main program code with something like...

Code: Select all

    logging.basicConfig(filename='example.log',format='%(asctime)s %(message)s')
Then you should be able to add something like the following into the interfaceKitInputChanged function:

Code: Select all

    logging.info( "Input %i: %s" % ( e.index, e.state))
Hope this helps....
drc
Phidgetly
Posts: 16
Joined: Sat Nov 15, 2014 8:33 am
Location: Ottawa
Contact:

Re: Python code for handling Events from Digital input

Post by drc »

@pmpaq,
Thanks. This is a big help. I made good progress over the weekend, and will post back here when I have something working. I've run into a glitch handling signals (for example, a Ctrl-C, to exit from an endless loop). I'm going to report it in another thread so as not to hijack this one...
drc
Phidgetly
Posts: 16
Joined: Sat Nov 15, 2014 8:33 am
Location: Ottawa
Contact:

Re: Python code for handling Events from Digital input

Post by drc »

@freshmeat,
Attached is the little Python program I wrote (my first).
It monitors the operation of a dual-stage furnace and records outdoor temperature by reading 2 digital inputs and 1 analog input from a Phidgets I/O card.

The program is very simple, and is based on the Phidgets InterfaceKit sample from the website. Instead of using event handlers (i.e., interrupt-driven operation), I structured the program as an endless loop (while True:…) which sleeps for 60 seconds, checks the two inputs, logs any changes, then goes back to sleep.

The only problem I ran into was getting a ctrl-C to cleanly break out of the endless loop so the log files could be closed, etc. Ms. Google indicated I could do this using signals, but my first attempts were unsuccessful. I reached out to Phidgets for help. Patrick there recommended putting the signal.signal call (which hooks the SIGINT ctrl-C handler) after the call to waitForAttach. Initially, I had positioned it up near the top of the program, but that didn’t work.

I always intended to go back and re-write the program using the event handlers, but never did. The program works fine - it's been running 7x24 for many many months on an old Mac mini.

Good luck with your Phidgets project!
Attachments
FurnaceMonitor.txt
Python program to monitor two digital inputs and one analog input (temp sensor).
(6.96 KiB) Downloaded 440 times
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests