2x DHT 22 / NodeMCU

2x čídlo DHT 22 s odeslání do 2 channelů na thingspeak

 

#include <DHT.h>
#include <ESP8266WiFi.h>
 
// replace with your channel's thingspeak API key, 
String apiKey = "DTQNRNBI0LZ9641J";
String apiKey2 = "HE5YAU8QBYWZV4UH";
const char* ssid = "Brno4";
const char* password = "Zakosteli63";
 
const char* server = "api.thingspeak.com";
#define DHTPIN  D2  // D1 pin on Nodemcu
#define DHTPIN2 D1  // D2 pin on Nodemcu
 
DHT dht(DHTPIN, DHT22);
DHT dht2(DHTPIN2, DHT22);

WiFiClient client;

void setup() {                
  Serial.begin(115200);
  delay(10);
  dht.begin();
  delay(10);
  dht2.begin();
  
  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() {   
  float h = dht.readHumidity();

  float t = dht.readTemperature(); //read in celsius

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    //return;
  }


 
  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
           postStr +="&field1=";
           postStr += String(t); // prevadi na int-zruseno
           postStr +="&field2=";
           postStr += String((int)h); // převadi na int-ponechano
 
     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.print("Temperature: ");
     Serial.print(t);
     Serial.print(" degrees Celcius Humidity: "); 
     Serial.print(h);
     Serial.println("% send to Thingspeak");    

    
  }
  client.stop();



  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature(); //read in celsius

  if (isnan(h2) || isnan(t2)) {
    Serial.println("Failed to read from DHT2 sensor!");
    //return;
  }

  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr2 = apiKey2;
           postStr2 +="&field1=";
           postStr2 += String(t2); // prevadi na int-zruseno
           postStr2 +="&field2=";
           postStr2 += String((int)h2); // převadi na int-ponechano
 
     client.print("POST /update HTTP/1.1\n"); 
     client.print("Host: api.thingspeak.com\n"); 
     client.print("Connection: close\n"); 
     client.print("X-THINGSPEAKAPIKEY: "+apiKey2+"\n"); 
     client.print("Content-Type: application/x-www-form-urlencoded\n"); 
     client.print("Content-Length: "); 
     client.print(postStr2.length()); 
     client.print("\n\n"); 
     client.print(postStr2);
           
 
     Serial.print("Temperature: ");
     Serial.print(t2);
     Serial.print(" degrees Celcius Humidity: "); 
     Serial.print(h2);
     Serial.println("% send to Thingspeak");    

    
  }
  client.stop();


   
  Serial.println("Waiting...");    
  // time between updates
  delay(120000); // 2 mins
}

 

 

 

Napsat komentář