#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
//Relays for switching appliances
#define Relay1 15 //D15 LED-Orangs = Water-1
#define Relay2 2 //D2 LED-Blue = Water-2
#define Relay3 19 //D19 LED-Blue = Motor
#define Relay4 23 //D23 LED-RED = Fan
//Nenu For Control
int menu;
//Changs to:
#define sub1 "alex9ufo/2025/RELAY"
#define pub1 "alex9ufo/2025/DHT22"
//DHT11 for reading temperature and humidity value
#define DHTPIN 13 //D13 DHTPIN
// Uncomment whatever type you're using!
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
// Update these with values suitable for your network.
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org"; // Local IP address of Raspberry Pi
const char* username = "";
const char* pass = "";
char str_hum_data[10];
char str_temp_data[10];
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
//===============================================
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//==============================================================
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String message="";
for (int i = 0; i < length; i++) {
message += (char) payload[i]; // Convert *byte to string
}
Serial.print(message);
if (strstr(topic, sub1)) //Button 1
{
if (message == "1on" ) {
digitalWrite(Relay1, HIGH); // Turn on the LED
}
if (message == "1off" ) {
digitalWrite(Relay1, LOW); // Turn off the LED
}
if (message == "2on" ) {
digitalWrite(Relay2, HIGH); // Turn on the LED
}
if (message == "2off" ) {
digitalWrite(Relay2, LOW); // Turn off the LED
}
if (message == "3on" ) {
digitalWrite(Relay3, HIGH); // Turn on the LED
}
if (message == "3off" ) {
digitalWrite(Relay3, LOW); // Turn off the LED
}
if (message == "4on" ) {
digitalWrite(Relay4, HIGH); // Turn on the LED
}
if (message == "4off" ) {
digitalWrite(Relay4, LOW); // Turn off the LED
}
}
else{
Serial.println("unsubscribed topic");
}
}
//==============================================================
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(), username, pass) ) {
Serial.println("connected");
client.subscribe(sub1);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//==============================================================
void setup() {
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
client.subscribe(pub1);
}
//==============================================================
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 5000) {
float hum_data = dht.readHumidity();
Serial.println(hum_data);
/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(hum_data, 4, 1, str_hum_data);
float temp_data = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
dtostrf(temp_data, 4, 1, str_temp_data);
lastMsg = now;
Serial.println("Publish message: ");
Serial.print("Temperature: "); Serial.println(str_temp_data);
//client.publish("device2/temp", str_temp_data);
Serial.print("Humidity: "); Serial.println(str_hum_data);
//client.publish("device2/hum", str_hum_data);
String pubStr ,pubStr1 ,pubStr2;
pubStr1=String(str_temp_data);
pubStr2=String(str_hum_data);
pubStr=pubStr1 + "," + pubStr2;
// Length (with one extra character for the null terminator)
int pubStr_len = pubStr.length() + 1;
// Prepare the character array (the buffer)
char char_array[pubStr_len];
// Copy it over
pubStr.toCharArray(char_array, pubStr_len);
client.publish(pub1, char_array);
}
}