Introduction: A Smart Gas Valve for Home Safety

About: I am a keen IoT inventor as a hobbyist in Sydney and very passionate about IoT and Algorithms such as MACHINE LEARNING, COMPUTATIONAL STATISTICS and so on. Also, I have spent almost every day of the past 15 ye…


This instructable will cover the basic steps that you need to follow to get started with open sources such as Node-RED, MQTT v3.1, and Watson NodeRED for IBM Bluemix. MQTT(Message Queueing Telemetry Transport) is a Machine-To-Machine(M2M) or Internet of Things (IoT) connectivity protocol that was designed to be extremely lightweight and useful when low battery power consumption and low network bandwidth is at a premium. It was invented in 1999 by Dr. Andy Stanford-Clark and Arlen Nipper and is now an Oasis Standard.

The different tutorials can cause a great deal of confusion, which is why I have tried to make the easiest setup possible. Specifically, this instructable will cover how to code the Node-RED on Raspberry Pi2 as an MQTT client by connecting to your home wireless network and how to send sensor data. When you finish this project, I suggest you another M2M communication approach.

- How to use the Bluemix platform (Docs)

Step 1: Table of Contents

  • Step 0: Introduction
  • Step 1: Table of Contents
  • Step 2: Bill of Materials
  • Step 3: Setting up a smart gas valve with Raspberry Pi
  • Step 4: Programming NodeRED on Raspberry Pi2
  • Step 5: Setting up MQTT v3.1 on Raspberry Pi2
  • Step 6: Checking your NodeRED codes with MQTT on Raspberry Pi2
  • Step 7: Adding & Setting up IBM Watson, Play-Audio, E-Mail, Twitter, and Twilio
  • Step 8: Adding autostart files for every boot.
  • Step 9: Download list
  • Step 10: List of references

Step 2: Bill of Materials

  • Raspberry Pi2 X 1ea
  • Wifi dongle X 1ea
  • PIR motion sensor X 1ea
  • Android smartphone's portable battery X 1ea
  • Nod-RED software X 1ea
    • Free open source
    • Use the version pre-installed in Raspbian Jessie image since November 2015
    • Installation guide
  • MQTT v3.1 software X 1ea
    • Free open source
    • Installation guide includes at Step 5
  • NodeRED's Watson Nodes for IBM Bluemix
    • Text to speech node X 1ea
  • Speaker X 1ea

Step 3: Setting Up a Smart Gas Valve With Raspberry Pi

Assembly steps

(1) Connect the Raspberry Pi2 with a magnetic switch sensor as shown above in the circuit diagram.

(2) Add a pull-up resistor(4.7 k ohm). If you use a really long wire, you could change approximately 1 k ohm. It depends on your environment.

(3) Connect the magnetic switch sensor with raspberry pi.

  • Magnetic sensor <----> Raspberry Pi2
    • COM --- Ground
    • NO --- GPIO 18

(4) Assemble the magnetic switch sensor with a gas valve at home.

(The magnetic switch sensor should include an adhesive tape on the base.)

(5) Connect a portable battery with Raspberry Pi2.

(Use any portable battery to connect with the same size connector cable on Raspberry Pi2. )

Step 4: Programming NodeRED on Raspberry Pi2

How to start Node-RED on the web browser.

(1) Write down command shown below to a terminal window.


(2) You can find an IP address as below.

'Once Node-RED has started, point a browser at'

(3) Open your web browser.

(4) Copy the IP address and paste on the web browser.

(5) It will display a visual editor of Node-RED on the web browser.

(6) You can start coding with visual editor on the web browser.

(7) Try dragging & dropping any node from the left-hand side to right-hand side. It's really easy to code.

( You can conveniently use the visual editor offline as well as online. )

Download the 'SmartGasValve_NodeRED.txt' file.

(1) Click the number (1) at the right-hand side corner shown in NodeRED on the web browser.

(2) Click the Import button on the drop down menu.

(3) Open the Clipboard shown in the above 1st picture.

(4) Lastly, paste the given JSON format text of 'SmartGasValve_NodeRED.txt' in Import nodes editor.

Step 5: Setting Up MQTT V3.1 on Raspberry Pi2

Setting up MQTT v3.1 on Raspberry Pi2

This message broker(Mosquitto) is supported by MQTT v3.1 and it is easily installed on the Raspberry Pi and somewhat less easy to configure. Next, we step through installing and configuring the Mosquitto broker.

We are going to install & test the MQTT “mosquitto” on a terminal window.

curl -O
sudo apt-key add mosquitto-repo.gpg.key
rm mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo curl -O
sudo apt-get update

