Paso 1: El código
Crear una nueva carpeta que contendrá todos los archivos
sudo mkdir /home/pi/domo-emmeshop
Crear un nuevo fichero index.php
sudo nano /home/pi/domo-emmeshop/index.php
con este contenido
<!DOCTYPE html> <html> <head> <title>EmmeShop Domotics</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <script src="domo-emmeshop.js"></script> </head> <body> <div data-theme="a" data-role="header"> <p align="center"><img src="http://www.emmeshop.eu/blog/themes/bartik/Logo-Emmeshop.png" alt="Home"></p> </div> <div class="content-output" > <div class="s-title"><center>Home Automation</center></div> <ul data-role="listview" data-inset="true" > <li> <button class="ui-btn ui-corner-all" id="0">Output 0</button> </li> <li> <button class="ui-btn ui-corner-all" id="1">Output 1</button> </li> <li> <button class="ui-btn ui-corner-all" id="2">Output 2</button> </li> <li> <button class="ui-btn ui-corner-all" id="3">Output 3</button> </li> <li> <button class="ui-btn ui-corner-all" id="4">Output 4</button> </li> <li> <button class="ui-btn ui-corner-all" id="5">Output 5</button> </li> <li> <button class="ui-btn ui-corner-all" id="6">Output 6</button> </li> <li> <button class="ui-btn ui-corner-all" id="7">Output 7</button> </li> </ul> </div> <div data-theme="a" data-role="footer"> <p align="center"><h2>Emmeshop Electronics</h2></p> </div> </body> </html>
Crear un nuevo archivo domo-emmeshop.js
sudo nano /home/pi/domo-emmeshop/domo-emmeshop.js
con este contenido
$(document).ready(function(){ $("button").click(function(){ $.post("action.php", { outId:(this.id) }, function(data,status){ //alert("Data: " + data + "\nStatus: " + status); }); }); });
Crear un nuevo action.php archivo
sudo nano /home/pi/domo-emmeshop/action.php
con este contenido
<?php $gpin="0"; if (isset($_POST['outId'])) { $gpin=$_POST['outId']; } shell_exec('sudo python /var/www/domo-emmeshop.py'.' '.$gpin); ?>
Hacer un enlace de domo-emmeshop de /home/pi/domo-emmeshop a www/domo-emmeshop de httpd.
sudo ln -s /home/pi/domo-emmeshop /var/www/domo-emmeshop
Por último, crear un archivo de python domo-emmeshop.py
sudo nano /var/www/domo-emmeshop.py
con este contenido
#!/usr/bin/env python #Basic imports from ctypes import * import sys import random import os #Phidget specific imports from Phidgets.PhidgetException import PhidgetErrorCodes, PhidgetException from Phidgets.Events.Events import AttachEventArgs, DetachEventArgs, ErrorEventArgs, InputChangeEventArgs, OutputChangeEventArgs, SensorChangeEventArgs from Phidgets.Devices.InterfaceKit import InterfaceKit outId = int(sys.argv[1]) #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 print("InterfaceKit %i Detached!" % (detached.getSerialNum())) 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 #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) else: if interfaceKit.getOutputState(outId)==1: interfaceKit.setOutputState(outId,0) else: interfaceKit.setOutputState(outId,1) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) exit(0)