#include <PubSubClient.h>
#include <WiFi.h>
#include "DHTesp.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#define BUZZER 13
// pin and instance of the DHT sensor
const int DHT_PIN = 12;
DHTesp dhtSensor;
WiFiClient espClient;
// mqttClient the variable
PubSubClient mqttClient(espClient);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
bool isScheduledON = false;
unsigned long scheduledOnTime;
char tempAr[6];
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup() {
Serial.begin(115200);
setupWifi();
setupMqtt();
//initiate the temperature sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
timeClient.begin();
timeClient.setTimeOffset(5.5*3600);
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop() {
if (!mqttClient.connected()){
connectToBroker();
}
mqttClient.loop();
updateTemperature();
Serial.println(tempAr);
// publish the temperature
mqttClient.publish("ENTC-ADMIN-TEMP", tempAr);
checkSchedule();
delay(1000);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void buzzerOn(bool on){
if (on){
tone(BUZZER,256);
}else{
noTone(BUZZER);
}
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setupMqtt(){
// setting the server
mqttClient.setServer("test.mosquitto.org", 1883);
mqttClient.setCallback(receiveCallback);
}
// receving msg part >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void receiveCallback(char* topic,byte* payload,unsigned int length){
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
// to take the value another variable
char payloadCharAr[length];
for (int i= 0; i<length; i++){
Serial.print((char) payload[i]); // convert bite into char to print
payloadCharAr[i] = (char)payload[i]; // each i will be casted
}
Serial.println(); // new line to print values after the msg
// depending on the on off topic, on off the LED
if(strcmp(topic, "ENTC-ADMIN-MAIN-ON-OFF") == 0){ // strcmp: string compare meethod
// if(payloadCharAr[0]='1'){
// buzzreOn(true);
// }else{
// buzzerOn(false);
// }
// insterd of above if else function we can use
buzzerOn(payloadCharAr[0]='1');
// then we have to subscribe
}else if(strcmp(topic, "ENTC-ADMIN-SCH-ON") == 0){
if(payloadCharAr[0]='N'){ // NOT N the 1st character we send
isScheduledON = false;
}else{
isScheduledON = true;
scheduledOnTime = atol(payloadCharAr); // array to long
}
}
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void connectToBroker(){
while (!mqttClient.connected()){
Serial.println("Attempting MQTT connection...");
if (mqttClient.connect("ESP32-56465466")){
Serial.println("Connected");
// after connected we can subscribe
mqttClient.subscribe("ENTC-ADMIN-MAIN-ON-OFF");
mqttClient.subscribe("ENTC-ADMIN-SCH-ON");
}else{
Serial.print("failed");
Serial.print(mqttClient.state()); // give the state that connected or not
delay(5000);
}
}
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void updateTemperature(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String(data.temperature,2).toCharArray(tempAr,6);
// fectch the temp value and convert into 2 decimal string
// only get char array with 6 values
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setupWifi(){
Serial.println();
Serial.print("Connecting to ");
Serial.println("Wokwi-GUEST");
// connet to the wifi
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected");
Serial.println("IP Address: ");
// get the IP address
Serial.println(WiFi.localIP());
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
unsigned long getTime(){
timeClient.update();
return timeClient.getEpochTime();
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void checkSchedule(){
if(isScheduledON){
unsigned long currentTime = getTime();
if (currentTime > scheduledOnTime){
buzzerOn(true);
isScheduledON = false;
mqttClient.publish("ENCT-ADMIN-MAIN-ON-OFF-ESP", "1"); // then sink
mqttClient.publish("ENCT-ADMIN-SCH-ESP-ON", "0");
Serial.println("Scheduled ON");
}
}
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>