Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectricImpSampleCode/26054419fa7e225144902dece24ebcb6 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/26054419fa7e225144902dece24ebcb6 to your computer and use it in GitHub Desktop.
An Introduction To Squirrel Applications Example 2
// Simple Refrigerator Monitor Application Agent Code
// ---------------------------------------------------
// CLOUD SERVICE LIBRARY
// ---------------------------------------------------
// Libraries must be required before all other code
// Initial State Library
#require "InitialState.class.nut:1.0.0"
// REFRIGERATOR MONITOR APPLICATION CODE
// ---------------------------------------------------
// Application code, listen for readings from device,
// when a reading is received send the data to Initial
// State
class SmartFridge {
// On Intial State website navigate to "my account"
// page find/create a "Streaming Access Key"
// Paste it into the variable below
static STREAMING_ACCESS_KEY = "";
// Class variables
iState = null;
agentID = null;
constructor() {
// Initialize Initial State
iState = InitialState(STREAMING_ACCESS_KEY);
// The Initial State library will create a bucket
// using the agent ID
agentID = split(http.agenturl(), "/").top();
// Let's log the agent ID here
server.log("Agent ID: " + agentID);
device.on("reading", readingHandler.bindenv(this));
}
function readingHandler(reading) {
// Log the reading from the device. The reading is a
// table, so use JSON encodeing method convert to a string
server.log(http.jsonencode(reading));
// Initial State requires the data in a specific structre
// Build an array with the data from our reading.
local events = [];
events.push({"key" : "temperature", "value" : reading.temperature, "epoch" : reading.time});
events.push({"key" : "humidity", "value" : reading.humidity, "epoch" : reading.time});
events.push({"key" : "door_open", "value" : reading.doorOpen, "epoch" : reading.time});
// Send reading to Initial State
iState.sendEvents(events, function(err, resp) {
if (err != null) {
// We had trouble sending to Initial State, log the error
server.error("Error sending to Initial State: " + err);
} else {
// A successful send. The response is an empty string, so
// just log a generic send message
server.log("Reading sent to Initial State.");
}
})
}
}
// RUNTIME
// ---------------------------------------------------
server.log("Agent running...");
// Run the Application
SmartFridge();
// Simple Refrigerator Monitor Application Device Code
// ---------------------------------------------------
// SENSOR LIBRARIES
// ---------------------------------------------------
// Libraries must be required before all other code
// Temperature Humidity sensor Library
#require "HTS221.device.lib.nut:2.0.1"
// 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
// YOUR_HAL <- {...}
// REFRIGERATOR MONITOR APPLICATION CODE
// ---------------------------------------------------
// Application code, take readings from our temperature
// humidity and light sensors. Use the light level to
// determine if the door is open (true or false) and send
// the door status, temperature and humidity to the agent
class SmartFridge {
// Time in seconds to wait between readings
static READING_INTERVAL_SEC = 5;
// The lx level at which we know the door is open
static LX_THRESHOLD = 3000;
// Hardware variables
i2c = null; // Replace with your sensori2c
tempHumidAddr = null; // Replace with your tempHumid i2c addr
// Sensor variables
tempHumid = null;
constructor() {
// Power save mode will reduce power consumption when the radio
// is idle, a good first step for saving power for battery
// powered devices.
// NOTE: Power save mode will add latency when sending data.
// Power save mode is not supported on impC001 and is not
// recommended for imp004m, so don't set for those types of imps.
local type = imp.info().type;
if (type != "imp004m" && type != "impC001") {
imp.setpowersave(true);
}
initializeSensors();
}
function run() {
// Set up the reading table with a timestamp
local reading = { "time" : time() };
// Add temperature and humidity readings
local result = tempHumid.read();
if ("temperature" in result) reading.temperature <- result.temperature;
if ("humidity" in result) reading.humidity <- result.humidity;
// Check door status using internal LX sensor to
// determine if the door is open
reading.doorOpen <- (hardware.lightlevel() > LX_THRESHOLD);
// Send readings to the agent
agent.send("reading", reading);
// Schedule the next reading
imp.wakeup(READING_INTERVAL_SEC, run.bindenv(this));
}
function initializeSensors() {
// Configure i2c
i2c.configure(CLOCK_SPEED_400_KHZ);
// Initialize sensor
tempHumid = HTS221(i2c, tempHumidAddr);
// Configure sensor to take readings
tempHumid.setMode(HTS221_MODE.ONE_SHOT);
}
}
// RUNTIME
// ---------------------------------------------------
server.log("Device running...");
// Initialize application
fridge <- SmartFridge();
// Start reading loop
fridge.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment