1
00:00:00,180 --> 00:00:06,870
Now, let's have a look at the first version of the original sketch to the original sketch version for

2
00:00:06,870 --> 00:00:08,940
the first iteration of the project.

3
00:00:09,720 --> 00:00:21,000
The sketch is based on the E.S.P and Take Client Library, which you can find here on GitHub at this

4
00:00:21,000 --> 00:00:21,690
location.

5
00:00:22,050 --> 00:00:25,380
So it's a fairly simple and quiddity library.

6
00:00:25,380 --> 00:00:28,700
But what I really like about it is that it's integrated.

7
00:00:29,130 --> 00:00:37,140
It allows you to handle EMCDDA team client requests, but it also handles especially to Wi-Fi, which

8
00:00:37,140 --> 00:00:40,440
means that we don't need to have a separate library for the wi fi bit.

9
00:00:40,890 --> 00:00:43,380
And there's very good documentation here.

10
00:00:43,380 --> 00:00:51,840
And examples, basically, you can get started with the library and subscribe and publish messages to

11
00:00:51,840 --> 00:00:55,740
the Breuker very, very quick with a very small amount of coding.

12
00:00:56,100 --> 00:01:00,360
Can have a look at the examples here as well, especially the what I used.

13
00:01:00,990 --> 00:01:06,540
I started with this example here and then implemented the various features based on this.

14
00:01:06,540 --> 00:01:13,380
As long as you know how to create a client and then subscribe to a topic or published topic, then you

15
00:01:13,380 --> 00:01:18,060
can pretty much do anything you want or need to do with the broker.

16
00:01:18,510 --> 00:01:21,390
And within a sketch, as you can see here.

17
00:01:22,310 --> 00:01:28,680
Start from line 17 and 18, you can interact with more than one topics.

18
00:01:29,400 --> 00:01:35,520
As the project becomes larger, you'll see that I'll be adding more topics here.

19
00:01:36,270 --> 00:01:37,840
So let's take things from beginning.

20
00:01:37,860 --> 00:01:47,040
First, I am creating the entity client here, and instead of having all the various credentials in

21
00:01:47,040 --> 00:01:54,260
the main application file, I've got them in a separate file called E.S.P through to Sigrid's, which

22
00:01:54,270 --> 00:01:55,120
looks like this.

23
00:01:55,410 --> 00:02:02,760
So this we can define your wi fi as a side password and username and password for the brokedown.

24
00:02:02,770 --> 00:02:09,000
Remember that these are the credentials that I created in the previous section when we were setting

25
00:02:09,000 --> 00:02:11,560
up the mosquito illiquidity broker.

26
00:02:11,970 --> 00:02:19,410
And you also need to get your broker's IP address for your Raspberry Pi so you can use this information

27
00:02:19,410 --> 00:02:19,800
here.

28
00:02:20,220 --> 00:02:23,370
And there is it's called Secrets The Page File.

29
00:02:24,300 --> 00:02:33,180
After that, I'm setting up the various pins, as we saw in the previous lecture in the schematic show.

30
00:02:33,180 --> 00:02:43,020
You really quickly here, we've got the thirty two for the transistor and then for the moisture sensor,

31
00:02:43,140 --> 00:02:48,530
in our case, for the potential metal that is connected to GPO 35.

32
00:02:49,260 --> 00:02:52,620
So I used this information right here.

33
00:02:53,190 --> 00:02:57,150
Then I have set up the the two topics.

34
00:02:57,570 --> 00:03:02,850
This is a topic that I am publishing, the soil humidity.

35
00:03:03,240 --> 00:03:13,620
And this topic here, the control topic is where my schedule is, where the data is subscribed to receive

36
00:03:14,580 --> 00:03:21,870
control, ratifications of values from the node red flow that I'll show you in the next lecture.

37
00:03:22,980 --> 00:03:31,070
Now, this line here, this value called soil humidity error range, I've set it here so that the SB

