主要内容

搜索

My goal is to get the data from two DHT22 sensors, with a single sensor I had no problem but with two sensors I get only a few data and then it interrupts reporting "Read DHT22 failed." I share my code, any suggestions I appreciate

    #include <SimpleDHT.h>
    #include <ESP8266WiFi.h>
    String apiKey = "xxxxx";
    const char* ssid = "xxxxx";
    const char* password = "xxxxx";
    const char* server = "api.thingspeak.com";
    int pinDHT22 = D0;
    int pinDHT221 = D1;
    SimpleDHT22 dht22(pinDHT22);
    SimpleDHT22 dht221(pinDHT221);
    WiFiClient client;
    void setup() {
      Serial.begin(115200);
      delay(10);
      WiFi.begin(ssid, password);
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
    }
    void loop() {
     byte temperatura = 0;
     byte humedad = 0;
     byte temperatura1 = 0;
     byte humedad1 = 0;
      if (dht22.read(pinDHT22, &temperatura, &humedad, NULL)) {
        Serial.print("Read DHT22 failed.");
        return;
      }
      if (dht221.read(pinDHT221, &temperatura1, &humedad1, NULL)) {
        Serial.print("Read DHT221 failed.");
        return;
      }
      if (client.connect(server,80)) {
        String postStr = apiKey;
        postStr +="&field1=";
        postStr += String((int)temperatura);
        postStr +="&field2=";
        postStr += String((int)humedad);
        postStr +="&field3=";
        postStr += String((int)temperatura1);
        postStr +="&field4=";
        postStr += String((int)humedad1);
        postStr += "\r\n\r\n";
        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.print(postStr);
        Serial.println("% send to Thingspeak");
      }
      client.stop();
      Serial.println("Waiting…");
      delay(120000);
    }