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

#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int BUZZ_PIN = 14;
int LED_PIN = 12;

const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2474667 ;
const char* myApiKey = "8HCXMC58NL0B3O0O";
const char* server = "api.thingspeak.com";

WiFiClient client;


void setup() {
  Serial.begin(115200);
  dht.begin();
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZ_PIN, OUTPUT);

  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() {
DHT();
}

void DHT() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  Serial.println("------------------------------");
  Serial.println("Temp: " + String(t) + "°C");
  Serial.println("Humidity: " + String(h) + "%");

  ThingSpeak.setField(1, t);
  ThingSpeak.setField(2, h);

  if (t > 40) {
    Bunyi(1000, 200);
    ThingSpeak.setField(3, 40);
  }
  else if (t > 30) {
    Bunyi(200, 1000);
    ThingSpeak.setField(3, 30);
  }
  else {
    Bunyi(0, 0);
    ThingSpeak.setField(3, 0);
  }

    int x = ThingSpeak.writeFields(myChannelNumber,myApiKey);

  
  if(x == 200){
    Serial.println("Data pushed successfull");
  }else{
    Serial.println("Push error" + String(x));
  }
  Serial.println("---");
  delay(5000);
}

void Bunyi(int x, int y) {
  digitalWrite(LED_PIN, HIGH);
  tone(BUZZ_PIN, x);
  delay(y);
  digitalWrite(LED_PIN, LOW);
  noTone(BUZZ_PIN);
  delay(y);
}

$abcdeabcde151015202530fghijfghij