After completing the the Trailhead Electric Imp project I was left wanting an excuse to access a number of other features of the imp001 hardware. Namely the accelerometer and pressure sensor. Yet there didn't seem many opportunities for a static fridge to fully utilize them.
While I was experimenting with this back in May 2018 the #BeABuilder challenge happened and I got sidetracked using the hardware for that instead. Somewhere in that process I never got around to hitting publish on this blog post, so here we are a year or so latter with a better late than never post.
Firstly I expanded the data model in Salesforce so I'd have fields to receive the additional data. This included the acceleration and pressure readings.
Tinkering with a #Salesforce Data Model for a current hobby project as part of the #BeABuilder challenge. Will be interesting to see if I can get it to work in practice. pic.twitter.com/XRuGWwEDej
— Daniel Ballinger 🦈 (@FishOfPrey) May 1, 2018
Out of interest I tried getting the IoT Orchestration Traffic into a Lightning component. I didn't have much luck at the time and it didn't really liked to be iframed in.
Day 4 of #BeABuilder challenge. The IoT Orchestration Traffic wasn't very cooperative with being iFramed into the page, so it needed some DOM based persuasion. pic.twitter.com/4HAvojxsV8
— Daniel Ballinger 🦈 (@FishOfPrey) May 2, 2018
Hunting through the Salesforce Labs I found the salesforce-iot-toolkit. This provided some great visualizations via platform events as the data came in from the fridge. It directly monitored the platform events to drive the graphs. I found I needed to use the source based version rather than the app exchange version, which was missing a number of features.
Day 8 of the #BeABuilder challenge. Using the Platform Event Stream Visualizer by @DanHca & @MikeBrosseau. App Exchange: https://t.co/O3OvgHNYqu Source: https://t.co/iGHK2Zo8Fy pic.twitter.com/12Plq1lPoC
— Daniel Ballinger 🦈 (@FishOfPrey) May 8, 2018
Detect changes in fridge orientation. pic.twitter.com/VFiwzPqxVr
— Daniel Ballinger 🦈 (@FishOfPrey) May 10, 2018
Dramatic reenactment of sensor usage
Modified Agent Nut Source
A few points of interest:
- My source was the version before they added proper refresh token support to handle expired sessions. So you probably don't want my version of
getStoredCredentials()
- Added
#require
statements to access additional sensors. - Dropped the
READING_INTERVAL_SEC
down to 1 second to improve the accelerometer data. - Retrieved the X,Y, and Z axis acceleration values.
- Retrieved the pressure sensor and additional temperature reading from that sensor as well.
- Set the RGB color based on the current orientation indicated by the acceleration.
- The LIS2DH12 sensor needed a non-default I2C address of
0x32
.
Modified Salesforce Platform Event Trigger Code
Nothing fancy here. Just collect the additional properties the map them to the new custom fields.
trigger SmartFridgeReadingReceived on Smart_Fridge_Reading__e (after insert) { Listrecords = new List (); 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; record.accX__c = event.accX__c; record.accY__c = event.accY__c; record.accZ__c = event.accZ__c; record.pressure__c = event.pressure__c; record.temp2__c = event.temp2__c; records.add(record); } insert records; }