Skip to main content

A Refrigerator Monitor Application

An Introduction To Squirrel Applications Example 4

In this example we will create a refrigerator monitoring application that takes an asynchronous reading from the temperature/humidity sensor. We will use the internal light sensor to determine if the refrigerator door is open. We will conserve power by turning off the WiFi or cellular connection and taking readings while offline then connecting periodically to send the readings we have collected to the cloud. This code can be easily configured for use with an imp006 Breakout Kit, impExplorer Kit, impAccelerator Battery Powered Sensor Node or impC001 Breakout Board.

Skill level


This example will focus on writing Squirrel code. Please visit the Getting Started Guide on the Electric Imp Dev Center to learn how to configure your device with BlinkUp™ and how to use the Electric Imp IDE, impCentral™.

What You Learn

  • How to use Electric Imp libraries.
  • How to send data to a cloud service such as Initial State.
  • How to use a Hardware Abstraction Layer (HAL).
  • How to write a class in Squirrel.
  • How to configure a sensor to take an asynchronous reading.
  • How to program your device to run offline.
  • How to send data between device and agent using the Message Manager library.

What You Need


// ---------------------------------------------------
// HAL's are tables that map human readable names to
// the hardware objects used in the application.

// Copy and Paste Your HAL here
ExplorerKit_001 <- {
    "LED_SPI" : hardware.spi257,
    "SENSOR_AND_GROVE_I2C" : hardware.i2c89,
    "ACCEL_I2C_ADDR" : 0x32,
    "PRESSURE_I2C_ADDR" : 0xB8,
    "POWER_GATE_AND_WAKE_PIN" : hardware.pin1,
    "AD_GROVE1_DATA1" : hardware.pin2,
    "AD_GROVE2_DATA1" : hardware.pin5
  • Assign your hardware class variables. In the Application class before the constructor you will find a number of class variables. You will need to re-assign the hardware variables so they look something like the example below. Do not copy and paste from this example, as these values may differ from the ones in your HAL.
// ---------------------------------------------------
// Application code, take readings from our sensors
// and send the data to the agent

class Application {

    // Time in seconds to wait between readings
    static READING_INTERVAL_SEC = 30;
    // Accelerometer data rate in Hz
    static ACCEL_DATARATE = 1;

    // Hardware variables
    i2c             = ExplorerKit_001.SENSOR_AND_GROVE_I2C;
    tempHumidAddr   = ExplorerKit_001.TEMP_HUMID_I2C_ADDR;
    pressureAddr    = ExplorerKit_001.PRESSURE_I2C_ADDR;
    accelAddr       = ExplorerKit_001.ACCEL_I2C_ADDR;

    // Sensor variables
    tempHumid = null;
    pressure = null;
    accel = null;

    constructor() {...}
  • Copy and paste the Agent Code into the Agent Code pane in the impCentral code editor.
  • Sign into Initial State.
  • Find your Streaming Access Key on the My Account page.
  • Navigate back to impCentral.
  • In the Agent code enter your Streaming Access Key into the Application class static variable STREAMING_ACCESS_KEY on line 24.
  • Hit the Build and Force Restart button to start the code.
  • Note the agent ID in the logs.
  • Navigate back to Initial State, find the Bucket that matches your agent ID.
  • Watch your data update in the Source, Lines, Waves and Tile views on the Initial State website.


Device Code

Agent Code