#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <U8g2lib.h>
//#include "ESPAsyncWebSrv.h"
//#include <DHT.h>
//#include <time.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int LED_BUILT = 4;

const float BETA = 3950; // should match the Beta Coefficient of the thermistor

// Create AsyncWebServer object on port 80
//AsyncWebServer server(80);
/*=======================================================================================
                                  Relay Definitions
========================================================================================*/

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

  pinMode(15,INPUT);
  analogReadResolution(10);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20, 10);
  // Display static text
  display.println("Hello, world!");
  display.display(); 
}

void loop() {
  //Assumes a 10K@25℃ NTC thermistor connected in series with a 10K resistor.
     int analogValue = analogRead(15);
  float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
  Serial.print("Temperature: ");
  Serial.print(celsius);
  Serial.println("C");
  digitalWrite(LED_BUILT, HIGH);
   display.clearDisplay();
   display.setCursor(0,0);  
  display.println("Temperature: ");
  display.println(celsius);
  display.println(" ℃");
  display.display();
  delay(500);  
  digitalWrite(LED_BUILT, LOW);
  delay(500);  
}