How to add Over-the-Air updates to your MicroPython projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I've built a lot of projects that have embedded microcontrollers in them that are really hard to access without taking them apart again do you wish you could update your micro python devices wirelessly today I'll show you how to add some simple code to your micropython projects to make this a reality using a technique called over-the-air or OTA updating it's super simple to do let's get straight to it so typically we load our python code with a USB cable and then it stays on there until we manually update it again and if we have some new code in GitHub there's no way to get down to the micropython device automatically with an OTA update we can simply check on GitHub if the latest version of the file is available download that locally to our micropython device save the contents of the file locally and then overwrite the original file we reset the device that means we've now got the new code loaded ready to go so the easiest way to do this is to head over to github.com Kevin McAleer OTA stands for over the air so once we're over here on OTA we click on this ota.pi file we can then simply click on this download button here and that will download the contents of that file to our downloads folder alternatively you can just click on the RAW button and you can cut and paste the contents of this file onto your IDE such as Visual Studio or phony [Music] so once we have that file downloaded and saved we've got it here as ota.pi we then need to right click in thony upload to our micro python device it's going to say overwrite that because I've already got it on there and then we need to create a little program to use it so I created this test underscore ota.pi program the first thing we need to do is import the OTA Library so from OTA import OTA updater that's the name of the class that I've created that'll do all the magic for us second thing we need to do is create a file that's going to store the Wi-Fi username and the Wi-Fi password so I'll call this one Wi-Fi config I've got it there and I've also uploaded this to the micropython device as well we typically have that as a secret file so we don't share that with other people obviously if it's our Wi-Fi username and password the next thing we need to do then is Define what the firmware URL is so this is the location on GitHub the name of the repository where the files will exist so I've already created a test repository I've called it OTA underscore test on GitHub let's go and have a look at that very quickly so I've got this test repository and it's only got three files in it so one of them is the readme file we can ignore that so the other one is the the actual name of the file that we want to maintain we want to upload so this one I've just simply called it main.pi doesn't actually do anything in this case it simply just has a comment in it well that's a main.pi and then the other one is called version.json Json stands for JavaScript object notation and it's just a formatter file that's used quite simply with things like micropython so in there we have a line that says version and then colon and then three so that says that the version the current version of the file is three so this is where we store it on GitHub and we can update this as we go we can change the version numbers we can play around with all that kind of thing right let's get back to funny so the next thing that we want to do is we want to pass all those details into the OTA updater so I'm going to create a little variable that's called OTL data I'm going to use the class that we've defined from the OT Library we're going to pass in the the SSID and the password for our Wi-Fi we're going to pass in that URL which is our Kevin McAleer OTA test you can put in wherever you mic there and then the actual name of the file that we want to maintain with the current version so main.pi is the version that we want to make sure is always the same on our micropython device and then finally we simply call the routine OTA underscore updater which is the name of that variable download and install update if available so let's run this and see what happens so when I click run it's connected to my Wi-Fi it knows that the current version is three if I look on my micropython device and open up this I've currently got version three as the latest version let's actually say that for example that this is version one it's going to save that back to the micropython device I'm going to run this now now think about this the latest version is actually version three on GitHub we currently have version one on our device there we can actually we could even delete that Json file if we wanted to and it will pretend that that's version zero well let's just use that as a version one for now so you can see there that the current device firmware is version one it's connected to Wi-Fi the latest version is version three a newer version is available it's fetching the latest firmware code the status is 200 that's the HTTP status code we can actually see the contents of the file there just been printed out and then it says updating device restarting device and then it says don't worry if there's an error message after this because it's just rebooting the micropython device and funny will disconnect momentarily and think that there's been a problem with the back end which there isn't and if we now check on the version.js and we can now see micro python device thinks it's on version 3 which is the current version and if we look at that main.pi we can see there that this is the file with the OTA code in it so we go back to our test repository for a second and let's put something else in that main.pile let's put a new line in here that just says print hello world something like that save those changes should also bump up the version number let's just edit that we don't actually have to have that date in there that was just me playing around with some ideas let's commit that back to the repository go back to our funny device and then let's just run our test program again okay so let's rerun this the current version is version three hopefully the version 4 will now be available on GitHub sometimes there's a bit of a caching issue and it doesn't detect that instantly but we typically only run this every every day every week or so on right so it's detected the new version let's check our main.pi we can see there we've now got that new print hello world and if we check the version of our version.json we can see that's now version four as well so that's how simple it is to add over the air updating to our projects and if you enjoy this video please give it a like drop a comment let me know if this is that you're going to use in your code and make sure you subscribe to the channel as well really means a lot to me when people do subscribe so please subscribe to the channel and I do go live every single Sunday at seven o'clock UK local time so hopefully I can catch you on one of the live shows and say hi in person I also want to give a shout out to some of the supporters of the show who've been kind enough to donate or join the YouTube membership program so we had a somebody bought me a coffee who wanted to remain nameless we have Steve Phillips a slighty about fast and Tom I've also bought coffees and they're members of the buy me a coffee we have Dan carte Marley Brent John rank we have Tom we have shemi and Steve Phillips as well on the YouTube membership side we have tinkering rocks we have hybrid robotics which is Dale we have a Cassie we have JDM Johnny Banks Bill High orchestrad39 Jose traffic goal we have a hansman chair lights we have Michael and of course Tom so hope you enjoyed this short video and I shall see you next time bye for now [Music] thank you
Info
Channel: Kevin McAleer
Views: 4,403
Rating: undefined out of 5
Keywords: Kevin McAleer, Small Robots, robotics, raspberry pi pico w, pico w, ota, over the air, micropython ota, ota micropython, pico w ota, ota pico, micropython, raspberry pi projects, raspberry pi, ota update
Id: f1widOJYQDc
Channel Id: undefined
Length: 6min 45sec (405 seconds)
Published: Sun Aug 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.