// #include <WiFi.h>
// #include <PubSubClient.h>
// const char* ssid = "Wokwi-GUEST";
// const char * pw = "";
// const char* mqtt_sv = "broker.mqtt-dashboard.com";
// const int mqtt_port = 1883;
// WiFiClient espClient;
// PubSubClient client(espClient);
// void connectWF() {
// WiFi.begin(ssid, pw, 6);
// Serial.println();
// while (WiFi.status() != WL_CONNECTED) {
// Serial.print(".");
// delay(500);
// }
// Serial.print("Connected to ");
// Serial.println(WiFi.localIP());
// }
// void connectMQTTsv() {
// while(!client.connect("26-6-24-09-01"));
// delay(500);
// Serial.println("Connected to MQTT server");
// client.subscribe("/MinAh/LED");
// }
// void redirect(char* topic, char* context) {
// Serial.println("Redireting...");
// if (strcmp(topic, "/MinAh/LED") == 0)
// led_management(context);
// }
// void mqttCallback(char* topic, byte* payload, unsigned int length) {
// payload[length] = 0;
// Serial.print(topic);
// Serial.print(" - ");
// Serial.println((char*)payload);
// redirect(topic, (char*)payload);
// }
// void setup() {
// Serial.begin(115200);
// pinMode(led, OUTPUT);
// connectWF();
// client.setServer(mqtt_sv, mqtt_port);
// client.setCallback(mqttCallback);
// connectMQTTsv();
// }
// void loop() {
// if (!client.connected())
// connectMQTTsv();
// client.loop();
// }
//------------------------------------------------------------------------------
#define forLoop(x, n) for(int x = 0; x < n; x++)
#include <DHT.h>
#include <Wire.h>
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#define DHTTYPE DHT22
const int button = 5;
const int dht_pin = 18;
const int potentio = 12;
const int simu_flame = 19;
const int piezo = 13;
const int relay = 14;
const int servo_pin = 17;
DHT dht22(dht_pin, DHTTYPE);
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
Servo servo;
void setup() {
Serial.begin(9600);
dht22.begin();
pinMode(button, INPUT);
pinMode(potentio, INPUT);
pinMode(simu_flame, INPUT);
LCD.init();
LCD.backlight();
pinMode(relay, OUTPUT);
pinMode(piezo, OUTPUT);
servo.attach(servo_pin);
}
bool screen = true;
int last_button_state = LOW;
unsigned long debounce = 50;
unsigned long last_debounce = millis();
float dht_humidity = 0, dht_temperature = 0;
unsigned long dht_delay = 2000;
unsigned long dht_previous = millis();
void LCD_display(bool screen, bool renew) {
if (screen)
LCD.display();
else {
LCD.noDisplay();
return;
}
if (!renew)
return;
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Nhiet do: ");
LCD.print(dht_temperature);
LCD.print("C");
LCD.setCursor(0, 1);
LCD.print("Do am: ");
LCD.print(dht_humidity);
LCD.print("%");
}
void spin(int val) {
int ang = map(val, 0, 4095, 0, 180);
servo.write(ang);
delay(15);
}
void check_flame() {
int flame = digitalRead(simu_flame);
if (flame) { // Replicate fire occur
digitalWrite(relay, LOW);
tone(piezo, 1000);
}
else {
digitalWrite(relay, HIGH);
noTone(piezo);
}
}
void clicking() {
int val_button = digitalRead(button);
bool clicked = false;
if (val_button == HIGH && last_button_state == LOW && millis() - last_debounce > debounce) {
clicked = true;
}
last_button_state = val_button;
if (clicked)
screen = !screen;
}
bool rerun_dht() {
int val_poten = analogRead(potentio);
bool renew = false;
dht_delay = map(val_poten, 0, 4095, 1500, 6000);
if (millis() - dht_previous > dht_delay) {
dht_previous = millis();
dht_humidity = dht22.readHumidity();
dht_temperature = dht22.readTemperature();
renew = true;
}
return renew;
// return val_poten; // temporary for servo
}
void loop() {
check_flame(); // DigitalRead
clicking(); // DigitalRead
bool renew = rerun_dht(); // AnalogRead
int val_poten = map(analogRead(potentio), 0, 4095, 0, 180);
spin(val_poten); // Add something to replicate dusk sensor
LCD_display(screen, renew);
}