My kids leave the lights on at home. All the time. It’s so bad, I decided to tackle the problem at its source, by introducing an approval step into the ‘switch light on’ process via Force.com. Actually, I’m joking; Kevin O’Hara and Reid Carlberg‘s antics inspired me to try hooking my Raspberry Pi to the platform. I recently bought a PowerSwitch Tail II, so I was looking for a project where I could control some simple appliance; this is what I came up with.

//www.youtube.com/watch?v=ee2dT8cvJcs

Here’s how that all works…

Starting with the tactile button switch; it’s wired to pin 24 of my Raspberry Pi. A simple Python app running on the Pi uses the OAuth 2.0 Resource Owner Password Credentials Grant (also known as the username-password flow) to authenticate to Force.com and obtain an access token. The app samples the state of the button every hundredth of a second. If the state of the button (up/down) is different from the last sample, the app sends an HTTP POST to an Apex REST method with the access token in an HTTP header and the state of the button as its payload.

The Apex REST method queries for the appropriate Light__c custom object record (the code assumes there’s just one right now, but an identifier for the light could be added to the URL) and, if the state of the light is Off, modifies the state to Request On, updates the record, and fires the approval process. If the state of the light is On, it simply modifies the state to Off and updates the record. I’ll let my kids turn the light OFF whenever they like! The approval process includes an action to change the state from Request On to On, assuming I give my approval!

A trigger on Light__c detects the state transition to On or Off and, since it cannot make a callout directly, invokes a @future method. That method sends an HTTP POST with the desired state of the light to a second Python app running on the Pi. I used Flask, a lightweight Python web framework, for this app; the effectiveness of Flask is demonstrated by the fact that the web app required less than 20 lines of code. This blog post guided me in my choice of Flask, and its installation on the Pi. I also had to assign the Pi a static IP address, and configure my router to forward incoming traffic on port 5000 to the Pi, so I could access the Pi from the cloud.

The app completes the chain of events by changing the state of pin 25 of the Raspberry Pi according to the incoming POST, pin 25 driving the PowerSwitch, which makes a satisfying ‘clack’ as its relay actuates.

So – how useful is this? Not very, given that it’s easy to subvert by just removing the PowerSwitch and plugging the lamp into the outlet; not to mention the fact that the POST to control the lamp is not secured in any way, but it demonstrates that we can control external processes with building blocks like Force.com Approvals. Instead of a button, there might be an incoming SOAP message from some legacy system. Instead of the light, there might be an outgoing message sent to some ERP system.

And, you know what, hitting ‘Approve’ and seeing the lamp light was a LOT of fun.

What are you going to make with Force.com?

tagged , Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • http://www.laceysnr.com/ Matt Lacey

    Too good for words Pat, love it!

  • Joey Chan

    Awesome demo! Reminds me of the awesome entry by Mike Leach!

  • http://blog.shivanathd.com/ Shivanath D

    Respect ! … you deserve a Harlem Shake !

  • http://twitter.com/andyinthecloud Andrew Fawcett

    Nice job Pat, must get myself one of those! In the meantime my project to get a Lego Robot talking to Salesforce Chatter has just reached a key milestone as well, check out the video! http://brickinthecloud.com/2013/03/08/its-alive-lego-robot-and-salesforce-chatter-demo/

  • http://twitter.com/agileChris Chris David

    Nice! One of

  • Mitesh Chauhan

    Fantastic, Pat!
    I’d love to see what you could do with Force.com + Raspberry Pi + MakeyMakey!!!

  • Cloud Technologist | ISVForce

    Nice demo, I wonder what people will come up with in next couple of years?
    Any body in Austin, TX area wanting to take this to next level? or do a web conference (1 hour per week) to develop fun projects?

    • http://blog.superpat.com/ Pat Patterson

      I’m in the SF Bay Area, but I’d certainly be open to an hour a week on Google Hangout or similar…

  • Yogesh Bang

    Internet of THINGS ( IoT ) at work.

  • Mohammad Swaleh

    Hi Patt, This is really fantastic. Getting an inspiration from this post, I was trying to assemble this very setup. I got all the hardware needed. Can you please explain the connection with PowerSwitch, the lamp and the R-Pi? Thanks you very much for your help in advance. :-)