180 lines
4.8 KiB
C++
180 lines
4.8 KiB
C++
#include <WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
// WiFi credentials
|
|
const char* ssid = "feda";
|
|
const char* password = "66661758";
|
|
|
|
// MQTT broker details
|
|
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 = 0;
|
|
const int relay2Pin = 1;
|
|
|
|
// Button pins (connected to GND with internal pull-ups)
|
|
const int button1Pin = 2;
|
|
const int button2Pin = 3;
|
|
|
|
// MQTT topics
|
|
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;
|
|
|
|
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);
|
|
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() {
|
|
// 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.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 == "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 == "1") {
|
|
relay2State = LOW;
|
|
// digitalWrite(relay2Pin, LOW);
|
|
} else if (message == "0") {
|
|
relay2State = HIGH;
|
|
// digitalWrite(relay2Pin, HIGH);
|
|
}
|
|
digitalWrite(relay2Pin, relay2State);
|
|
publishRelayState();
|
|
}
|
|
}
|
|
|
|
void reconnect() {
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// 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=");
|
|
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();
|
|
}
|
|
}
|
|
|
|
// Button 2 handling with debounce
|
|
if (digitalRead(button2Pin) == LOW) {
|
|
if ((millis() - lastDebounceTime2) > debounceDelay) {
|
|
relay2State = !relay2State;
|
|
digitalWrite(relay2Pin, relay2State);
|
|
publishRelayState();
|
|
lastDebounceTime2 = millis();
|
|
}
|
|
}
|
|
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
client.loop();
|
|
}
|