How to configure your MicroPython project with JSON files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back my name is babesh and I teach micro python on my blog and on YouTube I really enjoy it and clearly you guys do too because we have over 500 subscribers now which is amazing that's like more than double what I had last video so clearly this is really important and helpful and I'm really glad to be doing that for the community today we're going to learn how to save user settings in microplankton and what I mean by that is basically say you have like a colorful RGB lamp like the one we've built before right you've set it to an amazing color or like a color pattern that you really like but then one day the power goes out or you unplug it or you want to flash a new firmware onto it what happens is it's gonna forget that beautiful color it's going to forget your settings that really sucks because there's no persistent memory for it to remember but there is a very simple solution and we do it using configuration files or basically in simpler words saving data onto the flash memory of the microcontroller and this is very similar to desktop and web development if you've done that before but it's really cool how easy it is to do this in micro python because you get a file system and you get uh Json libraries and you get lots of stuff that makes things a lot easier to work with so just to give you a quick demo of what we're building today I've got a little microcontrol over here and the setting that it's going to remember is whether the user has switched the light on or off so I'm going to try to switch it on so there you go it's on so unplug it bye-bye plug it back in move lights on again so remember it all right we do the same in reverse right off and plug it plug it back in blue lights off cool I can control the has memory wow let's use it okay how are we gonna do that so let's start as we always do in our Dawning editor and I'm just going to connect to it collect killing the connection there hit Ctrl C okay cool we got a rebel uh this is all the answers so I'm not gonna let us cheat so no answers just delete all that bye-bye yeah all right cool and let's start from Square so to keep things simple call it main.pi actually you have to call it main.i if you want to run right away and uh the first thing we're going to do before any memory any Shenanigans or Json files let's just make it switch on the LED when we press the button that's all we got to do okay so we're gonna do like our nice night code while true um actually you know before the while true Let's uh let's initialize the LED in the body right so I believe LED is on pin I'm just going to read over here at least he's on pin two on this one and we're going to set it to an output so make sure to import the necessary library for that so um and also I'm just going to do from time and import steep just because then you're going to need to put a sleep in the main Loop later so that's the led the button is on pin zero and it's an input so you can just leave it as a default and then the main Loop the button should toggle to delete it and what we're going to do is check the button value foreign we're going to do LED dot value not LED dot value psych right so that just means toggle the LED if it's one make it a zero to zero make it a one and uh that's it pretty much go to sleep for like half a second or so so we're not doing anything complicated with the button like no debouncing anything like that just every half second check if the buttons press if it's pressed I'll go the LED that's it so I'm going to save that go down here hit Ctrl D and programs running so let's try that out I hope it works so press the button and it is on press button I really got so just like what we had before um if I unplug it plug it back in it forgot totally forgot that the LED was on so this is where the magic of saving stuff to flash memory comes in and for that what we're going to do is we're going to pull in the Json Library which you can import as ujson or just Json I'm more useless I'm just going to do that and what we want to do is we need to load the configuration file from flash memory but right now there is no configuration file so let's go create that I'm going to connect again since I unplugged it hit Ctrl C to break it out okay so yeah over here you see no config but let's create it file and let's give it an amazing name I'm going to give it the best possible name it's going to be called config.json wow cool okay and the only configuration we need to save here is just is the LED on or not all right so is the LED on I'm going to call it is Led on make my life much easier and we're going to store it as a one or a zero so I'm going to say zero is that would be on by default it is not on okay so LED is off by default we want to be able to pull that in using the Json Library so if you were to Jason before and like regular Python and C python this is pretty much the same you use the load function to load it in so let's load the config file please open so this is the file name config because using that load so this config here is actually going to be a dictionary so whenever you load a Json file into python you get as a dictionary so it pretty much looks exactly the same as this which is great because that makes it so much easier to understand once you've noted the config file the first thing you want to do is set the LED to what the config file says so if the config file says zero switch the LED off right away it says one switch it on and the way you do that so set the LED based on the config and same as below we just do LED the value and in this case it's going to be from the config dictionary and remember we called it is Led on so if this is a zero the LED gets set to off and if it's a one it gets set to one that's it that's the entire thing that's amazing it's like one line of code um the last thing is we want a way to save so we have a way to load the config when it switches on which is good we actually want a way to save this operation here so every time we toggle the LED we want to save we config file so that it knows next time it switches off and uh there are a couple of steps in that so I'm just gonna put it in a separate function you could just put it over here it doesn't matter too much but I like putting in a separate function because that lets you use it in multiple places if you have multiple different settings that you want to set so it's going to carry a function call Safe config and it's just going to use this Global intake dictionary so what we're going to do is we're going to open the config file again but this second we're going to open a right mode so make sure this equal to W here which means that you're making the file writable as that and then the opposite of json.load is Jason dot dump it's like garbage or sand I don't know it's it's a Json file that's that's how it's treated I guess okay so we're gonna give it the dictionary that we want to dump as well as the file handle that we want to dump the dicks into and that's it that that function will save your dictionary to the file so you can load it next time once you flash or reboot your microcontrollment so what we're going to do here is we want to first before we set it to the file let's set it in our dictionary so config is LED on equals LED dot value so LED value is now a getter to the LED value which returns to zero or one and then we can just call save big and that's that nothing else really changes in the code basically every time you have a setting that you want to save um suppose you have like five different settings in this code you would just call Save config after each of those and yes it's not the most optimal solution that you're saving the entire config every time but honestly for such small numbers of configurations it doesn't really matter and this works pretty well so let's try it I'm going to save this file come down here restart control d and so we have the same thing as before I'm gonna miss the button oh what's going on ah okay which is being finicky I think it's a half second delay it might be too long so anyway blue LEDs on unplug it plug it back in do whatever it is on cool they remember it yes cut it off plug it bring it back in blue LEDs up the microcontroller has memory and it only took just like barely 10 more lines of code to add that kind of functionality in I hope this is super useful to you I hope this takes your micro python projects to the next level where you connect now save user configurations and files and data and all kinds of stuff just using Json or honestly any format that you want to the flash memory I do teach lots of micro python content on my blog and on YouTube for totally free so if you like this subscribe here subscribe to my blog subscribe on both whatever format of learning that you enjoy I just want to make it easy for you and I really appreciate all you guys watching thanks so much bye
Info
Channel: Bhavesh Kakwani
Views: 709
Rating: undefined out of 5
Keywords:
Id: xcVGfjlLReE
Channel Id: undefined
Length: 11min 22sec (682 seconds)
Published: Mon Sep 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.