Add projects/mqtt-nodered/mqtt-nodered.ino
This commit is contained in:
parent
a5b1684ec9
commit
60fecad554
164
projects/mqtt-nodered/mqtt-nodered.ino
Normal file
164
projects/mqtt-nodered/mqtt-nodered.ino
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
// WiFi credentials
|
||||||
|
const char* ssid = "your_wifi_ssid";
|
||||||
|
const char* password = "your_wifi_password";
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
|
// Relay pins
|
||||||
|
const int relay1Pin = 26;
|
||||||
|
const int relay2Pin = 27;
|
||||||
|
|
||||||
|
// Button pins (connected to GND with internal pull-ups)
|
||||||
|
const int button1Pin = 14;
|
||||||
|
const int button2Pin = 15;
|
||||||
|
|
||||||
|
// 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";
|
||||||
|
|
||||||
|
// Relay states (use bools for easy toggle)
|
||||||
|
bool relay1State = LOW;
|
||||||
|
bool relay2State = LOW;
|
||||||
|
|
||||||
|
unsigned long lastDebounceTime1 = 0;
|
||||||
|
unsigned long lastDebounceTime2 = 0;
|
||||||
|
const unsigned long debounceDelay = 50;
|
||||||
|
|
||||||
|
void setup_wifi() {
|
||||||
|
delay(10);
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("");
|
||||||
|
Serial.println("WiFi connected");
|
||||||
|
Serial.print("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
void publishRelayState() {
|
||||||
|
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.print("Relay2 state published: ");
|
||||||
|
Serial.println(relay2State ? "ON" : "OFF");
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
|
Serial.print("Message arrived on topic: ");
|
||||||
|
Serial.println(topic);
|
||||||
|
|
||||||
|
String message;
|
||||||
|
for (unsigned int i = 0; i < length; i++) {
|
||||||
|
message += (char)payload[i];
|
||||||
|
}
|
||||||
|
message.toUpperCase();
|
||||||
|
Serial.print("Payload: ");
|
||||||
|
Serial.println(message);
|
||||||
|
|
||||||
|
if (String(topic) == relay1CommandTopic) {
|
||||||
|
if (message == "ON") {
|
||||||
|
relay1State = HIGH;
|
||||||
|
} else if (message == "OFF") {
|
||||||
|
relay1State = LOW;
|
||||||
|
}
|
||||||
|
digitalWrite(relay1Pin, relay1State);
|
||||||
|
publishRelayState();
|
||||||
|
} else if (String(topic) == relay2CommandTopic) {
|
||||||
|
if (message == "ON") {
|
||||||
|
relay2State = HIGH;
|
||||||
|
} else if (message == "OFF") {
|
||||||
|
relay2State = LOW;
|
||||||
|
}
|
||||||
|
digitalWrite(relay2Pin, relay2State);
|
||||||
|
publishRelayState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void reconnect() {
|
||||||
|
while (!client.connected()) {
|
||||||
|
Serial.print("Attempting MQTT connection...");
|
||||||
|
if (client.connect("ESP32C3Client", mqtt_user, mqtt_password)) {
|
||||||
|
Serial.println("connected");
|
||||||
|
client.subscribe(relay1CommandTopic);
|
||||||
|
client.subscribe(relay2CommandTopic);
|
||||||
|
publishRelayState();
|
||||||
|
} else {
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(client.state());
|
||||||
|
Serial.println(" try again in 5 seconds");
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
pinMode(relay1Pin, OUTPUT);
|
||||||
|
pinMode(relay2Pin, OUTPUT);
|
||||||
|
pinMode(button1Pin, INPUT_PULLUP);
|
||||||
|
pinMode(button2Pin, INPUT_PULLUP);
|
||||||
|
|
||||||
|
// Initialize relays off
|
||||||
|
relay1State = LOW;
|
||||||
|
relay2State = LOW;
|
||||||
|
digitalWrite(relay1Pin, relay1State);
|
||||||
|
digitalWrite(relay2Pin, relay2State);
|
||||||
|
|
||||||
|
setup_wifi();
|
||||||
|
|
||||||
|
client.setServer(mqtt_server, mqtt_port);
|
||||||
|
client.setCallback(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Button 1 handling with debounce
|
||||||
|
if (digitalRead(button1Pin) == LOW) {
|
||||||
|
if ((millis() - lastDebounceTime1) > debounceDelay) {
|
||||||
|
relay1State = !relay1State;
|
||||||
|
digitalWrite(relay1Pin, relay1State);
|
||||||
|
publishRelayState();
|
||||||
|
lastDebounceTime1 = millis();
|
||||||
|
// Wait for button release
|
||||||
|
while (digitalRead(button1Pin) == LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Button 2 handling with debounce
|
||||||
|
if (digitalRead(button2Pin) == LOW) {
|
||||||
|
if ((millis() - lastDebounceTime2) > debounceDelay) {
|
||||||
|
relay2State = !relay2State;
|
||||||
|
digitalWrite(relay2Pin, relay2State);
|
||||||
|
publishRelayState();
|
||||||
|
lastDebounceTime2 = millis();
|
||||||
|
// Wait for button release
|
||||||
|
while (digitalRead(button2Pin) == LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!client.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
client.loop();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user