An Introduction To Squirrel Applications Example 8
In this example we will create a refrigerator monitoring application. The application monitors the temperature and humidity of the refrigerator and sends alerts if the temperature or humidity is higher than a set threshold for too long. The application also monitors the refrigerator door using the accelerometer to wake on motion and the internal light sensor to determine the door status. The application saves power by sleeping between readings when the door is closed, and only connecting periodically to upload readings or when an alert is triggered. This code can be easily configured for use with an imp006 Breakout Kit, impExplorer Kit, impAccelerator Battery Powered Sensor Node or impC001 Breakout Board.
Advanced
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™.
.HAL.nut
file in the repository that matches your hardware.
// HARDWARE ABSTRACTION LAYER
// ---------------------------------------------------
// 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
}
// POWER EFFICIENT REFRIGERATOR MONITOR APPLICATION CODE
// --------------------------------------------------------
// 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;
// Time to wait between checks if door is open
static DOOR_OPEN_INTERVAL_SEC = 1;
// Accelerometer data rate in Hz
static ACCEL_DATARATE = 100;
// Set sensitivity for interrupt to wake on a door event
static ACCEL_THRESHOLD = 0.1;
static ACCEL_DURATION = 1;
// The lx level at which we know the door is open
static LX_THRESHOLD = 3000;
// Alert thresholds
static TEMP_THRESHOLD = 11;
static HUMID_THRESHOLD = 70;
// Time in seconds that door is open for before door alert triggered
static DOOR_ALERT_TIMEOUT = 30;
// Number of seconds the conditon must be over threshold before triggering env event
static TEMP_ALERT_CONDITION = 900;
static HUMID_ALERT_CONDITION = 900;
// Time in seconds after door close event before env events will be checked
// Prevents temperature or humidity alerts right after is opened
static DOOR_CONDITION_TIMEOUT = 180;
// 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 = false;
// Variable to track next action timer
_nextActTimer = null;
constructor() {...}