For Smarter Homes – Integrating CloudBit with Salesforce
minutes read
Imagine this! If you could control devices at home like your TV or light bulb when you are sitting at office miles away, and that too with just a few clicks.. Wow! But how?
Now you might be thinking it’s a herculean task where you will have to set up a platform, a database, a website host and what not to achieve this. Sounds like a lot of work! Then, you are wrong! All you need is:
- A free Salesforce developer account
- CloudBit
- And of course, an internet connection with WiFi
The concept is really simple. You create records for the respective devices in Salesforce and add an ON and OFF custom button to it. So, whenever a user clicks ON/OFF from a remote location, the device acts accordingly. So how do you execute this? To help you understand it better, here’s a flowchart for the process.
Flowchart
Setting up CloudBit
The first and foremost thing to get you started is setting up the Cloudbit device. CloudBit is a product of LittleBits and helps you make your own internet-connected creations. For this, go to the littleBits login page where you have to SignUp for an account and also configure your Wi-Fi Settings. Once signed up, you are ready to set up your CloudBit by just following the onscreen instructions. Make sure that your CloudBit device is in connected state.
Setting up Salesforce
Now, you move on to Salesforce and create Custom Settings named ‘CloudBit API Setting’. This will contain a record for storing the CloudBit Device Id, the Endpoint and also the Access Token required for authentication.
Setting up the Remote Site
You will also have to create a new remote site named ‘CloudBit_API’ with ‘https://api-http.littlebitscloud.cc’ as the value.
Once you are done with all the settings in the org, you can start making API calls to CloudBit servers. Here’s how I did this with an LED at home.
Step 1: I created a method within a class which is made to accept three values Percent, Duration and the Record Id as parameters. Percent represents the percentage of output that has to be displayed and duration represents for how long my output should be displayed. The record id contains the value of the current record in Salesforce which represents the device that we have to control. The method is also made to grab the Endpoint, Access Token and the Device Id from the Custom Settings which I had created earlier and appends DeviceId at the appropriate position in the Endpoint.
Step 2: Now the next step will be to construct the body of our Request which is of type JSON. Here is a sample of how the body has to be constructed.
{“percent”:75, “duration_ms”:1000}
As you probably noticed, I appended the values of percent and duration which were received as parameters to the body.
Step 3: The requests to LittleBits Cloud API will also require three extra headers.
- First one is the Authorization Header, which will contain the Auth Key.
Example:”Authorization: Bearer AUTH_KEY” - Second is the Accept Header, which should be done in the following way.
“Accept: application/vnd.littlebits.v2+json” - Third is the Content-Type header which tells the Server that I am sending a JSON body in our Request.
“Content-Type: application/json”
If the percent value received is not equal to zero, then we change the value of Switched_ON checkbox in our org to TRUE which denotes checked. And if the percent value is zero, change it to FALSE. This checkbox is to denote whether a switch is in ON or OFF position.
You can check out the code in the following link.
CloudBit : http://bit.ly/cloudbitclass
Step 4: I also created two Custom Buttons in which the percent, duration and record id values are passed to the method on clicking. In the ON button I set the percent not equal to zero and in OFF button I set the percent equal to zero. In both the cases, the duration value will be set to -1 which means, the output will be shown for infinite time. The unit for this parameter is milliseconds. We can give whatever value we want to display the output for. (Example: 5000ms, 6000ms )
CloudBit ON Button : http://bit.ly/cloudbiton
CloudBit OFF Button : http://bit.ly/cloudbitoff
Step 5: Once you have made a successful request of switching ON, the LED which is connected to CloudBit turns ON. And if the request is to turn OFF, then the LED turns OFF.
Fig 1.2: Device is Switched On
Fig 1.3 Device is Switched Off
You can try this out by replacing the LED used here with any other device. We’d love to hear your comments on trying this in the comments section below.