#include <Wire.h>
#include <SimpleDHT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>


//Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire, -1);
//Adafruit_SH1107 display(64, 128, &Wire, -1);
Adafruit_SH1106G display(128, 64, &Wire, -1);
//unsigned long delayTime;

// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//DHT Sensor;

uint8_t DHTPin = 7;
//DHT dht(DHTPin, DHTTYPE);
SimpleDHT22 dht11(DHTPin);  // SimpleDHT11 si utilizamos sensor DHT11
float Temperature;
float Humidity;
float Temp_Fahrenheit;

void setup(void) {

  pinMode(DHTPin, INPUT);

  Serial.begin(57600);
  Serial.println("TEMPERATURA Y HUMEDAD");  

 

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  //display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  // init done

  display.begin(0x3C, true); // Address 0x3C default
  display.display();
  delay(1500);
   // Clear the buffer.
  display.clearDisplay();
  
  display.setRotation(0);    //Rotar Pantalla
  display.drawRoundRect( 1, 18, 125, 29, 4, SH110X_WHITE);
  display.setTextSize(2);
  display.setCursor(5, 26);
  display.setTextColor(SH110X_WHITE);
  display.println("HIGROMETRO");
    
  display.display();
  delay(1500);
  display.clearDisplay();

  //dht.begin();

}

void loop() {
  
  delay(1500);

  int err = SimpleDHTErrSuccess;  
  if ((err = dht11.read2(&Temperature, &Humidity, NULL)) != SimpleDHTErrSuccess) {  //  read2 para obtener los datos con decimales
   Serial.print("Lectura sensor DHT11 failed, err="); Serial.println(err);delay(1000);  
   return;  
  }  

  Serial.print(F("TEMPERATURA: "));
  Serial.print(Temperature, 1);
  Serial.print(F(" °C    "));
  Serial.print(F("HUMEDAD: "));
  Serial.print(Humidity, 1);
  Serial.println(F(" % "));
 
  //Serial.print(Temp_Fahrenheit);
  //Serial.println(F("°F "));
  
 
  // Limpiar buffer
  display.clearDisplay();

  display.setTextSize(1);
  display.setCursor(30,0);
  display.setTextColor(SH110X_WHITE);
  
  display.print("TEMPERATURA: ");
  display.setTextSize(2);
  display.setCursor(30,12);
  display.print(Temperature, 1);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);      // Cargar tabla caracteres ASCII
  display.write(167);       // Escribe el simbolo grados ª 
  display.setTextSize(2);
  display.print("C");
  
  // display humidity
  display.setTextSize(1);
  display.setCursor(40, 35);
  display.print("HUMEDAD: ");
  display.setTextSize(2);
  display.setCursor(30, 47);
  display.print(Humidity, 1);
  display.println(" %");
  
  //display.display();
  //delay(1000);
  //display.display();

  //delay(10);
  //yield();
  display.display();
  //delay(1500);


}