使用blinker后,传感器读数一直为0
本帖最后由 speghetti 于 2022-9-28 08:55 编辑想做个实验,读取A0的光敏传感器数值显示到Blinker app上。
1. 单独测试光敏传感器,在串口观察读数正常,且在遮挡时有变化
2. 加入blinker 功能代码,同样连线,同样传感器,串口读数一直为0
代码如下:
#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "XXXX";
char ssid[] = "xxxx";
char pswd[] = "xxxx";
BlinkerNumber LIGHT("lgt");
int lgt_read = 0;
void heartbeat() {
LIGHT.print(lgt_read);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
Blinker.run();
BLINKER_LOG("LIGHT: ",analogRead(A0)); //<-- 串口一直 显示"LIGHT: 0"
lgt_read = analogRead(A0);
Blinker.delay(2000);
}
如果把Serial.println(analogRead(A0));加在Blinker.begin(auth, ssid, pswd);之前是有正常读数的
看来Blinker好像会影响analogRead
和blinker无关,对于esp芯片,在使用了wifi后,ADC2将不可用 那该怎么规避这个限制,来实现我的目的啊? 搞定了,感谢@coloz的提示。 增加如下代码
const int LGT_PIN = 33;//GPIO33, 对应我的板子D5,自行对照自己的板子管脚图修改
//in setup() add below codes
pinMode(LGT_PIN,INPUT);
adcAttachPin(LGT_PIN);
analogReadResolution(11);
analogSetAttenuation(ADC_6db);
//其余不变
详细原因在https://arduino.stackexchange.com/questions/84888/analog-read-not-working-while-using-wifi
The ESP32 has two ADCs. One of them, ADC2, is actively used by the WiFi.
From the IDF documentation:
Since the ADC2 module is also used by the Wi-Fi, only one of them could get the preemption when using together, which means the adc2_get_raw() may get blocked until Wi-Fi stops, and vice versa.
That means you can't use the ADC on any of the ADC2 channels while WiFi is on: GPIO4, GPIO0, GPIO2, GPIO15, GPIO13, GPIO12, GPIO14, GPIO27, GPIO25 and GPIO26.
But you can use ADC1, which uses pins GPIO36, GPIO37, GPIO38, GPIO39, GPIO32, GPIO33, GPIO34 and GPIO35. speghetti 发表于 2022-9-28 11:15
增加如下代码
const int LGT_PIN = 33;//GPIO33, 对应我的板子D5,自行对照自己的板子管脚图修改
忘了说要把loop()里的analogRead(A0)
改过来
analogRead(LGT_PIN);
页:
[1]