Created
June 2, 2017 08:24
-
-
Save SwitchScienceCode/0a2f554dc6e49d72e03ddef9b7d834da to your computer and use it in GitHub Desktop.
Electric Imp Explorer Kit Sample Code: Motion Detection by Tweet
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
// Import libraries | |
#require "Twitter.class.nut:1.2.1" | |
// Declare global variables | |
tweeter <- null; | |
// Define functions | |
function tweetback(data) { | |
// This function is called after a Tweet is successfully posted | |
server.log("Tweet tweeted"); | |
server.log(data.body); | |
} | |
function motion(data) { | |
local message = format("Data from my Explorer Kit! [Temp: %.2f | Humid: %.2f | Pressure: %.2f]", | |
data.temp, data.humid, data.press); | |
tweeter.tweet(message, tweetback); | |
server.log(message); | |
} | |
// Instantiate the Twitter class - you'll need your own access credentials | |
tweeter = Twitter("<YOUR_API_KEY>", | |
"<YOUR_API_SECRET>", | |
"<YOUR_AUTH_TOKEN>", | |
"<YOUR_TOKEN_SECRET>");; | |
// Register a function to receive sensor data from the device | |
device.on("reading.sent", motion); |
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
#require "HTS221.device.lib.nut:2.0.0" | |
#require "LPS22HB.class.nut:1.0.0" | |
#require "WS2812.class.nut:3.0.0" | |
// Define constants | |
const sleepTime = 120; | |
// Declare Global Variables | |
tempSensor <- null; | |
pressureSensor <- null; | |
led <- null | |
// Define functions | |
function takeReading(){ | |
local conditions = {}; | |
local reading = tempSensor.read(); | |
conditions.temp <- reading.temperature; | |
conditions.humid <- reading.humidity; | |
reading = pressureSensor.read(); | |
conditions.press <- reading.pressure; | |
// Send 'conditions' to the agent | |
agent.send("reading.sent", conditions); | |
// Flash the LED | |
flashLed(); | |
// Set the imp to sleep when idle, ie. program complete | |
imp.onidle(function() { | |
server.sleepfor(sleepTime); | |
}); | |
} | |
function flashLed() { | |
led.set(0, [0,0,128]).draw(); | |
imp.sleep(0.5); | |
led.set(0, [0,0,0]).draw(); | |
} | |
// Start of program | |
// Configure I2C bus for sensors | |
local i2c = hardware.i2cNM; | |
i2c.configure(CLOCK_SPEED_400_KHZ); | |
tempSensor = HTS221(i2c); | |
tempSensor.setMode(HTS221_MODE.ONE_SHOT); | |
pressureSensor = LPS22HB(i2c); | |
pressureSensor.softReset(); | |
// Configure SPI bus and powergate pin for RGB LED | |
local spi = hardware.spiAHSR; | |
spi.configure(MSB_FIRST, 6000); | |
hardware.pinH.configure(DIGITAL_OUT, 1); | |
led <- WS2812(spi, 1); | |
// Take a reading | |
takeReading(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment