1
00:00:00,720 --> 00:00:08,310
Let's have a look at the software side of this project in which I have assembled a script using components

2
00:00:08,310 --> 00:00:14,430
from previous lectures in previous parts of this course in order to create a single script that reads

3
00:00:14,430 --> 00:00:20,670
environmental data from the sensor, shows it on to the reality display, and then according to conditions,

4
00:00:21,060 --> 00:00:28,710
it generates an email using if this event that all of those you have already seen is a set in previous

5
00:00:28,710 --> 00:00:33,840
parts of this course and have simply put them together into a single script.

6
00:00:34,110 --> 00:00:40,890
Of course, I had to do a few modifications to make everything work well, and I want to now have an

7
00:00:40,890 --> 00:00:43,760
overlook of what the script looks like.

8
00:00:44,040 --> 00:00:50,550
I'm not going to go into the details because quite a lot that is happening here and I'm going to leave

9
00:00:50,550 --> 00:00:54,960
that part a detailed study of the script to you to do at your time.

10
00:00:55,320 --> 00:01:00,070
Now, remember that this script is a possible version of what this script could look like.

11
00:01:00,070 --> 00:01:06,390
And there's lots of room for improvement and better organization and better reliability and performance.

12
00:01:06,870 --> 00:01:10,790
But this is one of my first iterations and it works.

13
00:01:10,870 --> 00:01:18,450
I just wanted to show you what it looks like now and that from here onwards you can diverge to different

14
00:01:18,450 --> 00:01:22,490
directions depending on where you want to go with your own projects.

15
00:01:23,370 --> 00:01:25,810
So let's have a look at it.

16
00:01:25,950 --> 00:01:30,540
Of course, we've got at the header where we do all of the inputs whenever possible.

17
00:01:30,540 --> 00:01:37,870
I try to import micro python version of Limberis such as EU Time Here and you, Jason.

18
00:01:38,340 --> 00:01:43,260
Then I set a number of variables and the objects will be used later on.

19
00:01:43,260 --> 00:01:51,230
For example, here I've got the ice Quixey object that we're using ID zero, which works with Kypreos

20
00:01:51,240 --> 00:01:52,770
18 19.

21
00:01:53,040 --> 00:01:58,700
I've got the display object here and the BMY to the object for the center right there.

22
00:01:59,160 --> 00:02:04,680
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.

23
00:02:05,160 --> 00:02:10,590
Now up here I've got a bunch of variables that are used to keep track of statuses.

24
00:02:10,890 --> 00:02:18,810
So for example, I want to know if my gadget has been able to get Internet time and therefore display

25
00:02:18,820 --> 00:02:20,770
the appropriate time on the day.

26
00:02:20,820 --> 00:02:28,230
And if it doesn't have Internet time then to show and is not available on the screen.

27
00:02:28,380 --> 00:02:30,300
So we keep track of that like this.

28
00:02:30,630 --> 00:02:38,010
And another bunch of variables I have is these relating to the if this index notifications, I've got

29
00:02:38,010 --> 00:02:40,010
a condition down here.

30
00:02:40,020 --> 00:02:47,760
I can see that if the temperature is over twenty five degrees centigrade, then I want to send out an

31
00:02:48,360 --> 00:02:55,800
email notification via if this and that by calling the post it to if this and that routine or function.

32
00:02:56,160 --> 00:03:04,860
But I don't want to flood my inbox with notifications, so I only want to send those both when the temperature

33
00:03:04,980 --> 00:03:07,140
is over 25 degrees centigrade.

34
00:03:07,150 --> 00:03:12,600
But also I've got a counter here and I'm saying here, for example, is totally arbitrary and doesn't

35
00:03:12,600 --> 00:03:13,650
have to do anything with time.

36
00:03:14,040 --> 00:03:18,560
But I'm saying send a notification only once every six seconds.

37
00:03:18,570 --> 00:03:26,970
If a second is the counter that I'm using here or sticking two of them using here in my time r interruptive

38
00:03:26,990 --> 00:03:28,240
this written down here.

39
00:03:28,260 --> 00:03:32,670
So you can see I've said the period to one thousand milliseconds.

40
00:03:32,970 --> 00:03:36,780
So if I leave it at that and that is totally arbitrary, totally up to you.

41
00:03:36,780 --> 00:03:45,960
How often you want to get a reading from the censors, then this calendar here will result to sending

42
00:03:45,960 --> 00:03:50,190
at most one notification every six seconds.

43
00:03:50,190 --> 00:03:58,380
So I probably should not include this appendix here as she just leave it as an counter, perhaps something

44
00:03:58,380 --> 00:03:58,770
like that.

45
00:03:59,670 --> 00:04:00,880
Anyway, so they look to you.

46
00:04:00,930 --> 00:04:06,330
For me, it works just seconds because my timer interrupt period each one second.

47
00:04:07,390 --> 00:04:15,880
After that, I've got my network, and if this and that credentials file, which is something that you

48
00:04:15,880 --> 00:04:22,810
have seen before, this file right here, it's got my wi fi name, password and the efficient that key.

49
00:04:25,090 --> 00:04:31,480
I set up the headers and the Jerrell for this and that, and the post messages sent it through, then

50
00:04:31,480 --> 00:04:39,160
I create the wireless LAN object here in a set, the type of network that I want to build to an interface

51
00:04:39,160 --> 00:04:41,310
object or regular interface object.

52
00:04:41,890 --> 00:04:46,280
And I've got all the various individual functions that make up this project.

53
00:04:46,290 --> 00:04:53,160
So there's a function that allows my attitude to connect to my local network.

54
00:04:53,650 --> 00:04:56,140
I've got a disconnect function.

55
00:04:56,890 --> 00:05:00,130
It simply turns the wireless network off.

56
00:05:01,090 --> 00:05:04,680
I've got a static screen drawing function.

57
00:05:04,690 --> 00:05:11,080
Just print out things on the screen that don't change like everything pretty much on the left side of

58
00:05:11,080 --> 00:05:14,500
the screen function to clear the screen.

59
00:05:15,220 --> 00:05:17,530
A function to draw things do change.

60
00:05:17,860 --> 00:05:21,760
In this case, the temperature, humidity, pressure in the time is.

61
00:05:24,210 --> 00:05:28,540
Also, the notifications text down here down the bottom.

62
00:05:28,560 --> 00:05:36,090
This shows how many intervals are left before the next notification can be sent via if this in that

63
00:05:36,090 --> 00:05:43,380
post is a function here called to get Internet time, that is called when the gadget begins executing

64
00:05:43,380 --> 00:05:43,980
the script.

65
00:05:43,980 --> 00:05:51,450
And it attempts to synchronize the RTC with Internet time notices that are commented out.

66
00:05:51,570 --> 00:06:00,650
My custom empty server, and I've just left it up to the module itself, the empty time module to set

67
00:06:00,660 --> 00:06:05,280
time based on the server that is called the default server that is called in its own code.

68
00:06:05,970 --> 00:06:09,860
I found that this works more labor than if I said a customer service.

69
00:06:09,880 --> 00:06:12,510
So I just left it like that to improve reliability.

70
00:06:13,230 --> 00:06:18,840
This code is familiar to you from the relevant lecture in section of the nine.

71
00:06:18,840 --> 00:06:20,070
We talked about wi fi.

72
00:06:21,190 --> 00:06:27,310
Here's a function that makes a post request to efficient that to generate the email notification.

73
00:06:28,510 --> 00:06:37,420
And finally today, interrupt the service routine here, it will turn the lady on and off when the processing

74
00:06:37,420 --> 00:06:42,310
begins and ends, then you will call the disconnect function.

75
00:06:42,340 --> 00:06:51,010
If we don't have a wireless connection, grab the values from the sensor and call the posta.

76
00:06:51,010 --> 00:06:58,400
If this and that routine sent a post request to if that if this particular condition is true.

77
00:06:58,780 --> 00:07:06,970
Now, one thing to notice here is that in order for me to be able to do the miracle comparison for the

78
00:07:06,970 --> 00:07:13,210
temperature floating point number against twenty five, figure out whether it's larger than twenty five,

79
00:07:13,390 --> 00:07:18,400
I need to convert the string that comes back from the image values.

80
00:07:18,850 --> 00:07:21,460
This should be in values call.

81
00:07:21,760 --> 00:07:28,510
It grabs a couple of the values from the sensor and then that's most important assignment into these

82
00:07:28,510 --> 00:07:29,260
three variables.

83
00:07:29,380 --> 00:07:34,030
Now temperature ends up holding a string that looks like this.

84
00:07:34,660 --> 00:07:36,790
So notice that this is not a no.

85
00:07:36,790 --> 00:07:41,530
It's a string in the end as the centigrade symbol.

86
00:07:41,830 --> 00:07:45,620
So I want to get this part of this string.

87
00:07:45,650 --> 00:07:52,240
This is a substring and then convert it into a floating point number with this cast, and then we'll

88
00:07:52,330 --> 00:07:54,510
be able to do a numerical comparison.

89
00:07:54,820 --> 00:08:03,760
So to do that, I use the square bracket notation to grab a substring out of the string.

90
00:08:04,120 --> 00:08:13,720
So here I'm saying go to character index position zero to five to have code zero one, two, three,

91
00:08:13,720 --> 00:08:16,250
four, five, not included.

92
00:08:16,750 --> 00:08:19,120
So get this substring.

93
00:08:20,440 --> 00:08:26,770
From the variable temperatures converted into a floating point number and straight into this variable,

94
00:08:26,770 --> 00:08:28,260
and then it can make a comparison.

95
00:08:29,160 --> 00:08:38,050
All right, after that, if this comparison is true, then call the post to if this and that or if Tea

96
00:08:38,520 --> 00:08:44,010
Party is so verbose and then this will process a post request to efficient that.

97
00:08:45,650 --> 00:08:52,340
Once it's done, clear the dynamic part of the screen and draw the dynamic part of the screen, pass

98
00:08:52,340 --> 00:08:53,370
the necessary data.

99
00:08:53,540 --> 00:09:01,490
The time will be done locally and show the new context of the display and turn off the ability to indicate

100
00:09:01,490 --> 00:09:02,690
that processing is finished.

101
00:09:03,110 --> 00:09:05,120
And that's what's happening inside timer.

102
00:09:05,340 --> 00:09:06,050
I saw.

103
00:09:08,340 --> 00:09:14,610
Now, this is where the program begins, its execution, when we call it when we run, it will initialize

104
00:09:14,610 --> 00:09:21,770
a display for it to make sure that its awake will rotated so that we can see the text in the proper

105
00:09:21,780 --> 00:09:28,410
orientation will draw the static part of the screen, which is this content on the left side.

106
00:09:29,490 --> 00:09:31,830
Then we'll show that part of the screen.

107
00:09:31,830 --> 00:09:38,820
The static that we have already drawn will attempt to connect to the local Wi-Fi network called to get

108
00:09:38,820 --> 00:09:46,830
Internet time retained in order to sink the RTC with the Internet time server and then disconnect.

109
00:09:47,310 --> 00:09:54,930
So the thing that I decided to do here is to connect only when there is activity that relates to the

110
00:09:54,930 --> 00:09:55,430
Internet.

111
00:09:55,440 --> 00:10:01,620
In our case, when we want to sink the clock to the Internet time and when we want to make a post request

112
00:10:01,620 --> 00:10:08,370
of two, if this is that instead of having a permanently on connection, find that it's both more reliable.

113
00:10:08,370 --> 00:10:14,910
But it's of course better in terms of power consumption, especially if you power this gadget on battery.

114
00:10:14,940 --> 00:10:19,920
This is going to significantly improve the longevity of the battery.

115
00:10:21,090 --> 00:10:28,980
And in this case, it down here right to the bottom, while I'm doing my debugging, instead of having

116
00:10:28,980 --> 00:10:37,260
the timer here running every one second, which would then have to stop fire control, see, or by clicking

117
00:10:37,260 --> 00:10:41,550
on the red button and just run the whole thing once I just called.

118
00:10:41,560 --> 00:10:44,340
Time is a quick one here.

119
00:10:44,880 --> 00:10:52,980
And then that allows me to just save a bit of time, but not having to hit control, see or the red

120
00:10:52,980 --> 00:10:53,480
button.

121
00:10:53,910 --> 00:10:56,940
So I call the timer I saw.

122
00:10:56,970 --> 00:11:02,940
I just pass an arbitrary number one here because it is expecting a parameter.

123
00:11:03,390 --> 00:11:06,600
This whole thing will execute once and then we'll will finish.

124
00:11:06,720 --> 00:11:09,440
We will to do a bit of debugging and then run the script again.

125
00:11:09,810 --> 00:11:17,040
And when I'm finished with the debugging and I feel confident that the program works, I can switch

126
00:11:17,040 --> 00:11:25,560
that off these two lines and switch on the lines to four seven two four eight where I set the timer

127
00:11:25,800 --> 00:11:32,730
and then a trigger sort of starts running and calling the ISO once every second.

128
00:11:34,710 --> 00:11:36,570
I'm going to execute it in a minute.

129
00:11:36,570 --> 00:11:42,480
I'm just going to go back and see if there's anything else that is important at this point to mention.

130
00:11:43,710 --> 00:11:46,590
So in do connect function.

131
00:11:46,860 --> 00:11:51,890
One thing that I did this time around is to add a little sleep period here.

132
00:11:52,320 --> 00:11:59,460
I've noticed that when I'm about to connect to the network, very often I had failures.

133
00:11:59,940 --> 00:12:05,730
I wasn't sure exactly where those failures were, what was causing those failures.

134
00:12:06,240 --> 00:12:13,650
They error messages coming back, did not provide any reliable hints and noticed that if I added a little

