[2024 DigiKey Creative Competition] ESP32-S3-LCD-EV-BOARD lights up LCD, connects to the Internet, and uses SNTP to synchronize time[Copy link]
Hello everyone, I am Zheng Gong, a little engineer who is lost in this world.
I wanted to challenge myself in this competition, and also wanted to write a post on how to get started with esp idf development, so I decided to use esp idf to develop applications in this competition (it was really a big job for me T_T)
After many days of debugging, I finally got a good grasp of the WiFi networking and SNTP functions. Now I would like to share some of my experiences with you, hoping that it will be helpful to you.
1. Turn on the LCD screen
Since we are using the official development board from Espressif, it is actually very simple to light up the screen. We can just use the official routine. The GitHub address is as follows:
The factory program uses 86box_smart_panel. I have read the code and found that there is a lot of logic and interface written in it, which is not convenient for us to learn from scratch, so I decided to use the lvgl_demo example.
They directly helped us to transplant lvgl, which saved us a lot of development work. There are a lot of documents and videos about lvgl on esp32s3 on the Internet. Generally speaking, it is not difficult. You just need to download the official lvgl library and then connect the LCD interface and touch interface. I won't introduce it in detail here.
Then we can start to develop the interface. Here I learned some code to write the interface. It feels the same as developing the interface with tkinter. Each control needs to be created, resized, styled and laid out. There is no simulation, and the download speed of esp32s3 is not fast. After several adjustments, an hour has passed. So in the end I chose to use squareline studio to develop the interface.
In this way, you only need to drag and adjust the properties, and the images will be automatically transcoded, which can really save you a lot of effort.
2. Networking
How can you use Espressif's chip without using the networking function? The following is a simple networking test code
Connect to a Wi-Fi network : Use esp_wifi_connect()the function to connect to a Wi-Fi network.
ESP_ERROR_CHECK(esp_wifi_connect());
Waiting for the connection to complete : Usually you need to wait for the Wi-Fi connection to complete, which can be achieved by polling or registering an event callback function.
while (!esp_netif_is_connected()) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
Get IP address : After the connection is successful, get the IP address assigned to ESP32-S3.
Using the network : At this point, ESP32-S3 has been connected to the Wi-Fi network and can communicate over the network.
Then the function needs to add the following business code to determine the network status and automatically reconnect.
3. SNTP Time Synchronization
I have made two programs for this topic, one is to get the time based on the network time service API used in the previous follow me task, and the other is to use SNTP (simple network time portocol) provided by ESP-IDF. I will post the following code to explain the following.
The test code can add tasks via xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);.
The code mainly accesses http://api.pinduoduo.com/api/server/_stm Pinduoduo Time API through network request to obtain the timestamp. The format of the returned data is as follows:
{"server_time":1729698004538}
The timestamp is then parsed to obtain the value corresponding to "server_time", and then the timestamp is converted to standard time through the gmtin function. It should be noted that
1. The unit of the timestamp range is ms, and the unit of gmtime processing time is seconds, so the timestamp needs to be divided by 1000 before passing it into the function.
2. The timestamp obtained is the time on the prime meridian. Beijing time needs to be processed by adding 8.
This method is difficult to achieve accurate time display to the second. Perhaps you can request it once, then create a time maintenance internally, and then calibrate the time regularly. Otherwise, as in my example, requesting once every second will waste a lot of network resources, and the request return also takes time, and the second jump often occurs, the implementation effect is not ideal
SNTP Time Server
The second method is to use the SNTP time server provided by ESP-IDF. It is easy to use, does not occupy much system resources, and does not require system time maintenance. The code is as follows:
static void initialize_sntp(void);
static void obtain_time(void);
static void time_sync_notification_cb(struct timeval *tv)
{
ESP_LOGI(TAG, "Notification of a time synchronization event, sec=%lu", tv->tv_sec);
settimeofday(tv, NULL);
}
void app_sntp_init(void)
{
setenv("TZ", "CST-8", 1);
tzset();
obtain_time();
}
static void obtain_time(void)
{
initialize_sntp();
int retry = 0;
const int retry_count = 10;
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
if (retry == retry_count) {
ESP_LOGI(TAG, "Could not obtain time after %d attempts", retry);
}else {
ESP_LOGI(TAG, "Time synchronized");
}
}
static void initialize_sntp(void)
{
ESP_LOGI(TAG, "Initializing SNTP");
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
//设置3个时间服务器
esp_sntp_setservername(0, "ntp.aliyun.com");
esp_sntp_setservername(1, "time.asia.apple.com");
esp_sntp_setservername(2, "pool.ntp.org");
esp_sntp_set_time_sync_notification_cb(time_sync_notification_cb);
esp_sntp_init();
}
As you can see, the code is very simple. Basically, once executed, you can get the local time through the time function. The method of getting the time is also very simple. You only need to call two functions.
The time update interval can be set by opening the system settings using idf.py menuconfig, and setting Request interval to update time (ms) under Component config -> LWIP -> SNTP. I set it to calibrate once every 12 hours, which is usually sufficient.
It should be noted that it is best not to call the obtain_time function in any callback function or interrupt processing, otherwise it may be stuck. Combined with the above networking content, you can recalibrate the time after disconnecting and reconnecting.
Weather functions will be added later. At that time, you can use the network API method to register your personal business of Xinzhi Weather.
The unit of the timestamp range is ms, and the unit of gmtime processing time is seconds, so you need to divide the timestamp by 1000 before passing it into the function. This is a trick
Details
Published on 2024-10-25 07:24
The unit of the timestamp range is ms, and the unit of gmtime processing time is seconds, so you need to divide the timestamp by 1000 before passing it into the function. This is a trick
Yes, I didn't look carefully at first, and all I got were the maximum values, all fixed times, and I thought there was a problem and I didn't get the time.
Details
Published on 2024-10-30 11:44
Jacktang posted on 2024-10-25 07:24 The unit of the timestamp range is ms, and the unit of gmtime processing time is seconds, so you need to divide the timestamp by 1000 before passing it into the function. This is a trick
Yes, I didn't look carefully at first, and all I got were the maximum values, all fixed times, and I thought there was a problem and I didn't get the time.