Skip to main content

How To Build A Smart Fridge With Electric Imp, Salesforce And Heroku

A Smart Sensing Application For The impExplorer Kit

Imp In Fridge

Important This example has not yet been updated to make use of Electric Imp’s new IDE, impCentral™, so some screenshots and instructions here may not match what you see in your browser. We will be updating this tutorial shortly.

What You Need

General

  • Your WiFi network name (SSID) and password
  • A smartphone or tablet (iOS or Android)
  • A computer with a web browser

Accounts

Hardware

And if you want to install the board into a fridge:

  • Three AA Batteries

Software

Getting Started

1. Set up the Electric Imp hardware

First we will need to assemble the Electric Imp impExplorer Kit. The kit comes with:

  1. An imp001 card, which has a WiFi radio and a microcontroller that drives all the logic for the board.
  2. The impExplorer board into which the card is plugged. The board provides a set of sensors and peripherals which are ready to use. For this project, it will read temperature, humidity, accelerometer data and light level to determine the current state of your fridge.

1.1 Hardware setup

  1. Plug the imp001 card into the impExplorer board slot.
  2. Power up your impExplorer with the AA Batteries.
  3. The imp001 should now have power and be blinking amber.

Assembled it should look like this:

Explorer Kit

1.2 Electric Imp BlinkUp™

Use the Electric Imp mobile app to configure your device for network access:

  1. Log into your Electric Imp account.
  2. Select ‘Configure a Device’
  3. Enter your WiFi credentials.
  4. Follow the instructions in the app to configure your device.

If you have any issues getting started with your Electric Imp account or device, see the full Getting Started Guide.

2. Enter Code for the impExplorer

2.1 How Electric Imp’s connectivity platform works

The Electric Imp Platform has two main components: the device and the impCloud™. The device runs the device code, which in this use case consolidates the data gathered by the temperature, humidity and light sensors. Each device is paired one-to-one with its own ‘agent’, which runs the agent code in the impCloud. The device sends the data it gathers to its agent. In this example the agent code executes the logic on the sensor data (eg. light levels show the fridge is open) and communicates with the Salesforce cloud. Here’s a broad overview of this flow:

The Electric Imp Platform

The Electric Imp IDE provides all the tools you need to write and deploy the software (to the device and agent) that will control your imp-enabled connected product. The IDE runs in a desktop web browser.

If you'd like a quick overview of the IDE, please visit the full Getting Started Guide.

2.2 The Electric Imp IDE

  1. In your favorite web browser, log into the Electric Imp IDE
  2. Click the + button to create a new model: Empty IDE
     
  3. In the pop up name your model, select your device and click the Create Model button.
  4. Copy and Paste the agent code from Github into the left side, agent window.
  5. Copy and Paste the device code from Github into the right side, device window. IDE code windows
     

3. Create a Custom Object in Salesforce

Note To follow this section, please switch to the Salesforce Classic view by clicking on the profile icon at the top right corner of the screen and selecting ‘Switch to Salesforce Classic’.

3.1 Create a custom object in Salesforce

