Introduction: Home Automation Raspberry and Phidgets Part 3
This is the third article in Home automation with Phidgets and Raspberry.
In previous articles we saw how to interact with the digital outputs and digital inputs.
Now we will see how to read the values of some sensors directly on your smartphone or tablet.
You need:
a Raspberry Pi B or B+, with installed Web Server and the Phidgets library or you can use our Raspberry Pi - SBC, MODEL B+, 512M MicroSD 8GB Phidgets ready
a 1018_2 - PhidgetInterfaceKit 8/8/8
some sensors, we use a 1125_0 - Humidity/Temperature Sensor and a 1142_0 - Light Sensor 1000 lux.
Connect the 1018 to a USB port on Raspberry, the 1125_0 Temperature to analog pin 0, 1125_0 Umidity to analog pin 1 and 1142_0 to analog pin 2.
Step 1: The Code
The code
Create a new folder that will contain all the files
sudo mkdir /home/pi/domo-emmeshop
Create a new file sensor.html
sudo nano /home/pi/domo-emmeshop/sensor.html
with this content
<!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>
Create a new file sensor-emmeshop.js
sudo nano /home/pi/domo-emmeshop/sensor-emmeshop.js
with this content
$(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; } }); }
Create a new file action.php
sudo nano /home/pi/domo-emmeshop/action.php
with this content
<?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); } ?>
Make a link of domo-emmeshop from /home/pi/domo-emmeshop to /var/www/domo-emmeshop .
sudo ln -s /home/pi/domo-emmeshop /var/www/domo-emmeshop
Finally, create a python file sensor-emmeshop.py
sudo nano /var/www/domo-emmeshop/sensor-emmeshop.py
with this content
#!/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)
Step 2: How It Work
The sensor.html file creates a web page based on HTML, jQuery and
AJAX; when loaded, reads the values of the sensors connected to the 1018 and displays them.
The displayed values are calculated with the formulas provided by Phidgets for each sensor.
The application can be implemented with the addition of other sensors, perhaps to control the consumption of appliances, with voltage or current sensors...
Follow us on social to stay informed.
Discussions
6 years ago on Introduction
Oh! This is really great. I can't wait to see what's next, perhaps having the output respond with certain sensor changes? Looks like fun.