 good so hi my name is meals and this talk is about JavaScript on microcontrollers and that's yeah all right doesn't work no matter and it let's try again yes all right today we're gonna talk about JavaScript on microcontrollers and usually this talk is about 45 minutes long and we just have 25 minutes so I have to talk really really really fast or cut out some stuff so I remove all the jokes and the funny things I'm sorry this is just the boring stuff don't don't don't worry this is going to be fine this is the meat of the talk is the good stuff I promise so anyway I'm a Masuda tech speaker and I'm a Google developer expert I tweeted HTML5 tests and I love electronics so when I was a teenager I had this cardboard electronics set with all kinds of different components there it is and you could clip wires between springs and you could build a radio or a Morse code transmitter and it was just absolutely fantastic and this is what I have now it's basically the same thing still components that you connect together with some wires and they have become much more advanced instead of transistors and diodes and resistors we now play with microcontrollers and all kinds of sensors and everything still works together but I am a web developer by trade and I program in JavaScript so programming microcontrollers is usually done in C and I don't want to program in C I want program in JavaScript because I know JavaScript so what if we could leverage JavaScript a program microcontrollers in JavaScript and leverage the browsers to connect these microcontrollers to the web and and let them extend each other's capabilities now that that is all fine and well but microcontrollers are slow they are really really slow and when I say slow that really does not even convey the idea of how slow they actually are they are slow so take for example the the Raspberry Pi this is the zero W this is not a microcontroller this is a computer and it runs Linux and Node.js and can run a browser like Chrome or Firefox so this is not a microcontroller this is this is a microbit it's a little board for for school children and it runs at just 16 megahertz has only 16 kilobytes of RAM and it is slow but it's not even by far the slowest microcontroller you can buy but this is fast enough to run JavaScript so to put in context the Raspberry Pi is about 60 times faster than a microbit and it has way more memory so the microbit is on a completely different level but but running JavaScript on microcontrollers is actually pretty brilliant microcontrollers are actually very power efficient you can run JavaScript on a tiny little Bluetooth enabled board and powered using a coin cell battery and it can run for months so slow is a good thing because when we make things faster they tend to require more power and microcontrollers are fast enough yes they are slow but we only have to solve very tiny problems so microcontrollers are fast enough for everything I'm going to show you today yeah you can't run react on it but yeah I wouldn't run react anywhere anyway so so that's the one joke so let's take a look at that's how we can get JavaScript on our microcontrollers so there there's actually an open source project that allows us to do this it's called Espruino it's traded by Gordon Williams and you can also buy hardware with Espruino already installed actually Gordon is here with a stand in the AW building I think so check it out but there's also versions that you can flash yourself on your own hardware like on ESP32 and ESP8266 microcontrollers now it is an interpreter that supports most of JavaScript even some ES6 features like arrow functions and so and it has a REPL functionality so you can connect it using to the computer using a USB cable or Bluetooth and you can execute commands on the microcontroller just like you would in the console of a browser so now it's time for the demo I hope because yeah this this is well I have some warnings to give this is cutting-edge stuff for hardware demos with experimental technology and most of it is using Bluetooth which is a wireless connection and Bluetooth uses the same wire frequencies Wi-Fi and this is a room full of Wi-Fi devices so there's a lot of opportunity for interference so this is the moment I usually get nervous so yeah okay we're gonna create some some stuff first of all we're gonna create a connected light bulb in JavaScript running on a microcontroller and to do that we need some hardware we need a Neo pixel ring this is basically just 24 LEDs that can change to any color you want you can individually address them and change the color of each of the LEDs it's connected with three wires three wires and one for power one for ground and one with the data for turning on LEDs and it's connected to a tiny little micro controller it's not that big this is actually ESP32 and I flushed it with aspirino and basically that means we now can make it a connected light bulb so let's try this let's see if this works so when I apply power to this thing nothing happens but that's okay because I haven't programmed it yet so but that's that's actually what I expected so let's start with take a chair let's start with some code so I'm gonna need a color so this is just an hacks representative of any RGB color this case FF9900 and we need a buffer in this case we have 24 LEDs and for every LED we're gonna save an RGB value so 24 times 3 and we're gonna set that color into the buffer now the problem is these LED rings are a little bit weird they think GRB is the right order for RGB well that actually depends on which ring you buy sometimes it's also something completely different it's never RGB though yeah so finally we need to write our buffer to the data pin let's connect to the device do we have it here no not here and it can't find it so I have a problem but I have a backup fortunately there it is so when we write that value to the new pixel ring to the D15 pin it will light up so I pre-programmed this one so that it's actually cheating but will continue anyway so what we do next is we're gonna skip some stuff so what I'm doing here I still have the color I still have the buffer except I'm using a function called update and we're gonna fill the buffer with the color again and then I'm gonna write it to the D15 pin and call update it's the same thing now what I do then is I use a special event called in it event and whenever the board turns on that callback is called and we're gonna advertise our light using a special name and as per we know light we're gonna advertise it using Bluetooth after that we're gonna offer some services these are kind of like IDs or basically the URL endpoint of our Bluetooth API service in this case it's FFA, FFB now when we make this readable that means we can use a different device to read the color and when we make it writable we can use a device to write a color to it the default value is our RGB color and whenever we write something to it we're gonna update our color here with the new values and call update so this is 29 lines of code and with these lines I can make this light bulb connectable from a different device from a Bluetooth device so let's see I do quickly so let's try this so what we're gonna do using the browser using the web Bluetooth API connect to this device so we again have this FFA ID and we use the Espinoe light name as a filter and if everything works we have our light here that is being advertised we pair with it we get our device then we can connect to the GTT server if that's details don't worry about that and it takes a bit longer than I'm comfortable with but there it is and we get our service there it is and then we get a characteristic that is FFFB and that is the one we're advertising here and with that we have a value that we can write to so we can write any value to this color to this device like FFF0080 in the turns big but we can change it to any color we want we can change it to blue green basically any color any RGB value we want and we did that with just five lines of JavaScript in the browser and 29 lines of JavaScript running on a microcontroller so I think this is pretty amazing but we can do more with those five lines I can create a web app and I can just make it pretty can turn it black and even I can run CSS animations on it yeah okay anyway I do have one more demo to give and that is using a remote-controlled car and this car is it has a microcontroller it has a motor board and there is no soldering in wolves in any way it just put together and some what added some wires and that's it I'm going to drive it today I hope and it's so let's get rid of this connect to the car and write some code we need a function to drive the car we have want to turn left we want to turn right that has two motors if one go forward go both motors full-on backwards both motors reverse so that these these pins here those are determined by the motor board I'm using and this is basically all the lines of code you need to make it drive part to make it stop that's also very useful especially on this table we need another function and a little helper functions is also pretty nice forward reverse left and right so with 17 lines of code I can now make it go left and I can make it go right and I can make it stop all right okay this is good this is good so again we get prettier demo for this so somehow today we're gonna we're having some issues but we'll try again there is so no I can use the arrow keys to drive it around so but what I haven't told you is that I can actually drive this table of drive this car of the table because of well I can drive it off the table I can try but it will refuse to do so because of lasers well actually a little tiny laser that's right mounted here on front and it measures the time of flight it takes before the laser to reflect back and we can add that to our code so we have to measure distances and when the distance is over a certain threshold we and we don't are we are not yet in the emergency mode we're gonna turn on the emergency mode and reverse and when we're clear again we're gonna cancel emergency mode and stop and we must not forget to actually initialize the sensor because otherwise and this this should do it so important thing so I did this talk in Phoenix a week or two ago in Arizona which is well I used the American flag it actually drove off the table they didn't kick me out of the building immediately fortunately but yeah I probably won't do that again but here in Belgium we're safe that this is fine but yeah to be honest about 80% of the time this works