#include <PubSubClient.h>
#include <WiFi.h>
#include <Arduino.h>
#include <string>
#include "DHT.h"
#include <FirebaseESP32.h>
using namespace std;
// Init Firebase
int postId = 0;
#define FIREBASE_HOST "https://testfirebase-165c4-default-rtdb.asia-southeast1.firebasedatabase.app/"
#define FIREBASE_AUTH "AIzaSyC3HBYPCoFbrjZUk7O-XpU6YNw_vVnC_Xc"
FirebaseData firebaseData;
FirebaseJson json;
// Init DHT
const int dhtPin = 22;
const int dhtType = DHT22; // Type of dht
DHT dht(dhtPin, dhtType);
// Init type of Temperature
enum TypeTemp
{
Cescius = false,
Fahrenheit = true,
};
TypeTemp type = TypeTemp::Cescius;
bool stateTemp = false; // false: normal, true: dangerous
// Init topics to publish and receiver from / to MQTT
const char *tempTopic = "IOT_SMARTDOOR/Temp";
const char *humidTopic = "IOT_SMARTDOOR/Humid";
const char *typeTempTopic = "IOT_SMARTDOOR/TypeTemp";
const char *stateTempTopic = "IOT_SMARTDOOR/StateTemp";
const char *controlTopic = "IOT_SMARTDOOR/DoorControl";
//***Set server***
const char *mqttServer = "broker.hivemq.com";
int port = 1883;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
// function to connect Wifi
void connectWifi()
{
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// function to connect MQTT
void mqttConnect()
{
while (!mqttClient.connected())
{
Serial.println("Attemping MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str()))
{
Serial.println("connected");
//***Subscribe all topic you need***
mqttClient.subscribe(typeTempTopic);
mqttClient.subscribe(stateTempTopic);
}
else
{
Serial.println("Try again in 2 seconds");
delay(2000);
}
}
}
// MQTT Receiver
void callback(char *topic, byte *message, unsigned int length)
{
Serial.println(topic);
String strMsg;
for (int i = 0; i < length; i++)
{
strMsg += (char)message[i];
}
Serial.println(strMsg);
//***Code here to process the received package***
if (strcmp(topic, typeTempTopic) == 0)
{
if (strMsg = "false")
type = TypeTemp::Cescius;
else
type = TypeTemp::Cescius;
}
if (strcmp(topic, stateTempTopic) == 0)
{
if (strMsg.compareTo("true"))
mqttClient.publish(controlTopic, "Open");
stateTemp = true;
}
}
// function to get humidity
float humidity(DHT dht)
{
float humid = dht.readHumidity();
Serial.print("Humidity: ");
Serial.print(humid);
Serial.println("%");
return humid;
}
// function to get temperature
float temperature(DHT dht, bool type)
{
float temp = dht.readTemperature(type);
Serial.print("Temperature: ");
Serial.print(temp);
if (type)
Serial.println("°F"); // °: Option/Alt +Shift + 8
else
Serial.println("°C");
if (type && temp > 150)
{
Serial.println("DANGEROUS");
}
if (!type && temp > 60)
{
Serial.println("DANGEROUS");
}
return temp;
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
connectWifi(); // connect to Wifi Wokwi-Guest
dht.begin();
// set up for mqtt
mqttClient.setServer(mqttServer, port);
mqttClient.setCallback(callback);
mqttClient.setKeepAlive(90);
// set up for firebase
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
void loop()
{
delay(10); // this speeds up the simulation
if (!mqttClient.connected())
{
mqttConnect();
}
mqttClient.loop();
/*Function WARNING*/
float humid = humidity(dht);
float temp = temperature(dht, type);
delay(500);
char buffer[50];
// Publish humidity
snprintf(buffer, 50, "%.2lf", humid);
mqttClient.publish(humidTopic, buffer);
// Publish temperature
snprintf(buffer, 50, "%.2lf", temp);
mqttClient.publish(tempTopic, buffer);
// Publish type of temperature
snprintf(buffer, 50, "%s", type ? "true" : "false");
mqttClient.publish(typeTempTopic, buffer);
// set data to database
postId = postId + 1;
// push data to firebase
Firebase.setInt(firebaseData, "/" + String(postId) + "/Temperature", temp);
Firebase.setInt(firebaseData, "/" + String(postId) + "/Humidity", humid);
Firebase.setBool(firebaseData, "/" + String(postId) + "/TypeTemperature", type);
Firebase.setBool(firebaseData, "/" + String(postId) + "/StateTemperature", stateTemp);
//Firebase.setString(firebaseData, "/" + String(postId) + "/Label", String);
// get data from firebase
/*
// get value type Int
Firebase.getInt(firebaseData, "/Label");
// assign value into variable
variable = Firebase.intData();
*/
}