#include <WiFi.h>
#include <DHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ThingSpeak.h>

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const char* apikey = "30TXJNSXI5ZQJTQI";
WiFiClient client;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
DHT dht(4, DHT22);
float temperature;
float humidity;
int counter;

void setup() {
  WiFi.begin(ssid, password);
  Serial.begin(115200);
  pinMode(4, INPUT_PULLUP);
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }

  oled.clearDisplay();
  Serial.println("Hello, ESP32!");
  ThingSpeak.begin(client);
}

void loop() {
  oled.clearDisplay();
  oled.setCursor(0, 0);
  oled.setTextSize(1);
  oled.setTextColor(SSD1306_WHITE);

  temperature = dht.readTemperature();
  if (!isnan(temperature)) {
    oled.println(temperature);
    oled.setCursor(4, 15);
    oled.setTextSize(1);
    oled.setTextColor(SSD1306_WHITE);
  }

  humidity = dht.readHumidity();
  if (!isnan(humidity)) {
    oled.println(humidity);
  }

  oled.display();
  ThingSpeak.writeField(2512280, 1, humidity, apikey); // Assuming you want to send humidity data to ThingSpeak
  delay(1000);
}