The talk demonstrates a working system made of an Elixir program running on a Raspberry Pi that scans the Wi-Fi spectrum and collects presence data; an AWS Lambda JavaScript function that receives those data and stores them into a DynamoDB instance; a Telegram bot, also running on Lambda, which sends alerts based on the number of presences. The focus will be on the overall architecture and the most important parts of the code. We'll show some Lambda automation tools and how to test the components locally. All the source code will be on GitHub. Ideal talk to get started with those technologies.
16. elixir
The entry point
The processes
Dependencies, mix deps.get
Calling Erlang modules
Pattern matching
How to store state in a server
The |> operator
17. elixir
A more complex project
with object orientation and process supervision
github.com
pmontrasio/decibel
Slides at
connettiva.eu/elixir-phoenix-raspberry-wifi (Italian)
Also check “Elixir's object orientation” at
github.com/pmontrasio/elixir-oo
24. News
AWS Lambda environment variables and
serverless architecture
serverless 1.1
$ serverless invoke local -f FUNCTION
Alternatives
Google Cloud Functions, Azure functions
Apex, Gordon
When you talked to the bot Telegram’s backend sent a HTTPS request to a webhook with JSON data. The webhook is a JavaScript function on AWS Lambda.
It stores your user ID in a Dynamo DB table, Users.
The Raspberry sends the count of unique WiFi mac addresses to another JavaScript function on AWS Lambda, once per minute.
The count is timestamped and stored in another DynamoDB table, Presences.
A trigger on Dynamo DB calls a third AWS Lambda function every time some data is added to Presences. That function gets all the Telegram user ids from Users and sends them a message with the count.
The WiFi sensor on the Raspberry is implemented in Elixir.
Elixir is a concurrent functional language running on the BEAM virtual machine, derived from Erlang with a Ruby like syntax.
Elixir processes exchange data by sending messages to each other, much like OO method calls. A typical Elixir application has many processes because they are cheap to create, run and send messages to. Millions of processes are not unheard of, much like millions of objects in OO languages.
But processes are concurrent and use all the cores of the machine.
Finally there is a process supervision layer to restart processes if the fail, much like upstart of systemd.
The entry point is mix.exs, which by default runs the run function in lib/sensor.ex
Task.start_link creates a supervised process
ChannelHopper.hop: tail call optimization
MacAddressCounter: how to build a server
spawn fn - > creates a process. Think: new Class
:module.function is a call to an Erlang module
Everybody knows AWS Lambda?
Show the function at https://github.com/pmontrasio/wifi-receiver/blob/master/handler.js
new AWS.DynamoDB.DocumentClient() outside the function, to save time (container reuse)
Easy but we must automate the deployment and the creation of the https endpoint.
$ serverless create --template aws-nodejs --name receiver
serverless.yml defines how to create the environment on AWS Lambda and how to deploy the application.
docs/Serverless_Setup.md is step by step guide at how to create and deploy the receiver.
docs/DynamoDB.md to install Dynamo DB locally
test/README.md on how to test. The important point is to be able to run the assertions in the callback run at the end of the function.
The webhook must be registered on Telegram.
presenceAlertWebhook gets the message in event.body.message
presenceNotifier can be called once with many database Records, in the event.Records array.
The trigger must be configured manually in the AWS console. See README.md for the details
Messages are sent with a POST to Telegrams.
Don’t use npm modules unless you really have to: every ms is important. Billed by 100 ms units.