#include<WiFi.h>
#include<WiFiClient.h>
#include<Ticker.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "DHT.h"
#define DHTPIN 25                                                      //Change
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
#define MQ_PIN 26                                                   //Change smoke sensor
Ticker secondTick;
WiFiClient client;

// #include <LiquidCrystal_I2C.h>
// #define SDA_PIN 21
// #define SCL_PIN 22
#include <Wire.h>

// LiquidCrystal_I2C lcd(0x27,16,2);

#include <Adafruit_BMP085.h>
#define seaLevelPressure_hPa 1013.25

Adafruit_BMP085 bmp;
#define BMP_SDA_PIN 21
#define BMP_SCL_PIN 22

char *ssid = "Wokwi-GUEST";            /*Your Router's SSID */
char *password = "";      /*Your Router's WiFi Password */
char *server = "api.thingspeak.com";

volatile int watchdogCount = 0;

// void ISRwatchdog()
// {
//   watchdogCount++;
//   if(watchdogCount == 5)
//   {
//     //Serial.println();
//     //Serial.println("The watchdog bites!!!!!!");
//     ESP.reset();
//   }

// }
float Humi;
int val1;
float CO2reading;

void setup(){
  dht.begin();
  Serial.begin(9600);
  // Wire.begin(SDA_PIN,SCL_PIN);
  Wire.begin(BMP_SDA_PIN,BMP_SCL_PIN);
  // pinMode(D4, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  // digitalWrite(D4, HIGH);
  // secondTick.attach(1, ISRwatchdog);
  ConnectAP();
  pinMode(DHTPIN,INPUT);
  pinMode(MQ_PIN,INPUT);
  // lcd.init();
  // lcd.backlight();
  // lcd.setCursor(0,0);
  // lcd.print("Hello");
  // delay(2000);
  // lcd.clear();
}

void loop(){
  String DataStream;
  String apiKey = "LZLS040Z9OTWRKWD";
  
  Humi = dht.readHumidity();
  val1= analogRead(MQ_PIN);
  CO2reading = (val1)/40.96;
DataStream = "&field1=" + String(bmp.readTemperature()) + "&field2=" + String(Humi) + "&field3=" + String(CO2reading) + "&field4=" + String(bmp.readPressure()); 
//   apiKey = "7KET5RZPMWQK2AGJ";
  Serial.println("data received");
  Serial.println(DataStream);
  watchdogCount = 0;
  if(client.connect(server, 80)){
    String postStr = apiKey;
    postStr += DataStream;
    postStr += "\r\n\r\n";
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    Serial.print(postStr.length());
    client.print("\n\n");
    Serial.print("\n\n");
    client.print(postStr);
    delay(100);
    Serial.print(postStr);
    delay(100);
    String line = client.readStringUntil('\n');
    Serial.println(line);
    watchdogCount = 0;
  }
  client.stop();
  // Serial.write('B');
  watchdogCount = 0;
  yield();

  // lcd.setCursor(0,0);
  // lcd.print("Temp: ");
  // lcd.print(bmp.readTemperature());
  // lcd.setCursor(0,1);
  // lcd.print("Humi: ");
  // lcd.print(Humi);
  // delay(2000);


  // lcd.setCursor(0,0);
  // lcd.print("Co2 in %: ");
  // lcd.print(CO2reading);
  // lcd.setCursor(0,1);
  // lcd.print("Pressure: ");
  // lcd.print(bmp.readPressure());
  // delay(2000);

  Serial.print("Temp: ");
  Serial.print(bmp.readTemperature());
  Serial.print("     ");
  Serial.print("Humi: ");
  Serial.print(Humi);
  Serial.print("     ");
  Serial.print("Co2 in %: ");
  Serial.print(CO2reading);
  Serial.print("     ");
  Serial.println("pressure: ");
  Serial.println(bmp.readPressure());
  delay(2000);
}

//
/* Connect to WiFi Router             */
/* (Access Point)                     */
//
void ConnectAP(void){
  WiFi.mode(WIFI_STA);    /* Set WiFi to station mode */
  WiFi.disconnect();     /* disconnect from an AP if it was Previously  connected*/
  delay(100);
  WiFi.begin(ssid, password);
  // pinMode(D4 , OUTPUT);
  while(WiFi.status() != WL_CONNECTED){
    // digitalWrite(D4, LOW);
    // delay(250);
    // digitalWrite(D4, HIGH);
    // delay(250);
    Serial.println("SSID: " + String(ssid) + "   PW:" + String(password));
    watchdogCount = 0;
    yield();
  }
  // digitalWrite(D4, LOW);
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
}

bmp180Breakout
mq_co2Breakout