Upgrade your Electric Imp IoT Trailhead Project to use Platform Events

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.

Build an IoT Integration with Electric Imp


After buying an USB cable to supply it with power it now runs 24/7 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:



    Deployed
    
        DeviceId__c
        false
        false
        false
        false
        
        16
        true
        Text
        false
    
    
        Door__c
        false
        false
        false
        false
        
        10
        false
        Text
        false
    
    
        Humidity__c
        false
        false
        false
        false
        
        6
        false
        2
        Number
        false
    
    
        Temperature__c
        false
        false
        false
        false
        
        6
        false
        2
        Number
        false
    
    
        ts__c
        false
        false
        false
        false
        
        false
        DateTime
    
    
    Fridge Readings

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 updates = new List();
    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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.