Software Programming is naturally fun but making something physical, to interact with the world that you live in, is like magic. Is even funnier when you can reuse your knowledge and JavaScript to do it. This talk will present real use cases of automating a house using JavaScript, Instead of using C++ as usual, and Espruino as dev tools and Microcontrollers such as Arduino, ESP8266, RaspberryPI, and NodeRed to control lights, doors, lockers, and much more.
How I've been Using JavaScript to Automate my House
From:

JSNation 2022
Transcription
♪♪♪ Hello, my friends! Oh! Woohoo! How's it going, everyone? Going well? Yes. I'm so excited. It's been a while since my, like, pandemic and this whole stuff. I'm kind of nervous, but I think this is something, like, my experiences work at home, making my wife crazy at home. So I'm going to give some tips about it as well, okay? So I'm Erico Mando. I've been speaking about JavaScript, sharing a bit of my knowledge as well. And today I'm going to show you some content I've been doing. So everything here is already online, so you can take pictures, publish. Please mention the event as well, so it helps a lot us as well. And here I'm going to show some experiments using both JavaScript and C++. But don't blame me. I'm not a C++ developer. I'm just trying to experiment and actually to make my wife crazy, right? All this talk is just to make her crazy. Well, we know that software is not just something simple, right? We know that we struggle a lot. I'm not an expert in what I've been doing. This is just experiments, okay? So later on the QA I can show you some burnouts that I did on my hardware. Okay. When I started working with IoT stuff, the first thing I was like, oh, I should use Raspberry Pi for everything. And then I figured, well, it's not actually it, right? So I could use Raspberry and microcontrollers. And I wasn't aware of what does it mean. Well, to fix this first question, a Raspberry Pi is actually a supercomputer, right? It has its own operating system. You can run games, you can run processing, you can use for programming as well if you want it. You can use Linux, Windows, and a lot of other operating systems, and actually you can expand memory. Man, this is crazy. People are using Node.js inside it to make production apps. I don't recommend it, right? But you can do it as well. Okay. When we go to the microcontroller world, the microcontrollers are a bit smaller thing, right? We call them like the microservices of hardware. It's like a small unit of processing. You can run on your walls, control your lights, but it's much cheaper, right? You can buy it for $1 sometimes on Aliexpress. As you can see, the whole thing is like 320 kilobytes run, right? JavaScript developer doesn't care much about memory, right? This was my first struggle, and I'm going to show you later. It's a single core, but we can simulate threads and other things as well. But you might have heard of Arduinos as well, right? Who has heard about Arduino? So popular, right? This is the Brazilian version. I'm Brazilian, so I brought some of our country as well, just to show you a thing. Okay. Now I introduce you about what I've been doing, or what the difference is for you to get some context, and now I'm going to show you some experiments and some not good things that I've been doing as well. The first stuff I was like, man, Raspberry Pi. People are using Raspberry Pi to create HTTP servers. This is so boring. I don't care about creating products anymore. I should do something interesting. I like beers. I was like, why not make my own assistant, right? So here I have my beer cane, and there I have a Raspberry Pi. I'm going to show you later how it's working, but let's first understand the project, right? So I could have my beer cane on my main wall. I have a camera right there, and when I see a friend, I would just recognize. My beer cane would say, oh, welcome home, or just make some jokes, and that's it. I was like, yeah, let's make it. Well, this is my first demo. Right here I have a Raspberry Pi, a supercomputer running inside the beer cane, and on the other side I have other ones to recognize friends, right? Here I have a camera, and I'm using like Azure API. I don't know anything about machine learning here, just using APIs. But then I recognize my friends, and then it sends a message to the beer cane when it recognizes and says, oh, welcome back. The whole project was good, but turning on a LED, it's like a hello world on hardware, right? Well, I was like, I should do something more interesting, right? I've watched the Purg movie. I saw a kid there moving a car with a VR glass. I was like, hmm, I could do something like this. So this was my version. Well, it's an MVP, right? I'm a backend developer. This is a backend of the hardware, right? So here I just hacked my PS3 controller using the Bluetooth. Right there I have other microcontrollers, well, just controlling the other thing. And here it's working. Well, this is something good to understand, like threads, right? Because when you are accelerating, if you try to horn, like you press the horn, the car just stops, so you have to make it like in different units of processing, right? But it's working really well right there. Okay. Well, I start trying more at my home, so I build some wires. Now it starts to mess, right? But here I have some interface right there. When I press a button, the lights will change a bit, but this will control my main door right there. Let's see, like the lights are changing, I go faster there, and then I can see that my door is open. Well, I had a lot of struggles here, and I have a lot of cave-ins that we can talk a little bit more later. But yeah, it's good, it's good enough. But as I said, I have some tips for you who live with someone else. So how many of you are living with someone else? Like parents? Okay. So you should be a bit aware, because your weekends should look like this, right? All the main dinner table with a lot of hardwares, a lot of friends coming there, making a lot of projects, and I think they don't like seeing a lot of wires right there. Okay. Starting about the whole automation thing, I just moved to a bigger house right there, and I figured at night I have to walk through my whole home, like turning off all the lights. I was like, I'm a programmer, I'm a lazy person, so let's try doing something. So the whole idea here was to control first the light switch. So I Googled it, and I used the ESP8266, like a microcontroller, and I have a relay. Basically I send one to the relay, and it will just cut the energy. If I send zero, it will just keep the energy going on. Yeah, it's inverted. I struggle a bit to figure it out as well. But yeah, I have one pin for each light, and this is how it should be working. Well, just turning buttons on and off wasn't enough, so I was about to get some sensors. So I got humidity and temperature sensors, so I can have a dashboard, and I will start to think how I could make the lights turning on and off just automatically. So this was the MVP. MVP, right? You can see a lot of wires right there. My wife got crazy with me. She was like, no way we're going to do this. But it works, right? When I press the buttons, we can see it working. Just a thing. Well, I figured out some other problem. The whole thing about IoT is cheap, right? But the most expensive piece was to hide the wires. So this little box was more expensive than the whole project. So I could print the whole thing, but I was like, okay, let's make my wife happy. Well, my wife approved. I saw on her Instagram, she was testing it, and I was like, ooh, QA passed, right? For all this project, I've been using C++. I'm not a C++ developer, but I found the platform I own that helps a lot to upload the code, to generate the dependencies, and work really well. Right there, we can see, like, you can have a YAML file that you can put all of your configurations, your dependencies, and a lot of stuff. But for me, a web developer, this was the most interesting thing. Imagine a thing. You're not an expert as me, working with hardware. What if you put some cable in the wrong input? You're never going to know, right? It's already on the wall. How do you know that? So here I wrote some tests, some integration tests that I published my code to the board, and then I see all the sensors working fine, and then I can see, really, it's working or not. So this was really hacking for me. And as a developer, I'm a lousy person, right? So I've been using Node-RED, no code in this case. So here you can see all my flows in the left screen for you, right? And the right screen, we have the results right here. So I'm going to show first the interface. So if you click all the buttons on the switches, you can see on the back end, just changing all of the messages. You can see the temperature, the humidity. And here I'm going to show some flows. Well, when someone presses a button, I'm just showing the logs here, not something like confused, not something hard to make it. But for the weather, I have two JSON properties that I'm converting and just showing on the interface as well. And then in the last one, I'm logging some more data and actually trying to make the first MVP to make the lights turn on and off. But this is just another subject, okay? Well, I was... As a developer, right, we try to automate something. You automate something that you could have done in 20 minutes doing two hours, right? So here I was like, hmm, this is not smart enough. Because I had some buttons, right? And I create another problem. Imagine, I'm going to my main room, right, with my dishes. And I was like, oh, turn lights, right? So I put all the food on the table, I get my phone, I connect to the Wi-Fi, I open the app, I click the button, and then the light's on, right? You couldn't imagine my wife, right? She wasn't so happy with it. She was like, oh, it's not easier just to click on the button, right? Of engineering at my own house. So I was like, okay, let's try doing something. So this is my little brother, and we were drawing a POC right there. I'm using here a ultrasonic sensor. So it's like a radar. It's just sending signals and getting back. But the problem was... We're developers, right? We know that it's not 100% sure sometimes. Well, my wife, she's afraid of ghosts. This sensor is just reverberating. So sometimes the lights just turn on. Imagine, at the middle of the night, the lights just turn on. Well, I was like, okay, we have power right now. We have processing power right now that we can use. Let's try recognizing faces. So right here, I use the microcontroller that not only promises to detect faces, but to recognize that you are you. Man, this is 5 megabytes, like, processing power that can do a lot of stuff. Well, to do my first POC, I wasn't trying to use on the machine, because it was C++. I was like, oh, my god, this is so messy. Oh. C++ is hard. Yeah, sure. And here I was just taking pictures, staying until my note read, and just scratching some screen here. You can see the video is not that good, because I was just posting on the Instagram as I was getting some results. Well, C++ is hard. It's really hard for me. I've never touched C++. I was like, what if we try using JavaScript to reuse my knowledge from the web on the hardware, right? To really feel the power. Well, I found out Espruino. How many of you have heard about Espruino? Oh, nice. Espruino has a lot of benefits. I'm going to show you later. But the first thing is, it's JavaScript. You can drag and drop stuff as well. But you can also upload your code using SSH over the network. So you can have all of the machine connected to the wall, and over the network, you can send signals and upload new code. So this is really, really nice. Well, right here, one thing that we should care is, when we're using microcontrollers, we know that the memory and processing is limited, right? But here we can see it's just 120 flash that they use to make everything work with JavaScript. It's like a small, a tiny interpreter that we can use JavaScript inside of it. Well, I was like, okay, man. Really easy, right? Let's rewrite everything. Let's just convert everything to JavaScript. Well, I found the docs right there. And on the docs, I was like, okay, let's just use the docs. Let's rewrite everything. And then my code was something like this. I had an interval, send the data, receive the data, and send the light density as well. And then I upload this code to my machine. Well, it doesn't work, right? I just used all of the memory and processing, and I was like, oh, my God, I cannot use JavaScript for it. So I just changed the board to the ESP32, and everything starts working again. So this is my final POC for the piece that will go to the wall, and let's make it work. Right here, you can see a lot of logs. We backend developers love logs, right? We have all the other stuff working right here. But you can see that the camera is not working properly yet, because I haven't finished this project yet. Well, JavaScript is magic, right? We can use a lot of stuff, and for this one, I really feel the power. So if you want to use Esprino, you can use the interface or the terminal and make everything work so easily. This is not a tutorial, right? I'm just showing some of the challenges. Well, the first challenge, it was meant to work on Esprino devices, so using generic devices might not work well. The platform has a lot to improve. We have some generic errors, so we should use it more so we can improve it more, and, for me, I felt an imposter when I see that async doesn't work. A lot of features are not working on this interpreter, because it's just a small thing, right? Well, and just finally some benefits, right? I think this is the whole thing about this talk. You don't need to recompile the project every time. When I was working with C++, it was back and forth. I compile, I see errors, I come back, compile, see errors, and then the third time, it works. So I was like, no, using JavaScript is much better because you can do it on SSH remotely. And Esprino idea helps a lot, you can use less memory processing, and you can reuse your knowledge in JS, right? You're a web developer, you can make something to scare your mom, to make your cat better, to play with your friends for you if you don't want to, right? We are developers. Well, some final words for you. I think this conference is so amazing, so nice to be back. The whole thing here is for us just I know that CRUD pays the bill, right? Making, creating product, creating service, but when we're trying to challenge ourselves, when we're trying to make something for us, when we're seeing like a car moving, we can see a lot of new challenges, and we can feel like that adrenaline that was like, oh, I'm not sure if it will work, but when it works, it's like indescribable, right? And if you're trying to start on this, try adding more sensors, not just moving buttons. Try to make something smart to make decisions using it as well. And actually, learn JavaScript, right? Without async08, we can back to callbacks. So yeah. It's hard, right? But we're going to learn a lot of vanilla JavaScript as well. Well, all of my presentations are already on my website if you want to take a look. Everything about Node.js, JavaScript I've been putting there as well. I've actually just released a new course right now. It's about Node.js streams. If you want to learn the most powerful Node.js feature, go there and see. It's just on the pre-release. And if you want to take a look at the videos, my website, follow me on Twitter, take some pictures, show me your experiments on Twitter, please go there. Okay? So thank you so much for having me! Thanks a lot, Eric. We're going to go into Q&A. All right. Well, that was mind-blowing. I think you overengineered, and I love it. So I can imagine that it's a lot of fun to work on that stuff. And a bit challenging. First question is from Jen. It's just a compliment for your wife. Special dedication to your wife who resisted the divorce. You still wearing the ring? Yeah. It's tattooed. Tattooed. So you can't get a divorce. Smart. Smart. Question from Jalen. Where is the best place to get started to learn how to implement similar features in my own home? It's hard, right? It's like Google, right? Oh, just before going, let's take a picture with everyone, like, with hands to go there, right? Just a second. Should I take it? Yeah, yeah, sure. Let's count on three, and then we make some noise, okay? All right. One, two, three! I'm Brazilian. I have to do this, right? One more time. One more time. One more time. Three, two, one. Well, the best way to learn is to go to Google. And Google is a great place to learn. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. And if you want to learn how to use Google, you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com. And you can find a lot of great stuff there. So if you want to learn how to use Google, you can go to Google.com.