Guest User

4 Fach Relay

a guest
Apr 3rd, 2021
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. int Relay1 = 5;// D1
  5. int Relay2 = 4;// D2
  6. int Relay3 = 0;// D3
  7. int Relay4 = 2;// D4
  8.  
  9.  
  10. const char* ssid     = "YOURSSID";
  11. const char* password = "12345678";
  12.  
  13. //change with your MQTT server IP address
  14. const char* mqtt_server = "mein.mqttserver";
  15.  
  16.  
  17. WiFiClient espClient;
  18. PubSubClient client(espClient);
  19.  
  20.  
  21. void callback(char* topic, byte* payload, unsigned int length) {
  22.   Serial.print(topic);
  23.   Serial.print(" => ");
  24.  
  25. char* payload_str;
  26.   payload_str = (char*) malloc(length + 1);
  27.   memcpy(payload_str, payload, length);
  28.   payload_str[length] = '\0';
  29. Serial.println(String(payload_str));
  30.  
  31.   if ( String(topic) == "Haus/Garten/Relay1" ) {
  32.     if (String(payload_str) == "off" ) {
  33.       digitalWrite(Relay1, HIGH);   // turn the RELAY off
  34.       client.publish("Haus/Garten/Relay1/state","off");
  35.     } else if ( String(payload_str) == "on" ) {
  36.       digitalWrite(Relay1, LOW);    // turn the RELAY on
  37.       client.publish("Haus/Garten/Relay1/state","on");
  38.     } else {
  39.       Serial.print("I do not know what to do with ");
  40.       Serial.print(String(payload_str));
  41.       Serial.print(" on topic ");
  42.       Serial.println( String(topic));
  43.     }
  44.   }
  45.  
  46.  
  47.  if ( String(topic) == "Haus/Garten/Relay2" ) {
  48.     if (String(payload_str) == "off" ) {
  49.       digitalWrite(Relay2, HIGH);   // turn the RELAY off
  50.       client.publish("Haus/Garten/Relay2/state","off");
  51.     } else if ( String(payload_str) == "on" ) {
  52.       digitalWrite(Relay2, LOW);    // turn the RELAY on
  53.       client.publish("Haus/Garten/Relay2/state","on");
  54.     } else {
  55.       Serial.print("I do not know what to do with ");
  56.       Serial.print(String(payload_str));
  57.       Serial.print(" on topic ");
  58.       Serial.println( String(topic));
  59.     }
  60.   }
  61.  
  62.  
  63. if ( String(topic) == "Haus/Garten/Relay3" ) {
  64.     if (String(payload_str) == "off" ) {
  65.       digitalWrite(Relay3, HIGH);   // turn the RELAY off
  66.       client.publish("Haus/Garten/Relay3/state","off");
  67.     } else if ( String(payload_str) == "on" ) {
  68.       digitalWrite(Relay3, LOW);    // turn the RELAY on
  69.       client.publish("Haus/Garten/Relay3/state","on");
  70.     } else {
  71.       Serial.print("I do not know what to do with ");
  72.       Serial.print(String(payload_str));
  73.       Serial.print(" on topic ");
  74.       Serial.println( String(topic));
  75.     }
  76.   }
  77.  
  78. if ( String(topic) == "Haus/Garten/Relay4" ) {
  79.     if (String(payload_str) == "off" ) {
  80.       digitalWrite(Relay4, HIGH);   // turn the RELAY off
  81.       client.publish("Haus/Garten/Relay4/state","off");
  82.     } else if ( String(payload_str) == "on" ) {
  83.       digitalWrite(Relay4, LOW);    // turn the RELAY on
  84.       client.publish("Haus/Garten/Relay4/state","on");
  85.     } else {
  86.       Serial.print("I do not know what to do with ");
  87.       Serial.print(String(payload_str));
  88.       Serial.print(" on topic ");
  89.       Serial.println( String(topic));
  90.     }
  91.   }    
  92. }
  93.  
  94. void connect_to_MQTT() {
  95.  client.setServer(mqtt_server, 1883);
  96.   client.setCallback(callback);
  97.  
  98.   if (client.connect("beregnung_relay")) {
  99.     Serial.println("(re)-connected to MQTT");
  100.     client.subscribe("Haus/Garten/Relay1");
  101.     client.subscribe("Haus/Garten/Relay2");
  102.     client.subscribe("Haus/Garten/Relay3");
  103.     client.subscribe("Haus/Garten/Relay4");
  104.   } else {
  105.     Serial.println("Could not connect to MQTT");
  106.   }
  107. }
  108.  
  109. void setup() {
  110.   Serial.begin(115200);
  111.   delay(10);
  112.  
  113.   // Connecting to our WiFi network
  114.   Serial.println();
  115.   Serial.println();
  116.   Serial.print("Connecting to ");
  117.   Serial.println(ssid);
  118.  
  119.   WiFi.mode(WIFI_STA);
  120.   WiFi.begin(ssid, password);
  121.  
  122.   while (WiFi.status() != WL_CONNECTED) {
  123.     delay(500);
  124.     Serial.print(".");
  125.   }
  126.  
  127.   Serial.println("");
  128.   Serial.println("WiFi connected");
  129.   Serial.println("IP address: ");
  130.   Serial.println(WiFi.localIP());
  131.  
  132.   connect_to_MQTT();
  133.  
  134.   // initialize pin 5, where the relay is connected to.
  135.   pinMode(Relay1, OUTPUT);
  136.   pinMode(Relay2, OUTPUT);
  137.   pinMode(Relay3, OUTPUT);
  138.   pinMode(Relay4, OUTPUT);
  139. }
  140.  
  141. int tellstate = 0;
  142.  
  143. void loop() {
  144.   client.loop();
  145.  
  146.   if (! client.connected()) {
  147.     Serial.println("Not connected to MQTT....");
  148.     connect_to_MQTT();
  149.     delay(5000);
  150.   }
  151.  
  152.   // Tell the current state Relay 1-4every 60 seconds
  153.  
  154.  if ( (millis() - tellstate) > 60000 ) {
  155.     if ( digitalRead(Relay1) ) {
  156.        client.publish("Haus/Garten/Relay1/state","off");
  157.     } else {
  158.       client.publish("Haus/Garten/Relay1/state","on");
  159.     }
  160.     tellstate = millis();
  161.   }  
  162.  }
  163.  
  164.  
  165.   if ( (millis() - tellstate) > 60000 ) {
  166.     if ( digitalRead(Relay2) ) {
  167.        client.publish("Haus/Garten/Relay2/state","off");
  168.     } else {
  169.       client.publish("Haus/Garten/Relay2/state","on");
  170.     }
  171.    
  172.   }
  173.  
  174.     // Tell the current state Relay 3 every 60 seconds
  175.   if ( (millis() - tellstate) > 60000 ) {
  176.     if ( digitalRead(Relay3) ) {
  177.        client.publish("Haus/Garten/Relay3/state","off");
  178.     } else {
  179.       client.publish("Haus/Garten/Relay3/state","on");
  180.     }
  181.    
  182.   }
  183.  
  184.     // Tell the current state Relay 4 every 60 seconds
  185.   if ( (millis() - tellstate) > 60000 ) {
  186.     if ( digitalRead(Relay4) ) {
  187.        client.publish("Haus/Garten/Relay4/state","off");
  188.     } else {
  189.       client.publish("Haus/Garten/Relay4/state","on");
  190.     }
  191.     tellstate = millis();
  192.   }
  193. }
RAW Paste Data