使用ESP32可以成功获取到网络时间,怎么才能得到整形数据呢?我想显示到数码管上,谢谢大家!
#include <WiFi.h>
#include <time.h>
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 28800;
const int daylightOffset_sec = 28800;
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
delay(2000);// 等待配网
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
delay(500);
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
WiFi.setAutoConnect(true); // 设置自动连接
break;
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}
void setup()
{
{
Serial.begin(115200);
smartConfig();
}
{
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
}
}
void loop()
{
delay(1000);
printLocalTime();
}
离线
已经顺利实现时间的显示
做的比较丑陋大家不要笑话
代码也分享出来
实现了自动配网
手机需要下载ESPTOUCH这个软件
#include <WiFi.h>
#include <Arduino.h>
#include "NTPClient.h"
WiFiUDP ntpUDP; // 创建一个WIFI UDP连接
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60*60*8, 30*60*1000);
#define wei0 22 //左边的数码管
#define wei1 21 //右边的数码管
#define wei2 19 //左边的数码管
#define wei3 18 //右边的数码管
#define duan_a 26
#define duan_b 27
#define duan_c 13
#define duan_d 12
#define duan_e 14
#define duan_f 33
#define duan_g 32
#define duan_h 25
int duan[8] = {duan_a,duan_b,duan_c,duan_d,duan_e,duan_f,duan_g,duan_h};
int wei[4] = {wei0,wei1,wei2,wei3};
int displaywei[4] = {
timeClient.getFormattedTime().substring(0,1).toInt(),
timeClient.getFormattedTime().substring(1,2).toInt(),
timeClient.getFormattedTime().substring(3,4).toInt(),
timeClient.getFormattedTime().substring(4,5).toInt()};
int number[10][8] = {
{0,0,0,0,0,0,1,1},
{1,0,0,1,1,1,1,1},
{0,0,1,0,0,1,0,1},
{0,0,0,0,1,1,0,1},
{1,0,0,1,1,0,0,1},
{0,1,0,0,1,0,0,1},
{0,1,0,0,0,0,0,1},
{0,0,0,1,1,1,1,1},
{0,0,0,0,0,0,0,1},
{0,0,0,0,1,0,0,1}
};
void SmartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig...");
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
delay(500); // wait for a second
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
bool AutoConfig()
{
WiFi.begin();
//如果觉得时间太长可改
for (int i = 0; i < 20; i++)
{
int wstatus = WiFi.status();
if (wstatus == WL_CONNECTED)
{
Serial.println("WIFI SmartConfig Success");
Serial.printf("SSID:%s", WiFi.SSID().c_str());
Serial.printf(", PSW:%s\r\n", WiFi.psk().c_str());
Serial.print("LocalIP:");
Serial.print(WiFi.localIP());
Serial.print(" ,GateIP:");
Serial.println(WiFi.gatewayIP());
return true;
}
else
{
Serial.print("WIFI AutoConfig Waiting......");
Serial.println(wstatus);
delay(1000);
}
}
Serial.println("WIFI AutoConfig Faild!" );
return false;
}
void setup() {
pinMode(wei0, OUTPUT);
pinMode(wei1, OUTPUT);
pinMode(wei2, OUTPUT);
pinMode(wei3, OUTPUT);
pinMode(duan_a, OUTPUT);
pinMode(duan_b, OUTPUT);
pinMode(duan_c, OUTPUT);
pinMode(duan_d, OUTPUT);
pinMode(duan_e, OUTPUT);
pinMode(duan_f, OUTPUT);
pinMode(duan_g, OUTPUT);
pinMode(duan_h, OUTPUT);
digitalWrite(wei0,LOW);
digitalWrite(wei1,LOW);
digitalWrite(wei2,LOW);
digitalWrite(wei3,LOW);
for (int i = 0; i < 8; i++)//以此显示数字0到9
{
digitalWrite(duan[i],HIGH);
delay(50);
}
Serial.begin(115200);
if (!AutoConfig())
{
SmartConfig();
}
// WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop()
{
timeClient.update();
// Serial.println(timeClient.getFormattedTime());
int shi_shiwei = timeClient.getFormattedTime().substring(0,1).toInt();
int shi_gewei = timeClient.getFormattedTime().substring(1,2).toInt();
int fen_shiwei = timeClient.getFormattedTime().substring(3,4).toInt();
int fen_gewei = timeClient.getFormattedTime().substring(4,5).toInt();
digitalWrite(wei[0],HIGH);
for (int i = 0; i < 8; i++)//以此显示数字0到9
{
digitalWrite(duan[i],number[shi_shiwei][i]);
}
delay(2);
digitalWrite(wei[0],LOW);
digitalWrite(wei[1],HIGH);
for (int i = 0; i < 8; i++)//以此显示数字0到9
{
digitalWrite(duan[i],number[shi_gewei][i]);
}
delay(2);
digitalWrite(wei[1],LOW);
digitalWrite(wei[2],HIGH);
for (int i = 0; i < 8; i++)//以此显示数字0到9
{
digitalWrite(duan[i],number[fen_shiwei][i]);
}
delay(2);
digitalWrite(wei[2],LOW);
digitalWrite(wei[3],HIGH);
for (int i = 0; i < 8; i++)//以此显示数字0到9
{
digitalWrite(duan[i],number[fen_gewei][i]);
}
delay(2);
digitalWrite(wei[3],LOW);
}
离线
漂亮啊,有没有小白入门教程?
可以到B站搜一下ESP32 有很多视频可以参考 我是自己搞着好玩的
离线