|
- #include <Ethernet.h>
- #include <PubSubClient.h>
- #include <string.h>
-
- // Update these with values suitable for your network.
- byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
-
- EthernetClient ethClient;
- PubSubClient mqttClient(ethClient);
-
- // relais steuerung
- int relais1pin = 2;
- int relais2pin = 3;
-
- void door_open() {
- // Rechtslauf
- digitalWrite(relais1pin, HIGH);
- digitalWrite(relais2pin, LOW);
- Serial.println("Tür auf");
- }
-
- void door_close() {
- // Linkslauf
- digitalWrite(relais1pin, LOW);
- digitalWrite(relais2pin, HIGH);
- Serial.println("Tür zu");
- }
-
- void door_stop() {
- // keine Bewegung
- digitalWrite(relais1pin, HIGH);
- digitalWrite(relais2pin, HIGH);
- Serial.println("Tür stop");
- }
-
- void callback(char *topic, byte *payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (unsigned int i = 0; i < length; i++) {
- Serial.print((char) payload[i]);
- }
- Serial.println();
- if (strcmp(topic, "loxone/hof/Gansetorauf")==0) {
- if (payload[0] == '0') {
- door_stop();
- mqttClient.publish("Arduino-1/Hof/Gansetorauf", "0");
- } else {
- door_open();
- mqttClient.publish("Arduino-1/Hof/Gansetorauf", "1");
- }
- } else if (strcmp(topic, "loxone/hof/Gansetorzu")==0) {
- if (payload[0] == '0') {
- door_stop();
- mqttClient.publish("Arduino-1/Hof/Gansetorzu", "0");
- } else {
- door_close();
- mqttClient.publish("Arduino-1/Hof/Gansetorzu", "1");
- }
- }
- }
-
- void mqttreconnect() {
- if (mqttClient.connect("arduino-1", "loxberry", "OSVL0AMqISFXgr5g")) {
- // connection succeeded
- Serial.println("Connected ");
- boolean r = mqttClient.subscribe("loxone/hof/Gansetorauf");
- Serial.println("loxone/hof/Gansetorauf subscribe ");
- Serial.println(r);
- r = mqttClient.subscribe("loxone/hof/Gansetorzu");
- Serial.println("loxone/hof/Gansetorzu subscribe ");
- Serial.println(r);
- } else {
- // connection failed
- // mqttClient.state() will provide more information
- // on why it failed.
- Serial.println("Connection failed ");
- Serial.println(mqttClient.state());
- }
- }
-
- void setup() {
- // Open serial communications and wait for port to open:
-
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for native USB port only
- }
- Ethernet.begin(mac);
- // Allow the hardware to sort itself out
- delay(1500);
- //mqttClient.setServer(server, 1883);
- mqttClient.setServer("192.168.11.35", 1883);
- mqttClient.setCallback(callback);
-
- mqttreconnect();
-
- // Relais
- pinMode(relais1pin, OUTPUT);
- pinMode(relais2pin, OUTPUT);
-
- door_stop();
- }
-
- void loop() {
- //
-
- while (true) {
- if (!mqttClient.loop()) {
- mqttreconnect();
- }
- delay(1000);
-
- }
-
- }
|