AuthorDinesh Kumar Wickramasinghe

Salesforce is the world’s number one CRM (Customer Relationship Management) software provider. It is well known for their Sales CloudService Cloud and Marketing Cloud platforms. They recently introduced IoT (Internet of Things) cloud to create new business opportunities for the streaming data received through internet connected devices. Salesforce recently enabled a IoT cloud explorer edition free of charge for the developers to explore.

This blog post is about implementing a simple IoT project with Salesforce IoT Cloud Explorer edition and a Raspberry Pi single board computer. We will use a digital temperature sensor (DS18B20) to measure the temperature and generate Salesforce Platform Events from Raspberry Pi.

If you are willing to learn more about Salesforce platform, please visit their interactive learning platform: Salesforce Trailhead.

To start learning more about Salesforce, please create a free developer account by following this link: https://developer.salesforce.com/signup

Prerequisites

To continue this tutorial, you need to complete two existing projects. After that you are ready to begin. Those are,

Salesforce IoT Explorer edition basics trail

You can complete this trail by creating a free Salesforce developer account here (if you have not already done so.) This trail will teach you the following;

  • Introduction to Salesforce IoT Explorer edition
  • Creating Platform Events
  • Getting data from Salesforce standard objects in IoT scenarios
  • Creating contexts
  • Setting up Orchestration

DS18B20 Thermometer with Raspberry Pi Project

Above tutorial will guide you how to connect DS18B20 digital thermometer to Raspberry Pi and measure the temperature using a simple Python script.

Below is a video clip I took while doing this project. Quality is low since it was recorded using a mobile phone.

So, this tutorial will use both of above projects and build a real time temperature data service to Salesforce IoT Explorer edition.

Use Case

If you’ve already completed the Trailhead module, you know the use case. However it is included below.

“Flying Fridge is an existing Salesforce customer that builds and sells airplane refrigerators to commercial jet manufacturers. They have just started producing connected refrigerators and want to build a new service around their product. As a starting point, Flying Fridge wants to monitor the interior temperature of the refrigerators and open a service case if a refrigerator’s interior temperature is above 50 degrees Celsius.”

Modifying the Orchestration Rules

In this step we will do a little modification to the Orchestration that we created in the Salesforce IoT Explorer edition basics trail. Your existing orchestration rules should look like the below screenshot.

It has a rule to change the status from “Default” to “High Temperature” and also to create a new case entry along with the status change.

But as you see there is no existing rule to change the status back to “Default” (normal)  status from the “High Temperature” status.  Please follow these steps in order to add a new rule for that.

First, rename the Default status as “Normal”.

Click on the three dots icon on the right side of the High Temperature status and click on “Add rule”.

Click on the drop down menu icon and select Fridge__Event__e platform event.

 

Then add the below formula for the condition.

Fridge_Event__e.temperature__c <= Asset.Max_High_Temp__c

From Transition, select “Normal”.

 

This new rule will change the status back from “High Temperature” to “Normal” when the temperature is less than or equal to 50 degrees Celcius.

Click on “States” tab to see the state machine for your rules. Now it should look like the below image.

Activate your orchestration to continue.

 

Note : Tick the check box “Delete all existing orchestration instances” when you activate it.

Configuring Raspberry Pi

I used the Raspberry Pi 3 Model B for this project. I assume that you’ve already taken DS18B20 Thermometer trailhead module and done the system configuration to support the One Wire protocol for Pi. So, I will not explain those steps again here.

You need to install below two additional python libraries for this project.

  • Requests

The Requests library will help to perform web service requests. In some cases the request library will be available in Raspbian OS by default.

  • Simple Salesforce

Simple Salesforce is a nice library written for python programmers to deal with Salesforce easily. Follow this link to learn more about the Simple Salesforce library.

Simple Salesforce Link

Open a new terminal window and enter the below command to install the Request library. (These commands will download and install libraries. So your Pi should connected to the internet)

pip install requests

 
I have the Raspbian Jessy operating system and the requests library is already available in it.

Enter the below command to install the Simple Salesforce library

pip install simple_salesforce

 

Now your Raspberry Pi has the required software to complete this project.

Schematic

Use the above schematic to setup your devices. Waterproof version of the DS18B20 digital temperature sensor is used in this example. It needs an external 4.7 K resistor as on the above schematic.

Python Program

Here is the python source code for this project.


from simple_salesforce import Salesforce

import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

sf = Salesforce(username='your-sfdc-username', password='password', security_token='security-token')
print(sf);

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f
		
while True:
	print(read_temp())
	data = [{'serial_no__c': '1001','door_open__c': 'false','temperature__c':read_temp()[0]}]	
	sf.bulk.Fridge_Event__e.insert(data)
	time.sleep(5)
	print("Platform Event Sent")

 

Save the above code as a new python code. Use the file name sfdc18b20.py

Please note that the Simple Salesforce python library provides many ways to authenticate and connect to your Salesforce account. I used my Salesforce account’s username, password and the security token to authenticate and connect. So, change the below line accordingly.

sf = Salesforce(username='your-sfdc-username', password='password', security_token='security-token')

 
This program will read the temperature from the sensor and perform a web service call to generate platform events on Salesforce every 5 seconds. According to our use case, you can assume that the temperature reading is coming from the airplane refrigerator 😉 .

The Orchestration on Salesforce will open a new case when the temperature exceeds 50 degrees.

Running the Project

Run the above python program by entering the below command

Sudo Python sfdc18b20.py

 

 
The terminal window will show the temperature values in both Celsius and Fahrenheit formats and it will also show a message “Platform event sent” when the device successfully sends a platform event to Salesforce. If the temperature of the sensor is below 50 Celsius degrees, the status will displayed as Normal on the orchestration. Go to the traffic view of your Salesforce orchestration and you will see that it is in the Normal states.

Increase the temperature of the sensor above 50 degrees. (You can put your sensor inside a hot water glass for testing purpose.) Now when the temperature increased above 50 degrees, the status will be changed to “High Temperature” as shown below.

If you open cases on Salesforce, you will see that a new case has been created to indicate the service team about this incident.

Remove the sensor out of the hot water glass. When the temperature decrease below 50 degrees, the status will change back to Normal.

Conclusion

Hope you enjoyed this project. Please note that Salesforce introduced Two editions of the IoT cloud. Those are IoT Explorer edition (Freely available for developers) and the IoT cloud Scale edition which is the commercial version. The scale edition has more powerful features than the Explorer edition.Dinesh Wikramasinghe is a Senior Software Engineer at CMS.  He is very passionate about electronics and IoT. Dinesh was a presenter at Indiadreamin’ 2017 where he did a session on Real time sensor data. He did a tech talk on the same topic at CMS on 28th March, 2018. You can read more about his electronic projects on his personal blog.

Author : Administrator
Published Date April 9, 2018
Share Post
[ssba-buttons]