A Smart Gas Valve for Home Safety

by Stephen LEE in Circuits > Raspberry Pi

5541 Views, 33 Favorites, 0 Comments

A Smart Gas Valve for Home Safety

Smart Gas Valve For Home Safety
A smart JPEG camera for home security
Screenshot 2016-11-02 05.48.25.png
systemStructure.png
Screenshot 2016-10-25 11.26.56.png
MQTT-Gas-VV_phone.png.PNG
IMG_0198.PNG

Introduction

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.

https://www.instructables.com/id/Smart-JPEG-Camera-for-Home-Security/

- How to use the Bluemix platform (Docs)

https://console.ng.bluemix.net/docs/

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

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

Setting Up a Smart Gas Valve With Raspberry Pi

AddingPullupResistor4.7kohm.png
580ee5a94936d4e9150001eb.jpeg
580ee8be937ddb575f0017df.jpeg
IMG_0286.JPG
580eea512e7fb659a400126b.jpeg
IMG_0285.JPG

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. )

Programming NodeRED on Raspberry Pi2

Screenshot 2016-11-02 04.56.53.png
Screenshot 2016-10-20 01.37.11.png
5818d43d2e7fb608fe000558.jpeg
5809a568deafa40a6e00030f.jpeg
IMG_0206.JPG
Screenshot 2016-11-02 04.59.44.png
MQTT-Gas-VV_Web.png

How to start Node-RED on the web browser.

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

node-red-start

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

'Once Node-RED has started, point a browser at http://169.254.170.40:1880'

(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.

Setting Up MQTT V3.1 on Raspberry Pi2

Screenshot 2016-10-20 22.49.48.png
Screenshot 2016-10-20 22.53.56.png
Screenshot 2016-10-20 22.56.15.png
Screenshot 2016-10-20 22.50.40.png
580eef472e7fb659a4001280.jpeg

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 http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
rm mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo curl -O http://repo.mosquitto.org/debian/mosquitto-jessie.list
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:

mosquitto

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

Checking Your NodeRED Codes With MQTT on Raspberry Pi2

580d93b645bcebcec700003a.jpeg
topicgasvalve.png
topicPW.png
Screenshot 2016-10-20 18.05.26.png
Screenshot 2016-10-20 18.05.11.png
Screenshot 2016-10-20 18.03.05.png

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.)

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

Screenshot 2016-11-02 05.04.38.png
01_SetMyEmail_MQTT.png
03_SetMyTwitter_MQTT.png
02_SetmyTwilio_MQTT.png
GasVV.png
Screenshot 2016-10-19 12.58.16.png
IMG_0296.PNG
Screenshot 2016-10-20 19.19.18.png

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.
node-red-stop
node-red-start
  • Close your web browser and reopen the web browser.

Adding Autostart Files for Every Boot.

580eed4f50e1b680f600145d.jpeg

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] 
Type=Application
Name=flyMosquitto
Comment=Fly my mosquitto
Exec=cd /etc/mosquitto/conf.d/
Exec=mosquitto

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.

Download List

List of References