38
00:03:31,090 --> 00:03:42,090
32 will publish to this topic when the change in the detected soil humidity or the value from the consumer

39
00:03:42,090 --> 00:03:50,210
is large enough, because I don't want it to be posting constantly for single digit changes.

40
00:03:50,520 --> 00:03:53,430
So I wanted to have a range.

41
00:03:53,430 --> 00:04:01,650
And when the output from the sensor becomes larger or smaller by 10 units, then we'll send an update

42
00:04:01,650 --> 00:04:02,820
to Node read.

43
00:04:03,500 --> 00:04:11,250
I did this just to really conserve a bit of bandwidth and not to flood my note rate of flow with updates.

44
00:04:13,290 --> 00:04:19,410
All right, so these are a couple of additional variables here, the current soil humidity and then

45
00:04:19,410 --> 00:04:27,930
the last so committed so that the sketch remembers the last value that was sent to it by the note read

46
00:04:28,200 --> 00:04:36,900
Broken because they expected it to client cannot ask the entity broker for an update.

47
00:04:37,020 --> 00:04:40,500
The broker sends updates to the HP 32.

48
00:04:40,920 --> 00:04:46,290
So if the E.S.P 32 needs for some reason to remember the last value sent to it from the broker, they

49
00:04:46,290 --> 00:04:47,540
need to keep it locally.

50
00:04:47,880 --> 00:04:50,340
So that's what this variable is about.

51
00:04:51,810 --> 00:04:58,670
In the setup function, I'm setting up the analog to digital converter by attaching the serial humidity

52
00:04:58,680 --> 00:05:03,770
pin, setting the analog read resolution to 10 bits.

53
00:05:03,780 --> 00:05:12,850
So that gives us a range of zero to one thousand and twenty three for the readings from the soil humidity

54
00:05:12,850 --> 00:05:13,530
pin.

55
00:05:14,520 --> 00:05:15,990
And that is going to be an input.

56
00:05:15,990 --> 00:05:24,370
I'm setting the pump pen to be an output, these two lines to enable debugging messages and the Clippy

57
00:05:24,390 --> 00:05:31,470
Web up data, and that allows us to see debugging messages and information about what is happening with

58
00:05:31,470 --> 00:05:35,660
the requests in the serial monitor.

59
00:05:35,670 --> 00:05:39,430
And you can actually, once you do your testimony, you happy that everything works.

60
00:05:39,450 --> 00:05:46,880
You can just comment that out and not have to see any messages coming through this serial monitor.

61
00:05:47,130 --> 00:05:52,500
I just keep them in until we do a bit of testing later on in this section.

62
00:05:54,960 --> 00:06:04,350
The next function called On Connection Established, is a function that is called by the library when

63
00:06:04,380 --> 00:06:14,310
the entity broker sends an update to a topic that we ask, subscribe to the expected it to be subscribed

64
00:06:14,760 --> 00:06:24,470
so we can see here that we say client subscribed to control topic, which is this topic right here.

65
00:06:24,690 --> 00:06:34,980
And when the new data arrives to this topic from the note read flow, then the broker will forward it

66
00:06:34,980 --> 00:06:42,060
to any subscribed clients, which the ISP thirty two is going to be the subscriber client.

67
00:06:42,330 --> 00:06:49,230
And then when that happens, the library will call the pump control function, which if implemented

68
00:06:49,230 --> 00:06:57,830
further down and pass on the pump state that the note read flow has calculated for the controller.

69
00:06:58,290 --> 00:07:03,120
And let's jump into pump control since we have bumped into it.

70
00:07:04,180 --> 00:07:04,960
Here it is.

71
00:07:05,610 --> 00:07:10,200
So this pump control, it receives the pump state, which is a string.

72
00:07:10,830 --> 00:07:16,890
Pretty much everything in no direct messaging is really about strings, or at least that's how I have

73
00:07:16,890 --> 00:07:17,940
implemented the flow.

74
00:07:17,940 --> 00:07:19,590
It's going to send a string across.

