Building Connected Things with Electric Imp These slides are licensed under the MIT License http://opensource.org/licenses/MIT SSID: impdemo PW: electric Matt Haines | Tom Byrne [email protected] | [email protected] @beardedinventor | @tlbyrn
Nov 22, 2014
Building Connected Things with Electric Imp
These slides are licensed under the MIT License http://opensource.org/licenses/MIT
SSID: impdemo PW: electric
Matt Haines | Tom [email protected] | [email protected]
@beardedinventor | @tlbyrn
AgendaThe Internet of Things• What is the Internet of Things• What does the Internet of Things space look like• What goes into an Internet of Things application• Overview of the Electric Imp platform
Workshop – Your first imp project• Getting your first imp online• Digital I/O• Building a basic HTTPS request handler• Making HTTP requests
The Internet of Things (2025)
50B Connected
Devices
$6.2T Economic
Impact
McKinsey Global Institute, 2013
Internet of Things Verticals
What Goes Into an IoT Application• Hardware
• Microcontrollers, radios, sensors, actuators, etc• Firmware
• Device logic, drivers, network stack, etc• Server Code
• Heavy processing, external device API, data storage, etc• External Web Services
• Weather APIs, Twitter, Twilio, Gmail/email, IFTTT, etc• App / Website / Etc
• User interface, OAuth, etc
• Areas of knowledge required:• Electrical Engineering, embedded software, security, backend/server
development, web development, database admin, app development, …
The Electric Imp Platform
Connectivity Infrastructure
In-house
Great products
Focus on the core business
Platform
Server Infrastructure
In-house
Great products
Focus on the core business
Platform
The Electric Imp Platform – Overview
The Electric Imp Platform - Devices• 32-Bit ARM Cortex M3 processor• 802.11 b/g/n WiFi
• Open WiFi, WEP, WPA, WPA2• 80 KB* code space (device – 'firmware')
• 6/12 Configurable GPIO Pins• Digital In/Out• Analog In• 12 bit DAC (Analog Out)• PWM Out• I2C, UART, SPI
* This value changes a little bit each release (the imp originally only had about 40 KB of code space)
The Electric Imp Platform - Agents• Micro-server than can act on behalf of a device• Runs on the Electric Imp Servers• 1 MB code space• 1MB RAM
• Allows you to do things that you typically can’t on an embedded processor• Process incoming HTTP requests (create APIs for your device)• Make outgoing HTTP requests (interact with external APIs)• Persist small amounts of data
Let’s get started
BlinkUp - Getting Online• Create an Electric Imp Account: https://ide.electricimp.com• Download the Electric Imp mobile app:
• Android: play.google.com/store/apps/details?id=com.electricimp.electricimp• iPhone: itunes.apple.com/us/app/electric-imp
• Sign into the app with your Electric Imp Account• Enter WiFi credentials:
• SSID: impdemo• PW: electric
• Power up your imp• The imp’s internal LED must be blinking before continuing to the next step
• Click BlinkUp in the mobile app• Hold screen of phone against blinking light of imp until BlinkUp is complete
• Your device should now be blinking green (online)*
* Your imp will stop blinking after 1 minute to save power (it is still powered on)
BlinkUp Example
* Your imp will stop blinking after 1 minute to save power (it is still powered on)
Creating a New Model
Creating a New Model
Creating a New Model
Creating a New Model
Digital Output - Circuit
Digital Output - Device Code// Device Code
led <- hardware.pin9;led.configure(DIGITAL_OUT);
state <- 0;
function blink() {imp.wakeup(1.0, blink);state = 1 – state;led.write(state);
}
blink();
Blocking Loops are Bad!// Device Code// NOTE: THIS IS BAD CODE:
led <- hardware.pin9;led.configure(DIGITAL_OUT);
function loop() {local state = 0;while (1) {
led.write(state);state = 1 – state;imp.sleep(1.0);
}}
loop();
Digital Input – Circuit
Digital Input - Device Code// Device Code:
led <- hardware.pin9;led.configure(DIGITAL_OUT);
button <- hardware.pin1;
function onButton() {local state = button.read();led.write(state);
}
button.configure(DIGITAL_IN_PULLUP, onButton);
Event Driven Programming and Callbacks• Squirrel is an Event Driven Language
• Behaviour is defined by events and callbacks
• An event is a pre-programmed thing that can happen:• The timeout on an imp.wakeup ends• The state of a DIGITAL_IN pin changes• A message is passed from the device to the agent or vice versa • An agent receives an incoming HTTP request
• A callback is a function bound to a event:• imp.wakeup(5, blink);
• Tells the imp to run the function called blink in 5 seconds• button.configure(DIGITAL_IN, onButton);
• Tells the imp to run the function called onButton when the state of the pin assigned to button changes
HTTP Handlers• Each device has an agent – a tiny micro servers that act on behalf of their device• Each agent has a unique URL associated with it (see below)
• http.onrequest binds a callback to the 'received incoming HTTP Request' event
HTTP Handler – Agent Code// Agent Code:
function httpHandler(request, response) {if ("state" in request.query) {
local state = request.query["state"];device.send("led", state);
}
response.send(200, "OK");}
http.onrequest(httpHandler);
HTTP Handler – Device Code// Device Code:
led <- hardware.pin9;led.configure(DIGITAL_OUT);
function ledHandler(data) {if (data == "0") {
server.log("LED OFF")led.write(0);
} else {server.log("LED ON");led.write(1);
}}
agent.on("led", ledHandler);
Passing Messages
Making HTTP Requests – Device Code// Device Code:
led <- hardware.pin9;led.configure(DIGITAL_OUT);
function ledHandler(data) {if (data == "0") {
server.log("LED OFF")led.write(0);
} else {server.log("LED ON");led.write(1);
}}
agent.on("led", ledHandler);
Making HTTP Requests – Agent Code// Add to the bottom of your agent code:
const URL = "https://agent.electricimp.com/RO5HOrveo4I1";
function buttonPressHandler(state) { local url = URL + "?state=" + state; local request = http.get(url); request.sendasync(function(resp) { server.log(resp.statuscode + " - " + resp.body); });}
device.on("buttonPress", buttonPressHandler)
Passing Messages
Other activities• Device:
• Try to control your LED with PWM • electricimp.com/docs/examples/pwm-led• Bonus: Try to control the brightness with a web request
• Create multiple “states” for your LED (on, off, blinking slow, blinking fast)• Make the button switch between states
• Agent:• Grab the Wunderground API code
• github.com/electricimp/reference/tree/master/webservices/wunderground• Grab weather every 5 mins and change the brightness of the LED based on
the temperature• Bonus: Create an Umbrella reminder to keep by your front door:
• Every morning the agent checks the weather• If there’s > 50% chance of rain, LED turns on
• Grab the Twitter API• github.com/electricimp/reference/tree/master/webservices/twitter• Make your imp tweet whenever the button is pressed• Bonus: Control the state of the LED based on a Twitter stream
• Turn the light on whenever something is tweeted with a tracked tag• Click the button to clear the light
Electric Imp Resource
Getting Started Guide electricimp.com/docs/getting-startedAPI Documentation electricimp.com/docs/apiSquirrel Documentation electricimp.com/docs/squirrelReference Code github.com/electricimp/referenceCommunity Blog community.electricimp.comForums forums.electricimp.com