我有一个ESP8266,它连接到WiFi,然后调用NTP服务器来获取时间。最初,我将WiFi连接设置为使用SSID和密码处理一个"config.h“文件--普通的方法。稍后,我用WiFi库设置了一个AsyncWebServer管理器,以避免像Rui在这个教程中所示的那样硬编码凭证。
现在,用户在由提供无线接入点的WiFi托管的网页上输入ESP8266凭据。这些凭据存储在本地文件系统(LittleFS)上的文件中,并从那时起在进行WiFi连接时进行访问。连接后,ESP8266运行一个承载静态网页的web服务器.
这很好,ESP8266连接到WiFi,但是NTP服务不再工作了!我从NTP客户那里得到一个固定时间到午夜12点。当我恢复到以普通方式连接到WiFi的时候,NTP服务就像预期的那样工作。
下面是我在站模式下建立WiFi连接的代码。类GuruWebServer具有用于文件系统操作的方法initializeaLittle()、readaLittle()和writeaLittle() .当成功地使用保存在文件系统中的凭据进行连接时,关键方法initializeWiFi()返回true,否则返回false。该方法具有一个带有millis()的循环,其中尝试连接到WiFi直到超时(例如60)。
我观察到,SSID和密码被正确记录,并且成功地建立了WiFi连接。随后,使用LanScan (在Mac上),我可以看到在站点IP地址上配置的ESP8266。使用浏览器,我可以加载它提供的网页。但是,我再也无法从呼叫NTP服务中获得时间和日期.
bool GuruWebServer::initializeWiFi() {
/*
* Make WiFi connection when SSID and PWD are available
* and set up station for web-service.
*/
// Initialize LittleFS
this->initializeaLittle();
// Connect to WiFi
this->readaLittle(&SSID, Path2_SSID);
this->readaLittle(&PWD, Path2_PWD);
this->readaLittle(&IP, Path2_IP);
this->readaLittle(&Gateway, Path2_Gateway);
String conn_ack = (String)"SSID <" + SSID + "> | PWD <" + PWD + "> | IP <" + IP + "> | Gateway <" + Gateway + ">";
if (SSID == "" || PWD == "") { // Files not populated!
Serial.println("Found NO SSID or PWD!");
return false;
}
WiFi.mode(WIFI_STA);
localIP.fromString(IP.c_str());
localGateway.fromString(Gateway.c_str());
if (!WiFi.config(localIP, localGateway, subnet)) {
Serial.println("Configured NO station!");
return false;
}
WiFi.begin(SSID, PWD);
unsigned long _now = millis();
unsigned long _then = _now; // Marker for ticker
unsigned long _stop = _now; // Marker for timeout
unsigned long delta_minor = 0; // Time since last tick
unsigned long delta_major = 0; // Time since start
while (WiFi.status() != WL_CONNECTED) {
_now = millis();
delta_minor = _now - _then; // Time since last tick
delta_major = _now - _stop; // Time since start
if (delta_minor > 1000) { // Mark time and reset delta
_then = _now;
Serial.print (".");
}
if (delta_major > 60000) { // Stop attempting connection
String timely = (String)"Now " + _now + " Stop " + _stop + " Diff " + delta_major;
Serial.println(timely);
Serial.println("Made NO WiFi connection!");
return false; // Exit false
} // end IF
} // end WHILE WiFi
Serial.println(WiFi.localIP());
return true;
}为了获得时间,我在主程序中使用了NTPClient库,如下所示:
#include <NTPClient.h>
#include <WiFiUdp.h>
WiFiUDP NTP_UDP;
NTPClient timeClient(NTP_UDP, "us.pool.ntp.org");在setup()函数"main.cpp“中,我有:
// Connect to the NTP client
delay(500);
timeClient.begin();
timeClient.setTimeOffset(-18000); // Central time GMT-5 ~ -5 x 3600
timeClient.update();
Serial.println(timeClient.getFormattedTime());github分支rev007有带有WiFi管理器的代码,rev006有普通的WiFi。
我尝试了以下几点:
configTime()函数的时间。AsyncWebServer server(80);中的80以外的端口(例如8211)。谢谢你帮我解决这个问题,谢谢你。
更新
我简化了代码来说明这个问题。这个脚本将ESP8266设置为WiFi站点。该设备成功连接到WiFi,但未成功调用NTP服务。
#include <Arduino.h>
#include <time.h>
#include "LittleFS.h"
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
WiFiUDP NTP_UDP;
NTPClient timeClient(NTP_UDP, "pool.ntp.org");
// Alt.
#include "config.h"
IPAddress localIP;
IPAddress localGateway;
IPAddress subnet(255, 255, 255, 0);
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
// Comment out from here --->
localIP.fromString(IP);
localGateway.fromString(Gateway);
if (!WiFi.config(localIP, localGateway, subnet)) {
Serial.println("Configured NO station!");
}
// <--- upto here!
WiFi.begin(SSID, PWD);
unsigned long _now = millis();
unsigned long _then = _now; // Marker for ticker
unsigned long _stop = _now; // Marker for timeout
unsigned long delta_minor = 0; // Time since last tick
unsigned long delta_major = 0; // Time since start
while (WiFi.status() != WL_CONNECTED) {
_now = millis();
delta_minor = _now - _then; // Time since last tick
delta_major = _now - _stop; // Time since start
if (delta_minor > 1000) { // Mark time and reset delta
_then = _now;
Serial.print (".");
}
if (delta_major > 60000) { // Stop attempting connection
String timely = (String)"Now " + _now + " Stop " + _stop + " Diff " + delta_major;
Serial.println(timely);
Serial.println("Made NO WiFi connection!");
} // end IF
} // end WHILE WiFi
Serial.println(WiFi.localIP());
// Connect to the NTP client
delay(3000);
timeClient.begin();
timeClient.setTimeOffset(-18000); // Central time GMT-5 ~ -5 x 3600
delay(3000);
timeClient.update();
Serial.println(timeClient.getFormattedTime());
}
void loop() {
// put your main code here, to run repeatedly:
}当我注释掉代码时,为了不从设置中配置IP地址,但让它在运行时确定,那么一切都很好。

