#include <WiFi.h>
#include "ThingSpeak.h"

#include "DHT.h"
#define DHTPIN 18
#define DHTTYPE DHT22  

const char* WIFI_NAME = "Wokwi-GUEST";  //SSID_hotspot
const char* WIFI_PASSWORD = "";         //pswd_phone_wifi

const int myChannelId = 2684878;    //your channel id
const char* myWriteApiKey = "2VQ2JF9EQZ3S91RS"; //write apikeyof your channel
const char* server = "api.thingspeak.com";

DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;

void setup() {
  Serial.begin(115200);
  dht.begin();

  WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED){
    delay(1000);
    Serial.println("Wifi not connected");
  }
  Serial.println("Wifi connected !");
  Serial.println("Local IP: " + String(WiFi.localIP()));
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
}

void loop() 
{
  
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  ThingSpeak.setField(1,t);
  ThingSpeak.setField(2,h);
  
  int x = ThingSpeak.writeFields(myChannelId,myWriteApiKey);
  Serial.print("Temperature: \t");
  Serial.println(t);
  Serial.print("Humidity: \t");
  Serial.println(h);
  delay(1000); 
}