Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Clock-Temp-Humid-TFT-ESP32
- ==========================
- Displays current date and time using ntp sync via internal WI-FI.
- Measures temperature and humidity using DHT22 sensor.
- Data is displayed on KMRTM28028-SPI - 2.8" TFT SPI 240x320.
- Circuit:
- Humidity & temperature sensor/module DHT22
- 1) + --> ESP32 +5V
- 2) OUT --> ESP32 33
- 3) GND --> ESP32 GND
- 2.8' TFT SPI 240*320
- 1) VCC --> ESP32 +3.3V
- 2) GND --> ESP32 GND
- 3) CS --> ESP32 15
- 4) RESET --> ESP32 4
- 5) D/C --> ESP32 2
- 6) SDI (MOSI) --> ESP32 23
- 7) SCK --> ESP32 18
- 8) LED --> ESP32 +3.3V
- 9) SDO (MISO) --> ESP32 19
- 10) T_CLK --> SCK --> ESP32 18
- 11) T_CS --> ESP32 21
- 12) T_DIN --> SDI (MOSI) --> ESP32 23
- 13) T_OUT --> SDO (MISO) --> ESP32 19
- 14) T_IRQ --> not connected
- Created: 2021-01-11 by Zoltar358.
- Modified: 2021-01-13 by Zoltar358.
- Tested on ESP32-WROOM-32D.
- Special thanks to XTronical for his video on how to wire up
- ILI9341 TFT LCD to ESP32.
- Give him some love and subscribe to his channel.
- https://www.xtronical.com
- https://youtu.be/rq5yPJbX_uk
- */
- // include the libraries
- #include "WiFi.h"
- #include "Time.h"
- #include "Wire.h"
- #include "DHTesp.h"
- DHTesp dht;
- #include "Ticker.h"
- #include "SoftwareSerial.h" // Serial library
- #include "TFT_eSPI.h" // Hardware-specific library
- // define fonts
- #define FF21 FreeSansBold9pt7b
- #define FF22 FreeSansBold12pt7b
- #define FF23 FreeSansBold18pt7b
- #define FF24 FreeSansBold24pt7b
- TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
- static float toKelvin(float fromCelcius) { return fromCelcius + 273.15; };
- const char* ssid = "SSID";
- const char* password = "PASSWORD";
- const char* ntpServer = "pool.ntp.org";
- const long gmtOffset_sec = 3600;
- const int daylightOffset_sec = 3600;
- // background color
- #define TFT_DARKNAVY 0x0004 /* 0, 0, 128 */
- void setup() {
- tft.init();
- tft.setRotation(2);
- tft.fillScreen(TFT_RED);
- tft.fillRect(3, 3, 234, 314, TFT_DARKNAVY);
- // DHT22 Output Pin connection
- dht.setup(33, DHTesp::DHT22); // Connect DHT sensor to GPIO 33
- //connect to WiFi
- Serial.printf("Connecting to %s ", ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println(" CONNECTED");
- //init and get the time
- configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
- plotTime(120, 7);
- //disconnect WiFi as it's no longer needed
- WiFi.disconnect(true);
- WiFi.mode(WIFI_OFF);
- }
- // -------------------------------------------------------------------------------------------------
- void loop() {
- delay(dht.getMinimumSamplingPeriod());
- plotTime(120, 8);
- tft.fillRect(0, 122, 239, 3, TFT_RED);
- plotTemps(120, 130);
- tft.fillRect(0, 245, 239, 3, TFT_RED);
- plotHumid(120, 255);
- }
- // -----------------------------------------------
- void plotHumid(int posX, int posY) {
- float humidity = dht.getHumidity();
- String percent = "%";
- tft.setTextDatum(1);
- tft.setTextColor(TFT_GREEN, TFT_DARKNAVY);
- tft.drawString("Humidity:", posX, posY, 4);
- char bufH[5]; dtostrf(humidity, 2, 1, bufH);
- strcat(bufH, percent.c_str());
- posY += 28;
- tft.setFreeFont(&FF23);
- tft.setTextColor(TFT_ORANGE, TFT_DARKNAVY);
- tft.drawString(bufH, posX, posY);
- }
- // -----------------------------------------------
- void plotTemps(int posX, int posY) {
- float temperature = dht.getTemperature();
- String celcius = " C";
- String fahrenheit = " F";
- String kelvin = " K";
- tft.setTextDatum(1);
- tft.setTextColor(TFT_GREEN, TFT_DARKNAVY);
- tft.drawString("Temperature:", posX, posY, 4);
- tft.setTextColor(TFT_ORANGE, TFT_DARKNAVY);
- char bufT[6]; dtostrf(temperature, 2, 1, bufT);
- strcat(bufT, celcius.c_str());
- //tft.setTextDatum(1);
- tft.setFreeFont(&FF23);
- posY += 28;
- tft.drawString(bufT, posX, posY);
- float temperatureF = dht.toFahrenheit(temperature);
- tft.setTextColor(TFT_LIGHTGREY, TFT_DARKNAVY);
- char bufTF[7]; dtostrf(temperatureF, 2, 2, bufTF);
- strcat(bufTF, fahrenheit.c_str());
- tft.setFreeFont(&FF22);
- posY += 33;
- tft.drawString(bufTF, posX, posY);
- float temperatureK = temperature + 273.15;
- tft.setTextColor(TFT_LIGHTGREY, TFT_DARKNAVY);
- char bufTK[7]; dtostrf(temperatureK, 2, 2, bufTK);
- strcat(bufTK, kelvin.c_str());
- tft.setFreeFont(&FF22);
- posY += 25;
- tft.drawString(bufTK, posX, posY);
- }
- // -----------------------------------------------
- void plotTime(int posX, int posY) {
- struct tm timeinfo;
- if (!getLocalTime(&timeinfo)) {
- String errorMessage = "Failed to obtain time";
- tft.setTextColor(TFT_RED, TFT_DARKNAVY);
- tft.setFreeFont(&FF21);
- tft.drawString(errorMessage, posX, posY);
- Serial.println("Failed to obtain time");
- return;
- }
- char timeWeekDay[9];
- char fullYear[11];
- char fullTime[9];
- strftime(timeWeekDay,9, "%A", &timeinfo);
- strftime(fullYear,11, "%F", &timeinfo);
- strftime(fullTime,9, " %R ", &timeinfo);
- tft.setTextDatum(1);
- tft.setTextColor(TFT_RED, TFT_DARKNAVY);
- tft.setFreeFont(&FF23);
- tft.drawString(timeWeekDay, posX, posY);
- posY += 35;
- tft.setTextColor(TFT_WHITE, TFT_DARKNAVY);
- tft.setFreeFont(&FF22);
- tft.drawString(fullYear, posX, posY);
- posY += 33;
- tft.setTextColor(TFT_ORANGE, TFT_DARKNAVY);
- tft.setFreeFont(&FF24);
- tft.drawString(fullTime, posX, posY);
- }
- // -----------------------------------------------
RAW Paste Data