#include <WiFi.h>
#include "DHT.h"


const char* wifi_name = "SSID"; // Your Wifi network name here
const char* wifi_pass = "Password";    // Your Wifi network password here
WiFiServer server(80);    // Server will be at port 80

int relay_pin = 25;
#define DHTPIN 26 

#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);


void setup() 
{
  Serial.begin (115200);
  pinMode (relay_pin, OUTPUT);
  dht.begin();

  Serial.print ("Connecting to ");
  Serial.print (wifi_name);
  WiFi.begin("Wokwi-GUEST", "", 6);     // Connecting to the wifi network

  while (WiFi.status() != WL_CONNECTED) // Waiting for the response of wifi network
  {
    delay (500);
    Serial.print (".");
  }
  Serial.println("");
  Serial.println("Connection Successful");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());           // Getting the IP address
  Serial.println("Type the above IP address into browser search bar"); 
  server.begin();                           // Starting the server
} 

void loop() 
{
  WiFiClient client = server.available();     //Checking if any client request is available or not
  if (client)
  {
    boolean currentLineIsBlank = true;
    String buffer = "";  
    while (client.connected())
    {
      if (client.available())                    // if there is some client data available
      {
        char c = client.read(); 
        buffer+=c;                              // read a byte
        if (c == '\n' && currentLineIsBlank)    // check for newline character, 
        {
          float h = dht.readHumidity();
          float t = dht.readTemperature();
          float f = dht.readTemperature(true);
          if (isnan(h) || isnan(t) || isnan(f)) {
              Serial.println(F("Failed to read from DHT sensor!"));
            return;
          }
          float hif = dht.computeHeatIndex(f, h);
          float hic = dht.computeHeatIndex(t, h, false);
          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.print(hic);
          Serial.print(F("°C "));
          Serial.print(hif);
          Serial.println(F("°F"));


          //html stuff
          client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("<p><span class=\"dht-labels\">Temperature</span><span id=\"temperature\">f</span><sup class=\"units\">°C</sup></p>");    
          client.println("<p><span class=\"dht-labels\">Humidity</span><span id=\"humidity\">h</span><sup class=\"units\">%</sup></p>");
          client.print("<HTML><title>ESP32</title>");
          client.print("<body><h1>Sistema de monitoreo Aeroponia </h1>");
          client.print("<p>Control de spray de agua</p>");
          client.print("<p>Control de spray de agua</p>");
          client.print("<a href=\"/?relayon\"\"><button>ON</button></a>");
          client.print("<a href=\"/?relayoff\"\"><button>OFF</button></a>");
          client.print("</body></HTML>");
         
          break;        // break out of the while loop:
        }
        if (c == '\n') { 
          currentLineIsBlank = true;
          buffer="";       
        } 
        else 
          if (c == '\r') {     
          if(buffer.indexOf("GET /?relayon")>=0)
            digitalWrite(relay_pin, LOW);
          if(buffer.indexOf("GET /?relayoff")>=0)
            digitalWrite(relay_pin, HIGH);   
        }
        else {
          currentLineIsBlank = false;
        }  
      }
    }
    client.stop();
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module