Skip to content
September 5, 2015 / kiranpatils

How to integrate Raspberry Pi with Sitecore

Challenge:

As promised in following tweet :

Will be sharing how you can integrate Raspberry Pi with Sitecore, Eager to know – Let’s have a look at it!

Solution:

If you haven’t heard about Raspberry Pi Or heard. But don’t know much about it. Don’t worry, would encourage you to read this : https://kiranpatils.wordpress.com/2015/07/28/raspberry-pi-quick-start-guide/

You read it? Sounds clear? If not, please feel free to ask your questions. The more you ask, the more you know!

And I’m sure you also know about IOT — Internet of Things :

The Internet of Things (IoT), also called Internet of Everything[1] or Network of Everything,[2] is the network of physical objects or “things” embedded with electronics, software, sensors, and connectivity to enable objects to exchange data with the production, operator and/or other connected devices based on the infrastructure of International Telecommunication Union’s Global Standards Initiative.[3] The Internet of Things allows objects to be sensed and controlled remotely across existing network infrastructure,[4] creating opportunities for more direct integration between the physical world and computer-based systems, and resulting in improved efficiency, accuracy and economic benefit.[5][6][7][8][9] Each thing is uniquely identifiable through its embedded computing system but is able to interoperate within the existing Internet infrastructure. Experts estimate that the IoT will consist of almost 50 billion objects by 2020.[10]

Pi is really helpful to achieve IOT!

So, I was playing around with Pi since few days. And as it happens with all Sitecore folks that, Whatever new stuff we learn. We think. How can we integrate it in Sitecore? This happens with you as well — correct? It shows how much Sitecore has been engraved in our lives!

Then I recalled one scenario – We had with one of our company — Where publishing was mission critical operation. If anyone needs to publish anything, They either need to email a group, ask someone in IM, Or go and talk to someone in person that whether any publishing is going on or not?

So, thought to do a quick PoC.

Raspberry Pi : First we need to make sure Pi is set up correctly, You can find detailed information on my earlier article. But main things are:

  • Raspberry Pi — Connected with LED
  • Python script to start/stop LED
  • Flask installed on it [Flask is “Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.”]. So, we can invoke Python script over HTTP from Sitecore side. Flask script looks like this :
from flask import Flask
app = Flask(__name__)

@app.route("/on")
def ON():
    import RPi.GPIO as GPIO ##Import GPIO Library
    GPIO.setmode(GPIO.BOARD) ##Use board pin numbering
    GPIO.setup(13,GPIO.OUT) ## Setup GPIO on Pin13 to out
    GPIO.output(13,True) ##Turn on GPIO pin 13
    return "ON"

@app.route("/off")
def OFF():
    import RPi.GPIO as GPIO ##Import GPIO Library
    GPIO.setmode(GPIO.BOARD) ##Use board pin numbering
    GPIO.setup(13,GPIO.OUT) ## Setup GPIO on Pin13 to out
    GPIO.output(13,False) ##Turn on GPIO pin13
    return "OFF"


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

Make sure your Script is running. [Open Terminal | sudo python YOURSCRIPTNAME.py]. Also, make sure your Pi is connected with in same network as your Sitecore instance.In my case, my Pi and Sitecore workstation both are connected to m home WIFI.

Sitecore :

Before we start integrating it with Sitecore. You can open your browser and try to access Pi URL as given below:

  1. http://<IPADDRESSOFYOURPI>/on : To on LED
  2. http://<IPADDRESSOFYOURPI>/off : To off LED

Just a note : To get IP address of your Pi, Open terminal and type command “ifconfig”

If that works, Then rest of the work is very straightforward. Because that’s how Sitecore has been built. You can integrate it with anything. That’s why we love Sitecore!

  • ON LED on Publish Begin : To do that, We’ve added our event handle on publish begin.

Configuration:

      
        
      

Publish Begin Handler :

// https://sdn.sitecore.net/Scrapbook/Change%20items%20during%20the%20publishing%20process.aspx
namespace Sitecore.Hack.HI.Extension.Publishing
{
    public class PublishBegin
    {
        private const string BASEURL = "http://192.168.0.100/";
        public void OnPublish(object sender, EventArgs args)
        {
            SitecoreEventArgs sArgs = args as SitecoreEventArgs;
            if (sArgs != null && sArgs.Parameters.Length > 0)
            {
                Sitecore.Publishing.Publisher pbl = sArgs.Parameters[0] as Sitecore.Publishing.Publisher;
                if (pbl != null && pbl.Options != null && pbl.Options.RootItem != null)
                {
                    WebRequestHelper.CallURL(BASEURL + "on");
                    //Sitecore.Diagnostics.Log.Info("Publish begin...", this);
                }
            }
        }

    }
}
  • OFF LED On Publish Complete :

Configuration:

      <event name="publish:complete">
        <handler type="Sitecore.Hack.HI.Extension.Publishing.PublishComplete, Sitecore.Hack.HI.Extension" method="OnPublishComplete"/>
      </event>

Publish Complete Handler :

// http://blog.horizontalintegration.com/2014/03/19/sitecore-publishing-events-publishcomplete-and-publishend/
namespace Sitecore.Hack.HI.Extension.Publishing
{
    public class PublishComplete
    {
        private const string BASEURL = "http://192.168.0.100/";
        public void OnPublishComplete(object sender, EventArgs args)
        {
            var sitecoreArgs = args as Sitecore.Events.SitecoreEventArgs;

            if (sitecoreArgs == null) return;

            var publishingOptions = sitecoreArgs.Parameters[0] as IEnumerable;

            if (publishingOptions == null) return;

            //Log.Info(string.Format("OnPublishComplete: languages: {0}", string.Join(", ", publishingOptions.Select(x => x.LanguageName))), this);
            WebRequestHelper.CallURL(BASEURL + "off");
        }
    }
}
  • WebRequestHelper  : Here’s how Web Request helper looks like :
namespace Sitecore.Hack.HI.SPUT.Helpers
{
    public class WebRequestHelper
    {
        public static void CallURL(string url)
        {
            try
            {
                // Create a request for the URL. 
                WebRequest request = WebRequest.Create(
                  url);
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                // Get the response. // TODO : Async
                WebResponse response = request.GetResponse();                
                response.Close();
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error("Can't call URL : " + url, ex, new object());
            }
        }
    }
}

Now, things will be pretty much clear to you that how it works! — Basically, Sitecore sends signal to Pi to on/off LED as per publish operation.

Would like to say special thanks to my colleagues Daniel Schoenberg for sharing Pi Basics, and Nathan Miller for helping to get Raspberry Pi from Amazon US

So, this was my small PoC for IOT with Sitecore. And would like to see your BIG Idea? If you do so, would request you to tag it with #SitecoreIOT  hash tag!

Finally, Happy Teachers’ day! http://www.careers360.com/news/6756-Prakah-Iyer_Gift-your-teach-a-pencil

Advertisements

2 Comments

Leave a Comment
  1. Mrunal / Sep 14 2015 7:48 am

    Really impressive…. 🙂 good job Kiran

    • kiranpatils / Sep 14 2015 10:27 pm

      Mrunal – Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: