Transcript
Page 1: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Internet OfThings

Bologna 2015, June 17

Page 2: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Taking pictures!!For Internet Of Things!

Page 3: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Raspbery PiAbout 30$

Big as a credit card

A powerful ARM!

GNU/Linux on board

 

More...

Page 4: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

The Camera moduleAbout 30$

around 25 x 20 x 9 mm

5 Megapixels

Supports 1080p30, 720p60, VGA90 video

 

More...

Page 5: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Grab picturesEnable the camera module via raspi-config

Just use it: raspistill -o me.jpg

Page 7: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

       

The tweet processRaspberry read a twitter stream for #cloudconf2015 #picRaspberry upload the original picture to AWS S3AWS Lambda resize the image and optimize it for the webAWS Lambda tweet the picture to the final user

Page 8: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

On Raspberry Pi

GolangPowerfulavailable for multiple platforms (x86, ARM, ...)self-contained binariesMuch more!

Page 9: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Read the Twitter Streamimport (    tw "github.com/wdalmut/twitterstream/async")

client := tw.NewClient(    config.ConsumerKey,    config.ConsumerSecret,    config.AccessToken,    config.AccessSecret,)

client.TrackAndServe("cloudconf2015", func(tweet *twitterstream.Tweet) {    // Here the logic!})

github.com/wdalmut/twitterstream/asyncgithub.com/darkhelmet/twitterstream

Page 10: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Grab pictures using commands! (easy)cmd := exec.Command(    "raspistill", // Command name    "­a", "www.cloudconf.it ­ #cloudconf2015", //Watermarks    "­t", "500",    "­vf", "­hf", //Rotate    "­w", "1024", "­h", "768", //1024x768    "­­quality", "60", //60%    "­o", "/tmp/pic.jpg") // Put in /tmp foldererr := cmd.Run()

You can also take pictures using OS Signals

Page 11: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Upload on AWS S3// Upload it!err = bucket.Put(path, bytes, "image/jpg", s3.ACL("public­read"), s3.Options{})

Prepare the AWS clientAWSAuth := aws.Auth{    AccessKey: config.Key,    SecretKey: config.Secret,}

region := aws.EUWest

connection := s3.New(AWSAuth, region)bucket := connection.Bucket(BUCKET)

https://github.com/wdalmut/raspi-twitter-cloudconf

Page 12: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Cross-Compiling (install GO for ARM)

Prepare your environment

$ cd $GOROOT/src$ GOOS=linux GOARCH=arm ./make.bash ­­no­clean

Compile your source code

$ GOARM=6 GOARCH=arm GOOS=linux go build

Page 13: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Use a Makefiledefault: all

all: test        GOARM=6 GOARCH=arm GOOS=linux go build ­a        ssh pi@$(TARGET) 'killall picme | true'        scp start.sh pi@$(TARGET):~        scp picme pi@$(TARGET):~        scp config.json pi@$(TARGET):~        ssh pi@$(TARGET) './picme < /dev/null >/tmp/picme.log 2>&1 &'

test:        go test ­v ./...

Compile it locally, copy it into the Raspi and run it on board!

TARGET=192.168.1.53 make

Page 14: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Lambda receive the S3 upload event

... now we have to tweet the picture

Page 15: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

AWS Lambda works with Javascript

But

I prefer Coffeescript

Page 16: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Coffeescript and Lambda

/* Prepare the "twitter" client ... */

exports.handler = (event, context) ­> /** <­­ EVENT and the CONTEXT **/

  twitter.tweetAbout(event).then(    () ­>      context.done null, ""    (err) ­>      context.done err, "Unable to tweet!"  )                    

Just code with your event!

Page 17: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

S3 upload event!{    "Records": [        {            "s3": {                "bucket": {                    "name": "sourcebucket",                    "ownerIdentity": {                        "principalId": "XXXXXXXXXXXXXXXXX"                    },                    "arn": "arn:aws:s3:::example.walterdalmut.com"                },                "object": {                    "key": "walterdalmut/1924762.jpg",                    "size": 1024,                    "eTag": "11111111111111111111111111111111"                }            }        }    ]}

Please consider that this is not a real S3 event (just a part of it...)

Page 18: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Test your Lambdashandler = require '../src/lambda'

describe "Lambda callback", ­>  beforeEach ­>    @context = {}    @context.done = () ­>

    @event = {      "Records": [        { /* ... */ }      ]    }

  it "should expose the lambda handler", ­>    spyOn(handler, "handler").and.returnValue null

    handler.handler(@event, @context)

    expect(handler.handler).toHaveBeenCalled()

Page 19: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

A look into the Lambda functionhttps://github.com/wdalmut/lambda-twitter-cloudconf

Page 20: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Deploy on AWS LambdaSimple prepare a ZIP artifact    grunt dist

Page 21: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

tweet it! #cloudconf2015 #pic

Page 22: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Thanks for listening


Top Related