发布于 2022-09-27 03:12:30
我很好奇想试试你的密码。问题是如果没有正确的DNS设置,NTP(UDP)将无法解决NTP名称"pool.ntp.org",我对您的代码进行了稍微修改和简化的测试。如果不使用WiFi.config(localIP, localGateway, subnet)指定DNS,timeClient.update()就无法建立NTP连接,通过向WiFi.config(localIP, localGateway, subnet, dns)添加DNS IP,下面的代码工作起来就像一种魅力。
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
WiFiUDP NTP_UDP;
NTPClient timeClient(NTP_UDP, "pool.ntp.org");
#include "config.h"
IPAddress localIP(192, 168, 0, 120); // my local ip
IPAddress localGateway(192, 168, 0, 1); // my local gateway
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(1, 1, 1, 1); // CloudFlare DNS server
void setup() {
Serial.begin(9600);
while (!Serial);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PWD);
WiFi.config(localIP, localGateway, subnet, dns);
int count = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (count++ > 20) ESP.restart(); //timeout, reboot
}
Serial.println(WiFi.localIP());
// Connect to the NTP client
timeClient.begin();
timeClient.setTimeOffset(8*3600); //my timezone
if (timeClient.update())
Serial.println(timeClient.getFormattedTime());
}
void loop() {
}发布于 2022-11-01 12:14:16
如果您想使用WiFiManager,只需离开wifiManager.setSTAStaticIPConfig,在wifiManager.autoConnect之后使用这个
WiFi.begin();
WiFi.config(IPAddress(192, 168, 1, 148), IPAddress(192, 168, 1, 1), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));它在ESP8266上对我起作用。
https://stackoverflow.com/questions/73821049
复制相似问题