Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active July 24, 2020 09:29
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/34b6e852ec5c9d30e80af1f6f5a127fa to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/34b6e852ec5c9d30e80af1f6f5a127fa to your computer and use it in GitHub Desktop.
Introduction To Squirrel Applications Example 1
// Reading a Sensor Agent Code
// ---------------------------------------------------
// CLOUD SERVICE LIBRARY
// ---------------------------------------------------
// Libraries must be included before all other code
// Initial State Library
#require "InitialState.class.nut:1.0.0"
// SETUP
// ---------------------------------------------------
// Set up an account with Initial State. Log in and
// navigate to "my account" page.
// On "my account" page find/create a "Streaming Access Key"
// Paste it into the constant below
const STREAMING_ACCESS_KEY = "";
// Initialize Initial State
local iState = InitialState(STREAMING_ACCESS_KEY);
// The library will create a bucket using the agent ID
// Let's log the agent ID here
local agentID = split(http.agenturl(), "/").top();
server.log("Agent ID: " + agentID);
// RUNTIME
// ---------------------------------------------------
server.log("Agent running...");
// Open listener for "reading" messages from the device
device.on("reading", function(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" : time()});
events.push({"key" : "humidity", "value" : reading.humidity, "epoch" : 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.");
}
})
})
// Reading a Sensor Device Code
// ---------------------------------------------------
// SENSOR LIBRARY
// ---------------------------------------------------
// Libraries must be included before all other code
// Temperature Humidity sensor Library
#require "HTS221.device.lib.nut:2.0.1"
// SETUP
// ---------------------------------------------------
// The HTS221 library uses the sensor's i2c interface
// To initialize the library we need to configure the
// i2c and pass in the 12c address for our hardware.
// The i2c address for the Explorer Kits and the
// Battery Powered Sensor Node are all 0xBE.
const I2C_ADDR = 0xBE;
// Find the i2c for your hardware from the list below.
// Paste the hardware.i2c for your hardware into the
// i2c variable on line 32.
// imp006 Breakout Board Kit i2c = hardware.i2cLM
// impExplorer Dev Kit 001 i2c = hardware.i2c89
// impExplorer Dev Kit 004m i2c = hardware.i2cNM
// impAccelerator Battery Powered Sensor Node i2c = hardware.i2cAB
// impC001 Cellular Breakout Board Kit i2c = hardware.i2cKL
// Configure i2c
// Paste your i2c hardware in the variable below
local i2c = hardware.i2cLM;
i2c.configure(CLOCK_SPEED_400_KHZ);
// Initialize the temperature/humidity sensor
local tempHumid = HTS221(i2c, I2C_ADDR);
// Before we can take a reading we need to configure
// the sensor. Note: These steps vary for different
// sensors. This sensor we just need to set the mode.
// We are going to set up the sensor to take a single
// reading when we call the read method.
tempHumid.setMode(HTS221_MODE.ONE_SHOT);
// APPLICATION FUNCTION(S)
// ---------------------------------------------------
// The sensor is now configured to taking readings.
// Lets set up a loop to take readings and send the
// result to the agent.
function loop() {
// Take a reading
local result = tempHumid.read();
// Check the result
if ("error" in result) {
// We had an issue taking the reading, lets log it
server.error(result.error);
} else {
// Let's log the reading
server.log(format("Current Humidity: %0.2f %s, Current Temperature: %0.2f °C", result.humidity, "%", result.temperature));
// Send the reading to the agent
agent.send("reading", result);
}
// Schedule next reading in 10sec
// Change the first parameter to imp.wakeup to
// adjust the loop time
imp.wakeup(10, loop);
}
// RUNTIME
// ---------------------------------------------------
server.log("Device running...");
// Start the readings loop
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment