esp32获取网络时间

文章目录


esp32获取天气数据的流程类似的,我们从另一个网站获取时间。

#define TIME_URL "https://api.uukit.com/time"	//api接口

我们对其接口发起请求,返回结果如下:

{
    "status": 1,
    "data": {
        "timestamp": 1616556071,
        "microtime": 1616556071.259844,
        "gmt": "2021-03-24 03:21:11",
        "utc": "2021-03-24T03:21:11Z",
        "timezone": "Shanghai"
    },
    "req_id": "3b853233236df4a63608"
}

以下代码实现对该接口的请求,并使用cjson解析出时间信息。

            config.url = TIME_URL;  //设置url,请求方式
            config.method = HTTP_METHOD_GET;
            printf("start connect to url = %s\r\n",config.url);
            client = esp_http_client_init(&config);
            esp_http_client_perform(client);    //发起http连接
            esp_http_client_close(client);
            esp_http_client_cleanup(client);

            cJSON *root = cJSON_Parse(http_data);   //解析返回的时间json数据
            if(root!=NULL)
            {
                cJSON *time = cJSON_GetObjectItem(root,"data");
                time = cJSON_GetObjectItem(time, "gmt");
                char *t = cJSON_GetStringValue(time);
                if(t==NULL)
                {
                    ESP_LOGI(TAG, "time error");
                }else{

                ESP_LOGI(TAG, "time = %s",t);
                
                strncpy(str_time, t, 25);   //将字符串复制到str_time

                cJSON_Delete(root);
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我会为你提供一个基于STM32F051K8的ESP8266联网获取时间并在1.44寸LCD屏幕上显示实时时间的详细代码。以下是实现该功能的步骤和代码: 1. 配置STM32F051K8的时钟和GPIO引脚,使其可以与ESP8266和LCD屏幕通信。 ```c /* Configure USART1 clock and GPIO */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure LCD GPIO */ GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStruct); /* Configure ESP8266 GPIO */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); ``` 2. 配置USART1串口,用于与ESP8266通信。 ```c USART_InitTypeDef USART_InitStruct; USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStruct); USART_Cmd(USART1, ENABLE); ``` 3. 配置ESP8266网络连接并获取时间。在这个例子中,我们使用ESP8266模块连接到NTP服务器获取时间。这里我们使用AT指令来控制ESP8266模块。 ```c /* Configure ESP8266 */ usart_send_str("AT\r\n"); usart_wait_for_response("OK\r\n", 1000); usart_send_str("AT+CWMODE=1\r\n"); usart_wait_for_response("OK\r\n", 1000); usart_send_str("AT+CWJAP=\"SSID\",\"PASSWORD\"\r\n"); usart_wait_for_response("OK\r\n", 10000); usart_send_str("AT+CIPSNTPCFG=1,1,\"pool.ntp.org\"\r\n"); usart_wait_for_response("OK\r\n", 1000); usart_send_str("AT+CIPSNTPTIME?\r\n"); char* response = usart_wait_for_response("+CIPSNTPTIME:", 10000); char* p = strstr(response, "+CIPSNTPTIME:"); if (p != NULL) { p += strlen("+CIPSNTPTIME:"); time_t timestamp = atol(p); struct tm timeinfo; gmtime_r(&timestamp, &timeinfo); } ``` 4. 配置LCD屏幕并显示实时时间。 ```c /* Configure LCD screen */ lcd_init(); /* Display time */ while (1) { char buffer[32]; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeinfo); lcd_clear(); lcd_write_string(buffer); delay(1000); } ``` 完整代码如下: ```c #include "stm32f0xx.h" #include "lcd.h" #include <time.h> #include <stdlib.h> #include <string.h> void usart_send_char(char c) { while (!(USART1->ISR & USART_ISR_TXE)); USART1->TDR = c; } void usart_send_str(const char* str) { while (*str) { usart_send_char(*str++); } } char usart_receive_char(void) { while (!(USART1->ISR & USART_ISR_RXNE)); return USART1->RDR; } int usart_receive_str(char* buffer, int bufsize, int timeout) { int received = 0; int start = HAL_GetTick(); while (received < bufsize - 1) { if (HAL_GetTick() - start > timeout) { break; } char c = usart_receive_char(); if (c == '\r' || c == '\n') { if (received == 0) { continue; } else { break; } } buffer[received++] = c; } buffer[received] = '\0'; return received; } char* usart_wait_for_response(const char* expected, int timeout) { static char buffer[256]; int start = HAL_GetTick(); while (1) { if (HAL_GetTick() - start > timeout) { return NULL; } int received = usart_receive_str(buffer, sizeof(buffer), timeout); if (received > 0) { if (strstr(buffer, expected) != NULL) { return buffer; } } } } int main(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStruct); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); USART_InitTypeDef USART_InitStruct; USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStruct); USART_Cmd(USART1, ENABLE); usart_send_str("AT\r\n"); usart_wait_for_response("OK\r\n", 1000); usart_send_str("AT+CWMODE=1\r\n"); usart_wait_for_response("OK\r\n", 1000); usart_send_str("AT+CWJAP=\"SSID\",\"PASSWORD\"\r\n"); usart_wait_for_response("OK\r\n", 10000); usart_send_str("AT+CIPSNTPCFG=1,1,\"pool.ntp.org\"\r\n"); usart_wait_for_response("OK\r\n", 1000); usart_send_str("AT+CIPSNTPTIME?\r\n"); char* response = usart_wait_for_response("+CIPSNTPTIME:", 10000); struct tm timeinfo; char* p = strstr(response, "+CIPSNTPTIME:"); if (p != NULL) { p += strlen("+CIPSNTPTIME:"); time_t timestamp = atol(p); gmtime_r(&timestamp, &timeinfo); } lcd_init(); while (1) { char buffer[32]; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeinfo); lcd_clear(); lcd_write_string(buffer); delay(1000); } } ``` 这是一个简单的示例,可以帮助你了解如何使用STM32F051K8、ESP8266和LCD屏幕实现从NTP服务器获取时间并显示实时时间。当然,在实际项目中,你需要考虑更多的细节和错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值