ESP32入门基础之SNTP时间显示
文章目录
- 1 实验简介
- 2 实验分析
1 实验简介
- 在esp-idf合集下就有该该工程,目录如下Espressif\frameworks\esp-idf-v4.4.2\examples\protocols\sntp。
- 在此工程直接编译烧录即可。
2 实验分析
程序分析如下,注意以下程序经过增删改
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_sleep.h"
#include "nvs_flash.h"
#include "esp_sntp.h"
// struct tm
// {// int tm_sec; //秒钟
// int tm_min; //分钟
// int tm_hour; //小时
// int tm_mday; //日期:日,从1开始
// int tm_mon; //日期:月,从0开始
// int tm_year; //年,距离1900年的差值,默认是70
// int tm_wday; //星期,1对应星期一
// int tm_yday; //一年的过去的天数
// int tm_isdst; //是否为夏时制
// #ifdef __TM_GMTOFF
// long __TM_GMTOFF;
// #endif
// #ifdef __TM_ZONE
// const char *__TM_ZONE;
// #endif
// };
static const char *TAG = "user_sntp.c";static void obtain_time(void);
static void initialize_sntp(void);void time_sync_notification_cb(struct timeval *tv)
{ESP_LOGI(TAG, "Notification of a time synchronization event");
}void user_sntp_init(void)
{char strftime_buf[64];time_t now;struct tm timeinfo;time(&now); //获取网络时间, 64bit的秒计数localtime_r(&now, &timeinfo); //转换成具体的时间参数// Is time set? If not, tm_year will be (1970 - 1900).if (timeinfo.tm_year < (2025 - 1900)) {ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP.");obtain_time();// update 'now' variable with current timetime(&now);}// Set timezone to China Standard Timesetenv("TZ", "CST-8", 1);tzset();localtime_r(&now, &timeinfo);strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);ESP_LOGI(TAG, "The current date/time in Shanghai is: %s", strftime_buf);while(1){time(&now);localtime_r(&now, &timeinfo);ESP_LOGI(TAG, "timeinfo.tm_isdst: %d", timeinfo.tm_isdst); ESP_LOGI(TAG, "timeinfo.tm_yday: %d", timeinfo.tm_yday); ESP_LOGI(TAG, "timeinfo.tm_wday: %d", timeinfo.tm_wday); ESP_LOGI(TAG, "timeinfo.tm_year: %d", timeinfo.tm_year+1900); ESP_LOGI(TAG, "timeinfo.tm_mon: %d", timeinfo.tm_mon+1); ESP_LOGI(TAG, "timeinfo.tm_mday: %d", timeinfo.tm_mday); ESP_LOGI(TAG, "timeinfo.tm_hour: %d", timeinfo.tm_hour); ESP_LOGI(TAG, "timeinfo.tm_min: %d", timeinfo.tm_min); ESP_LOGI(TAG, "timeinfo.tm_sec: %d", timeinfo.tm_sec); strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);ESP_LOGI(TAG, "The current date/time in Shanghai is: %s", strftime_buf); vTaskDelay(1000 / portTICK_PERIOD_MS); }
}static void obtain_time(void)
{/*** NTP server address could be aquired via DHCP,* see LWIP_DHCP_GET_NTP_SRV menuconfig option*/
#ifdef LWIP_DHCP_GET_NTP_SRVsntp_servermode_dhcp(1);
#endifinitialize_sntp();// wait for time to be settime_t now = 0;struct tm timeinfo = { 0 };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);}time(&now);localtime_r(&now, &timeinfo);
}static void initialize_sntp(void)
{ESP_LOGI(TAG, "Initializing SNTP");sntp_setoperatingmode(SNTP_OPMODE_POLL);sntp_setservername(0, "pool.ntp.org"); sntp_set_time_sync_notification_cb(time_sync_notification_cb);sntp_init();
}
ESP32入门基础之SNTP时间显示相关推荐
- ESP32入门基础之UDP和TCP实验
文章目录 1 用户数据协议报UDP简介 1.1 UDP作为client进行数据收发实验 1.1.1 向app-wifi-udp-client工程添加udp client相关文件 1.1.2 网络调试助 ...
- ESP32入门基础之ESP32作为 WIFI Station去连接wifi热点
文章目录 1 工程简介 1.1 在menuconfig配置WiFi账号.密码 1.2 在程序中配置WiFi账号.密码 1 工程简介 参考工程为乐鑫官方例程: 实验目标: ESP32作为WiFi sta ...
- ESP32入门基础之ble spp client 和 ble spp server 的学习理解
文章目录 1 工程简介 2 工程分析 2.1 工程 ble_spp_client 分析 2.1.1 初始化分析 2.1.2 两BLE扫描连接.配置.参数同步分析 2.1.3 蓝牙数据发送流程分析 2. ...
- VS集成Qt开发入门(简易时间显示)
VS集成Qt开发入门(简易时间显示) 软件开发入门 开发环境 简单时间显示(LcdNumber) ui界面设计(clock.ui) 工程文件(clock.h,clock.cpp) 头文件clock.h ...
- esp32 Micropython驱动ST7735 1.8寸TFT屏幕 中文显示;时间显示、网络network实时时间获取utptime;urequests、upip等包安装
参考: https://blog.csdn.net/weixin_57604547/article/details/122274614 0.线连接 IO就是GPIO引脚 ESP32 ---- TFT ...
- Python入门基础篇 No.8 —— 时间的表示_unix时间点_毫秒_time模块
Python入门基础篇 No.8 -- 时间的表示_unix时间点_毫秒_time模块 文章目录 Python入门基础篇 No.8 -- 时间的表示_unix时间点_毫秒_time模块 前言 一.时间 ...
- ESP32基于Arduino框架下U8g2驱动I2C OLED 时间显示
ESP32基于Arduino框架下U8g2驱动I2C OLED时间显示
- ESP32 入门笔记08:1.54寸(240*240)彩色TFT 显示高清IPS LCD 屏幕 SPI接口
目录 1.屏幕规格 2.原理图 3.程序实现 3.1引脚定义 3.2Adafruit_GFX / Arduino_ST7789版 3.3TFT_eSPI库版 3.3.1配置TFT_eSPI a.选择屏 ...
- 二、ESP32基于Arduino IDE OLED 联网显示时间
上一篇学习笔记讲了如何在Arduino上配置ESP32开发环境,这次分享一下如何让ESP32联网获取时间再通过OLED来显示.此系列文章都是学习笔记,希望能有所帮助. ps:代码中调用的库如需要可在在 ...
最新文章
- android中方法调用super(..)的相关知识
- 为什么深度学习不能取代传统的计算机视觉技术?
- Python命令行补全设置
- yolov3-tiny神经网络FPGA(ZYNQ7020)实现
- [模板]洛谷T3379 最近公共祖先(LCA) 倍增+邻接表
- 十分钟让你对C++ Traits大彻大悟
- 移动API设计与安全存储
- Ubuntu 安装绿联CM448无线网卡驱动
- ML面试1000题系列(71-80)
- 当前系统缺少NTFS格式转换器(convert.exe)
- 市场战略再升级 解析融云的生态平台+纵深发展
- 大厂职级、薪资一览表,你处在哪一级?(BAT/TMD/华为)
- Obtaining Reliable Human Ratings of Valence, Arousal, and Dominance for 20,000 English Words
- 《计算广告》读书笔记——第一章 在线广告综述
- esp32 micropython 控制ws2812 RGB灯带
- 为android模拟器加速
- FFmpeg 源码之内存管理函数族
- ElementUI引入自定义图标
- buuctf [强网杯 2019]随便注 1
- 屏蔽Mac版迅雷更新提醒