// Modulo Sensor KY-001   
// Family DS18B20 Device

//#include <SPI.h>
//#include <Wire.h>
#include <OneWire.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;
 
#define DS18B20_PIN 2         // declaracion que funciona
OneWire ds(DS18B20_PIN) ;     // setup_tempsense () 
byte addr[8];                 //


// DS18S20 Temperature chip i/o
//OneWire ds(2);  // on pin 2
//#define MAX_DS1820_SENSORS 1
//byte addr[MAX_DS1820_SENSORS][8];

float Temperatura;
float Humidity;
float Fract; 
float degC;

void setup_tempsense ()
{
  
  if (!ds.search (addr))
  {
    Serial.println("No more addresses.") ;
    ds.reset_search () ;
    delay (250) ;
    return ;
  }
  
  Serial.print ("R=") ;
  for (byte i = 0 ; i < 8 ; i++)
  {
    Serial.print (addr[i], HEX) ;
    Serial.print (" ") ;
  }
  Serial.println () ;
  long unique = 0L ;
  for (byte i = 6 ; i >= 1 ; i--)
  {
    unique = (unique << 8) | addr [i] ;
  }
  Serial.print ("Unique=") ; Serial.println (unique, HEX) ;
    
  if (OneWire::crc8 (addr, 7) != addr[7])
  {
    Serial.println ("CRC is not valid!") ;
    return ;
  }
  
  /*
  if ( addr[0] == 0x10 || addr[0] == 0x28 ) {
      Serial.print("Device is a DS18S20 family device.\n");
  }
  else {
      Serial.print("Device family is not recognized: 0x");
      Serial.println(addr[0],HEX);
      return;
  }
  */

  if (addr[0] != 0x10)    //if (addr[0] != 0x28)  
  {
    Serial.println ("Device is not a DS18B20 family device.") ;
    return ;
  }
 
 int temp = read_temp () ;
 Serial.print (((float) temp) * 0.0625) ;
 Serial.println (" ºC") ;

}

int start_conversion ()
{
  // The DallasTemperature library can do all this work for you!

  ds.reset () ;
  ds.select (addr) ;
  ds.write (0x44) ;         // start conversion, with parasite power on at the end
}


int read_conversion_result ()
{
  byte present = ds.reset () ;
  ds.select (addr) ;    
  ds.write (0xBE) ;         // Read Scratchpad

  byte data[12];
  for (byte i = 0 ; i < 9 ; i++)           // we need 9 bytes
  {
    data[i] = ds.read();
  }
  int temp = (data [1] << 8) | data[0] ;
  return temp ;
}

int read_temp ()
{
  start_conversion () ;
  delay (800) ;     // maybe 750ms is enough, play safe
  return read_conversion_result () ;
}







void setup() {

  // initialize inputs/outputs

  Serial.begin (57600) ;
  
  setup_tempsense () ;

  display.begin(0x3C, true); // Address 0x3C default
 
   // 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("TERMOMETRO");
    
  display.display();
  delay(1500);
  
  

}


void oled() {


  // 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(Temperatura);
  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);
  display.println(" %");
  
  
  display.display();
  //delay(500);


}


void sensor() {


  
  //For conversion of raw data to C
  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
 
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
 
 //Find all devices connected on oneWire Bus
  if ( !ds.search(addr)) {
      Serial.print("No more addresses.\n");
      ds.reset_search();
      return;
  }
 
  Serial.print("R=");
  for( i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }
 
  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }
 
  if ( addr[0] == 0x10) {
      Serial.print("Device is a DS18S20 family device.\n");
  }
  else if ( addr[0] == 0x28) {
      Serial.print("Device is a DS18B20 family device.\n");
  }
  else {
      Serial.print("Device family is not recognized: 0x");
      Serial.println(addr[0],HEX);
      return;
  }
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
 
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratch pad
 
  Serial.print("P=");
  Serial.print(present,HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print( OneWire::crc8( data, 8), HEX);
  Serial.println();
 
  //Conversion of raw data to C
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
 
  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;
 
  Serial.print("Temprature:");
  if (SignBit) // If its negative
  {
     Serial.print("-");
  }
  Serial.print(Whole);
  Serial.print(".");
  if (Fract < 10)
  {
     Serial.print("0");
  }
  Serial.print(Fract);
 
  Serial.print("\n");
  //End conversion to C

  

}
 
void loop(void) {
   
  //sensor();
  //Temperatura = Fract;
  
  float degC = (float) read_temp () ;
  degC *= 0.0625 ;
  Serial.print (degC) ;
  Serial.println (" ºC") ;
  delay (1000) ;
  Temperatura = degC;
  oled();
  
  


}