Sending SMS And Email From Your IoT Device

Phone notifications are a good way to get alerted about an unusual activity. When we are developing an IoT solution it is always good to send SMS to user’s phone for certain activity as the smartphones are always within reach. It is not always possible for a user to monitor the data with a Mobile app or a website. If they get a notification about a certain activity or a sudden variation in data they will come to know this by time and can immediately check it with the mobile app and can take the further steps.

Earlier sending SMS with a microcontroller was a little clumsy process, you have to create an account on SMS service provider like Twilio and then you have to use some IoT channels like Thingspeak, Temboo, and IFTTT. Lots of work! But now as the IoT devices are getting powerful, there are some new revolutionary IoT Cloud services are immerging. They are aimed to take your data to cloud with an ease. One of the IoT cloud recently I am using in my projects is Ubidots. The best thing about Ubidots is, it is user-friendly. It won’t take much time to take your IoT solution to cloud. Ubidots visualize your data in a better way. You can even track the locations with its GPS widget, all you need is just to send the data with a simple API.

With Ubidots we can easily configure under what conditions an alert should be triggered. We can set any type of thresholds through simple "IF - THEN" statements. Once an event is triggered it will send an Email or SMS alerting someone about a specific event or you can perform any other task like trigger another action.

Let us create a simple project with a Burglar Alarm System with Intel Edison which will detect any intrusion and notify you with an SMS or an Email. I am using Ubidot’s Node.Js library and programming in Node.Js with Intel XDK IoT edition.

If you have an Intel Galileo board and programming using Arduino IDE or you want to know more about PIR sensor and its working have a look at another article of mine “Burglar Alarm with PIR motion sensor”.

Requirements

  • Intel Edison module
  • Arduino expansion board
  • PIR sensor
  • Buzzer
  • An LED
  • USB cable
  • Power Supply
  • Jumper cables

Requirements
                                                             Figure 1:
Requirements

Making Connections and Programming

  1. Connect your Edison to power supply and to your PC via USB cable.
  2. It will take 15-20 seconds to boot up, after that stack the Grove Base Shield.
  3. Connect the PIR motion Sensor to analog pin D2.
  4. Connect the Buzzer to pin with PWM, I connected to D5.
  5. Connect the LED to pin D3.
  6. Open Intel XDK IoT edition, if it is not already installed in your PC get it from here.

    If you have flashed you Edison with the Flash Lite Tool Node.Js will be already installed on your board.

  7. Connect IDE to your Edison board. It will ask you for username and password, default username is root with no password.
  8. Select a blank Node.JS template and create a new project.

Create a new project
                                                            Figure 2:
Create a new project

Code for PIR Sensor

  1. //Load Grove Motion module  
  2. var grove_motion = require('jsupm_biss0001');  
  3. // Instantiate a Grove Motion sensor on GPIO pin D2  
  4. var myMotionObj = new grove_motion.BISS0001(2);  
  5. setInterval(function()  
  6. {  
  7.     if (myMotionObj.value())  
  8.     {  
  9.        BlinkLED();  
  10.        melody();  
  11.        v.saveValue(1);  
  12.        console.log("Detecting moving object");  
  13.       
  14.     }  
  15.     else  
  16.         console.log("No moving objects detected");  
  17.         v.saveValue(0);  
  18. }, 1000);  
  19.   
  20. // Print message when exiting  
  21. process.on('SIGINT', function()  
  22. {  
  23.     console.log("Exiting...");  
  24.     process.exit(0);  
  25. });  
Code for PIR Sensor
                                                               Figure 3: Code for PIR Sensor

Sending Data to Ubidots
  1. var ubidots = require('ubidots');    
  2.     
  3. var client = ubidots.createClient('YOUR-API-KEY');    
  4.     
  5. client.auth(function() {    
  6.     this.getDatasources(function(err, data) {    
  7.          console.log(data.results);    
  8.     });    
  9.     var ds = this.getDatasource('xxxxxxxx');    
  10.    
  11.     ds.getVariables(function(err, data) {    
  12.         console.log(data.results);    
  13.     });    
  14.     ds.getDetails(function(err, details) {    
  15.         console.log(details);    
  16.     });    
  17.     
  18.     var v = this.getVariable('xxxxxxx');    
  19.     
  20.     v.getDetails(function(err, details) {    
  21.         console.log(details);    
  22.     });    
  23.     v.getValues(function(err, data) {    
  24.         console.log(data.results);    
  25.     });    
  26. }    
Creating triggers with Ubidots

 

  1. Log in to your Ubidots account or you can create one here.

  2. Select the “Sources” tab and then click on “Add Data Source” to create a new data source. Here I have added My Edison.

  3. Once the data source is added we will add our PIR sensor. PIR motion sensor is a digital sensor, it will only detect the motion. It doesn’t detect the distance and number of people. Hence if a motion is detected I am sending 1 else 0 to Ubidots.

    Add our PIR sensor
                                                       Figure 4: Add our PIR sensor

  4. Click on the variable and copy the variable ID. Paste this in your code also get your API key My Profile, then API Keys.

  5. Now we will create events. Select Event tab, click add Event.

  6. Select the data source and Variable. In our case it is My Edison and PIR Sensor.

    Select the data source and Variabl
                                                    Figure 5: Select the data source and Variabl

  7. Now it will present you with a simple if - then statement. Here we have to select the value of PIR Sensor and the action to be triggered.

    IF- Then statement
                                                                Figure 6: IF - Then statement

  8. We are sending 1 if PIR sensor detects a motion, hence to trigger an action our value should be equals to 1. Press continue.

  9. Now we have to add the trigger, we can send an SMS or an Email, can set another variable or can request a custom URL. Let us select SMS for now.

    Select SMS for now
                                                                Figure 7: Select SMS for now

  10. Enter the details like phone number and message to be send.

    Enter Phone number and Message
                                                       Figure 8: Enter Phone number and Message

    Enter the details
                                                                      Figure 9: Enter the details

  11. SMS event will be added with PIR sensor value equals to 1. Similar way you can add an Email trigger.


                                                                      Figure 10: Add Event

    Enter details
                                                                   Figure 11: Enter details


                                                                Figure 12: Intrusion Detected

Now we have added an Email and an SMS trigger. Build, upload and run your code on Edison. You will notice data sending to Ubidots in the console. If any motion is detected by the PIR motion sensor it will blink the LED, play the buzzer and an SMS and an Email will be sent to your Phone.

Type text message
                        Figure 13: Type text message

PIR Motion Sensor Detects a moving object
Figure 14: PIR Motion Sensor Detects a moving object

Up Next
    Ebook Download
    View all
    Learn
    View all