Update projects/mqtt-nodered/mqtt-nodered.ino

This commit is contained in:
Ghassan Yusuf 2025-10-08 00:26:37 +03:00
parent 60fecad554
commit d186ad68de

View File

@ -2,39 +2,47 @@
#include <PubSubClient.h>
// WiFi credentials
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
const char* ssid = "feda";
const char* password = "66661758";
// MQTT broker details
const char* mqtt_server = "your_mqtt_broker_address";
const int mqtt_port = 1883;
const char* mqtt_user = "mqtt_username"; // if required
const char* mqtt_password = "mqtt_password"; // if required
const char* mqtt_server = "87.237.194.228";
const int mqtt_port = 1883;
// The username and password are not used as per your request.
// const char* mqtt_user = "mqtt_username";
// const char* mqtt_password = "mqtt_password";
WiFiClient espClient;
PubSubClient client(espClient);
// Relay pins
const int relay1Pin = 26;
const int relay2Pin = 27;
const int relay1Pin = 0;
const int relay2Pin = 1;
// Button pins (connected to GND with internal pull-ups)
const int button1Pin = 14;
const int button2Pin = 15;
const int button1Pin = 2;
const int button2Pin = 3;
// MQTT topics
const char* relay1CommandTopic = "home/relay1/set";
const char* relay2CommandTopic = "home/relay2/set";
const char* relay1StateTopic = "home/relay1/state";
const char* relay2StateTopic = "home/relay2/state";
const char* deviceIpTopic = "home/device/ip";
const char* relay1CommandTopic = "home/relay1/set";
const char* relay2CommandTopic = "home/relay2/set";
const char* relay1StateTopic = "home/relay1/state";
const char* relay2StateTopic = "home/relay2/state";
// Relay states (use bools for easy toggle)
bool relay1State = LOW;
bool relay2State = LOW;
bool relay1State = LOW;
bool relay2State = LOW;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
const unsigned long debounceDelay = 50;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
const unsigned long debounceDelay = 50;
// Function prototypes
void setup_wifi();
void publishRelayState();
void callback(char* topic, byte* payload, unsigned int length);
void reconnect();
void setup_wifi() {
delay(10);
@ -56,12 +64,13 @@ void setup_wifi() {
}
void publishRelayState() {
client.publish(relay1StateTopic, relay1State ? "ON" : "OFF", true);
client.publish(relay2StateTopic, relay2State ? "ON" : "OFF", true);
// Use .c_str() for publishing
client.publish(relay1StateTopic, !relay1State ? "ON" : "OFF", true);
client.publish(relay2StateTopic, !relay2State ? "ON" : "OFF", true);
Serial.print("Relay1 state published: ");
Serial.println(relay1State ? "ON" : "OFF");
Serial.println(!relay1State ? "ON" : "OFF");
Serial.print("Relay2 state published: ");
Serial.println(relay2State ? "ON" : "OFF");
Serial.println(!relay2State ? "ON" : "OFF");
}
void callback(char* topic, byte* payload, unsigned int length) {
@ -77,18 +86,22 @@ void callback(char* topic, byte* payload, unsigned int length) {
Serial.println(message);
if (String(topic) == relay1CommandTopic) {
if (message == "ON") {
relay1State = HIGH;
} else if (message == "OFF") {
if (message == "1") {
relay1State = LOW;
// digitalWrite(relay1Pin, relay1State);
} else if (message == "0") {
relay1State = HIGH;
// digitalWrite(relay1Pin, relay1State);
}
digitalWrite(relay1Pin, relay1State);
publishRelayState();
} else if (String(topic) == relay2CommandTopic) {
if (message == "ON") {
relay2State = HIGH;
} else if (message == "OFF") {
if (message == "1") {
relay2State = LOW;
// digitalWrite(relay2Pin, LOW);
} else if (message == "0") {
relay2State = HIGH;
// digitalWrite(relay2Pin, HIGH);
}
digitalWrite(relay2Pin, relay2State);
publishRelayState();
@ -98,10 +111,16 @@ void callback(char* topic, byte* payload, unsigned int length) {
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32C3Client", mqtt_user, mqtt_password)) {
// Connect without a username or password
if (client.connect("ESP32C3Client")) {
Serial.println("connected");
client.subscribe(relay1CommandTopic);
client.subscribe(relay2CommandTopic);
// Convert IP to String once, then get C-string pointer
String ipString = WiFi.localIP().toString();
client.publish(deviceIpTopic, ipString.c_str());
publishRelayState();
} else {
Serial.print("failed, rc=");
@ -140,8 +159,6 @@ void loop() {
digitalWrite(relay1Pin, relay1State);
publishRelayState();
lastDebounceTime1 = millis();
// Wait for button release
while (digitalRead(button1Pin) == LOW);
}
}
@ -152,8 +169,6 @@ void loop() {
digitalWrite(relay2Pin, relay2State);
publishRelayState();
lastDebounceTime2 = millis();
// Wait for button release
while (digitalRead(button2Pin) == LOW);
}
}