The Fireplace Hack – Revisited

3

Back in February 2013 I wrote an article entitled “Hacking a Fireplace”. In that article I wrote about how I used an Arduino to create an interface that would allow me to directly control my fireplaces from my home automation system. That solution worked great and I was successful in running two fireplaces from the single controller for a year with no issues. What I didn’t like though was that the completed project was bulky and I wanted something much smaller that I could put under the fireplace, thus out of sight! To accomplish this, I decided to buy a couple of Model B Raspberry Pi’s to replace the Arduino.

Raspberry Pi

Raspberry Pi

Following is how I set them up.

WARNING: Let me interject here that one should exercise EXTREME caution before deciding to implement any kind of automated home controls such as those I will outline below! If you’re not comfortable dealing with electrical circuits then STOP here. Also be aware that your fireplace is supplied with natural gas or propane and working with its components presents a hazard. If you don’t know what you’re doing STOP and hire a professional to help you.

Prep Work

The first thing I did was set my DHCP server up to assign specific addresses to each Pi. These are the addresses that my HA system uses to trigger the on/off events.

Server Setup

Start by installing Raspbian. My implementations will be headless so I chose not to run a desktop thus reducing a little bit of overhead. There are tons of install examples online so I won’t cover that here. Once complete, run:

sudo raspi-config

Now choose to expand the filesystem and reboot.

Install Software

sudo su
apt-get install php5-cli php5-curl apache2 php5 libapache2-mod-php5 vim

Install WiringPi

I’m a CLI guy and WiringPi lets me control the GPIO pins via a command line utility – LOVE that!!

git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
./build

Setup Wi-Fi

There is no GUI to configure Wi-Fi so we’ll need to configure it manually. Don’t forget to use your own SSID and password.

sudo su
vim /etc/network/interfaces

Replace the contents with the following:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

#iface wlan0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid "yourSSID"
wpa-psk "yourPASSWORD"

iface default inet dhcp
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

By default, Raspian will try to manage the adapter’s power use by putting it to sleep. This causes all kinds of issues so we need to disable that “feature”.

sudo su
vim /etc/modprobe.d/8192cu.conf

Now paste the following and reboot:

# Disable power saving
options 8192cu rtw_power_mgnt=0 rtw_enusbss=1 rtw_ips_mode=1

Interfacing the Relay to the Raspberry Pi

I picked up a couple of these simple single-relay boards off of Amazon:

Relay Board

Relay Board

Then I wired the relay to the Raspberry Pi as follows:

Relay Raspberry Pi
vcc 3v3
gnd gnd
in #4

Here’s a layout of the Raspberry Pi pin layout along with the WiringPi assigned pin numbers:

GPIO Layout

GPIO Layout

My relay is a single pole, double throw relay. Connect to the common and normally open side of the relay and use the following commands (note that wiringPi changes the pin numbers):

gpio mode 7 out to setup the gpio pin for output mode

gpio write 7 1 to turn off (high)
gpio write 7 0 to turn on (low)

If connecting to the normally closed side of the relay the commands would be opposite of the above to turn off/on.

With my setup, when connecting this way, it is CRUCIAL to use the normally open side of the relay. If connecting to normally closed, a power failure would turn the fireplace on – NOT the desired result.

Startup Script

In order for this solution to function correctly we need to initialize the Raspberry so that our GPIO pin behaves in the desired way.

sudo su
vim /etc/init.d/relayinit

Now past the following:

#! /bin/bash
# /etc/init.d/relayinit


# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting Relay"
# Turn 7 on which keeps relay off
/usr/local/bin/gpio write 7 1
#Start Gpio
/usr/local/bin/gpio mode 7 out
;;
stop)
echo "Stopping gpio"
;;
*)
echo "Usage: /etc/init.d/relayinit {start|stop}"
exit 1
;;
esac

exit 0

Make the file executable:

chmod 777 /etc/init.d/relayinit

Now tell your pi to run this script at boot:

update-rc.d -f relayinit start 4

(Note: You can safely ignore the “missing LSB tags” warning.)

Fireplace Code

Now we need to set up an interface on the Raspberry Pi so we can control the relay from an external source (e.g. my HA system). A simple PHP application will serve this purpose perfectly:

vim /var/www/fireplace.php

Now paste the following:

<?php

#Turn Fireplace Off
if ($_GET["RELAY2"] == "2") {
  exec('gpio write 7 1',$output, $return);
  if ($return == 0) {
    echo "2";
  } else {
    echo "problem";
  }
} elseif ($_GET["RELAY2"] == "1") {
#Turn Fireplace On
  exec('gpio write 7 0',$output, $return);
  if ($return == 0) {
    echo "1";
  } else {
    echo "problem";
  }
} else {
#Get status
  exec('gpio read 7',$output, $return);
  if ($output[0] == 0) {
    echo "on";
  } elseif ($output[0] == 1) {
    echo "off";
  } else {
    echo "unknown";
  }
}
?>

Now all I need to do is wire the relay in parallel with the Skytech system and make some minor tweaks to my HA scripts and I’m off an running.

Until next time – GEEK OUT!

~GT~

   

Posting Blog Entries to Facebook

facebook

Have you ever written a blog post and then tried to post it to Facebook, only to have it format incorrectly or not display an image?

The Geek-Tips blog is powered by WordPress and leverages various plugins and customizations and, for the most part, functions without issue. When I finish writing a post, I like to post it to my Facebook page for my friends and family to read. Occasionally, when I try and post a new article, the results are not what I want. For me, the most common issue is that the image displays incorrectly, not at all or Facebook chooses an image that I don’t want to use and won’t allow me to modify its selection. So how does one go about addressing this issue?

A quick Google search reveals that Facebook uses Open Graph META Tags to identify key components of a post. Knowing this makes fixing the problem a cinch! I installed the “Add Meta Tags” WordPress plugin and then use the META tags outlined below to tell Facebook what information to use in my post:

TAGS

The image META tag tells Facebook to use the specified image when the page is shared:

<meta property="og:image" content="http://geek-tipe.com/image.png"/>

The title META tag tells Facebook what title to accompany the URL:

<meta property="og:title" content="Facebook Open Graph META Tags"/>

The URL META tag tells Facebook what the proper URL is for the linked page:

<meta property="og:url" content="http://geek-tips.com/myurl"/>

The site_name META tag tells Facebook the name that you would like your website to be recognized by:

<meta property="og:site_name" content="Geek-Tips Blog"/>

Last but not least, the type META tag tells Facebook how you would like your site to be categorized:

<meta property="og:type" content="blog"/>

There you have it, now you know how to “force” Facebook to display your posts correctly.

Until next time – GEEK OUT!

~GT~

   

Using SNMP MIB Data to Capture Ink Levels

0

mibb-traceMy regular readers know that I have implemented an extensive home automation system that allows full control and automation of everything from doors to lights to fireplaces to full multi-zone temperature management and includes various tracking and reporting functionality. One of the items that I’ve been tracking for years is the level of ink in the family printer.  When the level gets low, it sends emails to my wife and myself to remind us to change and, if necessary, purchase new cartridges.  That functionality has been working just fine since 2011. About two weeks ago we started having problems with our printer. The issue turned out to be the print head.  Unfortunately replacement parts were almost as much as a new printer so I opted to toss the old machine and buy a new one.

The system I had devised for pulling data off the old printer involved screen-scraping data off of the printer’s built-in web server. I was hoping my scripts would continue to work with the new printer but they didn’t.  Rather than resort to screen-scraping again, I thought I would look for an API so I could pull the data “correctly” and process it into my home automation system.  A bit of searching didn’t turn up anything very promising so I thought I would look at SNMP. I quickly found that OS/X has an snmpInk command that ships as part of it’s Developer Library. Since my old script ran on my Mac Server, and the updated script will run in its place, this was a good fit.

Since the SNMP MIB data doesn’t report a percentage of the cartridge used or a percentage of the cartridge available as my old system did, I needed to get a bit more creative. The OIDs that were available to me were prtMarkerSuppliesMaxCapacity and prtMarkerSuppliesLevel. With this data I’m able to quickly calculate the amount if ink available in each cartridge and then update the appropriate variables in my home automation system via web services.

