Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- int Relay1 = 5;// D1
- int Relay2 = 4;// D2
- int Relay3 = 0;// D3
- int Relay4 = 2;// D4
- const char* ssid = "YOURSSID";
- const char* password = "12345678";
- //change with your MQTT server IP address
- const char* mqtt_server = "mein.mqttserver";
- WiFiClient espClient;
- PubSubClient client(espClient);
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print(topic);
- Serial.print(" => ");
- char* payload_str;
- payload_str = (char*) malloc(length + 1);
- memcpy(payload_str, payload, length);
- payload_str[length] = '\0';
- Serial.println(String(payload_str));
- if ( String(topic) == "Haus/Garten/Relay1" ) {
- if (String(payload_str) == "off" ) {
- digitalWrite(Relay1, HIGH); // turn the RELAY off
- client.publish("Haus/Garten/Relay1/state","off");
- } else if ( String(payload_str) == "on" ) {
- digitalWrite(Relay1, LOW); // turn the RELAY on
- client.publish("Haus/Garten/Relay1/state","on");
- } else {
- Serial.print("I do not know what to do with ");
- Serial.print(String(payload_str));
- Serial.print(" on topic ");
- Serial.println( String(topic));
- }
- }
- if ( String(topic) == "Haus/Garten/Relay2" ) {
- if (String(payload_str) == "off" ) {
- digitalWrite(Relay2, HIGH); // turn the RELAY off
- client.publish("Haus/Garten/Relay2/state","off");
- } else if ( String(payload_str) == "on" ) {
- digitalWrite(Relay2, LOW); // turn the RELAY on
- client.publish("Haus/Garten/Relay2/state","on");
- } else {
- Serial.print("I do not know what to do with ");
- Serial.print(String(payload_str));
- Serial.print(" on topic ");
- Serial.println( String(topic));
- }
- }
- if ( String(topic) == "Haus/Garten/Relay3" ) {
- if (String(payload_str) == "off" ) {
- digitalWrite(Relay3, HIGH); // turn the RELAY off
- client.publish("Haus/Garten/Relay3/state","off");
- } else if ( String(payload_str) == "on" ) {
- digitalWrite(Relay3, LOW); // turn the RELAY on
- client.publish("Haus/Garten/Relay3/state","on");
- } else {
- Serial.print("I do not know what to do with ");
- Serial.print(String(payload_str));
- Serial.print(" on topic ");
- Serial.println( String(topic));
- }
- }
- if ( String(topic) == "Haus/Garten/Relay4" ) {
- if (String(payload_str) == "off" ) {
- digitalWrite(Relay4, HIGH); // turn the RELAY off
- client.publish("Haus/Garten/Relay4/state","off");
- } else if ( String(payload_str) == "on" ) {
- digitalWrite(Relay4, LOW); // turn the RELAY on
- client.publish("Haus/Garten/Relay4/state","on");
- } else {
- Serial.print("I do not know what to do with ");
- Serial.print(String(payload_str));
- Serial.print(" on topic ");
- Serial.println( String(topic));
- }
- }
- }
- void connect_to_MQTT() {
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- if (client.connect("beregnung_relay")) {
- Serial.println("(re)-connected to MQTT");
- client.subscribe("Haus/Garten/Relay1");
- client.subscribe("Haus/Garten/Relay2");
- client.subscribe("Haus/Garten/Relay3");
- client.subscribe("Haus/Garten/Relay4");
- } else {
- Serial.println("Could not connect to MQTT");
- }
- }
- void setup() {
- Serial.begin(115200);
- delay(10);
- // Connecting to our WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- connect_to_MQTT();
- // initialize pin 5, where the relay is connected to.
- pinMode(Relay1, OUTPUT);
- pinMode(Relay2, OUTPUT);
- pinMode(Relay3, OUTPUT);
- pinMode(Relay4, OUTPUT);
- }
- int tellstate = 0;
- void loop() {
- client.loop();
- if (! client.connected()) {
- Serial.println("Not connected to MQTT....");
- connect_to_MQTT();
- delay(5000);
- }
- // Tell the current state Relay 1-4every 60 seconds
- if ( (millis() - tellstate) > 60000 ) {
- if ( digitalRead(Relay1) ) {
- client.publish("Haus/Garten/Relay1/state","off");
- } else {
- client.publish("Haus/Garten/Relay1/state","on");
- }
- tellstate = millis();
- }
- }
- if ( (millis() - tellstate) > 60000 ) {
- if ( digitalRead(Relay2) ) {
- client.publish("Haus/Garten/Relay2/state","off");
- } else {
- client.publish("Haus/Garten/Relay2/state","on");
- }
- }
- // Tell the current state Relay 3 every 60 seconds
- if ( (millis() - tellstate) > 60000 ) {
- if ( digitalRead(Relay3) ) {
- client.publish("Haus/Garten/Relay3/state","off");
- } else {
- client.publish("Haus/Garten/Relay3/state","on");
- }
- }
- // Tell the current state Relay 4 every 60 seconds
- if ( (millis() - tellstate) > 60000 ) {
- if ( digitalRead(Relay4) ) {
- client.publish("Haus/Garten/Relay4/state","off");
- } else {
- client.publish("Haus/Garten/Relay4/state","on");
- }
- tellstate = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement