Monday, September 14, 2015

Controlling lights using Raspberry Pi, and relays thru Bluemix IoTF.



With the Lord's festival (remover of obstacles ) coming up, I thought of how to use Bluemix's IoTF  for this occasion, so that I can demonstrate the ability of Bluemix , Raspberry Pi to control some standard lights controlled by a 240V power outlet.

My Objectives were
- Use Relays and control lighting of an idol of Lord Ganesha from Bluemix IoTF.
- Demonstrate how I can publish / subscribe commands from a device ( Raspberry Pi) to Bluemix IoTF, and likewise Pub/Sub from Bluemix to my app ( i.e my laptop )
i.e Showcase a command from the application ( i.e my laptop ) sending a "enable lights " to Bluemix IoTF,
- Simulate a temperature sensor sitting on my device and sending the data across to Bluemix IoTF and if temperature was above a certain threshold - turn the lights on, and turn off when temperature fell below threshold.
- Show how we can control the command to ON / OFF  from a web interface.
( at this point - I have not published my IP address to the general public,  so my web server runs local and one can access it by running http://127.0.0.1/get.html )


Here are some snapshots :












The code used was :
Client side on the device was a simple C Program - which subscribes to the IoTF and listens to any commands.

App side -i.e my laptop, was a Node.js which sends commands to turn on / off depending on instructions from Bluemix IoTF or the device.
In the below example, the application receives data from the IoTF and acts by sending some commands.

applicaton.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) {

    console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload);
    var json = JSON.parse(payload);
    console.log ("temperature is " +json.d.temp );

    if ( json.d.temp > 10) {
                console.log ("since temp is > 10, CRITICAL ");
                //payload = '{ "numOfTimes" : 1 }';
                //applicaton.publishDeviceCommand(deviceType, deviceId, "blink", "json",payload);

                payload ="critical";
                console.log ("sending payload " + payload );
                applicaton.publishDeviceCommand(deviceType, deviceId, "blink", "string",payload);

    } else {
                console.log ("since temp is < 10, SAFE");
                //payload = '{ "numOfTimes" : 0 }';
                //applicaton.publishDeviceCommand(deviceType, deviceId, "blink", "json",payload);

                payload = "safe"
                console.log ("sending payload " + payload );
                applicaton.publishDeviceCommand(deviceType, deviceId, "blink", "string",payload);


    }

Friday, September 11, 2015

Sending event notification from Facebook IoT Platform Parse.com to Raspberry Pi

Facebook has a IoT Platform called Parse - which acts like a foundation to receive and send events from various devices ( e.g Raspberry Pi and other hardware devices ), This platform can connect to Apps on desktops, mobile devices, etc and send events to devices.

Below is my experience on using this platform to push events to my Raspberry Pi from the Parse IoT Platform.

First I created a simple starter app called RPi and followed the procedure to install the necessary code on my device to connect to Facebook's  Parse platform.

Followed this link :
https://www.parse.com/apps/quickstart?app_id=rpiapp#embedded/raspberrypi

Sounded pretty quick and simple - however the sending of events from the device ( Raspberry Pi) to the Facebook's IoT Platform seems a bit challenging at time. - Working on it.

Here is a brief up of how to send events from Facebook's IoT to your Raspberry Pi.

Clicked on Test button on the parse.com page.



and on my Raspberry saw these events show up :



Sunday, September 6, 2015

Sample C Code to turn a LED On/Off on Raspberry Pi on GPIO Pin


This "C" code below will turn the LED on Pin 17, also known as  GPIO17, also known as GPIO GEN0/GPIO17, (6th pin on inside column from top - if you are holding RPi with the USB ports facing down )


This was tested on a Raspberry Pi Model B.
 
This is defined by the API call "wiringPiSetupGpio()" in the beginning of the code.

However, there is yet another API called "wiringPiSetup()", Here Pin 17 would mean +3V !! - so have to watch out !

Be Careful how you want to define your pin numbering, and use the appropriate API call before sending signals to the Pins on the RPi.

#include <wiringPi.h>
#include <stdio.h>
int main (void)
{
  if ( wiringPiSetupGpio () == -1 ){
        printf ("wiring failed - returning ! ");
        return 0;
  }
  pinMode (17, OUTPUT) ;
  for (;;)
  {
    digitalWrite (17, HIGH) ;
        delay (500) ;
    digitalWrite (17,  LOW) ;
        delay (500) ;
  }
  return 0 ;
}