Raspberry Pi, Node.js and Twilio – Is It Raining Arvind? #IoT

This post is part 9 of 9 in the series Raspberry Pi
Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

Usually my mom dries the clothes on the terrace and whenever I am at home she keeps asking me to look outside and tell her if it raining or not. Literally every half an hour or every time she hears a swoosh or a woosh from outside.

I was fed up and wanted to eliminate the middle man (me). So I came up with an app. It is called “Is it raining Arvind?” (LOL I know) . All the app does is tell my mom if it is raining outside now. And when it starts raining, the app triggers a buzzer and sends an SMS to my mom. Smart right! And I was off the hook.

So, this is how the app works. It uses the conductive property of the water. There are 2 wires that are connected to the Pi between a GPIO pin and 5v. And when these 2 pins are connected, it triggers a High on GPIO pin. So, we place these 2 wires outside on the terrace inside a bowl. Once the bowl is filled with water because of the rain, the circuit is completed and a High is triggered on the GPIO.

We take advantage of the GPIO high and then trigger the buzzer and send an SMS using Twilio. Smart right!?

Below is a quick demo of the app

Sweet right!

You can find the completed code here.

So, let us get started.

Prerequisites

If you are new to Raspberry pi and have not yet installed Node.js on it, I would recommend going through Getting Started with Raspberry pi and Node.js.

If you are new to electronics devices and circuits, I would recommend going through the video lectures from All About Circuits.

If you are new to Twilio, refer this.

Also do check out Resistance of water.

Components needed

  1. 1 – Raspberry pi B+
  2. 1 – Breadboard
  3. 1 – Buzzer
  4. 4 – Jumper wires
  5. Some rain

If you are new to Raspberry Pi GPIO, please refer this.

Setup project

Now that we have a basic idea of what we are going to build, let us get started. First, we will setup the circuit.

Screen Shot 2014-11-23 at 6.07.39 pm

We will connect the positive end of the buzzer to GPIO pin 18 and the negative end to GND PIN 06 of the Pi. We will connect the 2 probes that are placed inside the bowl to GPIO PIN 17 and 5V PIN 02 of the PI.

This completes our circuit. To quickly test the circuit, take a bowl of water and dip the 2 probes and you should hear the buzzer “buzz”.

DO NOT connect them directly. This WILL damage the GPIO pin. If you want to connect the 2 pin directly and test, you need a voltage divider circuit, that will take the load off the GPIO pin.

Screen Shot 2014-11-23 at 6.19.08 pm

Nodejs Program

Lets get started with the Node.js program.

Login to your pi via ssh – terminal/putty. As soon as you ssh into pi, you will be landing inside the /home/pi folder. We will create a new folder here named node_programs. And inside this folder, we will be maintaining all our programs. Run

mkdir node_programs

To step inside that folder, run

cd node_programs

 For this post, we will create a new folder named checkRain and will step inside this folder. Run

mkdir checkRain && cd checkRain

Note : You can run multiple commands separated by a &&.

First we will initialize a new node project here. Run

npm init

Fill it up as applicable.

Now we will install 3 node modules

npm install --save onoff tiny-router twilio

Next, we will create a new file named index.js. And we will open the same in the nano editor. Run

nano index.js

Paste the below code into the nano editor

Things to notice

Line 1 : We require a twilio module which we will be writing soon. This file will consist of the logic to send an SMS

Line 3 : We require onoff module and declare Pin 18 as out, which will be connected to the buzzer and Pin 17 as In, which we will connect to the probe inside the bowl.

Line 7 : We listen for changes on the Probe GPIO.

Line 9 : If the value is high, we trigger the buzzer on line 11 and send an SMS on line 20.

Line 25 : Clean up the GPIO pins on exit.

Line 33 : We require a module named router, that we will be writing soon. This module will consist of our web app logic.

Let us save the file and run the program. To save the program, press (ctrl+x). This will ask you to save the file. Press Y and press enter key to complete the operation.

Next, we will write the logic to send the SMS. Create a new file

nano twilio.js

Paste the below code

Line 1 :  We init the Twilio client with the Account SID and Auth Token. Refer prerequisites for more info.

Line 4 : We create a new method, that will send an SMS to the provided number

Save the file. And exit the nano editor.

Now create another file

nano router.js

This file will consist of our router logic. Paste the below in the nano editor

Things to notice

Line 1,2,3 : We require the dependencies

Line 7 : We are going to use SVG for showing the status of rain. So, we need to add that MIME type explicitly. (tiny-router is a light weight version of Express)

Line 12 : Declare the static folder

Line 16 : When the /status endpoint is hit, we check the GPIO pin value and dispatch the same. If it is raining, it will be a high else it will be a low.

Line 23 : Our home page

Save the file and exit the nano editor. Next, we will create the home page.

nano index.html

Standard HTML5/Bootstrap/jQuery page.

We will be using 3 animated SVGs from here. You can download the same from here, create a new folder named public and place it inside it.

Save all the files and run

sudo node index.js

Now launch the web page wrt the IP of your Pi. For me it would be  http://192.168.2.2:2000 and BAM!!! you should see

Is it raining_no

And when you pour some water or when it actually rains, you should see

Is it raining_yesAnd finally, if it is night, we show a different skin

Is it raining_nightSweet right!

Hope this post gave you an idea on how to implement a simple Rain detector.


Thanks for reading! Do comment.
@arvindr21

 

Series Navigation<< Raspberry pi, an Ultrasonic Sensor and Node.js – Measure Distances
Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page
  • Олег П

    Aravind, how is sent sms, is it free for any mobile operator?

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Hello Олег П, for sending sms, Twilio rates apply and for receiving sms your service provider rates apply.

  • Ganesan S

    Nice application. Well done Arvind.

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks Ganesan.

  • Pratap Raj

    Hey Arvind is it raining now ?

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      LOL.. It is not now 😀