Moisture Sensor Automation and Accuracy Quick-Fix (Raspberry Pi autogardener project ep#3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i will show you two simple but useful things regarding my little raspberry pi auto gardener project first how to automate the moisture sensor so it reports moisture level automatically every 15 minutes and then secondly how to quick fix a little accuracy issue with the moisture sensor hi i'm toby meister from data36.com and this is the next episode of a series where i document a hobby project of mine called growing strawberries with a raspberry pie on autopilot if you are interested in how we got here check out the previous episodes of the series but for now let's get into it okay let's start with the automation part because that will be fairly simple first i will log back into my raspberry pi with the ssh command i add my password cool just a quick note this ssh plus command is just an extra comment installed on my computer you can use the simple ssh command instead of this one on your computer for me this helps automatically connect my raspberry pi to my sublime text editor which is already opened on the left side so this is sublime text and this is the command line and so i'm logged back in to my raspberry pi and i will open in sublime on the left side the script that i have written in the previous video which was called um let me check it was moisture test dot pi okay sublime moisture test dot pi this is the script pretty nice and let's run the script and just for fun i put my moisture sensor into a glass of water so it will report some moisture level let's run the script python3 moistures test dot pi okay it says 520 532 500 okay so as i said we have two problems here the first one is the spread in the moisture level we will get back to that soon the second one is that i don't want this script to be run manually by myself so what i want is an automation that will start the script and right now it reports the moisture level every second and instead of that i want to report the moisture level in you know in like every 15 minutes um but for that first i need to change this script so it won't run in an infinite loop instead of that i want to run this moisture underscore red and get underscore temp functions only once when i run the script and i will do that first then i will show you how i make this fully automated and run in every 50 minutes so first because i don't want to mess up my original moisture underscore test script i will make a copy of it copy moisture test pi which is not bi but py whatever into moisture automation dot p y and i will go ahead and open this moisture automation file in sublime this is pretty much the same thing but i will edit this a little bit okay as i said i want to run this part only once so the first thing that i will do is that i remove the vial loop i will remove the indentations of course and i can actually remove this time dot sleep function as well i won't need this and let's save it save i saved it and let's run this new moisture automation script on the right side in the terminal python 3 moisture underscore automation dot pi and it reported the temperature and the moisture level only once as expected it also says this is said by us in the previous video that watering is not needed for now i will remove this part as well i will put it back when it's needed or should i just comment it out now i will completely remove it okay save it run it temperature moisture level cool now as i said i can keep this script as is and run it in every 15 minutes from the command line and for that i will need a tool called chromejob which i can open with the crontab dash e comment since for me and probably for you this is the first time that i run this comment on my pie i have to pick a text editor which for me will be mcedit because i prefer that for this one i don't need sublime so i will choose two actually if you mess this up accidentally i will show this to you you can and you accidentally picked nano or veeam or anything else you can lick recalibrate this with the select editor command select editor it will ask you again and you can choose two so you can use mcedit now let's get back to chrome tab chrome tab dash e and here you can set up automations i've created a separate video about how to use crontab so i won't go into that here and now i will just show you what i will do in this specific case first i have to set when i want to run my script and the automation for that would look something like this this means that on every day in every month on every day of the week in every hour or script will run in every 15 minutes and then i will have to specify the command or in this case the script that i want to run and it's going to be the python 3 that it's the command itself and the script will be moisture automation dot pi the script that we have just created oh and one more thing in chrome tab you will always have to add a new line after your automation so this is going to be our automation it will run or new moisture automation script in every 15 minutes but in itself it works nothing right because it's not enough to run this script i will actually have to store these values when i run this script and how would i do that in this specific case i would do something like this so this means that the output of this command and this script won't be printed on the screen but into a file and let's call the file moisture data dot txt let's create a txt file for now we can tweak this data file further in the future make it nicer but for now it's going to be moisture data dot txt get moisture data dot txt there it is if i run the script again it will store the data again so ups one issue if you put one arrow that will always overwrite this data file so i will actually have to put here two arrows which will add the output as new line to this data file so test that out and check the moisture data.txt file now now it has two lines and so actually to have this automation correctly run and store this data into this moisture underscore data.txt file i will have to update my automation in chrometab as well so instead of this the full command will be something like this and so if you took one of my courses is ever any data science courses i created you know that i hate to add scripts or file names with a relative path because that's where the scripts and automations fail and so i have to actually add the full path name here which you can get with the pwd command and for me these moisture underscore automation.pi file and the moisture underscore data dot txt file are located in the home slash pi directory on my pi so i will go back to chrome tab again and i will add the path here now this chrome tab automation looks properly set so let's give it a try let's check out the date time right now so in a little bit more than five minutes in theory a new line should be added to my moisture text sorry it's moisture data dot txt file right now it has two lines and let me pause the recording right now and i will get back to you in five minutes and we will check out whether the new line is there or not okay i'm back and let's check out the date right now we are 15 minutes after 5 pm in theory a new line should show up in the moisture underscore data.txt file and there it is there is the third line it says the moisture level is 519. now i actually want to add one more thing to this line and i go back to my moisture underscore automation dot pi script and since we are building here the moisture data log of the moisture levels and temperatures we will need the most important part of a log into this file as well which is of course the date time data and for that i will need to add the datetime function and first the module to my moisture underscore automation file it's from that time import the time and i will need here a new variable what time is it and let's call it date war not the best naming convention but i can fix this later so date war is did time dot now dot ease of format and the separator will be a space before i save and run this let me show you quickly in python what i have done what this will be and i will show that to you in the command line i cleared my screen and i will open python3 so i imported the time from that time import date time and i saved this output into a variable so this is that time now the current time and date in a not too nice format and with the ease of format function i can make this nicer and the only thing that i don't like in this formatting is this little t between the time and the date and i can replace this by adding an argument here which is a space and from this point on in our script this data will be saved into the date underscore war variable which will be important because every time i run this little script or actually when chrome tab will automatically run this script in every 15 minutes the date and the time will be saved into our file or actually it won't because i have to add this into the output of this file okay you know what i will do one more thing i will just remove this temp and moisture text because i already know that we're talking about temperature and moisture level i don't need that in my data file i save this just in case i will do a test run in the command line i closed python on the command line and let me run my updated moisture automation script good it's the date the time the temperature and the moisture level and so in a few minutes this will be saved to this moisture data.txt file or you know what i can just simply run this script again and add this new line to the moisture data.txt file let's print the file okay here it is and since in the first three lines we haven't done this proper formatting and adding the data and the time i will just remove the first three lines from this file or you know what i will delete the whole file and i will start it from scratch here it is i run the script again and so my new moisture data.txt file will look something like this not the nicest format right now but this is what we need for the time being and just in case check out weather or crontab automation adds a new line to this file so i will pause the recording again and we'll start it again in like three minutes okay i'm back and let's check out whether we have the new line in our file perfect we have it and we can also see that it was recorded 30 minutes after 5 pm just as it should have been so in theory from this point on if i leave my raspberry pi turned on it will run this moisture underscore automation dot pi every 15 minutes and it will add a new line in every 15 minutes with the new data with the new measured moisture level temperature and date time to this file cool and we can move on to the next issue which is the accuracy of or moisture level and for that i will clear my screen close this moisture dot automation script and i will open the moisture test dot p y file in my sublime text again and if i run this one which you know measures the moisture level in every second you will see that the moisture level changes every second and it's for me at least it's quite a big spread so i will stop this so sometime we see 512 or we see 556 or we see 547 and so on and as i said currently when i run this script my moisture sensor is in a glass of water and you know the high school felicity class was a very long time ago but as far as i remember the moisture level of water is or should be quite constant and stable so this spread in the moisture levels for me shows that this moisture sensor that i use is not 100 accurate but it's not an issue because we can actually average these values and i will show you my approach with this and i will test something simple for now and i will move back to editing my moisture automation script the one that we have just created for the automation and my trick will be well it won't be that tricky let me remove these comments so the trick will be that instead of measuring the moisture level once when this automation runs i want to measure these five times so i will run a for loop and i will measure the moisture level five times and i will leave five seconds between these and so i have to tweak this a little bit actually this touch value be the last step of this for loop and this moisture read data will go into a list okay so i will create a moisture list again a bit lame naming convention but it will work for now so i will put this moisture read data into this moisture list list with the append function and with this in the moisture list we will have five data points that are sampled in five seconds after each other about our moisture level and you guessed it we will calculate the averages of these five data points which can be done easily by summing the elements in our moisture list and then dividing it with the length of the moisture list which will be 5 by the way and this will be our new touch value now i will run this and i hope that it won't throw an error python 3 moisture automation dot pi so far so good this will take 25 seconds of course because of the time.sleep part so it will take a bit time and there you go there is our average and i hope that this will lower the spread between the values and i can test it if i run this script a few times so you don't have to wait 25 seconds i will speed the video up okay so far so good one more cool okay i accept this as a good enough result the spread is a bit lower of course i will be able to evaluate this after one or two days when i check out this file where we will store all these values and you know in one or two days i will see here like 100 plus lines and if the spread is smaller than it was a bow then i will be happy and just in case let me tell you that i have actually run this little experiment before i recorded this video and so i can promise that this small averaging trick will do the job and that also means that we are done with the two tasks that i set for myself today just to recap the first one was to automate the measurement of the moisture level and we have created this moisture automation script from the script from the previous video we put it into crown tab and so it runs in every 15 minutes and the second one was fixing this moisture level accuracy issue and for that we just took five samples instead of one sample to our moisture measurement and we averaged it and it fixed the accuracy issue of course we can improve this further but for now this will be good enough before i wrap this up just a quick comment that every learning material that i mentioned in this video will be linked in the description as well as the github link of this script so if you are not 100 sure what happened here you can go to my github check out this script test it for yourself run parts of it separately next to my video whatever point is links are in the description and this is the end of this episode if you liked it please subscribe leave a like or a comment and if you have done projects like this and you spot any mistakes i made or you have best practices you want to share please do not hesitate to share it in the comments as of the next episode i finally got both the plant lights as you can see and the water pump as well so i will pick one of those topics and we'll continue with that anyways don't forget to subscribe to my newsletter at data36.com newsletter because this is the only place where i really communicate to my audience right now so this is where you can get notified about new content as well and if you do subscribe you will get access to a lot of free data science learning materials too like a python cheat sheet an sql cheat sheet or a free mini course called how to become a data scientist thank you for watching i'm tomine from data36.com see you in the next video [Music] [Applause] [Music] [Applause] [Music] [Applause] you
Info
Channel: Data36 - Online Data Science Courses
Views: 2,712
Rating: undefined out of 5
Keywords: online data analysis, data science, data science for beginners, startup, data analysis, analytics, online research, yt:cc=on, data36, tomi mester, data science tutorial
Id: ZcKCElb9gMk
Channel Id: undefined
Length: 24min 48sec (1488 seconds)
Published: Thu Mar 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.