#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 = 2483926;
const char * myWriteAPIKey = "WCH81HJIUCGOS0JD";


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);

int sensorPin = A0;

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);
  Serial.println("CEIS490 Part 1 Project ");
  Serial.println(String(__DATE__) + "\t" + String(__TIME__));
  Serial.println("Name: XXXXX ");// Replace xxxxx with your name

}

unsigned long t=0;
unsigned long interval = 10000;

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

  
  display.clearDisplay();
  display.display(); 
  
  // Menetapkan Saiz Perkataan
  display.setTextSize(1.7);
  //display.display(); 

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

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

  display.setCursor(0, 15);
  display.println("Humidity: " +String(data.humidity, 1) + " %");
  display.setCursor(0, 30);
  display.println("pH: " + String(pH, 1));
  display.display();
  if ( (millis()-t) > interval){
    ThingSpeak.setField(1, temp);
    ThingSpeak.setField(2, hum);
    ThingSpeak.setField(3, pH);
   
  // 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));
    }
    t = millis();

  }
  
  


}