#include <WiFi.h>
#include <DHTesp.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#define DHT_PIN 15
#define BUZZER 12
// making an instance of wificlient class.
// through this the mqttclient is initiated.
WiFiClient espClient;
PubSubClient mqttClient(espClient);
// initializing fetching time from ntpserver.
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// to store the temp data from the sensor.
char tempAr[6];
// creating an instance of dht sensor
DHTesp dhtSensor;
// variable to store wether the sheduling has happened or not.
bool isScheduledON = false;
unsigned long scheduledOnTime;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
setupWifi();
setupMqtt();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// initializing updating time with the ntp server.
timeClient.begin();
timeClient.setTimeOffset(5.5*3600);
pinMode(BUZZER, OUTPUT);
digitalWrite(BUZZER, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
// establishing the connection to the broker
if (!mqttClient.connected()) {
connectToBroker();
}
mqttClient.loop(); // essential to call mqttClient.loop() regularly within the main loop to ensure that the MQTT client functions correctly and maintains a stable connection to the broker.
updateTemperature();
Serial.println(tempAr);
mqttClient.publish("prl-admin-temp", tempAr);
checkSchedule();
delay(1000);
}
void buzzerOn(bool on) {
if (on) {
tone(BUZZER, 256);
}
else {
noTone(BUZZER);
}
}
// setting up the mqtt server
// setting up the receivecallback function to hand calling back from the broker.
void setupMqtt() {
mqttClient.setServer("test.mosquitto.org", 1883);
mqttClient.setCallback(receiveCallback);
}
void updateTemperature() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String(data.temperature, 2).toCharArray(tempAr, 6);
}
// setting up the wifi connection.
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());
}
}
void connectToBroker() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
if (mqttClient.connect("ESP32-4646")) { // random address in this case
Serial.println("connected");
// subscribing to topics within the broker.
mqttClient.subscribe("prl-on-off");
mqttClient.subscribe("sheduled-out");
}
else {
Serial.println("failed");
}
}
}
void receiveCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("message arrived [");
Serial.print(topic);
Serial.print("]");
char payloadCharAr[length];
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
payloadCharAr[i] = (char)payload[i];
}
// get content from the subscribed topics by filtering them
if (strcmp(topic, "prl-on-off") == 0) {
buzzerOn(payloadCharAr[0] == '1');
}
// get content from the subscribed topics by filtering them
else if (strcmp(topic, "sheduled-out") == 0) {
if (payloadCharAr[0] == 'N') {
isScheduledON = false;
}
else {
isScheduledON = true;
scheduledOnTime = atol(payloadCharAr); // converting character array to a long integer.
}
}
}
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("main-on-off-esp_medibox", "1");
mqttClient.publish("main-on-off-esp-shedulSwitch", "0");
Serial.println("Scheduled ON");
}
}
}