#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define dht_pin 15
#define dht_type DHT22
DHT dht(dht_pin, dht_type);
const char* ssid ="Wokwi-GUEST";
const char* pass ="";
String bURL="https://api.thingspeak.com/update?api_key=LGMMLP5WIIQ1GV3N&field1=0";
String field2 ="&field2";
int temp = 0;
int humid= 0;
void setup() {
Serial.begin(115200);
delay(500);
Serial.println("Upload data on thingspeak server!!!");
dht.begin();
delay(500);
ConnectToWifi();
}
void loop() {
temp=dht.readTemperature();
humid = dht.readHumidity();
if(isnan(temp)||isnan(humid))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
String msg="weather temperature "+String(temp)+"°C"+" humidity " +String(humid)+"%";
Serial.println(msg);
delay(500);
makeGETRequest();
Serial.println("Data uploaded sucessfuly");
delay(20000);
}
void ConnectToWifi()
{
Serial.println("Conecting to wifi......");
WiFi.begin(ssid,pass);
while(WiFi.status()!= WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting....");
}
Serial.println("Connected");
}
void makeGETRequest()
{
String url = bURL + String(temp)+ field2 +String(humid);
Serial.print("sending GET request to:");
Serial.println(url);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode >0)
{
String payload = http.getString();
Serial.print(payload);
}
else
{
Serial.print("[HTTP] Get.... Failed, error :%s\n");
Serial.print(http.errorToString(httpCode).c_str());
}
http.end();
}