
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;

const char* ssid = "aBcAdfh";//inserire nome rete WiFi
const char* password = "wyZ23tX";//inserire password rete WiFi

WiFiServer server(80);

#define pressure_offset 13.51
#define temperature_offset -2
#define humidity_offset -4

String header;
void setup() {
  Serial.begin(115200);
  bool status;

  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }

  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}
void loop() {
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New Client.");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        header += c;
        if (c == '\n') {
          if (currentLine.length() == 0) {

            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");

            client.println("<style>body { text-align: center; font-family: \"TrebuchetMS\", Arial;background-color:#99ff99}");
            client.println("table { border-collapse: collapse; width:100%; marginleft:auto; margin-right:auto; }");
            client.println("th { padding: 12px; background-color: #ff0000; color: white;}");
            client.println("tr { border: 1px solid #595959; padding: 12px; }");
            client.println("tr:hover { background-color: #ffb84d; }");
            client.println("td { border: none; padding: 12px; }");
            client.println(".sensor { color:white; font-weight: bold; background-color:#ffb84d; padding: 1px; }");

            client.println("</style></head><body><h1>STAZIONE METEO ESP8266/BME280</h1>");
            client.println("<table><tr><th>MISURAZIONE</th><th>VALORE</th></tr>");
            client.println("<tr><td>TEMPERATURA CELSIUS</td><td><span class=\"sensor\">");
            client.println(bme.readTemperature() + temperature_offset);
            client.println(" *C</span></td></tr>");
            client.println("<tr><td>TEMPERATURA FAHRENHEIT</td><td><span class=\"sensor\">");
            client.println(1.8 * bme.readTemperature() + 32 + temperature_offset);
            client.println(" *F</span></td></tr>");
            client.println("<tr><td>PRESSIONE ATMOSFERICA</td><td><span class=\"sensor\">");
            client.println(bme.readPressure() / 100.0F + pressure_offset * 10);
            client.println(" hPa</span></td></tr>");
            client.println("<tr><td>ALTITUDINE APPROSSIMATA</td><td><span class=\"sensor\">");
            client.println(bme.readAltitude(SEALEVELPRESSURE_HPA));
            client.println(" m</span></td></tr>");
            client.println("<tr><td>UMIDITA'</td><td><span class=\"sensor\">");
            client.println(bme.readHumidity() + humidity_offset);
            client.println(" %</span></td></tr>");
            client.println("</body></html>");
            client.println();
            break;
          } else {
            currentLine = "";
          }
        } else if (c != '\r') {
          currentLine += c;
        }
      }
    }
    
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}