You will need to create a custom object with fields that correspond to each key in the data table sent by the impExplorer via its agent:

  1. Log into Salesforce and click on the ‘Setup’ tab in the top right navigation menu: Salesforce Navbar
     
  2. In the sidebar under ‘Build’, unfold the ‘Create’ menu and select ‘Objects’.
  3. At the top of the page, click the ‘New Custom Object’ button: Salesforce Custom Object
     
  4. In the ‘New Custom Object’ form, enter:
    • Custom Object Information:
      • Enter a ‘Label’: for example, “SmartFridge”
      • Enter a ‘Plural Label’: for example, “SmartFridges”
      • Enter an ‘Object Name’: for example, “SmartFridge”
    • Enter Record Name Label and Format:
      • Enter a ‘Record Name’: for example, “Reading Id”
      • For the ‘Data Type’, select ‘Auto Number’
      • Enter a ‘Display Format’: for example, “R-{0000}”
    • Finally, click ‘Save’
  5. On the ‘Custom Objects’ page, click on your object name.
  6. You will be redirected to the ‘Custom Object’ page.
    You will repeat step 7 four times to add fields for each sensor reading collected.
    The ‘Field Name’ must match the data table from the device. The ‘Field Names’ in the example code are:
    • temperature
    • humidity
    • door
    • ts
  7. At the bottom of the page under ‘Custom Fields & Relationships’, click the ‘New’ button.
    • Step 1
      • For ‘Data Type’, select ‘Number’ for temperature and humidity, ‘Text’ for door, and ‘Date/Time’ for ts
      • Now click the ‘Next’ button
    • Step 2
      • Enter a ‘Field Label’: “temperature”, “humidity”, “door” and then “ts”
      • Enter a ‘Length’: for temperature and humidity, 4; for door, 10
      • Enter a ‘Decimal Places’ value: for temperature and humidity, 2
      • Enter a ‘Field Name’ — this must match the keys from the device code: “temperature”, “humidity”, “door” and “ts”
      • Enter a ‘Description’: for example, “Temperature reading in °C”
      • Now click the ‘Next’ button
    • Step 3
      • Click the ‘Next’ button again
    • Step 4
      • Click ‘Save & New’
        Repeat steps 1-4 for each of the remaining data-source fields
  8. We need to create one more Field: Device Id
    • Step 1
      • For ‘Data Type’, select ‘Text’
      • Click the ‘Next’ button
    • Step 2
      • Enter a ‘Field Label’: “deviceId”
      • Enter a ‘Length’: 16
      • Enter a ‘Field Name’: “deviceId”
      • Check ‘Required’
      • Check ‘Unique’
      • Check ‘Test "ABC" and "abc" as different values (case sensitive)’
      • Check ‘External ID’
      • Now click the ‘Next’ button
    • Step 3
      • Click the ‘Next’ button again
    • Step 4
      • Click ‘Save’
  9. You will be redirected to the ‘Custom Object’ page. It should look something like this: Salesforce API Name
     

4. Create a Custom Case Field in Salesforce

We want the cases opened to contain the Device ID for our refrigerator. To do this we need to create a custom field for our Salesforce case:

  1. In the sidebar under ‘Build’ unfold the ‘Customize’ menu then unfold ‘Cases’ and select ‘Fields’.
  2. Scroll to the bottom of the page and under ‘Case Custom Fields & Relationships’ click the ‘New’ button: Salesforce Case Custom Field
     
  3. In the ‘New Custom Field’ form, fill in:
    • Step 1
      • For ‘Data Type’, select ‘Lookup Relationship’
      • Click the ‘Next’ button
      • Select ‘SmartFridge’ as the related object
    • Step 2
      • Enter a ‘Field Label’: “Related Fridge”
      • Enter a ‘Field Name’: “Related_Fridge”
      • Optionally enter ‘Description‘: for example, “Device Id of the associated refrigerator”
      • Now click the ‘Next’ button
    • Step 3
      • Click the ‘Next’ button again
    • Step 4
      • Click ‘Save’
  4. You will be redirected to the ‘Case Custom Field’ page: Salesforce Case Custom Field
     

