#include <WiFi.h>
#include <PubSubClient.h>
#include <DHTesp.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#define DHT_PIN 18
#define BUZZER 12
#define LDR_PIN 34
WiFiClient espClient; //creating a instance of Wifi Client library
PubSubClient mqttClient(espClient); //Initializing the PubSub Client Library
WiFiUDP ntpUDP; //creating instance of Wifi UDP library
NTPClient timeClient(ntpUDP); //creating instance of NTPClient library
char tempAr[6]; //character array of length 6 to store temperature
DHTesp dhtSensor; //creating a instance to initiate DHT sensor
bool isScheduledON = false; // scheduled switch
unsigned long scheduledOnTime;
void setup() {
Serial.begin(115200);
setupWifi();
setupMqtt(); //Initializing MQTT connection
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); //initiate the DHT sensor
timeClient.begin(); //begin TimeClient library
timeClient.setTimeOffset(5.5*3600); //GMT5.30 in seconds
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW); //switch off the buzzer at the beginning
}
void loop() {
if(!mqttClient.connected()){ //checking the MQTT connection
connectToBroker();
}
mqttClient.loop(); //handle the MQTT client, check incoming messages
updateTemperature(); //take temperature and updating
Serial.println(tempAr); //prints updated temp array
mqttClient.publish("ADMIN-TEMP", tempAr); //publish temperature to temp Array
//updateIntensity();
checkSchedule();
delay(1000);
}
void buzzerOn(bool on){ //switching on the buzzzer
if(on){
tone(BUZZER, 256); //256 = frequency
}else{
noTone(BUZZER);
}
}
void setupMqtt(){
mqttClient.setServer("test.mosquitto.org", 1883);
//Initializing MQTT connection
mqttClient.setCallback(receiveCallback);
//To get incoming messages, setting callback
}
void receiveCallback(char * topic, byte * payload, unsigned int length){ // setting callback function
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]"); //printing topic
char payloadCharAr[length]; //saving payload in a character array
for (int i=0; i<length; i++){
Serial.print((char)payload[i]); //printing incoming payload
payloadCharAr[i] = (char)payload[i];
}// assign the casted value to payload array
Serial.println();
if (strcmp(topic, "ADMIN-MAIN-ON-OFF")==0){ //checking incoming topic
buzzerOn(payloadCharAr[0] == '1');
}else if (strcmp(topic, "ADMIN-SCH-ON")==0){
if(payloadCharAr[0] == 'N'){
isScheduledON = false;
}else{
isScheduledON = true;
scheduledOnTime = atol(payloadCharAr); //converting array to long
}
}
}
void connectToBroker(){ //checking the MQTT connection
while (!mqttClient.connected()){
Serial.print("Attempting MQTT connection...");
if(mqttClient.connect("ESP32-1234564545")){ //ID of ESP32 to authentication
Serial.println("Connected");
//Subscribe functions
mqttClient.subscribe("ADMIN-MAIN-ON-OFF");
mqttClient.subscribe("ADMIN-SCH-ON");
}else{
Serial.print("Failed");
Serial.println(mqttClient.state());
delay(5000);
}
}
}
//update temperature to temp array
void updateTemperature(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String(data.temperature, 2).toCharArray(tempAr, 6);
}
void setupWifi(){
Serial.println();
Serial.print("Connecting to ");
Serial.println("Wokwi-GUEST");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected");
Serial.println("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("ADMIN-MAIN-ON-OFF-ESP32", "1");
mqttClient.publish("EN-ADMIN-SCH-ESP-ON", "0");
Serial.println("Scheduled ON");
}
}
}