/*
ESP32 + ILI9341 LCD Basic Example
https://wokwi.com/projects/325324981270479442
*/
#include <WiFi.h>
#include <ThingSpeak.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "DHTesp.h"
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char pass[] = ""; // your network password
WiFiClient client;
unsigned long myChannelNumber = 2153855;
const char * myWriteAPIKey = "1LVM9IPG37Q9NVFW";
const int DHT_PIN = 33;
const int DELAY_UPDATE = 1000;
uint32_t timerUpdate = 0;
DHTesp dhtSensor;
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define gas_pin 36
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
tft.begin();
tft.setCursor(20, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
pinMode(gas_pin, INPUT);
ThingSpeak.begin(client);
tft.println("Home Automation ");
tft.setCursor(80, 160);
}
float t, h;
void loop() {
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
int gas_value=analogRead(gas_pin);
if (gas_value<700)
{
tft.setCursor(20, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Alert");
delay(6000);
}
if (millis() - timerUpdate >= DELAY_UPDATE) {
timerUpdate = millis();
TempAndHumidity data = dhtSensor.getTempAndHumidity();
//Serial.println("\x1b[2J\x1b[;H");
t = data.temperature;
h = data.humidity;
Serial.printf(" Temperature: %.2f %s", t, "°C");
Serial.printf(" Humidity: %.1f %s", h, "%\n");
}
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
ThingSpeak.setField(3, gas_value);
int x=ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x==200){
Serial.println("Data is successfully uploaded");
}
else{
Serial.println("Data is NOT successfully uploaded");
}
tft.setCursor(10, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Temp: ");
tft.setCursor(80, 160);
//tft.setTextColor(ILI9341_BLACK);
tft.println(t);
tft.setCursor(10, 240);
tft.println("Hum: ");
tft.setTextColor(ILI9341_GREEN);
tft.setCursor(80, 240);
tft.println(h);
delay(500);
tft.fillScreen(0x0000);
}