Using Actors for the Internet of (LEGO) Trains & Raspberry Pis

Johan Janssen

Recorded at GOTO 2017


Get notified about Johan Janssen

Sign up to a email when Johan Janssen publishes a new video

[Music]
thank you um okay so my name's Johansen
hello everyone
um I heard that you need a disclaimer
with presentation so I have one as well
Lego wasn't harmed however some
raspberry pies died but who cares anyway
there are more than enough of them so
what's a bit of the content for the next
45 minutes it's around this questions
were put at the end but like already
mentioned feel free to interrupt me in
between I'll try and answer the
questions directly first you might think
okay why is this guy playing with Lego
um and there are a couple of reasons one
of course it's fun so you probably could
have imagined that one and the other
reason was we're a company where we
build java application scholar
applications we build those for large
financial institutions governments etc I
have to use a big hype and we were
looking okay maybe as a company we want
to do something in the IT direction as
well but typically IT software is
written in C Python or some other
language so if we had to learn that it
would cost us quite some time so we
wanted to figure out if we could use the
languages that and tools that we were
used to and run it on IOT hardware like
a raspberry point other thing is it's
nice for kids as well to teach them more
about programming and that it's
interesting and one of the funny things
I found out was it's also easy to
explain concepts for instance if I try
to explain to my wife what event-driven
architecture is and I say that it's
sending message M from system X to
system Y she probably would have stopped
listening at even driven and that's not
her fault I mean she doesn't have the
technical background we have and we
speak some alien dialect that nobody
understands what if I told her like okay
I send a message from my laptop to the
computer on the train to instruct it to
play a song then she can fully
understand it and basically I'm saying
more or less the same thing so it helps
to explain some concepts as well if you
are thinking about starting with it be
careful because you might end up with a
living room like this this was only the
beginning I have to say it got even
worse than this
but it's pretty easy to get started so
question for you who has a raspberry pi
okay keep your hand in the air if it's
collecting dust on a shelf so okay now
you know what you can do with it because
to do this you need a Raspberry Pi
if you already have a Raspberry Pi it
will probably cost you 25 bucks for the
ingredients if you have to buy a
Raspberry Pi it's like 50 bucks so it's
not really expensive you need to use be
Wi-Fi dongle you need a battery pack you
need an infrared transmitter and where
you need that for I will explain in a
minute but you can do a lot more this
train has a speaker so I can scare
burglars away or just my kids when I'm
at work of course only during lunch time
I can play with my trains there is an
rfid reader to determine the position on
the track it's like the of a chip card
which we have in the Netherlands with
the card which we use that's also RFID
basically and there is a full HD camera
on top of the train so I can have a live
image feet from my train which is of
course really useful if you're not in
the same room as a train so what are
they are used from a Raspberry Pi
perspective in the trains I used the
Raspberry Pi a plus because they're
relatively small and they don't consume
a lot of power so I can run them longer
on a battery pack at that time the
Raspberry Pi 0 wasn't available if I
would do it again I would probably pick
the Raspberry Pi 0 because it's even
smaller and consumes even less power if
you are not on a USB battery pack but
just on power from any outlet it's
probably advisable to use a quicker
Raspberry Pi because it enables you to
develop quicker and run things quicker
to start a spring application on a
Raspberry Pi A+ took like two to three
minutes without tweaking so that takes
quite a while architecture I don't know
if you're familiar with this if you buy
Lego bricks you you cannot buy them a
piece you have to buy them in a back
basket like this and they're round so
it's really annoying you cannot easily
put bricks in it and I'm Dutch so I want
to put as much bricks in it for the
value that I pay so I end up with this I
didn't need those breaks but I mean you
when we started we built basically this
setup LTCC stands for the Lego train
control center
we are angular front end where the Java
back-end both of these were running on
my laptop
we used rest this by the way is the
international symbol for rest which you
already recognize that of course
fortunately none of my colleagues got it
but okay then I had some applications
running on the Raspberry Pi so I have a
device control applications with wrench
on the train and for the ferris wheel
and had a switch control to control the
switches and underwater I use some C and
Python libraries that were already
available and I tweaked them a little
bit to my needs
so Java was merely like an integration
layer and a communication layer and I
used the raspberry pi web interface for
video streaming basically that was all
nice it was working perfectly
but we had a skull on a course and we
were really excited about akka but we
wanted to try it and some something more
than hello world so we decided to throw
away everything that we built in Java
and build from scratch in Scala and akka
I will show a bit of a car later to get
you a bit more understanding of it but
the biggest change was that between the
different Java or sorry the different
Scala applications so this one's running
on my laptop and this on Raspberry Pi we
no longer communicate with rest we use
remote actor communication which is akka
over TCP so no HTTP calls anymore we use
a more low-level approach and that was
the biggest change but to go to the
outside world we needed some rest
endpoints for instance for angular and
for the RFID layer library um from a
hardware perspective we started with
everything wireless until we put this on
a booth and our first conference and
Wi-Fi basically didn't work because
there's too much Wi-Fi in conferences so
we ended up putting everything on wires
that we could wire except of course the
trains because it would look silly if
you have a wire going at the back of the
train but the rest the cameras and
everything is it's simply wired so
we then send
mom's wireless or to the wire to the
Raspberry Pi and then the Raspberry Pi
uses this infrared transmitter to
control the infrared receiver from a
Lego train so this is this is standard
so this everybody recognized Wally and
automated a little bit this team for red
receiver we have two different engines
and we have a battery pack at the front
this is the default Lego receiver they
have two different ones that's the other
one but they they work the same so you
and this works quite nice until you have
a distance of around two to three meters
that infrared doesn't work anymore so
you cannot control
Wally anymore or your Lego trains so to
come up with a solution I came up with
this a simple Raspberry Pi used to be
Wi-Fi dongle and an infrared transmitter
so now I use this little infrared
transmitter to send the same signals as
the infrared transmitter from Lego
ascending so I didn't need to change the
engines or anything I could just send
some other commands to it and of course
sounds like I already mentioned a train
needs to make train sounds
I wanted to determine the position on
the track where the train was running so
I use RFID cards the small ones on there
and he used the camera but camera was
really tricky at first I wanted to do
video streaming but with the Raspberry
Pi that's difficult and in the end I
found an application that doesn't do
video streaming but it refreshes
pictures really quickly you cannot
really see it at least I cannot see it
maybe if you have really good eyes you
can know it's it but it looks real-time
and quake but it's just images being
refreshed at a really high speed without
a delay because if you would have used
video you would have had like a five
second delay of your video and then the
train is already across the track
another time so that's not really useful
if you do something with raspberry pi
cameras really look up this program it
works really well it does motion
detection and stuff like that as well we
also wanted to automate the track
switches if you're a bit older like
myself maybe you know the old old Lego
where you had automated track switches
and everything they don't do that
anymore they don't have automated track
switches anymore so you just have a
switch with a yellow lever so we we
bought some servos we bought a large
adapter because it consumed quite a lot
of power and a special servo adapter
board for the Raspberry Pi and with it
we could control these switches but it
was quite hard to do that because these
paper clips
you need to bend them at the exact good
length because they were either the
switch wouldn't open or wouldn't closed
these are probably
Leda for most expensive paper clips in
the world because we worked on it with
three colleagues for half a day before
it worked and it's a bit brittle so I
don't take it to conferences anymore
it's a bit hard but this is one of the
few solutions that I've seen were you
didn't have to fiddle with the Lego I
saw other solutions where people drilled
something off of the Lego to make the
switches go smoother but if you tell
that on a Lego forum on the Internet
those people will hate you forever
so don't do that I thought cranes of
course were awesome but when I saw the
ferris wheel of Lego I was like okay I
need to have that one as well so I
bought one and then I was like okay I'm
going to automate that one and it
actually was really simple because it's
the same infrared receiver as I use in
wall-e or in my trains so I could use
the exact same software I was running on
my trains and run it on another
Raspberry Pi so that was really easy and
then every IOT project I mean you need
to do something with LEDs and I thought
the Raspberry Pi was a bit overkill to
control a few LEDs because what I wanted
to do was display the speed of the
ferris wheel so here it's moving at
speed 3 and the different trains on LEDs
so I ended up not using a Raspberry Pi
but a particle photon who's heard of the
particle photon ok few people it's like
an Arduino but then with a Wi-Fi chip on
board and they have their own cloud
environment so it's if you buy one you
connect to it with your smartphone
enter your router credentials and it's
in the cloud then you add a little bit
of software to the web interface IDE
which is basically here so you program
Arduino code and you can flash it on one
of the different particle photons that
you have and then simply call them it
works really nice and it's only like 25
bucks but you cannot run Java or Scala
on it so it's a bit more low level stuff
but I can really recommend it if you
want to do something which doesn't need
lots of computing power I had a lot of
fun with these things you're of course
already wondering for like 15 minutes
like when are the trains going to drive
some
let's do a demo and so this is the
angular application that I created you
can see the video over here that's from
the camera that's on the boxes at the
back in the right I disabled the camera
on the train for now because sometimes
if the Wi-Fi the conference is a bit
there's too much interference I don't
have a live video stream and the video
stream is also blocking my commands that
I sent to the train so then I also
cannot control the train so for safety
reasons I simply disabled the camera for
now so I can make the trains run any
direction basically Ferris wheel okay
that was nice nice crash it's a demo so
you need something that goes wrong we
had that
at the top right you can see that these
ones were used to control the track
switches so either go forward or bent
with the track switch and every track
switch had a Lego minifigure on top of
it so I could distinguish the different
ones and let's let them make a little
bit less noise I cannot see it properly
but I think you see three dots going
clockwise now because the ferris wheel
is going at speed three forward ever now
make it go backward it should go with
four dots backwards so now I can see
what the LED is what the speed of my
different devices is for the trains you
see maybe three dots there I don't know
it's really visible I can increase the
brightness a bit so now you see three
dots
I will decrease the brightness a bit
because probably you get annoyed by it
but and over here
you can see where the blue train is so
it's now at the code guard bicycle etc
and there's only one feature that we
Mason that's the sound song and let's
make sure it makes some noise so
basically you can play any noise simply
train silence or simply try to scare
of course that's work now but you can
play any sound basically it's just an
patrice which are running on on a device
a bit confused why does work but okay
and so this is easy this is just manual
control we're sending some commands to
the train and they're being executed so
okay that's cool but can we make it a
bit more advanced and what we did then
was create auto pilot stuff so we can
simply send a set of commands to the
trains so that was one I was waiting for
so you can use any sound to scare
burglars away anything I just have fun
with your kids or your wife or husband
so in this case I sent three commands to
the Train first I say that the train has
to drive at speed three for three
seconds then it has to stop for three
let's write that one again because it's
now let's not do that of course you
always see that something listen ah the
infrared transmitter was going upwards
so then it doesn't receive any
instructions to the Train so now it
drives for three seconds stops for three
seconds and then should play the song
but it's already playing the song okay
so this these commands are sent to the
Train and I can shut down my laptop and
we'll just continue running these
commands so that's okay but we wanted to
have something a bit more advanced so we
want to do something location-based so
what we now do there is something wrong
with my icons probably I've run an
update which I didn't check that all the
icons were working but it's simply
buttons to add something to play
something so what I'm typing here is
that if the Blue Train reaches the
bicycle then the ferris wheel will move
at speed minus three if the Blue Train
reaches the cow it will start moving at
speed three and let's do something more
speak to the crossing and open another
screen for this one and make the Blue
did apply this one probably not so now
it's at the crossing it'll move at the
speed that I gave it to at the cow so
now it's moving at speed three bicycles
so speed minus three so this way I can
can interact between the different
devices I can even say that if the Blue
Train reaches the bicycle that it should
reverse or anything else that I would
so that's insured how it works
in a short demo because I also want to
explain something more and not just have
fun but also try to learn you something
new who already worked with actors in
ARCA a few people so the first few
slides you probably are quite familiar
with it after that we are going to talk
them some other subjects just say c-dit
and you probably will learn something
new so what's an actor an actor
basically something that receives
messages you extend the actor and you
define a receive method and in this case
I simply print everything that's sent to
the actor I create an actor system I
retrieve a reference to the actor so the
worker actor I defined here and I can
send a message with the exclamation mark
so I simply sent a string here and the
string is being prints and nothing fancy
going on there the nice thing is that
working with those actors locally so
within one JVM you can communicate with
different actors with each other which
you can also communicate across Jeff
yams so I have an actor running on my
laptop and I have an actor running on my
Raspberry Pi or even multiple ones and I
can communicate with them normally you
would need to have rest endpoints for
that or you use soap or something like
lot easier so one of the previous slides
you saw that with this we retrieve the
actor reference so we could send a
message to it if we want to send a
to specify the IP address port the name
of the actor system an actor name here I
put it all in the code but you could put
this in a configuration as well but I
wanted to make a simple example for all
of you so this is everything you need to
send a message across different JVMs
that's a lot easier than rest and
nowadays you have lots of discussions on
monolith versus micro-services and some
people say okay start with a monolith
and if it's too big
create smaller applications out of it
but if you want to do that you need to
define all kinds of rest endpoints
communicate with those rest endpoints
and it's basically all boilerplate code
it's nothing business-related if you
have a monolith in a car you can
simply extract parts of it and use those
as remote actors so it's it's a lot
easier from a refractor standpoint you
need a little bit of configuration but
this is basically copy pasting you
enable remote actors the IP address and
the port so if you run multiple
applications you probably want to run
them on different ports but it's fairly
easy to do something you need to do is
you need to define what messages you
sent from one JVM to the other JVM so
you know if I want to say send a play
message from the actor on my laptop to
the actor on the Raspberry Pi I need to
define those messages at both sides I
could have sent the string but the
string is not really the recommended way
of doing this so what we did was we
created like a shirt library called
message protocol where we define all the
messages and we use that library on the
laptop and on a Raspberry Pi and it's
really simple this is basically
everything for the music service we
define a case cross-play which simply
gets a file name so here I can say which
file I want to play and here I can
retrieve a list of file names and that's
everything and if I want to use that in
my application I can simply say ok I use
the package name where that music
service message which we saw in this
slide as being located and simply send
those messages between my JVMs so we
really liked using akka and using remote
actors like I said you don't need to
convert to Jason or create rest
endpoints it felt more natural although
of course HP has some advantages its
independent of technology you can create
a service in c-sharp and connect to it
with a java application or a scholar
application if you have remote actors in
akka you can only communicate within the
JVM so you cannot communicate with
another system written for instance
Internet there is a card or net but you
cannot communicate with it directly so
it's a bit more in dependent on the
technology HTTP is a bit less dependent
on it and HP is a bit more loosely
coupled you can change parts without for
instance on a server side without having
to change the client etc
um okay so we were really interested in
it but we were like okay how can we
convince people to use it I can say I
like it but I don't have blue eyes so
you probably don't trust me on my eyes
so can we get some hard numbers on it so
at first we try to see okay is there a
mismatch or a big difference in the
number of dependencies that we need for
it so we created fat jars which
basically is a jar including all the
dependencies that we need and these were
the sizes so here you can see that when
we use remote actors it's a lot smaller
than our HTTP but it's still quite
comparable to spring boots so no clear
winner here it's not really a winning
point for acha so don't work okay can we
do a bit of performance testing whose
shoes gatling already okay
who've you refused jmeter or some other
tools okay if you have used a meter you
have a new project where you need to do
performance testing have a look at
Gatling I've started with jmeter myself
but I like Gatling a lot better it's
more intuitive more more easy if you ask
me
and it's even so easy that I'm going to
try to explain it to everyone here so
this is the scenario scenario that I
used so create a scenario with a name
and I say that it has to be repeated
hundred times so this is basically a
loop of a hundred times I basically do a
get request and I check that to return
status is 200 and then I do a pause
between every request here I say I do a
ramp-up of users so I will get a
thousand users over ten seconds so after
five seconds I would probably have
around 500 uses of my application and
you can vary this of course this is just
the numbers that are picked so a
thousand users will execute this loop a
hundred times so that's basically
everything you need with Gatling to have
a simple test up and running so what are
we testing here the left side is the
same for both scenarios so we have our
HTTP endpoint but at the top we
connected to another rhtp endpoint in
another JVM and at the bottom we
connected to our remote actor instead of
acha HTTP to try and see what the
difference is between communicating
through
rest and wait remote actors if we done
look at the mean response times in many
seconds we can here see that with 50
users it's almost the same but if the
number of users go up remote actors
perform a lot better there are a lot
quicker and the same goes for the max
response time so I mean this is like 8
times as much for our HTTP compared to
remote actors I had a graduation student
that also did research on the
performance of akka and he tried out a
setup which was quite similar but not
exactly the same as I had and he figured
out that rest could handle around 600
users but remote actors could handle
like 3300 users so that's the difference
between either buying one server or
buying five servers so from a hardware
perspective in a business perspective
that's also a good business guys so
quite easy you would say all right maybe
I have to compare it to some more
traditional software that were more used
to like spring boot another joke most of
people that the of my colleagues didn't
get at first I tend to use pictures that
nobody understands but I found it was a
nice picture the unfortunate thing was
that spring boot was faster if we look
at the mean response times and of course
I could have falsified these results
because never trust any results you
haven't falsified yourself but I try to
make it nice and not fiddle with my
results although I did a little tweak
which spring boot are you if you use the
rest template to call a rest endpoint
that doesn't perform at all some people
even say it's not production ready so I
replaced it with a patchy connection
factory and got a better performance
with it that was the only tweak I did
and that was only because else I
couldn't handle more than 300 users so
it already broke down around here for
the rest I kept everything the same just
default settings and so unfortunately
for my story it didn't end up that good
but then I looked at the max response
times and and to my surprise that the
more load the application was getting
the better the remote actors were
performing
that's because rest was consuming a lot
of memory and a lot of processing
capacity when handling lots of users
while Akko is scaling a lot better with
a higher amount of users of course I
mean this is one check it might differ
for your applications but I think oak is
really something to consider if you want
to build highly concurrent fault
tolerant systems because they can
perform likewise as HTTP applications or
maybe even better depending of course
when you're set up okay something about
the challenges this isn't my house
unfortunately because else I would have
probably had a good insurance but I've
burned down some stuff I read a manual
on the internet on how to connect the
infrared transmitter I connected it got
something to drink and came back in my
room was like hey it smells weird and
then I tried to touch it but was really
hot so disconnected it and that was my
infrared transmitter luckily most of the
hardware I use is relatively cheap so if
you burn something you just get a new
one
the only disadvantages mostly it comes
out of China and it takes four weeks to
get a new one so if you go try and play
with this buy a few extra ones so you
can just continue on playing hardware
resources with my java application and
some other applications running on the
Raspberry Pi A+ I reached the limits of
the memory so it still runs but it's
just running it doesn't have much room
left and that's because I use a plus
raspberry point if you would have used
to zero it has twice the amount of
memory so with a new Raspberry Pi I
wouldn't have any issues at all but it's
something to take into account that it's
not as unlimited as your laptop or your
desktop at home it's a Raspberry Pi
Raspberry Pi connectors so I made a
complete set up everything worked and
then I started using the Raspberry Pi A+
and inserted a USB wife I don't know hey
what's a DAT simple ok it works so I
connected it didn't work connected
another one didn't work and then the
third one I first connected to my
television screen it worked
inserted to USB Wi-Fi don't know it
didn't work anymore so I pulled out the
USB Wi-Fi don't know when I looked into
the connector and I saw that the pins
that normally
keep your connector fixed they were bent
like this because it was stuck in it and
that's basically the end of your
Raspberry Pi apparently because they use
cheap connectors on a Raspberry Pi A+ so
if you use a plus first use a
screwdriver to push the pins a bit up
and then insert your USB wife I don't
know I really like Lego but I don't like
the way on how they work with batteries
batteries are really you have to use
non-rechargeable batteries because then
it works and if you use rechargeable
ones even like famous brands they often
don't work or some of them work and
others don't so the battery pack is
really picky RFID as well so you see
here that the RFID cards they're quite
close to the reader and then it goes
well but if my train will speed up then
it won't catch it anymore
so you can either buy more expensive
RFID readers because some of them they
even have power so they can send a
stronger signal or find another solution
but for me this was a cheap solution
which is working perfectly as long as I
don't drive two trains at high speed
we've had a few Wi-Fi issues why fight
conferences 2.4 gigahertz was awful
everybody was using it and I couldn't
use 5 gigahertz because 5 gigahertz USB
Wi-Fi dongles for Raspberry Pi have
really poor support luckily lazy being
lazy pays off now everybody is moving to
5 gigahertz and I'm still at 2.4
gigahertz so I'm getting better and
better with my reception of the Wi-Fi
but all I know let's not really
plug-and-play it it took me quite a
while to set up everything I often get
the question how long did it took you to
set this up and I honestly don't know
and even if I knew it I would probably
not tell you because that my family
would hate me forever on playing too
much with Lego because there are so many
options when I started with this I
wanted to use docker not for real good
business guys but I mean I like docker
and wanted to run it on a Raspberry Pi
with my trains but back then there was
our clinics I had docker running on it
worked perfectly just a few commands and
it was running but all the modules and
stuff I needed to use for the infrared
trance
murder and everything there was not
really documentation or software for our
clinics only for raspbian but raspbian
had really poor docker support you
needed to cross compile everything so in
the end I just thought okay I don't
really need docker so I just true that
requirement away and could just continue
and nowadays raspbian has better darker
support as well so if I would have
started again I could probably achieve
it a lot easier but there's there's lots
of documentation and and not every
documentation is right I found out so I
connected some stuff the wrong way and
burned some stuff takes a while to set
up everything and Lego with children you
would think it's a good idea except if
your children are like three years old
then you need to get it away quickly out
of the room after you played with it but
for me the biggest challenge probably
was time because you keep on playing
whether to keep on inventing more stuff
and if you think okay it's just a sensor
or just connect to it and it's done in
an evening that never happens because
you have either issues with the hardware
or with the software and you don't know
where the issue is because you first
have to figure out if it's an hardware
or a software issue so it took me a lot
more time than simply programming this
because if you're programming you write
a unit test or you debug and you're done
debugging a combination of hardware and
maybe you're thinking ok let's look nice
can I do it myself and actually you can
do this yourself quite easily I recently
released a software not the account
scholar part because I think that's a
bit hard to set up if you're not used to
Scala naka but I built a new setup using
spring boot and it's running on here so
this is a really small let's see if I
can show it to you at the bottom here is
a Raspberry Pi 0 so that's a really
small one for the rest this setup is
basically the same as on my train let's
again there's an infrared transmitter
with an infrared receiver let's see if
we can make it drive around so what I
did was I created basically one
repository where
two applications LCC Ensor script is the
leg of Control Center in Seoul script it
installs all the dependencies that you
need all the software that you need and
LCC is a simple spring boot application
that exposes a rest endpoint with which
you can control a car a train or
basically anything you would want to
control we see the camera feet
unfortunately it's not looking at
something that's not really interesting
let's see if I can make it move a little
so I mean this is a really simple rest
interface which I intentionally did I
didn't want to make too fancy stuff okay
it crashed but this is to give you a
simple idea I mean if you download this
stuff and you buy a raspberry poignant
for Rhett transmitter you're good to go
basically okay thanks for putting it in
a good direction
let's see up gotta make it go a bit to
that direction so this is just simply
executing a few rescue modes of course
it could have created a nice interface
or control it with a steering wheel or
make it okay I'll stop it so with this
you can create your own front-end for it
by just doing rest calls and just have
fun
and so for the conclusion I mean I think
we achieved what we wanted we got a lot
of experience with running our normal
software on a Raspberry Pi and we
learned that it was perfectly possible
to run Java or Scala applications you
sometimes have to be a bit patience when
a patient when it is booting but when
it's running it runs perfectly commands
are received almost instantly you don't
really notice a delay in it and we
learned a lot from it because later we
did projects for customers where we used
this software again and then we already
had experience with Scala naka and
everything around it so it seems like
fun but we learned a lot of useful
things from it and I can really
recommend to do something like this if
you want to try out a new technology
I could have written hello world with
Scala naka and probably that would have
worked because if a framework doesn't
support hello world it's probably really
crappy but if you want to figure out how
a framework is working and if it's
useful for enterprise products you need
to write more than hello world and then
yeah I advise you create something cool
out of it and something that can grow
bigger and bigger so you can experiment
more and more with it so the first it
was really useful learned a lot from it
got a lot of attention from it gets like
it I got to all kinds of nice places
although Amsterdam for me being from
Holland isn't really that adventurous
but it brought me basically all over the