#228 ESP32 Over the Air (OTA) Updates with ONE LINE are EASY!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now today i'm going to show you how we can get code to this device from the arduino ide and update it over the air not the usb cable like this easy keep watching indeed now it really is easy right and i hope i've made it even easier and more useful but let's start at the beginning let's assume that this device that's flashing the led and whatnot on my workbench is in fact controlling my entire workshop here probably sending a rocket to the moon counting b's in the beehive whatever yeah it's doing some work and where that device is now installed is pretty inaccessible and you think if only i could get to it to change the code well now you can by using over the air updates you can just say to your wi-fi basically i want to go to this page which is on that device you go here's my new code trella well you saw in the intro didn't you it really is that easy right what the steps we need to do i want to do a shout out for jlc pcb no stop stop don't go away look what they're doing two dollars for aluminium circuit boards this is absolutely incredible if you've ever wanted to try an aluminium pcb now's the time to do it now remember aluminium pcb is a single sided normally with the aluminium on the bottom then you have a dielectric layer that transfers the heat up to the top copper layer now aluminium is very very strong and it will suck the heat away out of your components without the need for extra heat sinks for example go and have a look at their website and check them out and there's more jlc pcb now allow you to create your own parts library because there's nothing more disappointing than creating a pcb and then finding you can't get the parts or there's a big long delay now you can create your own custom parts library to ensure you get the components you need and of course the associated footprints so you know they're going to fit on the pcb itself to get to this page that describes everything you ever need to know about creating your parts library simply go to their home page and then click on the link at the top very very simple and a really really useful feature go and check out jlcpcb now okay here are the steps we need to do first step we need three libraries now one you can get directly via the arduino ide but the other two you have to go to github download them and then put them into the arduino ide let's do that very quickly because i'm assuming even if you are a beginner you know how to install libraries into your environment so let's just whiz through that in a couple of minutes right there are three libraries we need the fourth one is already included into your esp32 or esp8266 environment so we can not do anything with the wi-fi library that's there but the three you need there they're on screen now follow the links in my description below or on the github go to github and do the following so this is one of the libraries we need it's uh async tcp um the me no dev uh moniker up there is in fact a chap called haristo gotchkoff and he's um i think he's actually employed by espressif if not i'd like to know why he's producing everything there is to know about esp32s and the like but anyway he produces all these libraries so i'm guessing it's expressive based but anyway this is the first one now what you do for all three the same process go to the github link that's down there right there's three libraries this is one of them go to the code button that you see there click that and where it says download zip do just that click that and it will appear in your downloads file right let's assume you've downloaded all three libraries now they're sitting in your downloads folder or some other folder where you know where they are that's fine go back to your arduino sketch and say go to the top of the menu bar there and say sketch and you say include library and you see there's a drop down list there well you can see straight away that i've actually got some libraries already included what you say is add zip library click that and it will go where is this library and it always defaults to this funny funny folder i don't know what this is anyway if we go to downloads now so here we are here's my downloads folder and you can see that the ones we're interested in here like async tcp and esp async web server yes they've all got this dash master bit on the end right that's that's a github thing don't worry about it you select those and arduino will import those lobbies and they'll be available for use exactly as they are for me great let's see you've got your libraries in place let's move on right just to recap of what this board is doing it's flashing an led as you can see but really controlling nasa or something right and it's also outputting stuff yeah ironically to the serial port by the usb just so that we can see what's happening as we upload over the end you can see what's happening normally of course if this was now buried deep in my beehive you wouldn't have that cable attached so let's go back to the code and see what what marvelous code i've written to make that led flash right very simply then we've defined the led pin as pin 15 it's just one i've chosen out that array of pins we have on esp32 in the setup we're saying the pin mode is output obviously it's quite right to it and then we have the magic line that says ota init yeah this is the initialization of the over the air stuff yeah but we'll come on to that let's let's see this working yeah so the loop basically says um well there's a couple of counters in here and we're saying we're outputting something to the serial monitor via the usb cable just to have a little count so you can see it awaken any other messages that might come out there and then this line here flashes the led every so often right so okay so we'll pretend this is doing a lot more than that um how are we going to upload this then to a bare device that knows nothing about over the air okay we're in a catch 22 aren't we you've got to make sure that the very first upload is the traditional way via the cable otherwise there's no there's no code on the esp32 to do anything so the first time around just upload it via the usb method as normal and get the thing running let's let's see actually in action running so we know it's there now i've reset the device because there's something on it you need to look at and it's important right in the code i output the ip address that that device is running on and you can see that here in the actual serial monitor i'm using cool term but you could use the standard one built into the arduino ide the important bit though it says is look here i'm running on 192.168.1.83 and that address has been assigned by the dhcp server of myrusa and if you know about networking you can either make it fixed or not it's up to you but it's important that you output the ip address so you know what to type in to find it especially if you've got more than one of these devices lying around yeah okay so now we know the ip address that that device is running on just like any other web server in the whole world they all need an ip address but your one is a local ip address it only applies to you nobody else can get hold of it great now we know that it's running and you see it's outputting counters every three seconds right um how are we now going to update that then well we know the address you've just seen it yeah 192.168.1.83 well let's go to a web page now a standard chrome browser whatever you use and let's see what happens so here we here we have a fresh web page so i'm just going to type in the ip address that we just saw on that serial monitor so 192 168 1.83 and there it is now this this landing page if you like this home page um i've put together a sort of added value on top of the stuff that other people who have come before me okay because i found it much more useful doing it this way and the reason i've done this because the other way was a little bit generic you never knew which device you'd actually connected to even if you'd known the ip address or thought you'd known the ip address right so here it says great you've reached your beehive counter ah yes it's the right one that's the one i want to update and it tells me i'm on that right address look 192.168.1.83 as i suspected okay right there's a project description here that you can write as part of your code simple little bit of text just to remind you perhaps there's some very funny things about your particular project you think i must not forget this next time well you will forget unless you write it as part of the sketch because then you can read it here in future okay and the uh the title of this page the actual tab title is the title you've typed in that project as well one basically here it's called beehive counter it's very important of course that you don't upload code from your mega marvelous project to the wrong device okay so what this is saying is right how do we get this code up here using ota right it tells you here but let's go and do it step by step shall we right so you've written your code and you've compiled it in the normal fashion using the the verify button at the top there that one yeah this is verify and it's all working you think okay time to upload now over the air to my device the way you do that you go to sketch and you say export compiled binary i don't know if they're going to change the functionality in this but all it does is basically does another recompilation yeah i know it takes forever and puts it in um the same sketch folder well as your sketch so if we say export compiled binary you'll see what off it goes and i might speed this up a little bit actually because it takes a while okay it's just coming into the linking phase now which means basically the compilation error didn't find any errors now it will link it and it will put this output into a special folder and you'll go where's my sketch i can't remember where my sketch was yeah that's easy to find you just go to sketch again at the top there on the menu and you say show sketch folder so this is the build folder now where you've actually created your sketch there's your sketchula e-s-p-o-t-a-ino e-no file yeah and there's a new folder called building it so if we double click the build there's another subfolder these names are made up i'm sure and then we have these bits in here now let's let's keep away from the partitions bit we'll cover that in a future video perhaps uh inner.elf folder we don't want the elf it's the bin folder we're after bin right that's the one we need so when we come to upload our code over the air that's the one we need from that sketch folder and the good thing is the over-the-air system remembers where you last got it from so let's go back to the browser and see if we can pick up this code right here we are then at the landing page the home page of your device and it gives the same instructions here a little bit more tersely as to what i've just shown you and i've even given you a little link there so we can either type that in the address bar up here yeah or just click it i guess what i'm going to do right now this is now going to say where is that file that you are going to use to upload once the page is loaded just click the button to say choose file and you'll go back to that folder which we just found out about and choose the file from there so choose file where is it it says now it's going to download because i've been mucking about but if we go to my arduino sketches folder we'll find it in there and there's the file we just double click that and the update starts immediately as you can see it starts immediately and starts trundling on and there we are normally it's about the same speed as if you to upload with the usb cable okay right that's done then that now resets the device and which then of course runs on the new code right okay let's actually change the code now and prove that it's going to be uploaded over the air all right now um in between time of uh the last bit of video and now i'm taking the dog out for a walk and all that i've decided to change the board because it just makes it a little bit easier for me to use and that last board was a 16 megabyte ram one and it was it was playing up a little bit i've got to be honest it's the first time i've used it so i'm back to a tried and true trusted esp32 now so we're on pin 13 for our flashy flashy led and let's scroll down a bit uh to the code so here where we're saying you know make the led light for 100 milliseconds out of the full second let's change that to 500 so now it should be equal equal marks base ratio on and off should be the same just go back to the main window to see what it looks like so there it is at the moment now you can see i've got these two wires here now because i'm going to power the board not from the usb but from the power supply so if i disconnect right so it's still running but no usb okay so if we go back to the code window right now we've changed this code here okay got it on pin 13 that's fine now what we're going to do is compile and export that that binary into a new folder okay let's let me just do that quickly and then we'll go back to the web page to do it right so that's compiling and eventually it will throw over the wall to that folder and we can see when that's ready just by going to the browser so here's our browser choose file right so here's our pop-up window that says where where's the file then and what's the time 1655 and it is 1655 yes i know i work long days don't i right let's double click that and um right so that's uploading and whilst that's uploading we're going to whiz back to the desktop and you should see the flash rate change so there it's flashing as it always used to just a quick flash yeah out of the lot oh here i am i'm back oh stop flashing now because it's rebooting it's trying to get hold of the wi-fi connection again and do we have success there it goes right there we are now you can tell that's a much slower flash rate than what it was before so the over-the-air upload has worked fine now of course now let's just take it a step backwards here as you can imagine if you're uploading new code to your device that's stuck in the middle of a beehive and you get it wrong so that the device does not run that code properly anymore it hangs or crashes constantly you know rebooting all the time you're stuck so you do have to be really really careful about the quality of your code that you're delivering out into the wild as it were so if you've got an inaccessible esp32 device that you're uploading via over the air you really should have a a test rig with exactly the same hardware on it that you're doing all your updates to also over the air to make sure that still works but you can use a usb cable too before you go this is it finally tested now release that to live and that's exactly how i do it to my esp32 web radio i don't want that suddenly not working if i've got some little tweak done to it so you have to be very careful okay so well and good it all works i mean you can take it from me it works it works for millions of other people as well so what's the code behind it how does it work now so far we've only had a look at this little bit code and i said look it's this one line here that does all the work and that is true that is the one line of course there's a tiny bit more to it because we've added value here well i hope it's added value if we scroll to the top of this code you'll see exactly what's needed right there's two things first of all you have to type in first of all is the ssid of your wi-fi and your password as well now there's two other things that you need to put in one is the project title and keep this really short yeah not some long verbose thing because that's what's going to appear in both the the chrome or whatever browser you use tab and also on the page right so i've called mine beehive counter that's great and then you can say the project description this is really an aid memoir for you just to make sure you don't forget things like you know turn off this or disconnect that before you upload something of that ilk yeah so i've just typed something in here as you can see this is a long string so i've split it and you can split a string just by putting a backslash at the end of the line and then continuing it as though it was all just one one big long string just makes it a lot easier okay now the project description can be plain text or it can be html if you know html right good good time to try out really isn't it and then you include this library here the identification dot h this which is what i've put together standing on the shoulders of giants as it were um and i'll show you that in just a sec so that's that's all you have to do at the beginning of your project to make it well ota enabled and indeed web server enabled yeah okay right let's have a look at this code but i would suggest that unless you know what you're doing don't fiddle about too much with that file because you'll break things right now this is the identification dot h the header file but it contains a code okay here we've said look there are two external strings in other words declared somewhere else in another file but take it from me that they are there that's what we're telling compiler called project title project desk hence if you look at the main sketch here this is where you've defined your project title nice and short and then some kind of description there's your ssid and passwords are also defined externally here we start a async web server just so that we can access it and in the ota init which is that one line that we call we'd all the wi-fi stuff yeah we we set we connected the ssid set the power and all that print a few things like the ip address and all that down here and then say a couple of bits here just to say when i get a web request from a browser slash just just the name of the ip address it sends out all this stuff here now if you don't know html at all this will be like greek to you right absolutely like greek so do not touch it if you know html and ideally a bit of css as well there's no javascript though you'll be able to tweak this and make it your own but what i've found is i'm gonna i'm gonna put this into my libraries folder under a different uh you know the top level folder called identification so and then this will be in it and i'll be able to copy the same file in to all my esp32 projects then writing having a slightly different one or multiple copies of the same one but if you don't know what this is leave it alone basically right in the last two lines of this file just say right start the elegant ota server and the standard server and that's that's it that's all that file does and what that what it actually gives you is the bit we've already seen in the browser that okay and if you notice in this browser this is the bit that you wrote so project description all this is what you wrote in the sketch at the top and the tab which is which is called beehive count you can't see it's gone off the screen here because the way it captures the window but basically beehive counter is at the top tab so if you've got i know let's say two or three esp32 devices that you're updating over the air you'll see which one it is at a glance and there's some simple little instructions that you saw that was being compiled out of that uh little bit of html pretty standard stuff as i say yeah i mean to be quite honest that that file reminds me very much of 1999 that's when i started doing all this html stuff and it grew from there right that's it then so basically forget this identification.h just include that into your sketch like that having set these other things first if you don't set these first this will not compile correctly because it says after all i've declared some stuff externally it goes no you haven't kaboom right so make sure you put these in okay and that's it so it's those four declarations really the include file and then that one line ota init and it works and it adds value so i'm going to add this to my esp32 web radio it will be on my smart workshop heater controller which is coming along nicely uh and anywhere else i can think of really i wish i'd use the esp32s now my bin lid monitors but maybe i'll use it on the receiver side in here because i think it will just lend itself to it once you've got a thing all screwed up in a box you don't really want to have to go to it take it off the wall unscrew it find a usb cable somehow get it into the device just to upload a bit of code what it means is you don't bother anymore yeah and well if you've got an enhancement to make or sort of fix or just want to tweak something it's nice to be able to do it isn't it but beware what i said test it out first cool i think we're just about there actually right we'll leave we'll leave a nice shot of a flashing led so it does come in useful sometimes and so great if you've got any questions by all means put them down into the comments all you people who have done this to the nth degree before no i'm not using the arduino standard ota because i found it very cumbersome in the past i didn't like it and it put me off a bit really but when i found this elegant one then i thought hey this is good i like it uh right well if you if you like the video give it a good thumbs up yeah it's getting better it's getting better slowly yeah going back on thursday and have it redressed and um what else oh yes if you want to subscribe to the channel do so and then tick the notification bell so you get notified of new videos otherwise you'll never hear from me again subscription or not and i guess we're done see you next time i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching you
Info
Channel: Ralph S Bacon
Views: 10,957
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: OaHzlBTUrRQ
Channel Id: undefined
Length: 23min 26sec (1406 seconds)
Published: Fri Oct 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.