#include "DHT.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <BluetoothSerial.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#define SCREEN_WIDTH 128 // OLED width,  in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Configuración Bluetooth
BluetoothSerial SerialBT;
// librerias y configuraciòn del sensor de temperatura/humedad
#define DHTPIN 4
#define DHTTYPE DHT22
DHT DatosDHT(DHTPIN, DHTTYPE);
// Pines para los LEDs de los entornos a usar
const int luzSala      = 27;
const int LuzComedor   = 25;
const int PuertaGarage = 26;
void setup() {
  // put your setup code here, to run once:
  // initialize OLED display with I2C address 0x3C
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("failed to start SSD1306 OLED"));
    while (1);
  }
// declaro los LED como salida
  pinMode(luzSala, OUTPUT); pinMode(LuzComedor, OUTPUT); pinMode(PuertaGarage, OUTPUT); 
  Serial.begin(115200);
  Serial.println("Hola Mundo de Ramiro Lopera");
  DatosDHT.begin();
  Serial.println("Humedad, Temperatura 'C");
  SerialBT.begin("ESP32_BT_Alexis"); //Bluetooth device name
  oled.setTextColor(WHITE);    // set text color
  oled.clearDisplay(); // clear display
}
void loop() {
  // put your main code here, to run repeatedly:
float humedadDHT = random(10,1000)/10.00; // DatosDHT.readHumidity();
float    TempDHT = random(10,500)/10.00;; //DatosDHT.readTemperature();
  if (SerialBT.available()) {
    char input = SerialBT.read();
    
    switch(input) {
      case '1':
        digitalWrite(luzSala, HIGH);
        break;
      case 'A':
        digitalWrite(luzSala, LOW);
        break;
      case '2':
        digitalWrite(LuzComedor, HIGH);
        break;
      case 'B':
        digitalWrite(LuzComedor, LOW);
        break;
    }
  }
Serial.print(humedadDHT);
Serial.print("%, ");
Serial.print(TempDHT);
Serial.println("");
  oled.clearDisplay(); // clear display
 
  oled.setTextSize(1);         // set text size
  oled.setCursor(2, 5);       // set position to display (x,y)
  oled.print("Temp: ");
  oled.setTextSize(2);         // set text size
  oled.print(TempDHT); // set text
  oled.setTextSize(1);  oled.print((char)247);
  oled.setTextSize(2);  oled.print("C ");
  
  oled.setTextSize(1);         // set text size
  oled.setCursor(2, 30);       // set position to display (x,y)
  oled.print("Hum: ");
  oled.setTextSize(2);         // set text size
  oled.print(humedadDHT); // set text
  oled.print("% ");
  oled.display();              // display on OLED
delay(2000);
}