#include<WiFi.h>
#include <ThingSpeak.h>
#include "DHTesp.h"
#define SECRET_SSID "Wokwi-GUEST"
#define SECRET_PASS ""
#define SECRET_CH_ID 2032838
#define SECRET_WRITE_APIKEY "QZ7ZW8VJWD27VGTT"
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int keyIndex = 0;
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;


const int LDR_PIN = 32;
const int DHT_PIN = 15;
const int NTC_PIN = 34;
const int PTM_PIN = 35;
const float BETA = 3950;


DHTesp dhtSensor;

const float GAMMA = 0.7;
const float RL10 = 50;

void setup() {
  Serial.begin(115200);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  analogReadResolution(10);
  pinMode(34, INPUT);
  pinMode(32, INPUT);
  pinMode(35, INPUT);

   while(!Serial){
    ;
  }
    WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);

}

void loop() 
{
  if(WiFi.status()  !=WL_CONNECTED)
  {
    Serial.print("Attemptting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while(WiFi.status()  !=WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      Serial.print(".");
      delay(5000);
    }
    Serial.println("\nConnected.");
  }


  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  delay(1000); 
  
  int machine = analogRead(34);
  float celsius = 1 / (log(1 / (1023. / machine- 1)) / BETA + 1.0 / 298.15) - 273.15;
  Serial.print("Machine Temperature: ");
  Serial.print(celsius);
  Serial.println(" ℃");
  Serial.println("---");
  delay(1000);
  
  

  
  int Lumen = analogRead(32);
  float voltage = Lumen / 1024. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
  Serial.print("ADC:  ");//print data to the serial port as human-readable ASCII text
  Serial.println(Lumen);//print the newline data with the carriage of the analogvalue
  Serial.print("lux:  " );//print data to the serial port as human-readable ASCII text
  Serial.println(lux);//print the newline data with the carriage of the lux value
  Serial.print("Voltage:   ");
  Serial.println(voltage);
  Serial.println("---");
  delay(1000);
  
  

  
  int Particle = analogRead(35);
  Serial.print("Particle:  ");
  Serial.println(Particle);
  Serial.println("---");
  delay(1000);
  
 
  
  ThingSpeak.setField(1, data.temperature);
  ThingSpeak.setField(2, data.humidity);
  ThingSpeak.setField(3, celsius);
  ThingSpeak.setField(4, lux);//set the thingspeak field and which data that will be transmitted to the thingspeak
  ThingSpeak.setField(5, Particle);//set the thingspeak field and which data that will be transmitted to the thingspeak
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);//set the data value and set the communication to transmit and write data online on the exact channel thingspeak
  if(x == 200)//if the data in the range of 200 then transmit.

  {
    Serial.println("Channel update succesful.");//if the transmission succesful it will print this data.
  }
  else//the main frame of the if else programme, else usually will be the last frame in if else structure.
  {
    Serial.println("Problem updating channel HTTP Error code " + String(x));//if there is any error it will be print this data in the serial monitor
  }
  delay(15000);
}