Top Banner
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
31

Hackbright Workshop

Nov 22, 2014

Download

Technology

Matt Haines

Introductory workshop for the Electric Imp platform!
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hackbright Workshop

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

Page 2: Hackbright Workshop

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

Page 3: Hackbright Workshop

The Internet of Things (2025)

50B Connected

Devices

$6.2T Economic

Impact

McKinsey Global Institute, 2013

Page 4: Hackbright Workshop

Internet of Things Verticals

Page 5: Hackbright Workshop

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, …

Page 6: Hackbright Workshop

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

Page 7: Hackbright Workshop

The Electric Imp Platform – Overview

Page 8: Hackbright Workshop

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)

Page 9: Hackbright Workshop

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

Page 10: Hackbright Workshop

Let’s get started

Page 11: Hackbright Workshop

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)

Page 12: Hackbright Workshop

BlinkUp Example

* Your imp will stop blinking after 1 minute to save power (it is still powered on)

Page 13: Hackbright Workshop

Creating a New Model

Page 14: Hackbright Workshop

Creating a New Model

Page 15: Hackbright Workshop

Creating a New Model

Page 16: Hackbright Workshop

Creating a New Model

Page 17: Hackbright Workshop

Digital Output - Circuit

Page 18: Hackbright Workshop

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();

Page 19: Hackbright Workshop

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();

Page 20: Hackbright Workshop

Digital Input – Circuit

Page 21: Hackbright Workshop

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);

Page 22: Hackbright Workshop

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

Page 23: Hackbright Workshop

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

Page 24: Hackbright Workshop

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);

Page 25: Hackbright Workshop

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);

Page 26: Hackbright Workshop

Passing Messages

Page 27: Hackbright Workshop

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);

Page 28: Hackbright Workshop

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)

Page 29: Hackbright Workshop

Passing Messages

Page 30: Hackbright Workshop

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

Page 31: Hackbright Workshop

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