#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_9077874C"; // yourStudentID must be unique
int PORTNUM = 1883;
void setup_wifi()
{
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED )
{
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Given IP by the router to ESP32 is ");
Serial.println(WiFi.localIP());
}
#define DHTPIN 4
#define DHTTYPE DHT22
#define LED_PIN 27
#define LED1_PIN 25
#define BUZZER_PIN 26
#define TEMP_LOW 23
#define TEMP_HIGH 25
#define HUMI_LOW 40
#define HUMI_HIGH 60
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(LED1_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
delay(5000);
// Init
lcd.init();
lcd.backlight();
// Print something
lcd.setCursor(4, 0);
lcd.print("T&H Fire");
lcd.setCursor(2, 1);
lcd.print("Alarm System");
delay(1000);
lcd.clear();
setup_wifi();
setup_MQTT();
}
void loop() {
float temp = dht.readTemperature();
float humi = dht.readHumidity();
Serial.print("Temp: ");
Serial.print(temp);
Serial.println("'C");
Serial.print("Humidity: ");
Serial.print(humi);
Serial.println("%");
Serial.println("---");
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("'C");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(humi);
lcd.print(" %");
delay(1000);
if(temp > TEMP_HIGH)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("High Temp");
lcd.setCursor(0, 1);
lcd.print("Need Cooling");
delay(1000);
lcd.clear();
if (humi < HUMI_LOW)
{
digitalWrite(LED_PIN, HIGH); // Turn on Red LED
tone(BUZZER_PIN, 1000); // Turn on buzzer
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LowHumi");
lcd.setCursor(0, 1);
lcd.print("Need to Humdify");
delay(1000);
digitalWrite(LED_PIN, LOW); // Turn off Red LED
noTone(BUZZER_PIN); // Turn off buzzer
lcd.clear();
}
else if (humi > HUMI_HIGH)
{
digitalWrite(LED_PIN, HIGH); // Turn on Red LED
tone(BUZZER_PIN, 1000); // Turn on buzzer
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("High Humi");
lcd.setCursor(0, 1);
lcd.print("Need to Dehumdify");
delay(1000);
digitalWrite(LED_PIN, LOW); // Turn off Red LED
noTone(BUZZER_PIN); // Turn off buzzer
lcd.clear();
}
}
else if(temp < TEMP_LOW)
{
digitalWrite(LED1_PIN, HIGH); // Turn on Blue LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Low Temp");
lcd.setCursor(0, 1);
lcd.print("Beware Cold");
delay(1000);
digitalWrite(LED1_PIN, LOW); // Turn off Blue LED
lcd.clear();
if(humi < HUMI_LOW)
{
digitalWrite(LED1_PIN, HIGH); // Turn on Blue LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Low Humidity");
lcd.setCursor(0, 1);
lcd.print("Need to Humdify");
delay(1000);
digitalWrite(LED1_PIN, LOW); // Turn off Blue LED
lcd.clear();
}
else if(humi > HUMI_HIGH)
{
digitalWrite(LED1_PIN, HIGH); // Turn on Blue LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("High Humi");
lcd.setCursor(0, 1);
lcd.print("Need to Dehumdify");
delay(1000);
digitalWrite(LED1_PIN, LOW); // Turn off Blue LED
lcd.clear();
}
}
else if(temp > TEMP_LOW & temp < TEMP_HIGH)
{
if(humi < HUMI_LOW)
{
digitalWrite(LED1_PIN, HIGH); // Turn on Blue LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Low Humidity");
lcd.setCursor(0, 1);
lcd.print("Drink Water");
delay(1000);
digitalWrite(LED1_PIN, LOW); // Turn off Blue LED
lcd.clear();
}
else if(humi > HUMI_HIGH)
{
digitalWrite(LED1_PIN, HIGH); // Turn on Blue LED
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Need to Humdify");
lcd.setCursor(0, 1);
lcd.print("Need to Dehumdify");
delay(1000);
digitalWrite(LED1_PIN, LOW); // Turn off Blue LED
lcd.clear();
}
}
}
void connectMQTT()
{
while(!client.connected() )
{
Serial.println("Connecting to MQTT ...");
if (client.connect(espClientName) ) //, mqttUser, mqttPassword) )
{
Serial.println("Connected");
MQTTSubscribe();
}
else
{
Serial.print("Failed with state ");
Serial.print(client.state() );
delay(2000);
}
}
}
void callback(String topic, byte* payload, unsigned int length)
{
String messageTemp;
Serial.print("Message received in topic: ");
Serial.print(topic);
Serial.print(" length is: ");
Serial.println(length);
Serial.print("Data received from broker: ");
for (int i = 0; i<length; i++)
{
Serial.print( (char)payload[i] );
messageTemp += (char)payload[i];
}
if (topic == "9077874C/ROOM/IOT")
{
Serial.print("ADC value received back = ");
Serial.println(messageTemp);
}
}
void MQTTSubscribe()
{
client.subscribe("9077874C/room/analogADC");
}
void setup_MQTT()
{
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}