A Smart Sensing Application For Asset Monitoring
A revised version of this tutorial can be found here. The following guide is now archived and may no longer match current software interfaces
Asset Monitoring is an important horizontal solution for IoT. This project is an example of a retrofit Asset Monitoring use-case using an Electric Imp-powered device and the Electric Imp IoT Connectivity Platform. In this example, the asset you are monitoring is a refrigerator—integrating current temperature, humidity, and fridge door status information with Salesforce IoT. You analyze real-time data with Salesforce to detect anomalous conditions and create business processes within your Salesforce organization.
The project can be completed using one of two Electric Imp–powered devices: the impExplorer™ Kit or the Battery Powered Sensor Node (BPSN). Whichever of these devices you use, it reads temperature and humidity data from its onboard sensors, and sends those readings to Salesforce IoT using Platform Events. The device also senses light and uses this to determine whether the fridge door is open (bright light) or closed (no light). The light-level reading is also sent to Salesforce using Salesforce IoT Platform Events.
Because the Electric Imp Platform abstracts away the underlying hardware, the same software can run on both devices. The majority of IoT projects start with a PoC and eventually move to a production-grade product. Electric Imp’s technology makes that migration seamless. By moving from prototype to mass deployment on the same underlying platform, you can minimize complexity, risk, cost and time to market.
Salesforce stores the incoming data and opens a Case using IoT Explorer Orchestration if one of the following occurs:
Each of these thresholds will be defined later when you set up the IoT Explorer Orchestration.
Note 1 Salesforce has two different desktop user interfaces: Lightning Experience and Salesforce Classic. This project is designed for Lightning Experience. You can learn about switching between interfaces, enabling Lightning Experience and more in the Lightning Experience Basics module on the Salesforce Trailhead website.
Note 2 In the following tutorial, text in bold typically indicates an user-interface element, such as a web page section, a button or an option you select from a list. Text in code form
indicates information you need to type into a web page or app.
First we need to assemble the impExplorer Kit. The Kit comes with these components:
Now set up the hardware:
Assembled, it should look like this:
Assembled, it should look like this:
A challenge for connected products is being able to securely provision a device to a network without a user interface to enter credentials on the device. To solve this problem, Electric Imp uses an optical provisioning process called BlinkUp™, which securely provides a device with Wi-Fi access credentials and authenticates and enrolls it into the Electric Imp impCloud™ in a single step. This is done via the Electric Imp mobile developer app for this project and for other PoCs and prototypes. For production-level products, the BlinkUp functionality can be incorporated as an SDK in a customer’s mobile application or via JavaScript in a web browser.
You will need to use BlinkUp to add your device to your Electric Imp account:
Note When you enter your WiFi details, the app may set the Network Name field for you. It will enter the name of the network your phone or tablet is connected to. This may not be the one you wish to connect your impExplorer to; if not, just tap on the name to key in the correct SSID.
The impExplorer or BPSN needs to connect to a 2.4GHz 802.11n Wi-Fi network, so you will need to make sure one is available. This is especially the case if your phone is connected to a 5GHz network as the impExplorer or BPSN will not be able to connect to this. You may need to set up a separate or guest network to try this example. If in doubt, consult your network manager.
If you have any issues getting started with your Electric Imp account or device, please follow Steps 1 through 7 in the Getting Started guide.
What differentiates Electric Imp from traditional IoT approaches is that all components and services of a solution are designed, integrated and managed together.
The Electric Imp IoT solution starts with the purpose-built impOS™, which provides secure cloud connectivity services to products, enabling users to concentrate on creating code to bring their products’ functions to life.
impOS runs on an imp-Authorized module, which ensures silicon-level hardened security. The imp-authorized module is placed inside a device, component, or product and provides a powerful local compute capability and connections to application-specific sensors and actuators. The module also provides local intelligence and IP-based cloud connectivity via Wi-Fi, ethernet or cellular. The module is not manufactured or sold by Electric Imp, but instead available through Electric Imp’s hardware distribution partners.
The impModule connects securely to the impCloud agent, which is a fully programmable middleware. The impCloud agent enables custom in-flight data processing and integrations to the customer’s downstream cloud data and application services. This unique architecture delivers the flexibility necessary for real-world IoT solutions and is what makes scaling solutions effortless and automatic. The impCloud agent operate as the device’s front end to the Internet. It securely connects to Salesforce IoT via a high-performance cloud-to-cloud REST integration, letting you easily push data to and take commands from Platform Events. In this example, the agent code receives data from the device and forwards it to Salesforce as a Platform Event, as the following diagram shows.
When application code is deployed via the Electric Imp online development environment, impCentral™, to the device, impOS automatically restarts the device code and establishes a secure, managed connection to its paired agent. At a high level, the device code performs the following functions:
When the code is deployed via impCentral to the agent container, the agent code is restarted and connects to its paired device over a secure, managed connection. At a high level, the agent code performs the following functions:
Electric Imp applications are developed and managed in an online tool called impCentral. This web app provides all the facilities you need to write and deploy the software over the air to the device and to the agent that controls your imp-enabled connected product. impCentral also provides the tools for managing your connected devices once they have been deployed in the field, including deploying software to beta test groups, ensuring device operation isn’t interrupted by a deploy, and the ability to remotely diagnose issues from anywhere in the world. For more details on impCentral, see Introduction to impCentral
The data received from the device may be processed, stored and analyzed within Salesforce, and used to trigger business processes and workflows. This example demonstrates just a few of the many capabilities of combining Salesforce IoT with customer context. It can easily be modified and extended to meet your particular business use case.
In this example, you create the following Salesforce entities:
The Platform Event acts as an interface between the imp application and Salesforce. The Platform Event fields must have the names and types used in this example (see Step 6. If you change anything in the Platform Event definition, you will need to update the imp application’s agent source code. The name of the Platform Event is set in the agent code by the constant READING_EVENT_NAME.
All other entities listed above are fully independent of the imp application.
In this project, we explore a specific example, but this is just one scenario you can use. As you continue to explore using Electric Imp with Salesforce, you can try out different scenarios with new fields, rules, sets of entities, and more.
This stage is used to authenticate the imp application in Salesforce:
App Manager
in the Quick Find box and then select App Manager:Electric Imp SmartFridge
Electric_Imp_SmartFridge
.The Custom Object will be used to store the data received from the device.
SmartFridge
SmartFridges
SmartFridge
humidity
4
2
humidity
ts
ts
Platform Events transfer the data from the device to Salesforce. A Platform Event Trigger inserts the received data into the Custom Object we just defined.
The Platform Event fields must have the names and types mentioned here. If you change anything in the Platform Event definition, you will need to update the imp application’s agent code. The name of the Platform Event is entered into the agent code as a constant, READING_EVENT_NAME.
Platform Events
into the Quick Find box and then select Platform Events:temperature
4
2
temperature
humidity
4
2
humidity
door
10
door
ts
ts
deviceId
16
deviceId
trigger SmartFridgeReadingReceived on Smart_Fridge_Reading__e (after insert) {
List<SmartFridge__c> records = new List<SmartFridge__c>();
for (Smart_Fridge_Reading__e event : Trigger.New) {
SmartFridge__c record = new SmartFridge__c();
record.deviceId__c = event.deviceId__c;
record.temperature__c = event.temperature__c;
record.humidity__c = event.humidity__c;
record.door__c = event.door__c;
record.ts__c = event.ts__c;
records.add(record);
}
insert records; }
Note Typically, you would use an Orchestration to update the SmartFridge records, but since we are not implementing any logic or decision-making for this step, we use the Apex Trigger as an alternative to Orchestration. This approach may help you with any troubleshooting.
A context allows Salesforce IoT to match streaming event data with contextual data that you already store in Salesforce. Records between Platform Events and Salesforce Objects are matched via a key field, which you define when setting up the context, and is required for building an Orchestration. This combination allows you to set up rules in your Orchestration that can trigger relevant actions for your business.
Salesforce IoT
into the Quick Find box, select Get Started and then Enable Salesforce IoT in the bottom right of the panel:Contexts
into the Quick Find box and then select Contexts:Smart Fridge Context
Smart_Fridge_Context
This example uses customized Cases which are standard Case objects with an additional field for device ID.
A Salesforce Orchestration defines a state machine, with devices moving to between different states depending on real-time event data and historical customer and contextual data. Actions and outputs are defined by customizable rules for each State, and let you define tailored experiences for each device. You can see in the following diagram how a fridge is normally in the Default state but will move to one of the other states as it reacts to Platform Events.
In this example, as the device moves into these other states, the Orchestration opens Cases based on the following specific rules, which are set up as part of the Orchestration:
If you are wondering why there are no transitions between Open, Temperature Over Threshold, and Humidity Over Threshold, it is because such transitions would result in multiple cases being opened simultaneously. This should be avoided, so the state machine is set up to ensure that further cases won’t be created until a given issue is resolved and the impExplorer returned to the Default state.
Orchestrations
into the Quick Find box and select Orchestrations:TEMPERATURE_THRESHOLD
11
(for 11°C).HUMIDITY_THRESHOLD
70
(for 70%).DOOR_OPEN_LIMIT
3
(for three consecutive data readings with door status).door_open_counter
Smart_Fridge_Reading__e.door__c = "open"
.0
Smart_Fridge_Reading__e.door__c = "closed"
Click Rules and create three orchestration rules:
Door Open
as the new state name.Smart_Fridge_Reading__e.door__c = "closed"
door_open_counter >= DOOR_OPEN_LIMIT
Smart_Fridge_Reading__e.deviceId__c
Refrigerator Door Open
door has been opened for too long
Create Door Open Case
Temperature Over Threshold
as the new state name.Smart_Fridge_Reading__e.temperature__c < TEMPERATURE_THRESHOLD
Smart_Fridge_Reading__e.temperature__c >= TEMPERATURE_THRESHOLD
Smart_Fridge_Reading__e.deviceId__c
"Temperature Over Threshold"
"current temperature " + TEXT(Smart_Fridge_Reading__e.temperature__c) + " is over threshold"
.Create Temperature Case
Humidity Over Threshold
as the new state name.Smart_Fridge_Reading__e.humidity__c < HUMIDITY_THRESHOLD
Smart_Fridge_Reading__e.humidity__c >= HUMIDITY_THRESHOLD
Smart_Fridge_Reading__e.deviceId__c
"Humidity Over Threshold"
"current humidity " + TEXT(Smart_Fridge_Reading__e.humidity__c) + " is over threshold"
Create Humidity Case
[Agent] ERROR: Not logged into Salesforce
, it means your application is not authorized to connect to Salesforce. This example uses OAuth 2.0 for authentication, and the agent has been set up as a web server to handle the authentication procedure.
"Authentication complete - you may now close this window
.[Agent] Readings sent successfully
.Open your refrigerator and place the impExplorer Kit on a shelf in your refrigerator door.
If you don’t have a fridge handy for this scenario, you can test the example by emulating different conditions. For example, you can emulate a fridge door being open or closed by placing the impExplorer under a light or into a really dark place. Emulate the high-temperature case by moving the device from a cold place to a much warmer one.
You can use the Salesforce mobile app to see the data that your device sends. To do this, perform the following tasks.
This is needed to make the Custom Object with the stored data from the device accessible by the Salesforce mobile app.
Tabs
into the Quick Find box and select Tabs.SmartFridge
in the Find an app or item box, then click SmartFridge. From the List View dropdown menu, click All SmartFridges. You should see readings sent from your impDevice (you may need to refresh your browser to get the latest reading).Salesforce
into the Quick Find box and select Salesforce.Users
in the Quick Find box and select Users.You can access and run the Salesforce mobile app in three ways:
Use this approach if you want or need to run the Salesforce mobile app on your PC, not on a mobile phone. It is possible to emulate the Salesforce mobile app in the Chrome web browser.
lightning.force.com/lightning
. For example:You can now see transitions between the states that you defined in the IoT Explorer Orchestration section, as well as registered Cases.
Orchestrations
into the Quick Find box and select Orchestrations:Note The Orchestration Traffic view is on a seven-second refresh, but Platform Events are processed as they are received. This means you may see the Case created before the UI refreshes.
Congratulations! You’ve completed the first Salesforce IoT Trailhead project. If you’d like to carry your project forward and learn more about developing applications on the Electric Imp platform, you can check out our series of nine examples Introducing Squirrel Applications. These examples build up skills on core Electric Imp development concepts, including creating battery- efficient applications, integrating with external web services, and culminate in building the Salesforce Smart Fridge Application Code.