#include <PubSubClient.h>
#include <NTPClient.h>
#include "DHTesp.h"
#include <WiFi.h>
const int DHT_PIN = 12;
DHTesp dhtSensor;
WiFiClient espClient;
// mqttClient the variable
PubSubClient mqttClient(espClient);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
char tempAr[6];
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup() {
Serial.begin(115200);
setupWifi();
setupMqtt();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop() {
if (!mqttClient.connected()){
connectToBroker();
}
mqttClient.loop();
updateTemperature();
Serial.println(tempAr);
// publish the temperature
mqttClient.publish("MEDIBOX_TEMP", tempAr);
// updateTemperature();
// TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Serial.println(String(data.temperature, 2));
delay(1000);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setupWifi(){
Serial.println();
Serial.print("Connecting to ");
Serial.println("Wokwi-GUEST");
// connecting to wifi
WiFi.begin("Wokwi-GUEST","");
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.print("");
Serial.println("WiFi Connected");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setupMqtt(){
// setting the server
mqttClient.setServer("test.mosquitto.org", 1883);
// test.mosquitto.org
// 127.0.0.1
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();
// // 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-OFF") == 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
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>