Zoltar358

Untitled

Jan 13th, 2021
677
1 day
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Clock-Temp-Humid-TFT-ESP32
  3.   ==========================
  4.  
  5.   Displays current date and time using ntp sync via internal WI-FI.
  6.   Measures temperature and humidity using DHT22 sensor.
  7.   Data is displayed on KMRTM28028-SPI - 2.8" TFT SPI 240x320.
  8.  
  9.   Circuit:
  10.     Humidity & temperature sensor/module DHT22
  11.        1)  +         --> ESP32 +5V
  12.        2) OUT        --> ESP32 33
  13.        3) GND        --> ESP32 GND
  14.     2.8' TFT SPI 240*320
  15.        1) VCC        --> ESP32 +3.3V
  16.        2) GND        --> ESP32 GND
  17.        3) CS         --> ESP32 15
  18.        4) RESET      --> ESP32  4
  19.        5) D/C        --> ESP32  2
  20.        6) SDI (MOSI) --> ESP32 23
  21.        7) SCK        --> ESP32 18
  22.        8) LED        --> ESP32 +3.3V
  23.        9) SDO (MISO) --> ESP32 19
  24.       10) T_CLK      --> SCK        --> ESP32 18
  25.       11) T_CS       --> ESP32 21
  26.       12) T_DIN      --> SDI (MOSI) --> ESP32 23
  27.       13) T_OUT      --> SDO (MISO) --> ESP32 19
  28.       14) T_IRQ      --> not connected
  29.  
  30.   Created:  2021-01-11 by Zoltar358.
  31.   Modified: 2021-01-13 by Zoltar358.
  32.   Tested on ESP32-WROOM-32D.
  33.  
  34.   Special thanks to XTronical for his video on how to wire up
  35.   ILI9341 TFT LCD to ESP32.
  36.   Give him some love and subscribe to his channel.
  37.     https://www.xtronical.com
  38.     https://youtu.be/rq5yPJbX_uk
  39. */
  40.  
  41. // include the libraries
  42. #include "WiFi.h"
  43. #include "Time.h"
  44. #include "Wire.h"
  45. #include "DHTesp.h"
  46. DHTesp dht;
  47. #include "Ticker.h"
  48. #include "SoftwareSerial.h"     // Serial library
  49. #include "TFT_eSPI.h"           // Hardware-specific library
  50.  
  51. // define fonts
  52. #define FF21 FreeSansBold9pt7b
  53. #define FF22 FreeSansBold12pt7b
  54. #define FF23 FreeSansBold18pt7b
  55. #define FF24 FreeSansBold24pt7b
  56.  
  57. TFT_eSPI tft = TFT_eSPI();     // Invoke custom library
  58.  
  59. static float toKelvin(float fromCelcius) { return fromCelcius + 273.15; };
  60.  
  61. const char* ssid       = "SSID";
  62. const char* password   = "PASSWORD";
  63.  
  64. const char* ntpServer = "pool.ntp.org";
  65. const long  gmtOffset_sec = 3600;
  66. const int   daylightOffset_sec = 3600;
  67.  
  68. // background color
  69. #define TFT_DARKNAVY        0x0004      /*   0,   0, 128 */
  70.  
  71. void setup() {
  72.   tft.init();
  73.   tft.setRotation(2);
  74.   tft.fillScreen(TFT_RED);
  75.   tft.fillRect(3, 3, 234, 314, TFT_DARKNAVY);
  76.  
  77.   // DHT22 Output Pin connection
  78.   dht.setup(33, DHTesp::DHT22); // Connect DHT sensor to GPIO 33
  79.    
  80.   //connect to WiFi
  81.   Serial.printf("Connecting to %s ", ssid);
  82.   WiFi.begin(ssid, password);
  83.   while (WiFi.status() != WL_CONNECTED) {
  84.       delay(500);
  85.       Serial.print(".");
  86.   }
  87.   Serial.println(" CONNECTED");
  88.  
  89.   //init and get the time
  90.   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  91.   plotTime(120, 7);
  92.  
  93.   //disconnect WiFi as it's no longer needed
  94.   WiFi.disconnect(true);
  95.   WiFi.mode(WIFI_OFF);
  96. }
  97.  
  98. // -------------------------------------------------------------------------------------------------
  99.  
  100. void loop() {
  101.   delay(dht.getMinimumSamplingPeriod());
  102.   plotTime(120, 8);
  103.   tft.fillRect(0, 122, 239, 3, TFT_RED);
  104.   plotTemps(120, 130);
  105.   tft.fillRect(0, 245, 239, 3, TFT_RED);
  106.   plotHumid(120, 255);
  107. }
  108.  
  109. // -----------------------------------------------
  110. void plotHumid(int posX, int posY) {
  111.   float humidity = dht.getHumidity();
  112.   String percent = "%";
  113.  
  114.   tft.setTextDatum(1);
  115.   tft.setTextColor(TFT_GREEN, TFT_DARKNAVY);
  116.   tft.drawString("Humidity:", posX, posY, 4);
  117.  
  118.   char bufH[5]; dtostrf(humidity, 2, 1, bufH);
  119.   strcat(bufH, percent.c_str());
  120.   posY += 28;
  121.   tft.setFreeFont(&FF23);
  122.   tft.setTextColor(TFT_ORANGE, TFT_DARKNAVY);
  123.   tft.drawString(bufH, posX, posY);
  124. }
  125. // -----------------------------------------------
  126. void plotTemps(int posX, int posY) {
  127.   float temperature = dht.getTemperature();
  128.   String celcius = " C";
  129.   String fahrenheit = " F";
  130.   String kelvin = " K";
  131.   tft.setTextDatum(1);
  132.   tft.setTextColor(TFT_GREEN, TFT_DARKNAVY);
  133.   tft.drawString("Temperature:", posX, posY, 4);
  134.   tft.setTextColor(TFT_ORANGE, TFT_DARKNAVY);
  135.   char bufT[6]; dtostrf(temperature, 2, 1, bufT);
  136.   strcat(bufT, celcius.c_str());
  137.   //tft.setTextDatum(1);
  138.   tft.setFreeFont(&FF23);
  139.   posY += 28;
  140.   tft.drawString(bufT, posX, posY);
  141.  
  142.   float temperatureF = dht.toFahrenheit(temperature);
  143.   tft.setTextColor(TFT_LIGHTGREY, TFT_DARKNAVY);
  144.   char bufTF[7]; dtostrf(temperatureF, 2, 2, bufTF);
  145.   strcat(bufTF, fahrenheit.c_str());
  146.   tft.setFreeFont(&FF22);
  147.   posY += 33;
  148.   tft.drawString(bufTF, posX, posY);
  149.  
  150.   float temperatureK = temperature + 273.15;
  151.   tft.setTextColor(TFT_LIGHTGREY, TFT_DARKNAVY);
  152.   char bufTK[7]; dtostrf(temperatureK, 2, 2, bufTK);
  153.   strcat(bufTK, kelvin.c_str());
  154.   tft.setFreeFont(&FF22);
  155.   posY += 25;
  156.   tft.drawString(bufTK, posX, posY);
  157. }
  158. // -----------------------------------------------
  159. void plotTime(int posX, int posY) {
  160.   struct tm timeinfo;
  161.   if (!getLocalTime(&timeinfo)) {
  162.     String errorMessage = "Failed to obtain time";
  163.     tft.setTextColor(TFT_RED, TFT_DARKNAVY);
  164.     tft.setFreeFont(&FF21);
  165.     tft.drawString(errorMessage, posX, posY);
  166.     Serial.println("Failed to obtain time");
  167.     return;
  168.   }
  169.   char timeWeekDay[9];
  170.   char fullYear[11];
  171.   char fullTime[9];
  172.  
  173.   strftime(timeWeekDay,9, "%A", &timeinfo);
  174.   strftime(fullYear,11, "%F", &timeinfo);
  175.   strftime(fullTime,9, " %R  ", &timeinfo);
  176.  
  177.   tft.setTextDatum(1);
  178.   tft.setTextColor(TFT_RED, TFT_DARKNAVY);
  179.   tft.setFreeFont(&FF23);
  180.   tft.drawString(timeWeekDay, posX, posY);
  181.   posY += 35;
  182.   tft.setTextColor(TFT_WHITE, TFT_DARKNAVY);
  183.   tft.setFreeFont(&FF22);
  184.   tft.drawString(fullYear, posX, posY);
  185.   posY += 33;
  186.   tft.setTextColor(TFT_ORANGE, TFT_DARKNAVY);
  187.   tft.setFreeFont(&FF24);
  188.   tft.drawString(fullTime, posX, posY);
  189. }
  190. // -----------------------------------------------
RAW Paste Data