Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda
-
Upload
corley-srl -
Category
Technology
-
view
1.711 -
download
0
Transcript of Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda
![Page 1: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/1.jpg)
Internet OfThings
Bologna 2015, June 17
![Page 2: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/2.jpg)
Taking pictures!!For Internet Of Things!
![Page 3: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/5.jpg)
Grab picturesEnable the camera module via raspi-config
Just use it: raspistill -o me.jpg
![Page 6: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/6.jpg)
Hey @CorleyCloud here is your picture: pic.2015.cloudconf.it/CorleyCloud/61… pic.twitter.com/LW8jUSPIa73:33 PM 16 Jun 2015
16 JunCorley @CorleyCloud#cloudconf2015 #pic
CloudConf @_CloudConf_
Follow
Read for tweets and reply back with pictures!
![Page 7: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/7.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
![Page 8: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/11.jpg)
Upload on AWS S3// Upload it!err = bucket.Put(path, bytes, "image/jpg", s3.ACL("publicread"), 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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/12.jpg)
Cross-Compiling (install GO for ARM)
Prepare your environment
$ cd $GOROOT/src$ GOOS=linux GOARCH=arm ./make.bash noclean
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/15.jpg)
AWS Lambda works with Javascript
But
I prefer Coffeescript
![Page 16: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/20.jpg)
Deploy on AWS LambdaSimple prepare a ZIP artifact grunt dist
![Page 21: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/21.jpg)
tweet it! #cloudconf2015 #pic
![Page 22: Read Twitter Stream and Tweet back pictures with Raspberry Pi & AWS Lambda](https://reader035.fdocuments.net/reader035/viewer/2022062303/55be498dbb61eb3b0d8b4815/html5/thumbnails/22.jpg)
Thanks for listening