Electric Imp Device Code for Si7020 Breakout Board
// Define the Sensor Address | |
const ADDR = 0x80; | |
// Define register map | |
const HUMIDITY = "\xE5"; | |
const TEMP_FROM_RH = "\xE0"; | |
// Specify the update rate in seconds | |
const update_rate = 60; | |
// Define the i2c periphrial being used | |
i2c <- hardware.i2c89; | |
// Configure said periphrial | |
i2c.configure(CLOCK_SPEED_100_KHZ); | |
// function to read the Si7020 sensor via configured i2c periph | |
function readSensor() { | |
// Read the humidity, sensor uses i2c clock stretching to hold the transaction while it is | |
// taking the measurement, this will block the thread for 16.5ms (typical) | |
local rh = i2c.read(ADDR,HUMIDITY,2); | |
// Part of taking a humidity reading is getting the temp, so read that out | |
local t = i2c.read(ADDR,TEMP_FROM_RH,2); | |
// Push this data off to the Agent for processing, avoid dooing much math so that we | |
// can go back to sleep... | |
agent.send("sample",[rh[0],rh[1],t[0],t[1]]); | |
// Schedule the next reading | |
imp.wakeup(update_rate, readSensor); | |
} | |
// start the loop | |
readSensor(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment