ESP32_Thingspeak_DHT22.ino
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#define DHTPIN 27
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Robotics Lab";
const char* password = "Robotics@321";
String serverName = "https://api.thingspeak.com/update?api_key=XXNUJDNUCHY4FTYU";
void setup()
{
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
delay(2000);
dht.begin();
}
void loop()
{
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Temperature = "); Serial.println(t);
Serial.print("Humidity = "); Serial.println(h);
Serial.println("*******************************************************************************************");
String url = serverName + "&field1=" + t + "&field2=" + h ;
http.begin(url.c_str());
int httpResponseCode = http.GET();
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(15000);
}
else
{
Serial.println("WiFi Disconnected");
delay(1000);
}
}