Eep8266下调用analogRead 导致ssid不显示一例 ,又踩了一坑

32 篇文章 2 订阅
25 篇文章 0 订阅

下面的代码很简单:使用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,但是当时的解释是获取一个均衡的值。

问题解决了,耗费了大半天,但是依然不知道为啥原因。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值