5. Create a Heroku App

  1. Log into Heroku and click on the ‘New’ button, then click on the ‘Create new app’ button: Heroku apps
     
  2. Enter an app name and choose a runtime selection, then click the ‘Create App’ button: Heroku app create
     
  3. Go to the ‘Resources’ tab at the top of the page then scroll down to the ‘Add-ons’ section: Heroku add-ons
    • Install the Heroku Connect add-on:
      • Type “heroku connect” in the add-ons search field, and then select ‘Heroku Connect’ from the dropdown list: Heroku connect add-on
         
      • Choose ‘Plan’ and click the ‘Provision’ button:
        Heroku connect add-on
         
    • Install the Heroku Postgres add-on:
      • Type “heroku postgres” in the add-ons search field, and the select ‘Heroku Postgres’ from the dropdown list: Heroku postgres add-on
         
      • Choose ‘Plan’ and click the ‘Provision’ button:
        Heroku postgres add-on
         
    • Heroku Connect setup:
      • Click ‘Heroku Connect’ in the Add-ons list: Heroku connect add-on setup
         
      • Set up the connection:
        • Click ‘Setup Connection’: Heroku connect add-on setup
           
        • Click ‘Next’
        • Click ‘Authorize’
        • Authorize in your Salesforce account and click ‘Allow’ to let Heroku Connect access to your Salesforce account
      • Create a Mapping
        • Click ‘+Create Mapping
          Heroku connect mapping
           
        • Choose ‘SmartFridge__c’ object
          1. Check ‘Write to Salesforce any updates to your database’
          2. Choose ‘deviceId__c’ under ‘Upsert Field’
          3. Check the ‘door__c’, ‘humidity__c’, ‘temperature__c’ and ‘ts__c’ fields:
            Heroku connect mapping
             
        • Click the ‘Save’ button
        • Click ‘+Create Mapping’
        • Choose the ‘Case’ object
          1. Check ‘Write to Salesforce any updates to your database’
          2. Select ‘Description’, ‘Subject’, ‘Related_Fridge__c’, ‘Related_Fridge__r__deviceId__c’ fields
        • Click the ‘Save’ button
  4. Go to the ‘Deploy’ tab at the top of the page to deploy your heroku application files:
    Heroku deploy
     
    • Select the ‘Dropbox’ tile in the ‘Deployment method’ section
    • Click ‘Connect to Dropbox’ button in the ‘Connect to Dropbox’ section
      • In the popup window click the ‘Allow’ button to give Heroku access to your Dropbox account:
        Heroku - Dropbox
         
    • Upload the Heroku application files (index.js, package.json and Procfile) to your Dropbox account (Dropbox/Apps/Heroku/\<your-app-name\>)
    • Enter a commit message and click the ‘Deploy’ button in the ‘Deploy changes’ section:
      Heroku - Dropbox - deploy
       

6. Build and Run the Electric Imp Application

  • Open the Electric Imp IDE and click on the ‘chip’ menu at the top.
  • Find your impExplorer and under the ‘Actions’ column, click on the code icon (</>)
  • Find the *HEROKU APP URL* section at the bottom of the agent code and replace **URL of the Heroku application** with the URL from the Heroku site:
    URL of the Heroku application
     
  • Hit ‘Build and Run’ in the Electric Imp IDE to save and launch the code:
    IDE Screenshot
     
  • Click on the agent URL (at the top of the log readout) to launch the log-in page
  • Log into salesforce

Your App should now be up and running. You can monitor the device logs in the IDE, or log into Salesforce web portal to see updates there.

7. Install Your impExplorer in Your Refrigerator

Open your refrigerator and place the impExplorer on a shelf in your refrigerator door.

Imp In Fridge

If you don't have a fridge handy for this scenario, you can test the door being open by moving or tapping the Kit, and keeping it in a lit room. A door open for 30 seconds should register a case.

8. Monitor the data in Salesforce1

Now that you have connected your impExplorer to Salesforce, it might be handy to see that data on a mobile device. It’s easy to keep track of your Smart Fridge on the go using Salesforce1.

8.1 Create a custom object tab

First, let’s give the custom object a tab so that Salesforce1 can add it to the left navigation.

  1. Log in to your Developer Edition.
  2. In the top navigation bar, click ‘Setup’.
  3. In ‘Setup’, enter “Tab” into the Quick Find box and select ‘Build’ > ‘Create’ > ‘Tabs’.
  4. Under the ‘Custom Object’ tab, click ‘New’.
  5. Choose ‘SmartFridge’ from the ‘Object’ dropdown.
  6. Choose ‘Thermometer’ as the Tab Style:
    Custom Object
     
  7. Click ‘Next’, ‘Next’ again, and then ‘Save’.

8.2 Open Salesforce1 in Chrome

You can access the Salesforce1 mobile app in three ways:

  • As a downloadable mobile app (Salesforce1) that you install on your phone from the Apple iTunes AppStore or Google Play.
  • By navigating to https://login.salesforce.com/ using a mobile browser.
  • By using the Chrome Developer Tools.

