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

Post on 02-Aug-2015

1.708 views 0 download

Tags:

Transcript of Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda

Internet OfThings

Bologna 2015, June 17

Taking pictures!!For Internet Of Things!

Raspbery PiAbout 30$

Big as a credit card

A powerful ARM!

GNU/Linux on board

 

More...

The Camera moduleAbout 30$

around 25 x 20 x 9 mm

5 Megapixels

Supports 1080p30, 720p60, VGA90 video

 

More...

Grab picturesEnable the camera module via raspi-config

Just use it: raspistill -o me.jpg

       

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

On Raspberry Pi

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

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

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

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

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

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

Lambda receive the S3 upload event

... now we have to tweet the picture

AWS Lambda works with Javascript

But

I prefer Coffeescript

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!

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

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

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

Deploy on AWS LambdaSimple prepare a ZIP artifact    grunt dist

tweet it! #cloudconf2015 #pic

Thanks for listening