Stap 1: De code
De code
Maak een nieuwe map waarin u alle bestanden
sudo mkdir /home/pi/domo-emmeshop
Maak een nieuw bestand sensor.html
sudo nano /home/pi/domo-emmeshop/sensor.html
met deze inhoud
<!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="sensor-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><b>SEN 00</b></label> <span class="inputvalue" name="S00" id="S00">0</span> </li> <li> <label><b>SEN 01</b></label> <span class="inputvalue" name="S01" id="S01">0</span> </li> <li> <label><b>SEN 02</b></label> <span class="inputvalue" name="S02" id="S02">0</span> </li> <li> <label><b>SEN 03</b></label> <span class="inputvalue" name="S03" id="S03">0</span> </li> <li> <label><b>SEN 04</b></label> <span class="inputvalue" name="S04" id="S04">0</span> </li> <li> <label><b>SEN 05</b></label> <span class="inputvalue" name="S05" id="S05">0</span> </li> <li> <label><b>SEN 06</b></label> <span class="inputvalue" name="S06" id="S06">0</span> </li> <li> <label><b>SEN 07</b></label> <span class="inputvalue" name="S07" id="S07">0</span> </li> </ul> </div> <div data-theme="a" data-role="footer"> <p align="center"><h2>Emmeshop Electronics</h2></p> </div> </body> </html>
Maak een nieuw bestand sensor-emmeshop.js
sudo nano /home/pi/domo-emmeshop/sensor-emmeshop.js
met deze inhoud
$(document).ready(function(){ var jqxhr = $.getJSON('action.php?', function(data) { value_update(data); }) }); function value_update(data) { $.each(data, function (index, value) { switch(index) { case '00': var temperature= (parseFloat(value) * 0.22222)-61.11; $('#S'+index).text(temperature.toFixed(2)+' °C'); break; case '01': var humidity= (parseFloat(value) * 0.1906)-40.2; $('#S'+index).text(humidity.toFixed(2)+' %'); break; case '02': var luminosity= (parseFloat(value) * 1.15269)+40.061; $('#S'+index).text(luminosity.toFixed(2)+' lux'); break; } }); }
Maak een nieuw bestand action.php
sudo nano /home/pi/domo-emmeshop/action.php
met deze inhoud
<?php $read=shell_exec('sudo python /var/www/domo-emmeshop/sensor-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])); } // create json string echo json_encode($myArray); } ?>
Maak een link van domo-emmeshop van /home/pi/domo-emmeshop naar /var/ www/domo-emmeshop.
sudo ln -s /home/pi/domo-emmeshop /var/www/domo-emmeshop
Tot slot creëren een python bestand sensor-emmeshop.py
sudo nano /var/www/domo-emmeshop/sensor-emmeshop.py
met deze inhoud
#!/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 arrSenState=[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 def interfaceKitSensorChanged(e): source = e.device arrSenState[int(e.index)]=int(e.value) 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(arrSenState)): print ("%i" % arrSenState[index]) exit(0)