Raspberry Pi with Salesforce IoT Explorer Edition

Author : Dinesh Kumar Wickramasinghe

Salesforce IoT Explorer Edition

Salesforce is the world number one CRM (Customer Relationship Management) software provider. Salesforce is well known for their Sales Cloud, Service Cloud and Marketing Clout platforms. Salesforce recently introduced their IoT (Internet of Things) cloud to produce more new business opportunities from the streaming data coming from the internet connected products. Salesforce recently enabled the IoT cloud explorer edition for free developer editions.

This tutorial is about implementing a simple IoT project with Salesforce IoT Cloud Explorer edition and 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.

Before continue, here is a video clip I took while doing this project. Sorry for the bad quality, I recorded this using my mobile phone.

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

To start investigating more about Salesforce and to complete this tutorial, you can 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,

  1. Salesforce IoT Explorer edition basics trail

You need to complete the above trail to continue with this project. You can complete this trail by creating a free salesforce developer account by following the link given in the first section. This trail will teach you following things,

  • Introduction to Salesforce IoT Explorer edition
  • Creating Platform Events
  • Getting data from Salesforce standard objects in IoT scenarios
  • Creating contexts
  • Setting up Orchestration
  1. 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 s simple Python program.

So, this tutorial will merge above two projects and build a real time temperature data service to Salesforce IoT Explorer edition. So that, completing above two projects will be a good advantage to continue this project.

Use Case

If you’ve already completed the Trailhead module, you know the use case. Anyway, here again the use case from the trail.

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.

Salesforce IoT Explorer Orchestration Rules

It has a rule to change the status from Default status to High Temperature status and also create a new Case record when it change the status to High Temperature.

But there is no rule to change the status back to Normal (Default) status from the High Temperature status. So, we will add a new rule to do this. Follow these steps.

First, rename the Default status as "Normal"

Rename default orchestration rule Salesforce IoT Explorer

Click on the three dots icon on the right side of the High Temperature status and clock on "Add rule"

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

Salesforce IoT Explorer edition orchestration add event to rule

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 reduced less than or equal to 50 degrees.

Click on STATES to see the state machine for your rules. Now it should look like the below image.

Salesforce IoT Explorer edition orchestration STATES tab

Activate your orchestration to continue.

Salesforce IoT Explorer edition orchestration activate

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 done the DS18B20 Thermometer Project 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 for me.

Python install request library

Enter the below command to install the Simple Salesforce library

pip install simple_salesforce
Python install simple salesforce

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

Schematic
Raspberry Pi DS18B20 Schematic

Use the above schematic to setup your devices. I used the waterproof version of the DS18B20 digital temperature sensor. 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. 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 case when the temperature exceeds 50 degrees.

Running the Project

Run the above python program by entering the below command

Sudo Python sfdc18b20.py
Salesforce IoT Explorer edition run python raspberry pi

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 sent a platform event to Salesforce.

If the temperature of the sensor is below 50 celsius degrees, the status will be Normal on your orchestration.

Go to the traffic view of your Salesforce orchestration and you will see that it is in the Normal states.

Salesforce IoT Explorer edition orchestration normal state

Increase the temperature of the sensor above 50 degrees. (You can put your sensor inside a hot water glass for this)

Now when the temperature increased above 50 degrees, the status will be changed to "High Temperature" as below.

Salesforce IoT Explorer edition orchestration high temperature state

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

Salesforce IoT Explorer edition new IoT case created

When the temperature decreased 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 developer orgs) and the IoT cloud Scale edition which commercially available. The scale edition has more powerful features than the Explorer edition.

If you faced any difficulties while doing this project or if you have any question regarding this project, please comment. Comments are always welcome and I am ready to help. Good luck!

Comments Area
Add your questions and comments here. Comments will be published after the admin approval
Published By : Steve Fouracre
Added Date :5/8/2019 8:59:54 PM
Once IoT has done whatever actions in Salesforce if the response is "success" : false. How do I then get Raspberry to perform a different action ?
Published By : Karina Santos
Added Date :8/30/2018 12:04:43 AM
Hi, I'm trying to complete Salesforce IoT Explorer edition basics trail, but keep receiving this error: "message": "Error occurred during engine runtime: The flow couldn't find the CONTEXT_RECORD resource. (With Formula: {!Flying_Fridge_Event_PLATFORM_EVENT_VARIABLE.temperature__c} >{!CONTEXT_RECORD.Max_High_Temp__c})". If I put a constant, instead of a context reference, everything works fine. Any ideas on what might be happening? Thanks a lot!!!

 

Similar Projects

Go Top