135
00:12:13,650 --> 00:12:21,360
bit of delay after turning on the wireless interface and then after doing the connection, I noticed

136
00:12:21,360 --> 00:12:27,060
that this improved the reliability of connected to the Wi-Fi network.

137
00:12:27,060 --> 00:12:31,500
So that's why I've got these two sleep functions here.

138
00:12:34,560 --> 00:12:36,720
Let's see, what else do we have?

139
00:12:38,700 --> 00:12:40,470
This is all pretty straightforward.

140
00:12:40,500 --> 00:12:41,520
You've seen this before.

141
00:12:42,540 --> 00:12:47,370
Same thing with getting out any time I noticed that I was getting a little Seijas synchronizing with

142
00:12:47,370 --> 00:12:48,570
this particular server.

143
00:12:48,840 --> 00:12:58,230
So I just committed this line off and allowed the set time function of the A.P. time module to just

144
00:12:58,440 --> 00:13:01,610
use the default host for the Internet server.

145
00:13:01,620 --> 00:13:08,980
And that seemed like it worked better than me providing my own custom Internet time service.

146
00:13:08,980 --> 00:13:18,900
So I left it that finally here in the post to this that function using the global keywords to make it

147
00:13:18,900 --> 00:13:24,810
possible for this function to modify the values stored in these two variables.

148
00:13:24,820 --> 00:13:26,780
You can see or make some modifications here.

149
00:13:26,790 --> 00:13:30,210
For example, the if that counter incremented by one.

150
00:13:30,780 --> 00:13:34,860
And that's why I'm using the global keyword here.

151
00:13:35,640 --> 00:13:43,200
Another thing to notice is that I assume that when this function is called, the Wi-Fi is not connected

152
00:13:43,230 --> 00:13:46,350
or the ISP is not connected to my local Wi-Fi network.

153
00:13:46,680 --> 00:13:51,210
So I'll call the do connect function first, do whatever needs to be done here.

154
00:13:51,390 --> 00:13:57,600
And then as the last instruction in this function is to disconnect from the local Wi-Fi so that we know

155
00:13:57,600 --> 00:14:01,320
that when it comes back here, it will need to be reconnected.

156
00:14:02,180 --> 00:14:03,460
And that's it.

157
00:14:05,430 --> 00:14:12,780
All right, let's say it's file, it's saved already on the computer, too, and.

158
00:14:25,360 --> 00:14:26,270
OK, there we go.

159
00:14:26,840 --> 00:14:27,760
So it worked.

160
00:14:28,570 --> 00:14:35,620
We've got a temperature updating every one second with the correct time and date as well from the Internet

161
00:14:36,280 --> 00:14:36,850
clock.

162
00:14:37,480 --> 00:14:44,680
And you can see that the next update to efficient that or the next post request, if this, in fact,

163
00:14:44,680 --> 00:14:52,690
will be done in 42 seconds, assuming that the temperature at that time is over 25 degrees centigrade.

164
00:14:53,200 --> 00:14:54,550
And just looking at.

165
00:14:55,520 --> 00:15:00,920
My inbox of 22 new messages to get the latest one.

166
00:15:03,990 --> 00:15:04,630
OK.

167
00:15:04,650 --> 00:15:11,040
Zero minutes ago, so that this one just came in and got the latest environmental data at this time.

168
00:15:12,200 --> 00:15:20,510
All right, I'm going to give it another 10 seconds to see what happens when this count expires.

169
00:15:20,660 --> 00:15:21,560
GRISANTI zero.

170
00:15:36,180 --> 00:15:42,030
We should get a new e-mail up here any moment, Bill.

171
00:15:48,300 --> 00:15:48,820
She worked.

172
00:15:51,510 --> 00:15:58,260
One thing that I need to improve on is the day, the day she turned off, I can see that it is momentarily

173
00:15:58,260 --> 00:15:59,100
turning off.

174
00:16:01,000 --> 00:16:05,830
Not sure if it's coming, if that comes through the camera, but it does blink off momentarily.

175
00:16:06,080 --> 00:16:13,190
So one thing that I need to figure out is how I can make it actually link and stay off in between seconds.

176
00:16:13,640 --> 00:16:20,630
But I think that the operation that happens every one second takes almost the whole second and therefore

177
00:16:20,630 --> 00:16:23,330
we don't actually see the tentacles.

178
00:16:23,960 --> 00:16:25,850
But I can definitely improve that.

179
00:16:26,910 --> 00:16:33,460
OK, so that's it with this version of the script, this Single-file version.

180
00:16:33,750 --> 00:16:42,390
Let's move over to the next lecture where I show you how to split this script into two small screens.
