Paso 3: Phidgets
Phidgets
Ahora que la base de datos está listo instalamos el driver y la biblioteca Phidgets
tar zxvf libphidget.tar.gz
cd libphidget-2.1.8.20150410/
./configure
make
sudo make install
wget http://www.phidgets.com/downloads/libraries/PhidgetsPython.zip
sudo unzip PhidgetsPython.zip
cd PhidgetsPython/
sudo python setup.py install
#!/usr/bin/env python #Basic imports from __future__ import division from ctypes import * import sys import math import random import os import time import datetime #Phidget specific imports from Phidgets.PhidgetException import * from Phidgets.Events.Events import * from Phidgets.Devices.InterfaceKit import * #Mysql imports import MySQLdb #Create an interfacekit object try: interfaceKit = InterfaceKit() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) #Event Handler Callback Functions def interfaceKitAttached(e): attached = e.device def interfaceKitDetached(e): detached = e.device def interfaceKitError(e): try: source = e.device print("InterfaceKit %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) def interfaceKitInputChanged(e): source = e.device def interfaceKitSensorChanged(e): source = e.device def interfaceKitOutputChanged(e): source = e.device def logSensor(): interfaceKit.setRatiometric(False) interfaceKit.setSensorChangeTrigger(0, 3) for i in range(0,8): senValue=interfaceKit.getSensorRawValue(i) insert_to_db(i,senValue); def insert_to_db(id_sensor, value_sensor): db = MySQLdb.connect("localhost","root","raspberry","sensor_log" ) cursor = db.cursor() time_sensor = time.time() # Prepare SQL query to INSERT a record into the database. sql = "INSERT INTO table_sensor_log(timestamp, sensor_id, sensor_value) VALUES ('%d', '%d', '%d' )" % (time_sensor,id_sensor ,value_sensor) try: cursor.execute(sql) db.commit() except: db.rollback() db.close() #Main Program Code try: interfaceKit.setOnAttachHandler(interfaceKitAttached) interfaceKit.setOnDetachHandler(interfaceKitDetached) interfaceKit.setOnErrorhandler(interfaceKitError) interfaceKit.setOnInputChangeHandler(interfaceKitInputChanged) interfaceKit.setOnOutputChangeHandler(interfaceKitOutputChanged) interfaceKit.setOnSensorChangeHandler(interfaceKitSensorChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) try: interfaceKit.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) try: interfaceKit.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) logSensor() try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1)
sudo sudo python /home/pi/sensor_logger/ifkit.py
Tras conectarse la 1018_2 - PhidgetInterfaceKit 8/8/8 el puerto usb de frambuesa y algunos sensores a la entrada analógica de 1018