element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Raspberry Pi - Network Spy Energy Saver
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Matt
  • Date Created: 27 Mar 2015 10:22 AM Date Created
  • Views 2152 views
  • Likes 8 likes
  • Comments 8 comments
  • rpibeginner
  • homeautomation
  • energenie
  • home_automation
Related
Recommended

Raspberry Pi - Network Spy Energy Saver

Matt
Matt
27 Mar 2015

The subwoofer we had in our home cinema setup died a few weeks ago, so I did my research and found a nice replacement. The only thing I didn't spot was the fact it never goes into standby if there's no signal (unlike the old one). I tried using some eco plugs which turn off peripherals when the TV was turned off.. but it learns the TV remote signals, and completely turns the TV off as well.. meaning that you have to hit the power button twice to turn the TV back on; that doesn't work well with the Harmony all-in-one remote we use.

 

What I decided to do was buy an Energenie socket, which can be switched on/off wirelessly from a Raspberry Pi. The kitThe kit comes with 2 sockets and a transmitter to attach to the GPIO headers on the Pi, and costs about £20.

 

In my subwoofer scenario I basically want it switched on when the TV is on, and off when the TV is off. The TV has a Chromecast plugged into it which is visible on my local network. If you've got a Smart TV on your network, maybe that'll be visible in the same way. So when the TV/Chromecast appear on the network, we know to switch on the power socket.

 

image

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Here's the small Energenie transmitter attached to the GPIO headers on my B+.. it's pretty tiny and the case I've got still fits over the top. Notice the small hole where you can attach an aerial.. if you want extra range, then you'll need to solder one on.. I added a 135mm wire, since the range I got out of it just wasn't enough to get from the dining room cupboard to the living room.

 

image

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

To put this together, we can use the Raspberry Pi Network Spy code I wrote in my previous blog posts;

 

Raspberry Pi Network Spy - Part 1 - Initial Setup + d/b Schema

Raspberry Pi Network Spy - Part 2 - D/b Setup + PHP for the scanner

 

All we need is a new PHP page that'll call one of the functions we've already written.. we need a list of the MAC addresses that are currently visible on the network, then check whether the Chromecast is there. Once we know whether the TV/Chromecast is on or off, we then call a Python script that will turn the Energenie socket on/off.

 

Here's the PHP;

 

arp-chromecast.php

 

<?php
    include("_variables.php");
    include("_functions.php");
    include("_db.php");

    function writeStatus($file, $currentstates, $state) {
        $file = fopen("/var/www/arp-chromecast-status-".$file.".txt","w");
        echo fwrite($file, $state.$currentstates);
        fclose($file);
    }
    
    function getStates($file) {
        $filePath = "/var/www/arp-chromecast-status-".$file.".txt";
        $line = "";
        
        if (file_exists($filePath)) {
            $file = fopen($filePath,"r");
            while(!feof($file)){
                $line = fgets($file);
            }
            fclose($file);
        }
        
        return $line;
    }

    function deleteStates($file) {
        $filePath = "/var/www/arp-chromecast-status-".$file.".txt";
        unlink($filePath);
    }
    
    $mac_to_look_for_1 = "6c:ff:ff:ff:ff:8a";
    
    $ret = callArp();
    $arpEntries = processArpReturn($ret);
    $arpMacAddresses = getMacAddressesFromArpEntries($arpEntries);

    $switchedon_1 = false;
    foreach ($arpMacAddresses as $mac) {
        if ($mac==$mac_to_look_for_1) $switchedon_1=true;
    }

    if ($switchedon_1) {
        $states = getStates("1");
        writeStatus("1", $states, "1");
        print ("Switching socket 1 on");
        exec("python /var/www/callenergenie.py on 1");
    } else {
        $states = getStates("1");
        writeStatus("1", $states, "0");
        
        // Has it been off for 3 cycles/minutes? If so, turn the socket off
        $minues_before_off = str_repeat("0", 3);
        if (strcmp(substr($states, 0, strlen($minues_before_off)), $minues_before_off)==0) {
            deleteStates("1"); // We can get rid of the file to keep it's size down
            print ("Switching socket 1 off");
            exec("python /var/www/callenergenie.py off 1");
        }
    }
    
?>

 

The Python script could follow the Energenie example script, but there's actually an even more simple Python package which I've used in this project. To install it I did the following;

 

sudo apt-get install python-pip
sudo pip install energenie

 

Then I wrote this helper Python script (which we'll call from PHP) that accepts a couple of parameters, like this callenergenie.py [on|off] [switch_number];

 

callenergenie.py

 

from energenie import switch_on, switch_off
import sys

if len(sys.argv)==1:
    print 'Please specify arguments like this callenergenie.py [on|off] [switch_number]'
    print 'eg. callenergenie.py on 1'
    print 'eg. callenergenie.py off 2'
else:
    on_or_off = sys.argv[1]
    which_switch = int(sys.argv[2])

    if (on_or_off=='on'):
        print 'Switching on ', which_switch
        switch_on(which_switch)

    if (on_or_off=='off'):
        print 'Switching off ', which_switch
        switch_off(which_switch)

 

Now that we've written the PHP & Python, all we need to do is run the PHP every minute to scan the network and do the switching. We'll do this using another cron job;

 

crontab -e

 

*/1 * * * * sudo /usr/bin/php /var/www/arp-chromecast.php

  • Sign in to reply

Top Comments

  • shabaz
    shabaz over 10 years ago +1
    Hi Matt, This is a really great idea! Very effective : ) Very neat trick to know when the TV is on by using the Chromecast device. Slightly related to your network scan, I hope to sometime consistently…
  • mcb1
    mcb1 over 10 years ago +1
    Neat idea Matt. This searching for things has some other benefits as well. Thanks for sharing Mark
  • Matt
    Matt over 10 years ago +1
    I've updated the PHP code after a few problems we had at the weekend.. it turns out that the arp scan sometimes doesn't see a device, i.e. the Chromecast dropped off the network momentarily, which ended…
  • Matt
    Matt over 10 years ago in reply to Former Member

    The Pi uses a couple of watts, and the speaker is using 11w. The Pi was on all the time anyway.. being an FTP server for our webcams (motion detection), and also turns another socket on/off if my mobile is at home.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 10 years ago

    That is cool and all, but doesn't the Pi use more energy being on monitoring all the time than the speaker would in the first place?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 10 years ago
    Cool Project , new to this technology and very interested at the age of 63
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Matt
    Matt over 10 years ago in reply to mcb1

    No, the arp scan shouldn't wake it up, it'll just spot it on the network when it wakes up and does it's own thing. Yeah, I think detecting whether you're at home via your mobile has a whole host of uses. The 30 minute delay could be problematic for, say, arming home security, but I guess you could fine-tune that.. or turn the wifi power save off (if there is such a thing?) on the phone so that it's constantly connected.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 10 years ago in reply to Matt

    Nice adjustment.

    Does pinging your phone get a response/wake it up.

    The thought is to confirm it really isn't there.

     

    This has some great possibilities.

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube