Turn on a light by publishing an MQTT message!

In previous posts I played with Raspberry Pi’s, MQTT clients and X10 devices.
In this post I will bind these all together providing a way to control your lights by publishing messages to an MQTT broker.
Nice!

Step 1 – Prepare your Raspberry Pi

In a previous post I wireless installed a clean version of Raspbian on a Raspbian Pi 3.

Step 2 – Install MQTT Client

In a previous post I installed a MQTT client on a Raspbian Pi 3.

Step 3 – Install Heyu

In a previous post I installed Heyu on a Raspbian Pi 3.

Step 4 – Create a bash script to tie it all together

nano send_x10_messages.sh
#!/bin/bash
mqttserverip='192.168.XXX.XXX'
mqtttopic='home/lights/livingroom'
messageOn='On'
messageOff='Off'
mosquitto_sub -h $mqttserverip -t $mqtttopic | while read line
do
  if [[ $line == $messageOn ]]; then
    heyu on B4
  fi
  if [[ $line == $messageOff ]]; then
    heyu off B4
  fi
  echo $line
done
sudo chmod a=r+w+x send_x10_messages.sh

Step 5 – Auto run on startup

To run this script on startup we edit our rc.local file

sudo nano /etc/rc.local

Add the line:

sudo /home/pi/send_x10_messages.sh&

Now reboot and your good to go

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s