Introduction: Home Automation Raspberry and Phidgets

In previous tutorials we used Raspberry Pi to drive Phidgets InterfaceKit. We have also set up a web server on Raspberry.

In this tutorial we will use the information gathered to create an Home Automation System managed with a Web Application.
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

8 led.

Connect the 1018 to a USB port on Raspberry; the leds to the digital outputs of the 1018 and gnd.

Step 1: The Code

Create a new folder that will contain all the files

sudo mkdir /home/pi/domo-emmeshop

Create a new file index.php

sudo nano /home/pi/domo-emmeshop/index.php

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="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>

Create a new file domo-emmeshop.js

sudo nano /home/pi/domo-emmeshop/domo-emmeshop.js

with this content

$(document).ready(function(){
  $("button").click(function(){
    $.post("action.php",
    {
      outId:(this.id)
    },
    function(data,status){
      //alert("Data: " + data + "\nStatus: " + status);
    });
  });
});

Create a new file action.php

sudo nano /home/pi/domo-emmeshop/action.php

with this content

<?php
$gpin="0";
if (isset($_POST['outId']))
{
    $gpin=$_POST['outId'];
}
shell_exec('sudo python /var/www/domo-emmeshop.py'.' '.$gpin);
?>

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 domo-emmeshop.py

sudo nano /var/www/domo-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
 
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)

Step 2: How Does This Work

The index.php file creates a web page based on HTML, jQuery and AJAX with 8 buttons .

When a button is pressed the id of the button is passed as a parameter to the file domo-emmeshop.py, which is responsible for toggle the status of the corresponding output.

If the led was turned off it turn on otherwise turn off.

To run the domo-emmeshop.py file must be granted permissions to www-data.
Edit the file sudoers

sudo nano /etc/sudoers

and add this line at end of file

www-data ALL=(ALL) NOPASSWD: ALL

Step 3: Test the Application

Open your browser with Raspberry Pi address, in this case http://192.168.0.166/domo-emmeshop/ and press a button, the relative led turn on or turn off.

In this tutorial we turned on the led but with some relays connected to the digital outputs we could remotely control the heating, the lights in the apartment, or the coffee machine.

Follow us on social to stay informed.

www.emmeshop.eu

Comments

author
andres juan (author)2015-08-22

I like this

author
seamster (author)2014-11-03

Very nice!

author
emmeshop (author)seamster2014-11-03

Thank you for your feedback!