Now, I have mentioned briefly in the previous lecture that when we are continuously publishing data,
that's not really good for multiple reasons.
So we're going to use interval based publishing Iot devices in general, have a huge concern about energy
consumption because most of them run on batteries.
When you have a device that is running on battery, you want to make it work the least work possible
in order to save battery.
So it's not really enough to write the code that works, but you need to write code that is efficient
in energy consumption as well.
So usually a microcontroller gives executing instructions non-stop and thus consumes a lot of power.
And also when you are continuously transmitting, you are consuming bandwidth because Iot devices may
be working in technologies like 3G or 4G.
You want to also minimize the bandwidth and the unnecessary data that is being sent.
That is one concern.
Another concern.
If you are recording all of this data to a database, you don't really want to fill it with garbage
value that have no use.
I mean, what you keep reading every 10 milliseconds and publishing every 10 milliseconds, that's a
lot of useless information because maybe your potential meter, let's say, or temperature or whatever,
did not even change within that small interval.
So you need to know what's your use case, what you are doing with this data, and then you need to
decide how often do you want to read the data and publish it to your cloud, to your broker or to whatever
Iot service you are using.
So in this case, we might be sending data every one seconds, five seconds, 10 seconds, even minutes.
And it totally depends on the application we are using now for me here in this application, since I'm
really just borrowing it from USPI and we are on this application is really just for testing.
I'm going to be publishing every, let's say, three seconds.
Now, in order to do that, we're going to use something called the M.D., M.D., is it just a tumor
function?
It does not have anything that is so special, but it's much better than using a delay.
Now, Emily's and DeLay are very similar.
They are both timers, but there is two different type of timers, DeLay uses a timer that will block
your whole code from executing, and that is not really good, because if you are, let's say using
multithreaded, let's say you are using some functions or libraries that need multithreaded.
And it's doing this in the background.
You're going to really disturb its work with delay because delay, as I said, is going to block the
whole MCU and it will prevent it from multitasking.
This is why DeLay is a very, very bad function and in a lot of applications.
So we cannot really say read value sended and then delay for, let's say, five seconds or three seconds,
then send again, because this will also affect the BOP's library and it will do some really bad side
effects.
So delay is a no, no and no to go in this situation.
This is why we use Milli's is a more advanced type of weight because Milli's is actually a timer.
Now, every microprocessor, every microcontroller has an internal timer that can be launched and it
could be running independently of the program.
Now, who is the steps to use Milli's?
You just start the timer and the timer will start counting.
One, two, three, four, five, six milliseconds and will keep counting up.
That's a great since dimmers independent hardware on the microcontroller.
They will not interfere with your program so you can call Milley's, let the timer start counting and
just give back the control to the program to continue as usual.
So it does not take cycles more than just defining evaluable.
It's really not that hard work consuming.
So we give back the control to the program after we start time over the discounting.
Now, all we need to do is just write a very simple instruction that will compare what is the reading
of the timer with what we are expecting.
Let's say I want to count for 50 milliseconds.
This will count.
OK, now the loop is running, the avoid loop is running.
Now what I do is I just say if the counter right now equals 50 milliseconds, then I want to do something.
All I'm doing is I'm going to the counter just to that time or just read it.
If it's not equal, I will just leave it and continue my program.
So right now, I am not blocking anything.
And this is what we are going to do in the next lecture, we're going to apply all of this in order
to publish every certain internal.