Top Banner
Go and Get Physical with the Cloud Using Arduino with Google App Engine for Go Justin Grammens [email protected]
31

Physical Computing Using Go and Arduino

Aug 17, 2014

Download

Devices & Hardware

Justin Grammens

Using the Google's Go Programming language on App Engine to capture realtime sensor data from an Arduino over the internet.
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: Physical Computing Using Go and Arduino

Go and Get Physical with the CloudUsing Arduino with Google App Engine for Go !

Justin Grammens [email protected]

Page 2: Physical Computing Using Go and Arduino

I Want to Build Something!Goal : Track movement using a motion sensor Technology Requirements:

Use Go Use Google App Engine Use Arduino Send text message when movement occurs

Page 3: Physical Computing Using Go and Arduino

What We’ll Cover

Go Programming Language

Deployment on Google App Engine ( using Go )

REST interface on App Engine ( using go-restful )

Reading sensor data on Arduino

Sending data to our application on App Engine

Send message to 3rd party SMS provider ( Twillio )

Page 4: Physical Computing Using Go and Arduino

Block Diagram

App Engine

ArduinoBrowser

SMS Service

Page 5: Physical Computing Using Go and Arduino

Go Programming Language

Developed by Google in 2007

Statically typed language, loosely derived from C.

Automatic memory management

FAST compilation and execution!

Download at: https://code.google.com/p/go/

Page 6: Physical Computing Using Go and Arduino

Go Language Key Points

Some dynamic typing ( “x:= 0” instead of “int x = 0” )

Remote package management ( “go get” )

Built in mechanisms for concurrency

Able to produce statically linked native binaries

Strong focus on support for concurrency

Page 7: Physical Computing Using Go and Arduino

Go Language Key PointsNo type inheritance

No method overloading

Has pointers, but no pointer arithmetic (ie. can’t be changed)

No generics (a la List<String> in Java for instance)

No Exceptions - instead uses an error return type

func Open(name string) (file *File, err error)

Page 8: Physical Computing Using Go and Arduino

Workspaces

Designed from the ground up to work with code in public repositories

$GOPATH variable points to your workspace location

Put your code in a unique path

convention is: $GOPATH/src/github/user/project

Page 9: Physical Computing Using Go and Arduino

Example Workspacebin/ hello # command executable pkg/ darwin_amd64/github.com/justingrammens mydate.a # package object src/ github.com/justingrammens/ hello/ .git/ # git repository metadata hello.go # package source mydate/ .git/ # git repository metadata mydate.go # package source

Page 10: Physical Computing Using Go and Arduino

IDEs / Editors with Plugins

Emacs

Eclipse - ( GoClipse )

IntelliJ IDEA

Sublime Text

TextMate

Vim

Page 11: Physical Computing Using Go and Arduino

Example: hello.gogit clone https://github.com/justingrammens/gdev !

package main import "fmt" func main() { fmt.Println("hello world") } !

Page 12: Physical Computing Using Go and Arduino

Example: hello-world.gousage: type “go” on command line $: go run hello.go hello world !

$: go install $: $GOPATH/bin/hello

!

$ go build ./hello ( note the size of the executable )

Page 13: Physical Computing Using Go and Arduino

Writing a Library

package mydate !import "time" !func Birthdate() time.Time { d := time.Date(2014, time.May, 6, 0, 0, 0, 0, time.UTC) return d }

Return type

Capital letter means public access

Calling function

Page 14: Physical Computing Using Go and Arduino

Publish Librarygit push to public repository: https://github.com/justingrammens/gdev

Access with:

import “github.com/justingrammens/gdev/go-examples/mydate”

Anyone can install to their workspace with:

go get github.com/justingrammens/gdev/go-examples/mydate

Page 15: Physical Computing Using Go and Arduino

Calling Librarypackage main !import "fmt" import "github.com/justingrammens/gdev/mydate" import "time" !func main() { now := time.Now() birthday := mydate.Birthdate() diff := birthday.YearDay() - now.YearDay() fmt.Printf("There are only %d days to my birthday!\n", diff ) }

Page 16: Physical Computing Using Go and Arduino

Example: Functions can return multiple values:

func AddandSub(x, y int) (sum, difference int) {

sum = x + y

difference = x - y

return

}

Called with:

sum, difference := mydate.AddandSub(10, 11)

Page 17: Physical Computing Using Go and Arduino

App Engine Using Go

Platform as a Service (PaSS)

Run applications in Google’s infrastructure

Frees you up to develop, not system admin

Develop and test locally, deploy globally

Beware - Go is Experimental!

https://developers.google.com/appengine/downloads

Page 18: Physical Computing Using Go and Arduino

App Engine Using Go

Deployed to [your_app_id].appspot.com

Quotas:

Request & Response Size - 32 MB

Request Duration - 60 sec

Max total files - 10,000 total : 1,000 per directory

Max size files - 32 MB

Page 19: Physical Computing Using Go and Arduino

App Engine Using Go

Run “goapp” to see commands

goapp serve : runs local server

goapp deploy : upload to App Engine

Page 20: Physical Computing Using Go and Arduino

App Engine Using Go

Run Hello World Example

cd appengine-examples

goapp server

Local service: http://localhost:8080

Local datastore viewer: http://localhost:8000

Page 21: Physical Computing Using Go and Arduino

App Engine Using Go

No standard REST framework out of the box

Use “go-restful” project

Easy install and setup with “go get” command

Resources:

“Events”

“Sms”

Page 22: Physical Computing Using Go and Arduino

App Engine Using GoRun DevFest App

cd devfest

goapp server

Access swagger for API at http://localhost:8080/apidocs

Push update to google app engine at:

appcfg.py --oauth2 update my app

Manage at: https://appengine.google.com

Page 23: Physical Computing Using Go and Arduino

Test Web Service

Simple REST Client

Verify the values are being stored

Toggle values for SMS notifications

Page 24: Physical Computing Using Go and Arduino

Arduino

Open source microcontroller board

Started in Italy in 2005 as a college student project

Based on a simplified version of C++ ( Wiring )

Allows for adding additional boards via “shields”

WIFI shield is added for our project

Many readily available sensors and libraries!

Page 25: Physical Computing Using Go and Arduino

Arduino Wifi Shield

Page 26: Physical Computing Using Go and Arduino

Hardware PartsPIR Motion Sensor - $9.95

Arduino R3 Board - $30.00

Official Arduino WIFI Shield - $70.00

Page 27: Physical Computing Using Go and Arduino

Schematic

Page 28: Physical Computing Using Go and Arduino

Arduino BasicsOnly 2 functions are required !

void setup() { // put your setup code here, to run once. } !

void loop() { // put your main code here, to run repeatedly. }

Page 29: Physical Computing Using Go and Arduino

Motion Sample Code

Motion activated by:

Takes an infrared “snapshot” of the room

If anything changes, pin #2 goes LOW

Use the digitalRead of values

Use WifiClient to Post JSON to our web service.

Print the status to the serial monitor.

Page 30: Physical Computing Using Go and Arduino

Resources

Play with Go Code! http://tour.golang.org/

https://groups.google.com/forum/m/#!forum/google-appengine-go

https://developers.google.com/appengine/docs/go/

http://arduino.mn - Local Arduino user’s group!

Page 31: Physical Computing Using Go and Arduino

Thanks!

Questions?