|
nodemcu连接一个温湿度传感器可以成功读取温湿度,测试了两个传感器硬件也都是正常的,但是把两个传感器同时接在nodeMCU上后就不能读取数据了,难道这个板子不支持接两个相同的传感器?
代码如下,还是说我的代码有问题?
#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "1234ffda3"; //blinker账户生产的密码,小爱同学必须选用阿里云
//代码配网用下面这两段
char ssid[] = "SS"; //WiFi账号
char pswd[] = "SS"; //WiFi密码
BlinkerNumber HUMIOUT("humiout");
BlinkerNumber TEMPOUT("tempout");
BlinkerNumber DEWOUT("dewout");
BlinkerNumber HUMIIN("humiin");
BlinkerNumber TEMPIN("tempin");
BlinkerNumber DEWIN("dewin");
// Download Adafruit DHT-sensor-library library here:
// https://github.com/adafruit/DHT-sensor-library
#include <DHT.h>
#include <math.h>
#define DHTPINOUT D4
#define DHTPININ D0
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dhtout(DHTPINOUT, DHTTYPE);
DHT dhtin(DHTPININ, DHTTYPE);
uint32_t read_time = 0;
float humiout_read, tempout_read, dewout_read;
float humiin_read, tempin_read, dewin_read;
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void heartbeat()
{
HUMIOUT.print(humiout_read);
TEMPOUT.print(tempout_read);
DEWOUT.print(dewout_read);
HUMIIN.print(humiin_read);
TEMPIN.print(tempin_read);
DEWIN.print(dewin_read);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
dhtout.begin();
dhtin.begin();
}
void loop()
{
Blinker.run();
if (read_time == 0 || (millis() - read_time) >= 2000)
{
read_time = millis();
float hout = dhtout.readHumidity();
float tout = dhtout.readTemperature();
delay(5000);
float hin = dhtin.readHumidity();
float tin = dhtin.readTemperature();
if (isnan(hout) || isnan(tout)) {
BLINKER_LOG("Failed to read from DHT exernal sensor!");
return;
}
if (isnan(hin) || isnan(tin)) {
BLINKER_LOG("Failed to read from DHT internal sensor!");
return;
}
humiout_read = hout;
tempout_read = tout;
humiin_read = hin;
tempin_read = tin;
}
}
|
|