|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #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("Tr auf");
- }
-
- void door_close() {
- // Linkslauf
- digitalWrite(relais1pin, LOW);
- digitalWrite(relais2pin, HIGH);
- Serial.println("Tr zu");
- }
-
- void door_stop() {
- // keine Bewegung
- digitalWrite(relais1pin, HIGH);
- digitalWrite(relais2pin, HIGH);
- Serial.println("Tr 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 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);
-
- 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 ");
- }
- // Relais
- pinMode(relais1pin, OUTPUT);
- pinMode(relais2pin, OUTPUT);
-
- door_stop();
- }
-
- void loop() {
- //
-
- while (true) {
-
- delay(1000);
- mqttClient.loop();
- }
-
- }
|