1
00:00:06,060 --> 00:00:10,500
Now we will create a publisher for the battery percentage.

2
00:00:10,530 --> 00:00:14,910
Look at the circuit and we are getting the input at the PIN 36.

3
00:00:15,450 --> 00:00:19,990
Here it is, the voltage divider that is created by two resistors.

4
00:00:20,010 --> 00:00:26,100
They are giving us the output which is received in the Esp32 at PIN 36.

5
00:00:26,130 --> 00:00:28,320
For that, we are going to create a publisher.

6
00:00:28,320 --> 00:00:34,170
So we will be opening a opening, an example of publisher.

7
00:00:37,180 --> 00:00:39,040
This is the Microsoft publisher.

8
00:00:43,430 --> 00:00:45,650
And this topic will also be dependent on.

9
00:00:45,650 --> 00:00:47,490
Instead we can just work with it.

10
00:00:47,510 --> 00:00:52,580
We do not need to use more memory, so this is why we are not going to use Int32.

11
00:00:56,830 --> 00:01:00,980
A large part of you will be using everything same just like that.

12
00:01:00,990 --> 00:01:06,870
We are going to create a timer and we are also going to create a timer call just like this.

13
00:01:06,900 --> 00:01:08,160
Let's do this.

14
00:01:08,190 --> 00:01:11,310
The better win is 36 so we can define it like this.

15
00:01:11,340 --> 00:01:16,200
We are going to create the object of the publisher that will be publishing the battery.

16
00:01:16,200 --> 00:01:21,660
And here it is, the object of battery message and the setup.

17
00:01:21,660 --> 00:01:23,640
We are going to create a timer.

18
00:01:26,630 --> 00:01:31,160
And the timer will be repeating itself after 100 milliseconds.

19
00:01:31,160 --> 00:01:34,790
And we also have to add an executor of the timer.

20
00:01:34,790 --> 00:01:41,150
And because this is the third thread, we are going to change this number to three.

21
00:01:41,180 --> 00:01:43,640
Here it is the function of timer callback.

22
00:01:43,640 --> 00:01:46,070
Save it, compile the sketch.

23
00:01:46,070 --> 00:01:52,490
So so far this is absolutely fine, but this will not give us the value of the battery, so we will

24
00:01:52,490 --> 00:01:54,200
just have to modify a little bit more.

25
00:01:54,200 --> 00:02:00,140
And the void setup we will declare the battery pin as an input pin.

26
00:02:04,050 --> 00:02:10,980
Unlike this, now we are going to create a function that will be iterating the pin and give us the value

27
00:02:10,980 --> 00:02:12,930
of the percentage of the battery.

28
00:02:12,960 --> 00:02:14,670
This is the function.

29
00:02:14,880 --> 00:02:17,360
It should be get battery percentage.

30
00:02:17,370 --> 00:02:18,870
Here it is the prototype of this.

31
00:02:18,870 --> 00:02:21,840
And down here we can define that function.

32
00:02:22,020 --> 00:02:26,610
Here is the function that will be reading the value from the battery pin.

33
00:02:26,910 --> 00:02:29,040
Here is the map to percentage function.

34
00:02:29,040 --> 00:02:31,110
We will also create a prototype for this.

35
00:02:31,140 --> 00:02:35,120
And here, come down here we can define this function.

36
00:02:35,130 --> 00:02:36,030
Okay, here it is.

37
00:02:36,030 --> 00:02:41,880
Map to the percentage function that will receive the raw value and set a minimum voltage value and then

38
00:02:41,880 --> 00:02:48,270
the maximum voltage value and linearize it from 0 to 100, which will give us the percentage.

39
00:02:48,270 --> 00:02:50,610
And how do we find this value?

40
00:02:50,940 --> 00:02:57,270
Well, there is a simple mathematical formula as we have potential divider of this value.

41
00:02:57,270 --> 00:03:02,770
Battery is rated at 3.7V, but actually it gives us four point.

42
00:03:02,890 --> 00:03:08,080
Voltage at which the esp32 gives us the maximum value for zero nine.

43
00:03:08,080 --> 00:03:11,350
Five is the maximum it will give us via analog read.

44
00:03:11,350 --> 00:03:20,230
So we will divide it by 3.30.8 almost and we will multiply it by 4095.

45
00:03:20,260 --> 00:03:22,990
The Esp32 will give us this value.

46
00:03:22,990 --> 00:03:25,450
Our battery will be 100% charged.

47
00:03:25,480 --> 00:03:34,230
Okay, so we have written it here as a rounded of figure and there is also added some safety check.

48
00:03:34,240 --> 00:03:39,940
This is why this value a little bit higher when the battery is low and it will drop its voltage when

49
00:03:39,940 --> 00:03:48,460
it reaches three, it will give us 1.9 divided by 313 multiply 4095.

50
00:03:49,870 --> 00:03:54,430
So this is the value that it will give out at 0% charging.

51
00:03:54,430 --> 00:03:54,970
Okay.

52
00:04:22,340 --> 00:04:25,580
Murphy have written a rounded off figure in here.

53
00:04:25,610 --> 00:04:28,910
Now we have to add the publisher in it.

54
00:04:28,940 --> 00:04:31,430
We haven't declared it yet.

55
00:04:31,670 --> 00:04:34,640
And here we will declare the publisher.

56
00:04:34,670 --> 00:04:36,690
So the publisher is created.

57
00:04:36,710 --> 00:04:41,870
Now we will just publish percentage value and this timer callback function.

58
00:04:43,460 --> 00:04:48,320
Now we will plug this in, upload code, upload it successfully.

59
00:04:48,350 --> 00:04:49,490
Now plug out.

60
00:04:49,520 --> 00:04:51,380
Open your Microsoft agent.

61
00:04:53,930 --> 00:04:54,800
Turn on.

62
00:05:00,980 --> 00:05:02,480
This is the better topic.

63
00:05:08,570 --> 00:05:09,470
That is good.

64
00:05:09,920 --> 00:05:12,350
We are seeing the battery percentage.
