Upgrade your Electric Imp IoT Trailhead Project to use Platform Events

Posted by Johan on Thursday, October 5, 2017

As an avid trailblazer I just have to Catch ‘Em All (Trailblazer badges) and the project to integrate Electric Imp in my fridge was a fun one. [caption width=“200” align=“alignnone”] Build an IoT Integration with Electric Imp[/caption] After buying an USB cable to supply it with power it now runs 247 and I get cases all the time, haven’t really tweaked the setup yet.

I have looked at the new Platform Events and I thought that this integration can’t be using a simple upsert operation on an SObject, it’s 2017 for gods sake! Said and done, I set out to change the agent code in the Trailhead project to insert a Platform Event every time it’s time to update to Salesforce.

First of all you need to define your platform event, here is the XML representation of it:

<customobject xmlns="http://soap.sforce.com/2006/04/metadata">
    <deploymentstatus>Deployed</deploymentstatus>
    <fields>
        <fullname>DeviceId__c</fullname>
        <externalid>false</externalid>
        <isfilteringdisabled>false</isfilteringdisabled>
        <isnamefield>false</isnamefield>
        <issortingdisabled>false</issortingdisabled>
        <label>DeviceId</label>
        <length>16</length>
        <required>true</required>
        <type>Text</type>
        <unique>false</unique>
    </fields>
    <fields>
        <fullname>Door__c</fullname>
        <externalid>false</externalid>
        <isfilteringdisabled>false</isfilteringdisabled>
        <isnamefield>false</isnamefield>
        <issortingdisabled>false</issortingdisabled>
        <label>Door</label>
        <length>10</length>
        <required>false</required>
        <type>Text</type>
        <unique>false</unique>
    </fields>
    <fields>
        <fullname>Humidity__c</fullname>
        <externalid>false</externalid>
        <isfilteringdisabled>false</isfilteringdisabled>
        <isnamefield>false</isnamefield>
        <issortingdisabled>false</issortingdisabled>
        <label>Humidity</label>
        <precision>6</precision>
        <required>false</required>
        <scale>2</scale>
        <type>Number</type>
        <unique>false</unique>
    </fields>
    <fields>
        <fullname>Temperature__c</fullname>
        <externalid>false</externalid>
        <isfilteringdisabled>false</isfilteringdisabled>
        <isnamefield>false</isnamefield>
        <issortingdisabled>false</issortingdisabled>
        <label>Temperature</label>
        <precision>6</precision>
        <required>false</required>
        <scale>2</scale>
        <type>Number</type>
        <unique>false</unique>
    </fields>
    <fields>
        <fullname>ts__c</fullname>
        <externalid>false</externalid>
        <isfilteringdisabled>false</isfilteringdisabled>
        <isnamefield>false</isnamefield>
        <issortingdisabled>false</issortingdisabled>
        <label>ts</label>
        <required>false</required>
        <type>DateTime</type>
    </fields>
    <label>Fridge Reading</label>
    <plurallabel>Fridge Readings</plurallabel>
</customobject>

In short it’s just fields to hold the same values as on the SmartFridge__c object.

The updates to the agent code can be found on my GitHub account here.

When a Platform Event is created it needs to update the SmartFridge__c object to work as before, this is done with a trigger

trigger FridgeReadingTrigger on Fridge_Reading__e (after insert) {
    List<smartfridge__c> updates = new List<smartfridge__c>();
    for (Fridge_Reading__e event : Trigger.New) {
        System.debug('Event DeviceId ' + event.DeviceId__c);
        SmartFridge__c sf = new SmartFridge__c(DeviceId__c = event.DeviceId__c);
        sf.Door__c = event.Door__c;
        sf.Humidity__c = event.Humidity__c;
        sf.Temperature__c = event.Temperature__c;
        sf.ts__c = event.ts__c;
        updates.add(sf);
    }
    upsert updates DeviceId__c;
}

In Winter ‘18 you can use process builder on Platform Events but my developer edition is not upgraded until next Saturday.

So I made things a bit more complex by introducing Platform Events and a trigger but I feel better knowing that I use more parts of the platform. Next step will be to use Big Objects to store the readings from the fridge over time and visualize them.

Cheers