#!/usr/bin/php -q
<?php 
/** 
*  Author: Geek-Tips 
*    Date: 15 December 2013 
* Updated: 
*    File: printer.php 
* Version: 0.03 
* Purpose: This is the printer module for my Domotica solution.  The scripts purpose is to capture 
*          ink level information from the printer and post it to Indigo. 
* 
*   Notes: v0.03: Changed script to use snmpInk as data source. 
*          v0.02: Added test condition for printer availability. If the printer is off then the 
*                 program exits rather than updating the variables with blank data. 
*          v0.01: This is version 0.1 of the printer script. 
* 
*   Usage: printer.php 
* 
*   Kudos: This URL was helpful - http://www.phpro.org/examples/Parse-HTML-With-PHP-And-DOM.html 
* 
*   Legal: The contents of this file belong to Geek-Tips and may not be modified, copied or otherwise 
*          altered in any way without the express written consent of the author. 
*          This is a copyrighted work and ALL rights are reserved. 
*
*/ 

date_default_timezone_set('America/Chicago'); 
$indigourl = "http://127.0.0.1/modvars/"; 
$raw = shell_exec('/System/Library/Printers/Libraries/snmpInk Photosmart_7520_series'); 
$raw = trim(preg_replace('/[\t]/', '', $raw)); 
$temp = explode("\n", $raw); if (strlen($raw) --> 0) {

//black ink :: prtMarkerSuppliesMaxCapacity - 14 :: prtMarkerSuppliesLevel - 12
$max = preg_replace("/[^0-9,.]/", "", $temp[14]);
$lvl = preg_replace("/[^0-9,.]/", "", $temp[12]);
$blk = round($lvl/$max * 100);
//yellow ink :: prtMarkerSuppliesMaxCapacity - 28 :: prtMarkerSuppliesLevel - 26
$max = preg_replace("/[^0-9,.]/", "", $temp[28]);
$lvl = preg_replace("/[^0-9,.]/", "", $temp[26]);
$yel = round($lvl/$max * 100);
//cyan ink :: prtMarkerSuppliesMaxCapacity - 42 :: prtMarkerSuppliesLevel - 40
$max = preg_replace("/[^0-9,.]/", "", $temp[42]);
$lvl = preg_replace("/[^0-9,.]/", "", $temp[40]);
$cyn = round($lvl/$max * 100);
//magenta ink :: prtMarkerSuppliesMaxCapacity - 56 :: prtMarkerSuppliesLevel - 54
$max = preg_replace("/[^0-9,.]/", "", $temp[56]);
$lvl = preg_replace("/[^0-9,.]/", "", $temp[54]);
$mag = round($lvl/$max * 100);
//light black ink :: prtMarkerSuppliesMaxCapacity - 70 :: prtMarkerSuppliesLevel - 68
$max = preg_replace("/[^0-9,.]/", "", $temp[70]);
$lvl = preg_replace("/[^0-9,.]/", "", $temp[68]);
$pto = round($lvl/$max * 100);

$dte = urlencode(date("F j, Y, g:i a"));

$updateurl = $indigourl . "pBlack?_method=put&value=" . $blk;
get_data($updateurl);

$updateurl = $indigourl . "pYellow?_method=put&value=" . $yel;
get_data($updateurl);

$updateurl = $indigourl . "pCyan?_method=put&value=" . $cyn;
get_data($updateurl);

$updateurl = $indigourl . "pMagenta?_method=put&value=" . $mag;
get_data($updateurl);

$updateurl = $indigourl . "pPhoto?_method=put&value=" . $pto;
get_data($updateurl);

$updateurl = $indigourl . "pDate?_method=put&value=" . $dte;
get_data($updateurl);
}

//Function to get the data from a URL
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

With this script in place and running, via cron, every couple of hours, the central dashboard is always up to date and we, once again, receive notifications when ink levels are low.

Until next time – GEEK OUT!

~GT~

   

Go to Top