Last active
July 13, 2020 10:29
-
-
Save ElectricImpSampleCode/28a00fb67a19f404332a518cdf94a4dc to your computer and use it in GitHub Desktop.
imp006 Breakout Board impt tutorial correct version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// APPLICATION LIBRARIES | |
// Include the temperature/humidity sensor Library | |
#require "HTS221.device.lib.nut:2.0.2" | |
// APPLICATION CONSTANTS | |
// The I2C address for the sensor | |
const I2C_ADDR = 0xBE; | |
// APPLICATION GLOBALS | |
// Set up the imp I2C bus the sensor is connected to | |
i2c <- hardware.i2cLM; | |
i2c.configure(CLOCK_SPEED_400_KHZ); | |
// Initialize the sensor... | |
tempHumid <- HTS221(i2c, I2C_ADDR); | |
// ...and configure it to take a single | |
// reading when we call the read() method. | |
tempHumid.setMode(HTS221_MODE.ONE_SHOT); | |
// Set up the user LED for the colours we’ll flash | |
// to indicate successful or failed readings | |
redPin <- hardware.pinR; | |
redPin.configure(DIGITAL_OUT, 0); | |
greenPin <- hardware.pinXA; | |
greenPin.configure(DIGITAL_OUT, 0); | |
// Define a variable we'll use later | |
ledPin <- null; | |
// APPLICATION FUNCTIONS | |
function loop() { | |
// This is the primary loop, run every ten seconds | |
// 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); | |
flashLED("red"); | |
} else { | |
// Let's log the reading | |
// Note the escaping of the percentage sign for the humidity reading | |
server.log(format("Current Humidity: %0.2f%%, Current Temperature: %0.2f°C", result.humidity, result.temperature)); | |
flashLED("green"); | |
} | |
// Schedule next reading in 10 seconds | |
// Change the first argument adjust the loop time | |
imp.wakeup(10, loop); | |
} | |
function flashLED(color) { | |
// Select the correct imp006 pin for the color we want | |
ledPin = color == "red" ? redPin : greenPin; | |
// Set the pin high to light it | |
ledPin.write(1); | |
// Queue up a timer to fire in 1 second and turn off the LED | |
// Note that unlike 'loop()', here we pass in an anonymous function | |
// rather than a reference to function | |
imp.wakeup(1, function() { | |
ledPin.write(0); | |
}); | |
} | |
// APPLICATION RUNTIME | |
// Start taking periodic readings | |
loop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment