#include "DHTesp.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <Wire.h>
#define TFT_DC 2
#define TFT_CS 15
Adafruit_FT6206 ctp = Adafruit_FT6206();
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define BLACK 0X0000
#define BLUE 0X001F
#define SOFTBLUE 0x6C9C
#define RED 0XF800
#define SOFTRED 0XF40C
#define GREEN 0X07E0
#define SOFTGREEN 0X05D0
#define DARK_GREEN 0x0BA6
#define CYAN 0X07FF
#define MAGENTA 0XF81F
#define YELLOW 0XFFE0
#define WHITE 0XFFFF
#define ORANGE 0xFD20
#define DARKORANGE 0xFB60
#define MAROON 0x7800
int BtnGreenX = 30;
int BtnGreenY = 30;
int BtnRedX = 200;
int BtnRedY = 30;
#define LED 25
#define DHTPIN 32
DHTesp dhtSensor;
int h;
float t;
float f;
float hif;
float hic;
//----------------------------------------The x and y points for the Temperature bar
int x_bar_t = 20;
int y_bar_t = 60;
//----------------------------------------The variable to hold the conversion value from the temperature value to the value for the temperature bar
int T_to_Bar;
//----------------------------------------Menu = 0 to display the Main Menu Display, Menu = 1 to control the LED and Menu = 2 to display DHT11 sensor data
int Menu = 0;
//----------------------------------------Variable for the x, y and z points on the touch screen
int x;
int y;
char currentPage = 0;
unsigned long previousMillis = 0;
const long interval = 2000;
void setup()
{
Serial.begin(115200);
tft.begin();
tft.setRotation(3);
if(!ctp.begin(40))
{
while(1);
}
dhtSensor.setup(DHTPIN, DHTesp::DHT22);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
currentPage = '0';
Menu_display();
}
void loop()
{
if(!ctp.touched())
{
delay(10);
return;
}
if(Menu == 0)
{
TS_Point p = ctp.getPoint();
x = 320 - p.y;
y = p.x;
if(x > 17 && x < (17+280) && y > 90 && y < (90+40))
{
Menu = 1;
DrawButtonControlLEDPress();
delay(150);
DrawButtonControlLED();
delay(500);
tft.fillScreen(WHITE);
delay(500);
DrawButtonGreen(BtnGreenX,BtnGreenY);
DrawButtonRed(BtnRedX, BtnRedY);
if(digitalRead(LED) == 0)
{
tft.setTextSize(3);
tft.setTextColor(BLUE, WHITE);
tft.setCursor(100, 160);
tft.print("LED OFF");
}
else if(digitalRead(LED) == 1)
{
tft.setTextSize(3);
tft.setTextColor(BLUE, WHITE);
tft.setCursor(100, 160);
tft.print("LED ON");
}
DrawButtonBack(10, 200);
}
if(x > 17 && x < (17+280) && y > 160 && y < (160+40))
{
Menu = 2;
DrawButtonTempHumPress();
delay(150);
DrawButtonTempHum();
delay(500);
tft.fillScreen(WHITE);
delay(500);
tft.drawLine(15, 40, 300, 40, MAROON);
tft.drawLine(15, 39, 300, 39, MAROON);
tft.setTextSize(2);
tft.setTextColor(BLUE);
tft.setCursor(40, 13);
tft.print("Temperature & Humidity");
draw_bar(x_bar_t, y_bar_t);
tft.drawLine(190, 60, 190, 227, MAROON);
tft.drawLine(190, 127, 300, 127, MAROON);
tft.fillRect(202, 60, 100, 27, CYAN);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(205, 65);
tft.print("Humidity");
tft.fillRect(202, 140, 100, 43, GREEN);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(227, 145);
tft.print("Heat");
tft.setCursor(220, 165);
tft.print("Index");
DrawButtonBack(8, 6);
GetDHT22Data();
delay(1000);
}
}
if(Menu == 1)
{
TS_Point p = ctp.getPoint();
x = 320 - p.y;
y = p.x;
if(x > BtnGreenX && x < (BtnGreenX+84) && y > BtnGreenY && y < (BtnGreenY+84))
{
tft.setTextSize(3);
tft.setTextColor(BLUE, WHITE);
tft.setCursor(100, 160);
tft.print("LED ON ");
digitalWrite(LED, HIGH);
DrawButtonGreenPress(BtnGreenX, BtnGreenY);
delay(100);
DrawButtonGreen(BtnGreenX,BtnGreenY);
}
if(x > BtnRedX && x < (BtnRedX+84) && y > BtnRedY && y < (BtnRedY+84))
{
tft.setTextSize(3);
tft.setTextColor(BLUE, WHITE);
tft.setCursor(100, 160);
tft.print("LED OFF");
digitalWrite(LED, LOW);
DrawButtonRedPress(BtnRedX, BtnRedY);
delay(100);
DrawButtonRed(BtnRedX, BtnRedY);
}
if(x > 10 && x < (10+30) && y > 200 && y < (200+30))
{
Menu = 0;
DrawButtonBackPress(10, 200);
delay(100);
DrawButtonBack(10, 200);
delay(500);
tft.fillScreen(WHITE);
delay(500);
Menu_display();
}
}
if(Menu == 2)
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
GetDHT22Data();
}
T_to_Bar = map(t, 0.0, 50.0, 108, 0);
tft.fillRect(x_bar_t+8, (y_bar_t+8)+T_to_Bar, 19, 108-T_to_Bar, ORANGE);
tft.fillRect(x_bar_t+8, y_bar_t+8, 19, T_to_Bar, WHITE);
tft.setTextSize(2);
tft.setTextColor(ORANGE, WHITE);
tft.setCursor(75, 100);
tft.print(t);
if (t < 10) tft.print(" ");
tft.setCursor(160, 100);
tft.print((char)247);
tft.println("C");
tft.setCursor(75, 135);
tft.print(f);
if (f < 100) tft.print(" ");
tft.setCursor(160, 135);
tft.print((char)247);
tft.println("F");
tft.setTextSize(3);
tft.setTextColor(CYAN, WHITE);
tft.setCursor(205, 95);
tft.print(h);
tft.print(" %");
tft.setTextSize(1);
tft.setTextColor(GREEN, WHITE);
tft.setCursor(205, 200);
tft.print(hic);
tft.print(" ");
tft.print((char)247);
tft.print("C");
if(hic < 10) tft.print(" ");
tft.setTextSize(1);
tft.setTextColor(GREEN, WHITE);
tft.setCursor(205, 220);
tft.print(hif);
tft.print(" ");
tft.print((char)247);
tft.print("F");
if(hif < 100) tft.print(" ");
TS_Point p = ctp.getPoint();
x = 320 - p.y;
y = p.x;
if(x > 8 && x < (8+30) && y > 6 && y < (6+30))
{
Menu = 0;
DrawButtonBackPress(8, 6);
delay(100);
DrawButtonBack(8, 6);
delay(500);
tft.fillScreen(WHITE);
delay(500);
Menu_display();
}
}
}
//========================================================================DrawButtonControlLED()
void DrawButtonControlLED()
{
tft.fillRoundRect(17, 90, 280, 40, 10, BLACK);
tft.fillRoundRect(19, 92, 276, 36, 10, GREEN);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(65, 103);
tft.print("Control the LED");
}
//========================================================================DrawButtonControlLEDPress()
void DrawButtonControlLEDPress()
{
tft.fillRoundRect(17, 90, 280, 40, 10, BLACK);
}
//========================================================================DrawButtonTempHum()
void DrawButtonTempHum()
{
tft.fillRoundRect(17, 160, 280, 40, 10, BLACK);
tft.fillRoundRect(19, 162, 276, 36, 10, BLUE);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(25, 173);
tft.print("Temperature & Humidity");
}
//========================================================================DrawButtonTempHumPress()
void DrawButtonTempHumPress()
{
tft.fillRoundRect(17, 160, 280, 40, 10, BLACK);
}
//========================================================================DrawButtonGreen(x, y)
void DrawButtonGreen(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
tft.fillRoundRect(xp+2, yp+2, 80, 80, 10, GREEN);
tft.setCursor(xp+22, yp+32);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.println("ON");
}
//========================================================================DrawButtonGreenPress(x, y)
void DrawButtonGreenPress(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
}
//========================================================================DrawButtonRed(x, y)
void DrawButtonRed(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
tft.fillRoundRect(xp+2, yp+2, 80, 80, 10, RED);
tft.setCursor(xp+18, yp+32);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.println("OFF");
}
//========================================================================DrawButtonRedPress(x, y)
void DrawButtonRedPress(int xp, int yp)
{
tft.fillRoundRect(xp, yp, 84, 84, 10, BLACK);
}
//========================================================================DrawButtonBack(x, y)
void DrawButtonBack(int x_btn_back, int y_btn_back)
{
tft.fillRoundRect(x_btn_back, y_btn_back, 30, 30, 5, BLACK);
tft.fillRoundRect(x_btn_back+2, y_btn_back+2, 26, 26, 5, YELLOW);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.setCursor(x_btn_back+7, y_btn_back+7);
tft.print("<");
}
//========================================================================DrawButtonBackPress(x, y)
void DrawButtonBackPress(int x_btn_back, int y_btn_back)
{
tft.fillRoundRect(x_btn_back, y_btn_back, 30, 30, 5, BLACK);
}
//========================================================================GetDHT11Data()
void GetDHT22Data()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
h = data.humidity;
t = data.temperature;
f = data.temperature * (9/5) + 32;
if(isnan(h) || isnan(t) || isnan(f))
{
return;
}
hif = dhtSensor.computeHeatIndex(f, h);
hic = dhtSensor.computeHeatIndex(t, h, false);
}
//========================================================================draw_bar (Temperature Bar)
void draw_bar(int x_bar, int y_bar)
{
tft.fillRoundRect(x_bar, y_bar, 35, 120, 5, BLACK);
tft.fillCircle(x_bar+17, y_bar+140, 30, BLACK);
tft.fillRoundRect(x_bar+4, y_bar+4, 27, 120, 2, WHITE);
tft.fillCircle(x_bar+17, y_bar+140, 25, WHITE);
tft.fillRect(x_bar+8, y_bar+8, 19, 120, DARKORANGE);
tft.fillCircle(x_bar+17, y_bar+140, 21, DARKORANGE);
//tft.fillRect(41, 58, 19, 108, RED);
tft.drawLine(x_bar+37, y_bar+8, x_bar+42, y_bar+8, RED);
tft.setTextSize(1);
tft.setTextColor(RED);
tft.setCursor(x_bar+47, y_bar+4);
tft.println("50");
tft.drawLine(x_bar+37, y_bar+115, x_bar+42, y_bar+115, RED);
tft.setCursor(x_bar+47, y_bar+111);
tft.println("0");
}
//========================================================================Menu_display()
void Menu_display()
{
tft.setTextSize(3);
tft.setTextColor(BLACK);
tft.setCursor(120, 43);
tft.print("MENU");
DrawButtonControlLED();
DrawButtonTempHum();
}