#include <PubSubClient.h>
#include <WiFi.h>
#include "DHTesp.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#define DHT_PIN 15
#define BUZZER 12
char tempAr[6];
WiFiClient espClient;
PubSubClient mqttClient(espClient);
DHTesp dhtSensor;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
bool isScheduledON = false;
unsigned long scheduledOnTime;
void setup() {
Serial.begin(115200);
setupWifi();
setupMqtt();
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);
mqttClient.publish("ENTC-ADMIN-TEMP123", tempAr);
checkSchedule();
delay(1000);
}
void buzzerOn(bool on){
if(on){
tone(BUZZER, 256);
}else{
noTone(BUZZER);
}
}
void setupWifi(){
Serial.println();
Serial.print("Connecting to ");
Serial.println("Wokvi-GUEST");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void updateTemperature(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String(data.temperature,2).toCharArray(tempAr,6); //data.temperature is of double type
}
void setupMqtt(){
mqttClient.setServer("test.mosquitto.org",1883);
mqttClient.setCallback(receiveCallback); //to handle incoming messages
}
void receiveCallback(char* topic, byte* payload, unsigned int length){
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]");
char payloadCharAr[length]; //varibale to store incoming data
for (int i=0; i <length;i++){
Serial.print((char)payload[i]);
payloadCharAr[i] = (char)payload[i];
}
Serial.println();
if(strcmp(topic, "ENTC-ADMIN-MAIN-ON-OFF123")==0){
/*if(payloadCharAr[0]=='1'){
buzzerOn(true);
}else{
buzzerOn(false);
}*/
buzzerOn(payloadCharAr[0]=='1'); //same as above code
}else if(strcmp(topic, "ENTC-ADMIN-SCH-ON123")==0){
if(payloadCharAr[0]=='N'){
isScheduledON = false;
}else{
isScheduledON = true;
scheduledOnTime = atol(payloadCharAr); //converts array elemets to long
}
}
}
void connectToBroker(){
while(!mqttClient.connected()){
Serial.println("Attempting MQTT connection.....");
//if we don't want authentication, we can have any random name
if(mqttClient.connect("ESP32-665959999")){
Serial.println("Connected");
mqttClient.subscribe("ENTC-ADMIN-MAIN-ON-OFF123");
mqttClient.subscribe("ENTC-ADMIN-SCH-ON123");
}else{
Serial.println("Failed");
Serial.print(mqttClient.state());
delay(5000);
}
}
}
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("ENTC-ADMIN-MAIN-ON-OFF-ESP-123", "1");
mqttClient.publish("ESP-ENTC-ADMIN-SCH-ON123", "0");
Serial.println("Scheduled ON");
}
}
}