GA4

Monday, February 8, 2016

IoT Notifier through Push Button

Hello everyone. 

In my last blog Stepping towards IoT, you have read about the basic application based on Integrated circuit and MS Azure cloud service.

Now, in my new post, you will read on how we can leverage push button through an integrated circuit and send notifications through email from Cloud (MS Azure). Currently, MS Azure does not have any API which we can used to send emails. To perform email transaction we'll be using market place to add send grid notification. Wow....that's an another piece we'll be learning along with IoT. Again, I'll not go in depth, assuming you are aware of the technology and here we are learning on how we can integrate different technologies and API to the circuit to build IoT Notifier through Push Button.

Before I start with the steps on building end to end product, I would like to tell you some usage of it, so that when you are reading this blog you can start thinking on your own innovations. When I started my "IoT Notifier trough Push Button" project, it was on some thoughts like when there is no access to your mobile or phone and you want to send some notification to someone in case of emergency, then you push some button and a message is send to the respected recipient. Other usages like:

  • In a truck, the loader do loading and unloading of goods. He can push the button to notify that a loading or unloading to the truck has been done. The notification can be either in the form of email, SMS message or any other BI tool. You can further do the automation to your supply chain management automates processes.
  • Another, example you can take from the cab or taxi drivers. Sometime, these drivers do have unpleasant or troublesome customers and they are sometime unable to handle the situations or not able call or send SMS to their agencies for their help. What they can do is, push the button for the notification without knowing the passenger and help can be provided to the taxi driver by knowing the coordinates of the cab/taxi position through GPS.

I know you already started having great thoughts in your great minds. Let's now come to the technical part of it.


For this IoT application development, I have used the following technologies / hardware:

Technologies



1.      MS Azure Cloud Service & Send Grid APIs from Azure Market Place

2.      Python



Hardware 



1.      Raspberry Pi 2

2.      GIPO Bread Board to create circuit

3.    Three 1K Ohm 1/4 W 0.25W Resistor

4.      Two LEDs - Red and Green

5.      Three Wires

6.      RPi GPIO T-Cobbler

7.      8" 40 Pin Ribbon Cable for Raspberry Pi 2

8.    Tactile switch



Let us start with the creation of some code before we go to the circuit designing.


MS Azure Cloud Service

We would be creating  simple web MVC Web API which will be called from the circuit on the push of the button. This web API method in turn send out the notification to the specific recipient through an email.

For the notification I have used the SendGrid Library from the market place in MS Azure and called the SendGrid nuget package in the MVC Web API project to work with APIs.
 
public string Get(int id)
        {
            var msg = new SendGridMessage();
            msg.From = new MailAddress("ck@example.com");
            List recipients = new List
            {
                @"Recipient Name"
            };
            msg.AddTo(recipients);
            msg.Subject = "Testing the SendGrid Library";
            msg.Html = "Hello...The notification is send through Raspberry Pi Push Notification circuit";
            var username = "USERNAME";
            var password = "PASSWORD";
            var apiKey = "";
            var credentials = new NetworkCredential(username, password);
            var transportWeb = new Web(credentials);
            transportWeb.DeliverAsync(msg);
            return "true";
        }


If you see in the above method I have used input parameter as int which can be further enhanced to do specific operation based on the value passed by pressing the push button. Or in case we have multiple push buttons then we can call the same method and performed the specific action based on the button pressed.

Another stuff which is used in the above method is the Send Grid API which is a third party and is used to send the emails to the recipients. Currently, MS Azure does not supporting emails, therefore we have choose an Send Grid Library for sending emails.

In case you want to connect to any other MAPI tools or SMS sending tools then you can call them from the above method.



Once you have develop your WEB API method, please host on MS Azure web Role or you can test it on your local server.



Circuit Design

The circuit is very simple or I would say it is an extension to my old circuit design which I have mentioned it in my previous blog "Stepping towards IoT".

Please refer the following design for the complete circuit.



Connecting to Raspberry Pi2


Connect another section of Ribbon Cable to the Raspberry Pi 2 pins, as shown in the above figure.





Python Code

#!/usr/bin/env python2
 import urllib2
 import urllib
 import time
 import RPi.GPIO as GPIO
 DEBUG = True
 GPIO.setwarnings(False)
 GPIO.setmode(GPIO.BCM)
 GREEN_LED = 18
 RED_LED = 23
 GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 GPIO.setup(GREEN_LED, GPIO.OUT)
 GPIO.setup(RED_LED, GPIO.OUT)
 if DEBUG:
    
   GPIO.output(RED_LED,True)
   while True:
     input_state = GPIO.input(27)
     if input_state == False:
       print "Button Pushed....:-)"
       print('Calling IoT Notification Service')
       url='http://YourCloudService.cloudapp.net/api/iotNotification/1'
       data = urllib2.urlopen(url)
       info = data.read()
       if (info == 'true'):
         GPIO.output(RED_LED, False)
         GPIO.output(GREEN_LED, True)
         time.sleep(10)
     else:
       time.sleep(2)
       GPIO.output(GREEN_LED, False)
       GPIO.output(RED_LED, True)
        
 GPIO.cleanup()





Execute python program to enable the circuit

When you call the python program, the red light will get enabled (as shown in figure below) making the circuit enable to send the notification on the push of the button on the circuit.

Once you push the button , the messages will be shown on the screen, letting a user know that the button is pushed and the notification service has been called. A green light will also get lighted for some time to make user know the notification process is in progress.



Summary

You have read above, how you can use the push button through raspberry pi to send the email notification. I hope you have enjoyed reading the above blog.  

2 comments:

Unknown said...

Good article, Charan. Keep up your passion for technology.

Charan Kawal Singh (CK) said...

Thanks Ramesh.