我的 Wemos D1 mini 出现问题。 有人可以试试代码并告诉我它是否有效吗?
应该发生的事情是它尝试连接到指定的网络,然后询问时间。然而,如果网络不可用,则打开接入点。 在循环中每 2s 有一个检查:在 ap 模式下有一个异步扫描重新连接,在 sta 模式下它检查它是否仍然连接。
存在多个问题:
我的电路板可能坏了,因为 USB 电源不再工作,5V 输入也不工作。 (可能是我把它从支架上拿出来的时候毁掉的,没有先断开电源)
将不胜感激,谢谢!
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <NTPClient.h>
#include <TimeLib.h>
#include <Timezone_Generic.h>
#include <arduino-timer.h>
IPAddress ip(192, 168, 0, 2);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ch.pool.ntp.org", 0);
TimeChangeRule cet = {"CET", Last, Sun, Oct, 2, 60};
TimeChangeRule cest = {"CEST", Last, Sun, Mar, 3, 120};
Timezone europeanTime(cet, cest);
const char* DEVICENAME = "ESP8266";
const char* AP_PASS = "Password";
String WIFI_SSID = "";
String WIFI_PASS = "";
String TEMP_SSID;
String TEMP_PASS;
String wifiMode;
bool scanWifi;
bool specificScan = false;
uint32_t t;
auto timer = timer_create_default();
void setup() {
Serial.begin(115200);
Serial.println();
wifiManager("boot");
timeManager();
}
void loop() {
t = millis();
Serial.println(t);
if (t % 2000 == 0) {
wifiManager("check");
if (scanWifi) {
wifiManager("scan");
}
}
MDNS.update();
}
void wifiManager(String m) {
if (m == "boot") {
Serial.println("WiFi Boot");
WiFi.softAPdisconnect(true);
WiFi.disconnect();
Serial.println("SSID: " + String(WIFI_SSID));
if (WIFI_SSID != "" && WIFI_PASS != "") {
// Synchronous Scan
int networks = WiFi.scanNetworks();
bool success;
for (int i = 0; i < networks; i++) {
if (WiFi.SSID(i) == WIFI_SSID) {
success = wifi(WIFI_SSID, WIFI_PASS);
break;
}
}
if (!success) {
softAp();
}
}
}
// Check connection
else if (m == "check") {
int num = WiFi.scanComplete();
if (WiFi.status() == WL_CONNECTED && wifiMode != "wlan") {
// Connect to wifi
if (!wifi(WIFI_SSID, WIFI_PASS)) {
softAp();
}
}
if (WiFi.status() != WL_CONNECTED && wifiMode != "ap") {
// Open AP
softAp();
}
// Scan network if no stations are connected
if (wifiMode == "ap" && WiFi.softAPgetStationNum() == 0) {
scanWifi = true;
if (num > 0) {
wifiManager("connect", WIFI_SSID, WIFI_PASS, num);
}
}
if (WiFi.softAPgetStationNum() > 0) {
scanWifi = false;
}
if (num > 0 && specificScan) { // Manual connection
wifiManager("connect", TEMP_SSID, TEMP_PASS, num);
specificScan = false;
}
}
else if (m == "scan") {
// Asynchronous scan
if ((WiFi.scanComplete() > 0 || WiFi.scanComplete() == -2)){
Serial.println("Scanning Network...");
WiFi.scanNetworks(true);
delay(1);
}
}
}
// Check scan results
void wifiManager(String m, String ssid, String pass, int num) {
if (m == "connect") { // Connect to WiFi
for (int i = 0; i < num; i++) {
Serial.println(WiFi.SSID(i));
if (WiFi.SSID(i) == ssid) {
scanWifi = false;
if (!wifi(ssid, pass)) {
softAp();
}
}
}
}
}
bool wifi(String ssid, String pass) {
Serial.println("Connecting to " + String(ssid));
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_STA);
int counter = 0;
int maxCount = 100;
WiFi.begin(ssid, pass);
do {
delay(250);
counter++;
Serial.println("#" + String(counter));
if (counter == maxCount) {
return false;
}
} while (WiFi.status() != WL_CONNECTED);
wifiMode = "wlan";
MDNS.begin(DEVICENAME);
MDNS.addService("http", "tcp", 80);
return true;
}
void softAp() {
// https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/soft-access-point-class.html
Serial.println("Opening AP");
WiFi.disconnect();
WiFi.scanDelete();
WiFi.mode(WIFI_AP);
// Open AP
WiFi.softAPConfig(ip, gateway, subnet);
WiFi.softAP(DEVICENAME, AP_PASS);
wifiMode = "ap";
MDNS.begin(DEVICENAME);
MDNS.addService("http", "tcp", 80);
}
void timeManager() {
timeClient.begin();
timer.every(3600000, getRealTime);
timer.in(0, getRealTime);
}
bool getRealTime(void *) {
Serial.println("Time update");
time_t updatedTime;
if (WiFi.status() == WL_CONNECTED) {
timeClient.update();
updatedTime = timeClient.getEpochTime();
Serial.println("Unix Timestamp: " + String(updatedTime));
for (int i = 0; i < 5; i++) {
if (updatedTime < 1000000000) { // timeClient returned wrong value
Serial.println("Wrong time " + String(i));
delay(500);
timeClient.update();
updatedTime = timeClient.getEpochTime();
if (i == 4) {
return true;
}
}
else {
break;
}
}
Serial.println("Difference: " + String(abs(now() - timeClient.getEpochTime())));
// Set updated time
setTime(updatedTime);
}
else {
if (timeStatus() == timeSet) {
setTime(now());
}
else {
setTime(0);
}
}
}
int getLocalTime() {
String currentTime;
time_t utc = now();
time_t local = europeanTime.toLocal(utc);
int hour_local = hour(local);
int minute_local = minute(local);
currentTime = String(hour_local);
if (hour_local < 10) {
currentTime = "0" + String(hour_local);
}
if (minute_local < 10) {
currentTime += "0" + String(minute_local);
}
else {
currentTime += String(minute_local);
}
return currentTime.toInt();
}