Advertisement
AnindyaBiswas

DHT11 ThingSpeak

May 14th, 2023
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <DHT.h>
  3. #include <ThingSpeak.h>
  4.  
  5. #define dht_pin 2
  6. #define dht_type DHT11
  7.  
  8. DHT dht(dht_pin, dht_type);
  9. WiFiClient client;
  10.  
  11. //char ssid[] = "Anindya_Net 2.5";
  12. //char pwd[] = "07022001";
  13.  
  14. char ssid[] = "NindoFi";
  15. char pwd[] = "AnindyaKbiswas";
  16.  
  17. long channelID = 2132478;
  18. char writeAPIKey[] = "74XAOPKNHQOZYX74";
  19.  
  20. bool alt = true;
  21.  
  22. void setup() {
  23.   // put your setup code here, to run once:
  24.   Serial.begin(9600);
  25.   WiFi.begin(ssid, pwd);
  26.   while(WiFi.status() != WL_CONNECTED)
  27.   {
  28.     delay(200);
  29.     Serial.println("..");
  30.   }
  31.   Serial.print("Connected to -> ");
  32.   Serial.println(ssid);
  33.   Serial.println(WiFi.localIP());
  34.   dht.begin();
  35.   ThingSpeak.begin(client);
  36. }
  37.  
  38. void loop() {
  39.   // put your main code here, to run repeatedly:
  40.   float tempC = dht.readTemperature();
  41.   float humidity = dht.readHumidity();
  42.  
  43.   Serial.print("Temperature : ");
  44.   Serial.print(tempC);
  45.   Serial.print("        ");
  46.   Serial.print("Humidity : ");
  47.   Serial.print(humidity);
  48.   Serial.println();
  49.  
  50.   ThingSpeak.setField(1, tempC);
  51.   ThingSpeak.setField(2, humidity);
  52.   int x = ThingSpeak.writeFields(channelID, writeAPIKey);
  53.   delay(2000);
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement