Let's have a look at the software side of this project in which I have assembled a script using components
from previous lectures in previous parts of this course in order to create a single script that reads
environmental data from the sensor, shows it on to the reality display, and then according to conditions,
it generates an email using if this event that all of those you have already seen is a set in previous
parts of this course and have simply put them together into a single script.
Of course, I had to do a few modifications to make everything work well, and I want to now have an
overlook of what the script looks like.
I'm not going to go into the details because quite a lot that is happening here and I'm going to leave
that part a detailed study of the script to you to do at your time.
Now, remember that this script is a possible version of what this script could look like.
And there's lots of room for improvement and better organization and better reliability and performance.
But this is one of my first iterations and it works.
I just wanted to show you what it looks like now and that from here onwards you can diverge to different
directions depending on where you want to go with your own projects.
So let's have a look at it.
Of course, we've got at the header where we do all of the inputs whenever possible.
I try to import micro python version of Limberis such as EU Time Here and you, Jason.
Then I set a number of variables and the objects will be used later on.
For example, here I've got the ice Quixey object that we're using ID zero, which works with Kypreos
18 19.
I've got the display object here and the BMY to the object for the center right there.
I've also set the PIN object for the ality so that I can turn it on and off when I want to show activity.
Now up here I've got a bunch of variables that are used to keep track of statuses.
So for example, I want to know if my gadget has been able to get Internet time and therefore display
the appropriate time on the day.
And if it doesn't have Internet time then to show and is not available on the screen.
So we keep track of that like this.
And another bunch of variables I have is these relating to the if this index notifications, I've got
a condition down here.
I can see that if the temperature is over twenty five degrees centigrade, then I want to send out an
email notification via if this and that by calling the post it to if this and that routine or function.
But I don't want to flood my inbox with notifications, so I only want to send those both when the temperature
is over 25 degrees centigrade.
But also I've got a counter here and I'm saying here, for example, is totally arbitrary and doesn't
have to do anything with time.
But I'm saying send a notification only once every six seconds.
If a second is the counter that I'm using here or sticking two of them using here in my time r interruptive
this written down here.
So you can see I've said the period to one thousand milliseconds.
So if I leave it at that and that is totally arbitrary, totally up to you.
How often you want to get a reading from the censors, then this calendar here will result to sending
at most one notification every six seconds.
So I probably should not include this appendix here as she just leave it as an counter, perhaps something
like that.
Anyway, so they look to you.
For me, it works just seconds because my timer interrupt period each one second.
After that, I've got my network, and if this and that credentials file, which is something that you
have seen before, this file right here, it's got my wi fi name, password and the efficient that key.
I set up the headers and the Jerrell for this and that, and the post messages sent it through, then
I create the wireless LAN object here in a set, the type of network that I want to build to an interface
object or regular interface object.
And I've got all the various individual functions that make up this project.
So there's a function that allows my attitude to connect to my local network.
I've got a disconnect function.
It simply turns the wireless network off.
I've got a static screen drawing function.
Just print out things on the screen that don't change like everything pretty much on the left side of
the screen function to clear the screen.
A function to draw things do change.
In this case, the temperature, humidity, pressure in the time is.
Also, the notifications text down here down the bottom.
This shows how many intervals are left before the next notification can be sent via if this in that
post is a function here called to get Internet time, that is called when the gadget begins executing
the script.
And it attempts to synchronize the RTC with Internet time notices that are commented out.
My custom empty server, and I've just left it up to the module itself, the empty time module to set
time based on the server that is called the default server that is called in its own code.
I found that this works more labor than if I said a customer service.
So I just left it like that to improve reliability.
This code is familiar to you from the relevant lecture in section of the nine.
We talked about wi fi.
Here's a function that makes a post request to efficient that to generate the email notification.
And finally today, interrupt the service routine here, it will turn the lady on and off when the processing
begins and ends, then you will call the disconnect function.
If we don't have a wireless connection, grab the values from the sensor and call the posta.
If this and that routine sent a post request to if that if this particular condition is true.
Now, one thing to notice here is that in order for me to be able to do the miracle comparison for the
temperature floating point number against twenty five, figure out whether it's larger than twenty five,
I need to convert the string that comes back from the image values.
This should be in values call.
It grabs a couple of the values from the sensor and then that's most important assignment into these
three variables.
Now temperature ends up holding a string that looks like this.
So notice that this is not a no.
It's a string in the end as the centigrade symbol.
So I want to get this part of this string.
This is a substring and then convert it into a floating point number with this cast, and then we'll
be able to do a numerical comparison.
So to do that, I use the square bracket notation to grab a substring out of the string.
So here I'm saying go to character index position zero to five to have code zero one, two, three,
four, five, not included.
So get this substring.
From the variable temperatures converted into a floating point number and straight into this variable,
and then it can make a comparison.
All right, after that, if this comparison is true, then call the post to if this and that or if Tea
Party is so verbose and then this will process a post request to efficient that.
Once it's done, clear the dynamic part of the screen and draw the dynamic part of the screen, pass
the necessary data.
The time will be done locally and show the new context of the display and turn off the ability to indicate
that processing is finished.
And that's what's happening inside timer.
I saw.
Now, this is where the program begins, its execution, when we call it when we run, it will initialize
a display for it to make sure that its awake will rotated so that we can see the text in the proper
orientation will draw the static part of the screen, which is this content on the left side.
Then we'll show that part of the screen.
The static that we have already drawn will attempt to connect to the local Wi-Fi network called to get
Internet time retained in order to sink the RTC with the Internet time server and then disconnect.
So the thing that I decided to do here is to connect only when there is activity that relates to the
Internet.
In our case, when we want to sink the clock to the Internet time and when we want to make a post request
of two, if this is that instead of having a permanently on connection, find that it's both more reliable.
But it's of course better in terms of power consumption, especially if you power this gadget on battery.
This is going to significantly improve the longevity of the battery.
And in this case, it down here right to the bottom, while I'm doing my debugging, instead of having
the timer here running every one second, which would then have to stop fire control, see, or by clicking
on the red button and just run the whole thing once I just called.
Time is a quick one here.
And then that allows me to just save a bit of time, but not having to hit control, see or the red
button.
So I call the timer I saw.
I just pass an arbitrary number one here because it is expecting a parameter.
This whole thing will execute once and then we'll will finish.
We will to do a bit of debugging and then run the script again.
And when I'm finished with the debugging and I feel confident that the program works, I can switch
that off these two lines and switch on the lines to four seven two four eight where I set the timer
and then a trigger sort of starts running and calling the ISO once every second.
I'm going to execute it in a minute.
I'm just going to go back and see if there's anything else that is important at this point to mention.
So in do connect function.
One thing that I did this time around is to add a little sleep period here.
I've noticed that when I'm about to connect to the network, very often I had failures.
I wasn't sure exactly where those failures were, what was causing those failures.
They error messages coming back, did not provide any reliable hints and noticed that if I added a little
bit of delay after turning on the wireless interface and then after doing the connection, I noticed
that this improved the reliability of connected to the Wi-Fi network.
So that's why I've got these two sleep functions here.
Let's see, what else do we have?
This is all pretty straightforward.
You've seen this before.
Same thing with getting out any time I noticed that I was getting a little Seijas synchronizing with
this particular server.
So I just committed this line off and allowed the set time function of the A.P. time module to just
use the default host for the Internet server.
And that seemed like it worked better than me providing my own custom Internet time service.
So I left it that finally here in the post to this that function using the global keywords to make it
possible for this function to modify the values stored in these two variables.
You can see or make some modifications here.
For example, the if that counter incremented by one.
And that's why I'm using the global keyword here.
Another thing to notice is that I assume that when this function is called, the Wi-Fi is not connected
or the ISP is not connected to my local Wi-Fi network.
So I'll call the do connect function first, do whatever needs to be done here.
And then as the last instruction in this function is to disconnect from the local Wi-Fi so that we know
that when it comes back here, it will need to be reconnected.
And that's it.
All right, let's say it's file, it's saved already on the computer, too, and.
OK, there we go.
So it worked.
We've got a temperature updating every one second with the correct time and date as well from the Internet
clock.
And you can see that the next update to efficient that or the next post request, if this, in fact,
will be done in 42 seconds, assuming that the temperature at that time is over 25 degrees centigrade.
And just looking at.
My inbox of 22 new messages to get the latest one.
OK.
Zero minutes ago, so that this one just came in and got the latest environmental data at this time.
All right, I'm going to give it another 10 seconds to see what happens when this count expires.
GRISANTI zero.
We should get a new e-mail up here any moment, Bill.
She worked.
One thing that I need to improve on is the day, the day she turned off, I can see that it is momentarily
turning off.
Not sure if it's coming, if that comes through the camera, but it does blink off momentarily.
So one thing that I need to figure out is how I can make it actually link and stay off in between seconds.
But I think that the operation that happens every one second takes almost the whole second and therefore
we don't actually see the tentacles.
But I can definitely improve that.
OK, so that's it with this version of the script, this Single-file version.
Let's move over to the next lecture where I show you how to split this script into two small screens.