#include <WiFi.h>
#include "ThingSpeak.h"
#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SENSOR_PIN  2

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN 4
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);

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

WiFiClient  client;

unsigned long myChannelNumber = 2324677;
const char * myWriteAPIKey = "TJ98NLWI5UW7IU79";

// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 15000;


void setup() {
  Serial.begin(9600);

 // WiFi.begin(ssid, password);
  WiFi.begin("Wokwi-GUEST", "", 6);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }

 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());

   // initialize the OLED object
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Clear the buffer.
  display.clearDisplay();

  // Display Text
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,28);
  display.println("GAS REAL TIME MONITOR");
  display.display();
  delay(2000);
  display.clearDisplay();

  dht.begin();

  ThingSpeak.begin(client);
}

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.println(F("°C "));

  // Display Text
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Humidity: ");
  display.print(h);
  display.println("%");

  //display.setCursor(0,28);
  
  // Temperature in Celcius
  display.print("Temperature: ");
  display.print(t);
  display.println(" C");
  
  // Temperature in Fahreheit
  display.print("Temperature: ");
  display.print(f);
  display.println(" F");

 // display.setCursor(0,50);

  display.println("CO2 Level ");

  display.println("P.M.:");
  display.display();

  if (millis() - lastTime >= timerDelay) {
    ThingSpeak.setField(1, h);
    ThingSpeak.setField(2, t);
    ThingSpeak.setField(3, f);

    int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    if (x == 200) {
      Serial.println("Channel update successful.");
      } else {    // If cloud update wasnot successful
        Serial.println("Problem updating channel. HTTP error code " + String(x));
      }

    lastTime = millis();
}

}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
r1:1
r1:2
temp1:GND
temp1:DQ
temp1:VCC
r2:1
r2:2