Now we will create a publisher for the battery percentage.
Look at the circuit and we are getting the input at the PIN 36.
Here it is, the voltage divider that is created by two resistors.
They are giving us the output which is received in the Esp32 at PIN 36.
For that, we are going to create a publisher.
So we will be opening a opening, an example of publisher.
This is the Microsoft publisher.
And this topic will also be dependent on.
Instead we can just work with it.
We do not need to use more memory, so this is why we are not going to use Int32.
A large part of you will be using everything same just like that.
We are going to create a timer and we are also going to create a timer call just like this.
Let's do this.
The better win is 36 so we can define it like this.
We are going to create the object of the publisher that will be publishing the battery.
And here it is, the object of battery message and the setup.
We are going to create a timer.
And the timer will be repeating itself after 100 milliseconds.
And we also have to add an executor of the timer.
And because this is the third thread, we are going to change this number to three.
Here it is the function of timer callback.
Save it, compile the sketch.
So so far this is absolutely fine, but this will not give us the value of the battery, so we will
just have to modify a little bit more.
And the void setup we will declare the battery pin as an input pin.
Unlike this, now we are going to create a function that will be iterating the pin and give us the value
of the percentage of the battery.
This is the function.
It should be get battery percentage.
Here it is the prototype of this.
And down here we can define that function.
Here is the function that will be reading the value from the battery pin.
Here is the map to percentage function.
We will also create a prototype for this.
And here, come down here we can define this function.
Okay, here it is.
Map to the percentage function that will receive the raw value and set a minimum voltage value and then
the maximum voltage value and linearize it from 0 to 100, which will give us the percentage.
And how do we find this value?
Well, there is a simple mathematical formula as we have potential divider of this value.
Battery is rated at 3.7V, but actually it gives us four point.
Voltage at which the esp32 gives us the maximum value for zero nine.
Five is the maximum it will give us via analog read.
So we will divide it by 3.30.8 almost and we will multiply it by 4095.
The Esp32 will give us this value.
Our battery will be 100% charged.
Okay, so we have written it here as a rounded of figure and there is also added some safety check.
This is why this value a little bit higher when the battery is low and it will drop its voltage when
it reaches three, it will give us 1.9 divided by 313 multiply 4095.
So this is the value that it will give out at 0% charging.
Okay.
Murphy have written a rounded off figure in here.
Now we have to add the publisher in it.
We haven't declared it yet.
And here we will declare the publisher.
So the publisher is created.
Now we will just publish percentage value and this timer callback function.
Now we will plug this in, upload code, upload it successfully.
Now plug out.
Open your Microsoft agent.
Turn on.
This is the better topic.
That is good.
We are seeing the battery percentage.