75
00:07:20,340 --> 00:07:23,490
It's going to be either a zero or a one.

76
00:07:23,850 --> 00:07:30,300
And then depending on what it is, if it's a zero, it's going to turn off the pump by calling digital

77
00:07:30,300 --> 00:07:34,320
right on pump and taking that down to low.

78
00:07:34,710 --> 00:07:41,490
And if the pump state is one, then we're turning on the pump by calling digital.

79
00:07:41,490 --> 00:07:42,000
Right.

80
00:07:42,000 --> 00:07:44,100
And sending a high to pump in.

81
00:07:44,850 --> 00:07:53,190
Now, you'll notice here that I am using an array notation, and that's because I've got a string here

82
00:07:53,190 --> 00:07:58,170
sent from my note read a string is an array of characters.

83
00:07:58,650 --> 00:08:03,840
You want to know what the first cell in this array of characters contains.

84
00:08:03,840 --> 00:08:11,160
And that is why I am using the array notation and just passing index zero, because I'm only interested

85
00:08:11,430 --> 00:08:15,120
in the contents of the first cell of this array.

86
00:08:15,750 --> 00:08:19,950
There's other ways to implement this, but I thought I'd go with the string here just to keep things

87
00:08:19,950 --> 00:08:20,850
really simple.

88
00:08:21,840 --> 00:08:24,630
So let's have a look at what is happening inside the loop.

89
00:08:24,660 --> 00:08:29,700
The first thing that is happening inside the loop is to call the loop function on the client object,

90
00:08:29,700 --> 00:08:32,310
and that basically allows us to check for messages from the.

91
00:08:32,610 --> 00:08:34,740
And could it be broken?

92
00:08:35,280 --> 00:08:42,410
Apart from that, I take a reading from the soil to pen to see what is happening with your community

93
00:08:42,450 --> 00:08:44,820
in our prototyping phase that we are at the moment.

94
00:08:45,200 --> 00:08:53,730
I'll be taking readings from the potential metal and then printing out the various values stored in

95
00:08:53,760 --> 00:08:55,620
the variables.

96
00:08:56,430 --> 00:09:03,690
Now this is where publishing data to the end quiddity so your humidity topic takes place.

97
00:09:04,170 --> 00:09:10,830
If the difference between the current humidity and the previous soil humidity is larger than the range

98
00:09:10,830 --> 00:09:17,550
that I've specified earlier and larger than 10, then go ahead and send an update to the market it.

99
00:09:18,720 --> 00:09:27,840
So if this is true, then we use declined to publish function to send an update to the solar humidity

100
00:09:27,840 --> 00:09:29,400
topic, which is.

101
00:09:31,580 --> 00:09:32,900
This topic right here.

102
00:09:34,200 --> 00:09:39,010
And this is the value that we are publishing to this to this topic.

103
00:09:39,060 --> 00:09:46,950
You can see that I'm setting a string value of whatever I read from the soil humidity pin and then updating

104
00:09:46,980 --> 00:09:50,910
the lost soil humidity to be the current soil humidity.

105
00:09:51,870 --> 00:10:03,780
And actually to make this a little bit better, what I'll do is I will replace this with the value of

106
00:10:04,320 --> 00:10:08,700
the current soil humidity and fire, a little thing that makes it better.

107
00:10:10,290 --> 00:10:10,890
All right.

108
00:10:10,950 --> 00:10:18,200
So we've got a delay here and then the whole thing repeats again with calling the client loop.

109
00:10:19,230 --> 00:10:20,820
So that is about it.

110
00:10:20,940 --> 00:10:22,590
With the sketch.

111
00:10:22,590 --> 00:10:27,720
I'm going to compile it and upload it to my sensor.

112
00:10:29,510 --> 00:10:32,390
OK, and had a look at the serial monitor.

113
00:10:33,970 --> 00:10:40,300
All right, so it seems like it's working, there's nothing at the other end to interact with.

114
00:10:40,780 --> 00:10:45,340
So let's continue with the next lecture where we'll set up no direct.
