In my previous salesforce and littleBits Integration post, I have explained about sending information from the littleBits device to Salesforce, when there is a button click (physical button click) on the device it will insert a new record in salesforce. In a simpler word, we sent information from littleBits to Salesforce. Now in the part-2, we are going to send information from salesforce to littleBits device. In this post, I'm going to explain how to turn on and Off the LED light connected to my littleBits device from salesforce

Step 1: Setting up littleBits Device
For this demo, you need one cloud bits, one USB power, one LED module and an external charger for power. Setup your littleBit device kits as shown in the below picture

Now go to https://control.littlebits.cc and copy the device id and access token which is required to send information from salesforce to littleBits.
Step 2: Setting up Salesforce
Here we are going to setup salesforce to send information to littleBits devices to turn on and off the led light when there is an event occur in salesforce. In this example let take an example of chatter post, when I'm going to put chatter post with hashtag topic #lightON it will turn on the LED light if a post with hashtag topic #lightOFF it will turn off the LED light on the device. You can build as many use case you want like when opportunity is closed and when case record is closed etc., Let's see the logic
Apex Logic
The below apex trigger code will make an HTTP callout with the help of the helper class. If the chatter body contains #lightON then it will call out helper class littleBitsLEDhelper.turnOnLED and suppose if the chatter post contains #lightsOFF then it will call the littleBitsLEDhelper.turnOffLED to turn the off the LED light.
Below is the apex class helper class which makes the HTTP callout to the littleBits API
Demo:
Now we successfully sent information from salesforce to littleBits device to turn on and off the LED light

Step 1: Setting up littleBits Device
For this demo, you need one cloud bits, one USB power, one LED module and an external charger for power. Setup your littleBit device kits as shown in the below picture

Now go to https://control.littlebits.cc and copy the device id and access token which is required to send information from salesforce to littleBits.
Step 2: Setting up Salesforce
Here we are going to setup salesforce to send information to littleBits devices to turn on and off the led light when there is an event occur in salesforce. In this example let take an example of chatter post, when I'm going to put chatter post with hashtag topic #lightON it will turn on the LED light if a post with hashtag topic #lightOFF it will turn off the LED light on the device. You can build as many use case you want like when opportunity is closed and when case record is closed etc., Let's see the logic
Apex Logic
The below apex trigger code will make an HTTP callout with the help of the helper class. If the chatter body contains #lightON then it will call out helper class littleBitsLEDhelper.turnOnLED and suppose if the chatter post contains #lightsOFF then it will call the littleBitsLEDhelper.turnOffLED to turn the off the LED light.
LEDlights.apx
trigger LEDlights on FeedItem (after insert) {
for(FeedItem fdi : Trigger.New){
if(String.Valueof(fdi.body).touppercase().contains('#LIGHTON') ){
littleBitsLEDhelper.turnOnLED();
}
else if(String.valueof(fdi.body).touppercase().contains('#LIGHTOFF')){
littleBitsLEDhelper.turnOffLED();
}
}
}Below is the apex class helper class which makes the HTTP callout to the littleBits API
littleBitsLEDhelper.cls
global class littleBitsLEDhelper {
@future(callout=True)
public static void turnOnLED(){
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://api-http.littlebitscloud.cc/devices/<deviceID>/output');
req.setHeader('Authorization','Bearer <AccessToken>');
req.setHeader('Accept', 'application/vnd.littlebits.v2+json');
req.setBody('percent=100&duration_ms=32000' );
HttpResponse response = h.send(req);
System.debug('Response '+response);
}
@future(callout=True)
public static void turnOffLED(){
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://api-http.littlebitscloud.cc/devices/<deviceID>/output');
req.setHeader('Authorization','Bearer <AccessToken>');
req.setHeader('Accept', 'application/vnd.littlebits.v2+json');
req.setBody('percent=0&duration_ms=' );
HttpResponse response = h.send(req);
System.debug('Response '+response);
}
}Demo:
Now we successfully sent information from salesforce to littleBits device to turn on and off the LED light
