RTC design with automatic cloud calibration for simple programmable controller based on ESP8266
zhuyue
Posted on October 20, 2024
In the automation control system of industry and agriculture, real-time clock (RTC) is a very important function.
For example, the agriculture of the designated time period of automatic watering, between 8:00-9:00 every day to automatically open the solenoid valve watering;
This function requires the controller to have an RTC clock, and the calendar to determine the execution of the output to control the on/off of the solenoid valve;
An important feature of the RTC clock is that the controller continues to be powered by the battery after a power failure, which requires the power supply of the RTC module to be separate from the power supply of the other parts of the processor, and the operating current should be very low in order to ensure that the clock after a power failure is long enough to maintain the time .
STM32F103 processor, for example, the processor has a separate VBAT pin for the RTC module function, the RTC module's operating current is about 2uA or so, if the use of 5mAh rechargeable coin cell battery power supply, the clock retention time can reach 208 days.
In addition, the RTC clock also needs a low-frequency 32.768k crystal to provide a clock to achieve the purpose of low-power consumption, passive crystal can generally reach about 30ppm accuracy, the time error of a year is about 15 minutes.
Active crystal oscillator can generally achieve an accuracy of about 5ppm; time error is about 2 minutes a year;
ESP8266 does not have such a RTC module, if the interface through the I2C external RTC chip, will increase the cost of the product, can not reach the selling price of about 30 RMB.
But the advantage of ESP8266 is easy to connect to the Internet, you can automatically calibrate the time through the Internet.
Therefore, I designed a RTC clock module through the software, the normal practice is to write the code of the perpetual calendar, counting the seconds, dealing with the minutes, hours, days of the rounding as well as the leap year, the month and a series of issues;
The ESP8266 provides two functions, mktime and localtime, which can realize the mutual conversion of calendar and timestamp.
The use of these two functions only need to count the seconds, and then converted to the calendar can be, and the day of the week can be calculated by the year, month and day of the year through the Caille formula;
In the TCP server code of the cloud server, when a heartbeat message is received from the controller, the server parses out the current time value of the controller in the message and compares it with the server's time, and when the difference exceeds a certain value, then the server send the command to set the time and calibrate the controller's time.
In the controller's code, two variables V225 and V226 are used to record the state of the calibration time, where V225 is cleared to zero after the controller is powered up, and is set to 1 when a calibration time command from the server is received and the operation to modify the time is performed;
V226 is cleared to zero after power-on, and is accumulated to the maximum value in 1s tick, and only when the controller is calibrated by the server, it will be re-cleared to zero and continue to accumulate.
Users can use script to program to judge the variable V225 is 1 before executing some actions to ensure that the controller clock is accurate for automation control;
You can also judge V226 over a certain value, the implementation of the output, the control of sound and light alarm equipment, so that in the controller can not get the correct time, the instant alarm reminder.
Another idea is that some controllers can not be networked to automatically correct the time, you can save the timestamp in FLASH, the controller restarted after a power failure to read the saved timestamp from FLASH to continue to time, so that the controller will not reset the time to the default value when the reboot occurs, a power failure caused by the time error for the time interval between the saved timestamps plus the time of the power failure.
The problem is that there is a limit to times that FLASH can be erased, and the module's built-in FLASH can be erased only about 1000 times.
If the timestamps are saved at 1 hour intervals, then 1000 erasures will last the controller about 41 days.
This is clearly unacceptable.
Posted on October 20, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
October 20, 2024