For this step, we’ll use the last option. First ensure that Salesforce1 is available through a desktop browser and is enabled for your user:

  1. Log in to your Developer Edition.
  2. In the top navigation bar, click ‘Setup’.
  3. In ‘Setup’, enter “Salesforce1” into the Quick Find box and select ‘Mobile Administration’ > ‘Salesforce1’ > ‘Salesforce1 Settings’.
  4. Ensure ‘Enable the Salesforce1 mobile browser app’ is checked:
    App settings checkbox
     
  5. In ‘Setup’, enter “Users” into the Quick Find box and select ‘Administer’ > ‘Manage Users’ > ‘Users’.
  6. Click ‘Edit’ next to the name you used to create your developer edition.
  7. Ensure that ‘Salesforce1 User’ is checked.
    Salesforce1 User checkbox
     
  8. Update and save as needed.

Next, we’ll emulate the Salesforce1 Mobile App in the Chrome web browser:

  1. Open a new tab in Chrome and open the Developer Tools by clicking ‘View’ > ‘Developer’ > ‘Developer Tools’.
  2. Click the ‘Toggle Device Mode’ button to simulate your browser as a mobile device:
    Chrome Tools Mobile Simulator
     
  3. To simulate the Salesforce1 app in your browser, copy and paste in the URL from the previous tab. Remove the part of the URL immediately after salesforce.com/. For example:
    URL original
    URL removed
     
  4. Append /one/one.app to the end of the URL after salesforce.com to start the Salesforce1 Application simulator. For example:
    URL one/one.app
     
  5. If the display is too small, change the size to 100%:
    URL one/one.app
     
  6. Click the three white bars in the upper left to open the left navigation.
  7. Under the ‘Recent’ section, scroll down and click ‘More’:
    Menu
     
  8. You will see ‘SmartFridges’ somewhere on the list — Click it:
    Menu
     
  9. Select a record to view the details of the reading.

9. Use Process Builder to Chatter Fridge Data

Finally, let’s add some finesse to our application by using Process Builder to drive a Chatter conversation based on the incoming readings.

9.1 Create a new Process Builder

  1. Log in to your Developer Edition.
  2. In the top navigation bar, click ‘Setup’.
  3. In ‘Setup’, enter “Process Builder” into the Quick Find box, and the select ‘Create’ > ‘Workflow & Approvals’ > ‘Process Builder’.
  4. Click ‘New’.
  5. Name the new process “Post SmartFridge Case to Chatter”.
  6. Click ‘Save’.
  7. Click ‘Add Object’.
  8. In the ‘Find an Object’ dropdown, select ‘Case’:
    Object
     
  9. Click ‘Save’.

9.2 Set up the process criteria

Next, we’ll set up the criteria that will fire the process:

Criteria

  1. Click ‘Add Criteria’.
  2. For Criteria Name, enter “Related to SmartFridge”.
  3. In ‘Set Conditions’, click ‘Find a Field’.
  4. Use the field selector to find ‘Related_Fridge__c’.
  5. For Operator, select ‘Is null’.
  6. Keep the values of Boolean and False.
  7. Click ‘Save’.

9.3 Add an Action

In the ‘Immediate Actions’ box next to your new criteria, click ‘Add Action’ and configure as follows:

  1. Action Type: ‘Post to Chatter’.
  2. Action Name: “Post SmartFridge Case Data”.
  3. Post to: ‘User’.
  4. User: ‘Select a user from a record’.
  5. Click in the ‘Find a user...’ box, select ‘Owner ID (User)’, select ‘*User ID’, and then click ‘Choose’.
  6. Copy and paste the following text into the ‘Add a message, mention a user or group...‘ text box: The case ({![Case].Id}) was opened regarding the Smart Fridge {![Case].Related_Fridge__r.DeviceId__c}.
    This fridge was recorded at a temperature of {![Case].Related_Fridge__r.Temperature__c},
    humidity of {![Case].Related_Fridge__r.Humidity__c}, and a door status of {![Case].Related_Fridge__r.Door__c}.
    Action
  7. Click Save. Your process should now look like this:
    Process Builder
     
  8. Click Activate.

Now whenever a case is created that has a related Smart Fridge, the important data about that fridge will be accessible right from the case owner’s Chatter feed.

Test Your Process

Let’s try out the process by keeping the fridge door open for over 30 seconds. This should trigger Salesforce to create a case. Because the case is related to a fridge, the case will be posted to your Chatter feed:

Chatter