Next install the broker and command line clients:

  • mosquitto – the MQTT broker (or in other words, a server)
  • mosquitto-clients – command line clients, very useful in debugging
  • python-mosquitto – the Python language bindings
sudo apt-get install mosquitto mosquitto-clients python-mosquitto

As is the case with most packages from Debian, the broker is immediately started. Since we have to configure it first, stop it.

sudo /etc/init.d/mosquitto stop

Now that the MQTT broker is installed on the Pi we will add some basic security.

Create a config file:

cd /etc/mosquitto/conf.d/
sudo nano mosquitto.conf

Let's stop anonymous clients connecting to our broker by adding a few lines to your config file. To control client access to the broker we also need to define valid client names and passwords. Add the lines:

allow_anonymous false
password_file /etc/mosquitto/conf.d/passwd
require_certificate false

Save and exit your editor (nano in this case).

From the current /conf.d directory, create an empty password file:

sudo touch passwd

We will use the mosquitto_passwd tool to create a password hash
for user pi:

sudo mosquitto_passwd -c /etc/mosquitto/conf.d/passwd pi

You will be asked to enter your password twice. Enter the password you wish to use for the user you defined.

Testing Mosquitto on Raspberry Pi

Now that Mosquitto is installed we can perform a local test to see if it is working:

Open three terminal windows. In one, make sure the Mosquitto broker is running:


In the next terminal, run the command line subscriber:

mosquitto_sub -v -t 'topic/test'

You should see the first terminal window echo that a new client is connected.

In the next terminal, run the command line publisher:

mosquitto_pub -t 'topic/test' -m 'helloWorld'

You should see another message in the first terminal window saying another client is connected. You should also see this message in the subscriber terminal:

topic/test helloWorld

We have shown that Mosquitto is configured correctly and we can both publish and subscribe to a topic.

  • When you finish testing all, let's set up below that.
sudo /etc/init.d/mosquitto start

Step 6: Checking Your NodeRED Codes With MQTT on Raspberry Pi2

When you have already used the JSON format of the 'SmartGasValve_NodeRED.txt' on Node-RED, it's automatically set up & coded each data. I have already set up the each data in each node.

(1) Click each node.

(2) Check information inside each node has been prefilled.

(3) Please don't change the set data.

(The above can be customized for more advanced users.)

Step 7: Adding & Setting Up IBM Watson, Play-Audio, E-Mail, Twitter, and TwilioAdding & Setting Up E-Mail, Twitter, and Twilio

Searching the Nodes

Node-RED comes with a core set of useful nodes, but there are a growing number of additional nodes available for install from both the Node-RED project as well as the wider community.
You can search for available nodes in the Node-RED library or on the npm repository.

  • For example, we are going to search Twilio at the npm web. Click here.
  • Next step, we are going to install Twilio on the raspberry pi.

Installing npm packaged node

To add additional nodes you must first install the npm tool, as it is not included in the default installation. The following commands install npm and then upgrade it to the latest 2.x version.

sudo apt-get update
sudo apt-get install npm
sudo npm install -g npm@2.x
hash -r
cd /home/pi/.node-red
  • For example, 'npm install node-red-{example node name}'
  • Copy the 'npm install node-red-node-twilio' from the npm web. Paste it on terminal.
  • Then, we are going to install both node-red-node-watson and node-red-contrib-play-audio.
npm install node-red-node-twilio

<p>npm install node-red-node-watson node-red-contrib-play-audio</p>
  • You will need to restart Node-RED for it to pick-up the new nodes.
  • Close your web browser and reopen the web browser.

Step 8: Adding Autostart Files for Every Boot.

How to make an autostart file at every boot.

cd /etc/xdg/autostart/ 

(If there is no ‘autostart' folder, make it below)

mkdir autostart  
cd autostart 
sudo nano flyMosquitto.desktop

Type the below (this will enclose the file) Or Put ‘flyMosquitto.desktop’ file into autostart folder.

[Desktop Entry] 
Comment=Fly my mosquitto
Exec=cd /etc/mosquitto/conf.d/

Make it to autostart the Node-RED at every boot

sudo systemctl enable nodered.service

As shown in the above picture, please check the last line is as below.

23 Oct 06:21:22 - [info] [mqtt-broker:2be4dc46.47a5b4] Connected to broker: mqtt://localhost:1883

This is clearly working as a message broker(Mosquitto) on Raspberry Pi2.

Step 9: Download List

Step 10: List of References

IoT Builders Contest

Participated in the
IoT Builders Contest