下面的代码很简单:使用esp8266 的 nodemcu 建立一个wifi AP,并且在loop函数中使用analogRead 从A0读取值
下面的代码其实看起来确实没有问题,也能编译通过,实际运行起来好像也不报错。
但是,你是找不到“I am Wifi” 这个SSID的,建议你先试试下面这段代码。
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#define led 2
ESP8266HTTPUpdateServer httpUpdater;
ESP8266WebServer WifiServer(80);
bool isDisplayFromCell = false;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP("I am Wifi", "3.1415926"); //设立softAP
Serial.println("Soft ap is start.");
delay(200);
Serial.print("IP Address:");
Serial.println(WiFi.softAPIP());
WiFi.printDiag(Serial);
httpUpdater.setup(&WifiServer);
WifiServer.onNotFound([]() {
WifiServer.send(404, "text/html", "File not found");
});
WifiServer.begin();
Serial.println("Wifi Server is started..");
}
int light = 0;
void loop() {
// put your main code here, to run repeatedly:
WifiServer.handleClient();
light = analogRead(A0);
//delay(2);
if (isDisplayFromCell == false) return;//如果设置不显示
}
上面这段简单的并没有任何错误的代码又耗费了我将近一天来查找原因:为啥ssid没有了,甚至还换了一块nodemcu,重新刷了一块nodemcu,怀疑return 导致的,甚至还去查loop的结构。。。 。。。
硬件真的是坑啊。
各种折腾,各种尝试后最后发现问题出现在analogRead 上,如果你注释掉上面的
//light = analogRead(A0);
SSID就可以正常出来了
或者取消注释delay(2);
void loop() {
// put your main code here, to run repeatedly:
WifiServer.handleClient();
light = analogRead(A0);
delay(2);
if (isDisplayFromCell == false) return;//如果设置不显示
}
SSID一样可以出来。保守一点建议delay 更大的值!
之前见过别人使用analogRead时使用delay,但是当时的解释是获取一个均衡的值。
问题解决了,耗费了大半天,但是依然不知道为啥原因。