/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include <WiFi.h>
#include "SPI.h"
#include "DHT.h"
#include "time.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <ESP32Servo.h>
#define TFT_DC 21
#define TFT_CS 22
#define DHPIN 2
#define DHTTYPE DHT22
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
DHT dht(DHPIN, DHTTYPE);
Servo myservo;
const char* ssid = "Wokwi-GUEST";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
const int pinR = 12;
const int pinG = 27;
const int pinB = 14;
int angle = 0;
void setup() {
struct tm timeinfo;
dht.begin();
tft.begin();
WiFi.begin(ssid);
tft.setCursor(20, 120);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
while(!getLocalTime(&timeinfo)){
tft.print("Connecting...");
}
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
myservo.attach(13);
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
}
void setColor(int Rvalue, int Gvalue, int Bvalue){
analogWrite(pinR, Rvalue);
analogWrite(pinG, Gvalue);
analogWrite(pinB, Bvalue);
}
void loop() {
struct tm timeinfo;
getLocalTime(&timeinfo);
tft.fillScreen(ILI9341_BLACK);
double t = dht.readTemperature();
double h = dht.readHumidity();
tft.setCursor(20, 120);
tft.print("Time: ");
tft.print(&timeinfo, "%H:%M:%S");
tft.setCursor(20, 160);
tft.print("temperature:"+ String(t)+"C");
tft.setCursor(20, 200);
tft.print("Humidity:"+ String(h)+"%");
if(t >= 50){
setColor(map(t, -40, 80, 0, 255), 0 , 0);
angle += 60;
myservo.write(angle);
if(angle == 180) angle = 0;
}
delay(100);
}