/*
  ESP32  DALLAS 18B20 to TFT HEHUI MODULE 09-10-2023
*/
//==============================================================
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>             // Arduino SPI library

#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 14 on the Esp32
#define ONE_WIRE_BUS 14
// Setup a oneWire instance to communicate.
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int cel;
int far;
//======= For HEHUI Smart Display Scherm Lcd Tft Module =======
// ST7789 TFT module connections
#define TFT_CS  15
#define TFT_DC   2
#define TFT_RST  4
#define TFT_SCL 18
#define TFT_SDA 23
// Initialize Adafruit ST7789 TFT library
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
//==============================================================
const int tempPin = 14;  // LM35 is connected to GPIO 34

void setup() {
 Serial.begin(115200);
 delay(100);
 Serial.println("");
 Serial.println("ESP32 DALLAS 18B20 to TFT HEHUI MODULE 09-10-2023");
 Serial.println("");
 // if the display has CS pin try with SPI_MODE0
tft.init(240, 320, SPI_MODE2);    // Init ST7789 display 240x320 pixel
// if the screen is flipped, remove this command
tft.setRotation(0);
tft.fillScreen(ST77XX_RED);
tft.invertDisplay(false);
tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(1);
tft.setCursor(30, 10);
tft.println("18B20 is connected to GPIO 14");   
delay(2000);

  sensors.begin(); // Start up the 18B20 library
}
void loop() {
 sensors.requestTemperatures(); // Send the command to get temperatures
 cel = sensors.getTempCByIndex(0) / 10 * 10;
 Serial.print("Temp:");
 Serial.print(cel);
 Serial.print(" *C");
 Serial.print("   ");

 far = sensors.getTempFByIndex(0) / 10 * 10;
 Serial.print("Temp:");
 Serial.print(far);
 Serial.println(" F");
 Serial.println(" ");

 tft.fillScreen(ST77XX_BLACK);
tft.invertDisplay(false);

 tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(1);
tft.setCursor(0, 10);
tft.println("The 18B20 sensor is connected to GPIO 14");
 
 tft.setTextColor(ST77XX_GREEN);
tft.setTextSize(6);
tft.setCursor(30, 50);
tft.print("Temp.");  
tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(5);
tft.setCursor(20, 115);
tft.print(cel);  
tft.setCursor(160, 130);
 tft.setTextSize(3);
tft.setTextColor(ST77XX_GREEN);
tft.print("Gr/c");

tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(5);
tft.setCursor(20, 175);
tft.print(far);  
tft.setCursor(160, 190);
 tft.setTextSize(3);
tft.setTextColor(ST77XX_GREEN);
tft.print("F");
delay(25000);
}