Skip to main content

Remote Monitoring Application With An Interrupt

An Introduction To Squirrel Applications Example 7

In this example we will create a remote monitoring application that takes asynchronous sensor readings using Electric Imp’s Promise library and detects freefall events. We will conserve power by putting the device to sleep between readings. The device will connect periodically to send readings and will also wake and connect if a freefall is detected.

This code can be easily configured for use with an impExplorer™ Developer Kit (imp001 model only), impAccelerator™ Battery Powered Sensor Node or impC001 Breakout Board, each of which contains all the required sensors.

Skill Level


This example will focus on writing Squirrel code. If you have not already done so, please visit the Getting Started Guide 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 device sensors to take asynchronous readings.
  • How to program your device to run offline.
  • How to exchange data between device and agent using the Message Manager library.
  • How to configure and use nv storage.
  • How to put the imp into sleep mode.
  • How to change the default connection policy.
  • How to configure the accelerometer's freefall interrupt.
  • How to wake on an interrupt.

What You Need


  • Activate your device with BlinkUp.
  • Log into impCentral.
  • Create a new Product and Development Device Group.
  • Copy and paste the Device Code into the Device Code pane in the impCentral code editor.
  • Locate the HAL for your hardware. The HAL files can be found on Github in the repositories linked below. Find the .HAL.nut file in the repository that matches your hardware:
  • Copy and Paste the HAL table into the code in the HARDWARE ABSTRACTION LAYER section. Here is an example of what a HAL table will look like when inserted into the code. Note DO NOT copy and paste from this example; please use the HAL from Github.

    // ---------------------------------------------------
    // 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,
        "TEMP_HUMID_I2C_ADDR" : 0xBE,
        "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. Note 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;
        // Time in seconds to wait between connections
        static REPORTING_INTERVAL_SEC = 300;
        // Max number of stored readings
        static MAX_NUM_STORED_READINGS = 20;
        // Time to wait after boot before disconnecting
        static BOOT_TIMER_SEC = 60;
        // Accelerometer data rate in Hz
        static ACCEL_DATARATE = 10;
        // Hardware variables
        i2c             = ExplorerKit_001.SENSOR_AND_GROVE_I2C; // Replace with your sensori2c
        tempHumidAddr   = ExplorerKit_001.TEMP_HUMID_I2C_ADDR; // Replace with your tempHumid i2c addr
        accelAddr       = ExplorerKit_001.ACCEL_I2C_ADDR; // Replace with your accel i2c addr
        wakePin         = ExplorerKit_001.POWER_GATE_AND_WAKE_PIN; // Replace with your wake pin
        // Sensor variables
        tempHumid = null;
        accel = null;
        // Message Manager variable
        mm = null;
        // Flag to track first disconnection
        _boot = true;
        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 Initial State Streaming Access Key into the Application class static STREAMING_ACCESS_KEY variable 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