Paso 1: El código
El código de
Crear una nueva carpeta que contendrá todos los archivos
sudo mkdir /home/pi/domo-emmeshop
Crear un nuevo input.html de archivo
sudo nano /home/pi/domo-emmeshop/input.html
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="input-emmeshop.js"></script> <style type="text/css"> .label1 { display: inline !important; vertical-align: 1.0em; } </style> </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-input" > <div class="s-title"><center>Home Automation</center></div> <ul data-role="listview" data-inset="true" > <li> <label for="I00" class="label1"><b>Input 00 </b></label> <select disabled name="I00" id="I00" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> <li> <label for="I01" class="label1"><b>Input 01 </b></label> <select disabled name="I01" id="I01" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> <li> <label for="I02" class="label1"><b>Input 02 </b></label> <select disabled name="I02" id="I02" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> <li> <label for="I03" class="label1"><b>Input 03 </b></label> <select disabled name="I03" id="I03" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> <li> <label for="I04" class="label1"><b>Input 04 </b></label> <select disabled name="I04" id="I04" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> <li> <label for="I05" class="label1"><b>Input 05 </b></label> <select disabled name="I05" id="I05" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> <li> <label for="I06" class="label1"><b>Input 06 </b></label> <select disabled name="I06" id="I06" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> <li> <label for="I07" class="label1"><b>Input 07 </b></label> <select disabled name="I07" id="I07" data-mini="false" data-role="slider"> <option value="0">Open</option> <option value="1">Closed</option> </select> </li> </ul> </div> <div data-theme="a" data-role="footer"> <p align="center"><h2>Emmeshop Electronics</h2></p> </div> </body> </html>
Crear una nueva entrada de archivo-emmeshop.js
sudo nano /home/pi/domo-emmeshop/input-emmeshop.js
con este contenido
$(document).ready(function(){ var jqxhr = $.getJSON('action.php?', function(data) { value_update(data); }) }); function value_update(data) { $.each(data, function (index, value) { $('#I'+index).val(value).slider("refresh"); }); }
Crear un nuevo action.php archivo
sudo nano /home/pi/domo-emmeshop/action.php
con este contenido
<?php $read=shell_exec('sudo python /var/www/input-emmeshop.py'); if($read=="") { echo "Error"; } else { $tempArray=explode("\n",$read); // create array with read values for ($i = 0; $i<8; $i++) { $pin = sprintf('%02s', $i); $myArray[$pin]=intval(str_replace("\n","",$tempArray[$i])); } // creato json string echo json_encode($myArray); } ?>
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 entrada-emmeshop.py
sudo nano /var/www/input-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 arrInState=[0,0,0,0,0,0,0,0] #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 arrInState[int(e.index)]=int(e.state) 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) try: interfaceKit.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) for index in range(len(arrInState)): print ("%i" % arrInState[index]) exit(0)