我正在编写代码以读取DHT11值并控制4继电器但温度。传感器每次都向我显示“南”

时间:2019-01-16 09:03:06

标签: mqtt arduino-esp8266

我正在使用NodeMCU(ESP8266-12E)进行温度读数的家庭自动化项目。我正在使用带有DHT11.h库的DHT11传感器,但是我的温度传感器显示的是“ nan”而不是任何值。我不知道我在哪里落后。

我的代码如下:

#include "DHT.h" // including the library of DHT11 temperature and humidity sensor

#define DHTTYPE DHT11 // DHT 11

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define Relay1 D1
#define Relay2 D2
#define Relay3 D3
#define Relay4 D4

#define DHTPIN D0
    DHT dht(DHTPIN, DHTTYPE);

float temp_f;
String webString = "";

unsigned long previousMillis = 0;
const long interval = 2300;

#define WLAN_SSID "internet" // Your SSID
#define WLAN_PASS "*********" // Your password

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER "io.adafruit.com" //Adafruit Server
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "foo" // Username
#define AIO_KEY "bar" // Auth Key

//WIFI CLIENT
WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char TEMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME"/feeds/photocell");

Adafruit_MQTT_Subscribe Light1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Relay1"); // Feeds name should be same everywhere
Adafruit_MQTT_Subscribe Light2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Relay2");
Adafruit_MQTT_Subscribe Light3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Relay3");
Adafruit_MQTT_Subscribe Light4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Relay4");

void MQTT_connect();

void setup()
{
    Serial.begin(115200);
    delay(10);
    dht.begin();

    // Print temperature sensor details.

    pinMode(Relay1, OUTPUT);
    pinMode(Relay2, OUTPUT);
    pinMode(Relay3, OUTPUT);
    pinMode(Relay4, OUTPUT);

    // Connect to WiFi access point.
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(WLAN_SSID);

    WiFi.begin(WLAN_SSID, WLAN_PASS);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println();

    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    temp_f = dht.readTemperature(true);
    Serial.println();
    Serial.print("Initial Temp: ");
    Serial.println(temp_f);
    Serial.println();

    mqtt.subscribe(&Light1);
    mqtt.subscribe(&Light3);
    mqtt.subscribe(&Light2);
    mqtt.subscribe(&Light4);
}

int delayTime = 300000; //Wait 5 minutes before sending data to web
int startDelay = 0;

void loop()
{
    MQTT_connect();
    if (millis() - startDelay < delayTime) {
        Serial.println("waiting delaytime");
    }
    else {
        temp_f = dht.readTemperature(true); //Get temp in Farenheit
        startDelay = millis();
        Serial.print(F("\nSending temp: "));
        Serial.print(temp_f);
        Serial.print("...");
        if (!photocell.publish(temp_f)) { //Publish to Adafruit
            Serial.println(F("Failed"));
        }
        else {
            Serial.println(F("Sent!"));
        }
    }

    /* //int t = dht.readTemperature(true); 
    // t = t/100000000;
    Serial.print(F("\nSending photocell val "));
    Serial.print(t);
    Serial.print("...");

    if (! photocell.publish(t)) {
        Serial.println(F("Failed"));
    } else {
        Serial.println(F("OK!"));
    }*/

    Adafruit_MQTT_Subscribe* subscription;
    while ((subscription = mqtt.readSubscription(2000))) {
        if (subscription == &Light1) {
            Serial.print(F("Got: "));
            Serial.println((char*)Light1.lastread);
            int Light1_State = atoi((char*)Light1.lastread);
            digitalWrite(Relay1, Light1_State);
        }
        if (subscription == &Light2) {
            Serial.print(F("Got: "));
            Serial.println((char*)Light2.lastread);
            int Light2_State = atoi((char*)Light2.lastread);
            digitalWrite(Relay2, Light2_State);
        }
        if (subscription == &Light3) {
            Serial.print(F("Got: "));
            Serial.println((char*)Light3.lastread);
            int Light3_State = atoi((char*)Light3.lastread);
            digitalWrite(Relay3, Light3_State);
        }
        if (subscription == &Light4) {
            Serial.print(F("Got: "));
            Serial.println((char*)Light4.lastread);
            int Light4_State = atoi((char*)Light4.lastread);
            digitalWrite(Relay4, Light4_State);
        }
    }

    // this is our 'wait for incoming subscription packets and callback em' busy subloop
    // try to spend your time here:
    mqtt.processPackets(500);
}

void MQTT_connect()
{
    int8_t ret;

    if (mqtt.connected()) {
        return;
    }

    Serial.print("Connecting to MQTT... ");

    uint8_t retries = 3;

    while ((ret = mqtt.connect()) != 0) {
        Serial.println(mqtt.connectErrorString(ret));
        Serial.println("Retrying MQTT connection in 5 seconds...");
        mqtt.disconnect();
        delay(5000);
        retries--;
        if (retries == 0) {
            while (1);
        }
    }
    Serial.println("MQTT Connected!");
}

1 个答案:

答案 0 :(得分:1)

您尝试在setup()中读取温度和湿度。但是在 dht.begin 之后和读取之前,您需要等待至少2秒钟,因为传感器的读数可能还会长达2秒

因此在第一次阅读之前添加 delay(2000)