Update projects/mqtt-nodered/mqtt-nodered.ino
This commit is contained in:
parent
60fecad554
commit
d186ad68de
@ -2,27 +2,29 @@
|
|||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
// WiFi credentials
|
// WiFi credentials
|
||||||
const char* ssid = "your_wifi_ssid";
|
const char* ssid = "feda";
|
||||||
const char* password = "your_wifi_password";
|
const char* password = "66661758";
|
||||||
|
|
||||||
// MQTT broker details
|
// MQTT broker details
|
||||||
const char* mqtt_server = "your_mqtt_broker_address";
|
const char* mqtt_server = "87.237.194.228";
|
||||||
const int mqtt_port = 1883;
|
const int mqtt_port = 1883;
|
||||||
const char* mqtt_user = "mqtt_username"; // if required
|
// The username and password are not used as per your request.
|
||||||
const char* mqtt_password = "mqtt_password"; // if required
|
// const char* mqtt_user = "mqtt_username";
|
||||||
|
// const char* mqtt_password = "mqtt_password";
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient client(espClient);
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
// Relay pins
|
// Relay pins
|
||||||
const int relay1Pin = 26;
|
const int relay1Pin = 0;
|
||||||
const int relay2Pin = 27;
|
const int relay2Pin = 1;
|
||||||
|
|
||||||
// Button pins (connected to GND with internal pull-ups)
|
// Button pins (connected to GND with internal pull-ups)
|
||||||
const int button1Pin = 14;
|
const int button1Pin = 2;
|
||||||
const int button2Pin = 15;
|
const int button2Pin = 3;
|
||||||
|
|
||||||
// MQTT topics
|
// MQTT topics
|
||||||
|
const char* deviceIpTopic = "home/device/ip";
|
||||||
const char* relay1CommandTopic = "home/relay1/set";
|
const char* relay1CommandTopic = "home/relay1/set";
|
||||||
const char* relay2CommandTopic = "home/relay2/set";
|
const char* relay2CommandTopic = "home/relay2/set";
|
||||||
const char* relay1StateTopic = "home/relay1/state";
|
const char* relay1StateTopic = "home/relay1/state";
|
||||||
@ -36,6 +38,12 @@ unsigned long lastDebounceTime1 = 0;
|
|||||||
unsigned long lastDebounceTime2 = 0;
|
unsigned long lastDebounceTime2 = 0;
|
||||||
const unsigned long debounceDelay = 50;
|
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() {
|
void setup_wifi() {
|
||||||
delay(10);
|
delay(10);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
@ -56,12 +64,13 @@ void setup_wifi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void publishRelayState() {
|
void publishRelayState() {
|
||||||
client.publish(relay1StateTopic, relay1State ? "ON" : "OFF", true);
|
// Use .c_str() for publishing
|
||||||
client.publish(relay2StateTopic, relay2State ? "ON" : "OFF", true);
|
client.publish(relay1StateTopic, !relay1State ? "ON" : "OFF", true);
|
||||||
|
client.publish(relay2StateTopic, !relay2State ? "ON" : "OFF", true);
|
||||||
Serial.print("Relay1 state published: ");
|
Serial.print("Relay1 state published: ");
|
||||||
Serial.println(relay1State ? "ON" : "OFF");
|
Serial.println(!relay1State ? "ON" : "OFF");
|
||||||
Serial.print("Relay2 state published: ");
|
Serial.print("Relay2 state published: ");
|
||||||
Serial.println(relay2State ? "ON" : "OFF");
|
Serial.println(!relay2State ? "ON" : "OFF");
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback(char* topic, byte* payload, unsigned int length) {
|
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);
|
Serial.println(message);
|
||||||
|
|
||||||
if (String(topic) == relay1CommandTopic) {
|
if (String(topic) == relay1CommandTopic) {
|
||||||
if (message == "ON") {
|
if (message == "1") {
|
||||||
relay1State = HIGH;
|
|
||||||
} else if (message == "OFF") {
|
|
||||||
relay1State = LOW;
|
relay1State = LOW;
|
||||||
|
// digitalWrite(relay1Pin, relay1State);
|
||||||
|
} else if (message == "0") {
|
||||||
|
relay1State = HIGH;
|
||||||
|
// digitalWrite(relay1Pin, relay1State);
|
||||||
}
|
}
|
||||||
digitalWrite(relay1Pin, relay1State);
|
digitalWrite(relay1Pin, relay1State);
|
||||||
publishRelayState();
|
publishRelayState();
|
||||||
} else if (String(topic) == relay2CommandTopic) {
|
} else if (String(topic) == relay2CommandTopic) {
|
||||||
if (message == "ON") {
|
if (message == "1") {
|
||||||
relay2State = HIGH;
|
|
||||||
} else if (message == "OFF") {
|
|
||||||
relay2State = LOW;
|
relay2State = LOW;
|
||||||
|
// digitalWrite(relay2Pin, LOW);
|
||||||
|
} else if (message == "0") {
|
||||||
|
relay2State = HIGH;
|
||||||
|
// digitalWrite(relay2Pin, HIGH);
|
||||||
}
|
}
|
||||||
digitalWrite(relay2Pin, relay2State);
|
digitalWrite(relay2Pin, relay2State);
|
||||||
publishRelayState();
|
publishRelayState();
|
||||||
@ -98,10 +111,16 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||||||
void reconnect() {
|
void reconnect() {
|
||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
Serial.print("Attempting MQTT connection...");
|
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");
|
Serial.println("connected");
|
||||||
client.subscribe(relay1CommandTopic);
|
client.subscribe(relay1CommandTopic);
|
||||||
client.subscribe(relay2CommandTopic);
|
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();
|
publishRelayState();
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
@ -140,8 +159,6 @@ void loop() {
|
|||||||
digitalWrite(relay1Pin, relay1State);
|
digitalWrite(relay1Pin, relay1State);
|
||||||
publishRelayState();
|
publishRelayState();
|
||||||
lastDebounceTime1 = millis();
|
lastDebounceTime1 = millis();
|
||||||
// Wait for button release
|
|
||||||
while (digitalRead(button1Pin) == LOW);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,8 +169,6 @@ void loop() {
|
|||||||
digitalWrite(relay2Pin, relay2State);
|
digitalWrite(relay2Pin, relay2State);
|
||||||
publishRelayState();
|
publishRelayState();
|
||||||
lastDebounceTime2 = millis();
|
lastDebounceTime2 = millis();
|
||||||
// Wait for button release
|
|
||||||
while (digitalRead(button2Pin) == LOW);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user