and this is my Adruino code that I make it so Humidity and Temprature can run a random number because I don’t have sensor to run them and yes It’s accpect the ESP but it’s not apply data into them:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Wi-Fi and MQTT credentials
const char* ssid = “#”;
const char* password = “#”;
const char* mqtt_server = “192.168.1.241”;
const int mqtt_port = 1883;
const char* username = “#”;
const char* mqttpass = “#”;
// MQTT topics
const char* lastwill = “master/mqtt-explorer-26cd1ecb/writeattributevalue/temperature/29dd4TlxsvNcRQhACOr6Xp”;
const char* humidityTopic = “master/mqtt-explorer-26cd1ecb/writeattributevalue/humidity/29dd4TlxsvNcRQhACOr6Xp”;
// Objects
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“Connected to Wi-Fi”);
// Set up MQTT
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
// Attempt to connect to MQTT
reconnect();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Generate random temperature between 22C and 31C
int temp = random(22, 32);
String tempStr = String(temp) + “C”;
if (client.publish(temperatureTopic, tempStr.c_str())) {
Serial.println(“Temperature data published successfully”);
} else {
Serial.println(“Failed to publish temperature data”);
}
// Generate random humidity between 40% and 70%
int humidity = random(40, 71);
String humidityStr = String(humidity) + “%”;
if (client.publish(humidityTopic, humidityStr.c_str())) {
Serial.println(“Humidity data published successfully”);
} else {
Serial.println(“Failed to publish humidity data”);
}
delay(10000); // Wait 10 seconds before publishing new values
}
// MQTT callback
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.println(topic);
Serial.print("Message: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
// MQTT reconnect
void reconnect() {
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
if (client.connect("ESP8266Client", username, mqttpass)) {
Serial.println("-> MQTT client connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" -> try again in 5 seconds");
delay(5000);
}
}
}