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

const char* ssid = "Wokwi-GUEST";   // your network SSID (name) 
const char* password = "";   // your network password

WiFiClient  client;

unsigned long myChannelNumber = 2;
const char * myWriteAPIKey = "XYN1MN6X7JRK90X8";

unsigned long lastTime = 0;
unsigned long timerDelay = 10000;

const int DHT_PIN = 15;

DHTesp dhtSensor;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET     4
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  WiFi.mode(WIFI_STA);   
  ThingSpeak.begin(client);  // Initialize ThingSpeak

  // Connect or reconnect to WiFi
    if(WiFi.status() != WL_CONNECTED){
      Serial.print("Attempting to connect");
      while(WiFi.status() != WL_CONNECTED){
        WiFi.begin(ssid, password); 
        delay(5000);     
      } 
      Serial.println("\nConnected.");
    }

  
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);

    // Checked the OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.display();
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
}


void loop() {
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  float temp = data.temperature;
  float hum = data.humidity;
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  delay(3000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)

   // Kosongkan Paparan
  display.clearDisplay();
  display.display(); // Arahan dilaksanakan  
  
  // Menetapkan Saiz Perkataan
  display.setTextSize(1.7);
  display.display(); // Arahan dilaksanakan

  // Menetapkan Warna Perkataan
  display.setTextColor(SSD1306_WHITE);
  display.display(); // Arahan dilaksanakan

  // Menetapkan posisi pixel
  display.setCursor(0, 0);
  display.display(); // Arahan dilaksanakan
  
  // Memaparkan perkataan
  display.println("Suhu: " +String(data.temperature, 2) + " C");
  
  display.display(); // Arahan dilaksanakan

  display.setCursor(0, 15);
  display.println("Kelembaban: " +String(data.humidity, 1) + " %");
  
  ThingSpeak.setField(1, temp);
  ThingSpeak.setField(2, hum);
  display.display();
  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
    // pieces of information in a channel.  Here, we write to field 1.
    //int x = ThingSpeak.writeField(myChannelNumber, 1, temp, myWriteAPIKey);
    //uncomment if you want to get temperature in Fahrenheit
    //int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureF, myWriteAPIKey);
    int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    
    if(x == 200){
      Serial.println("Channel update successful.");
    }
    else{
      Serial.println("Problem updating channel. HTTP error code " + String(x));
    }


}