You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5KB

  1. #include <Ethernet.h>
  2. #include <PubSubClient.h>
  3. #include <string.h>
  4. // Update these with values suitable for your network.
  5. byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
  6. EthernetClient ethClient;
  7. PubSubClient mqttClient(ethClient);
  8. // relais steuerung
  9. int relais1pin = 2;
  10. int relais2pin = 3;
  11. void door_open() {
  12. // Rechtslauf
  13. digitalWrite(relais1pin, HIGH);
  14. digitalWrite(relais2pin, LOW);
  15. Serial.println("Tür auf");
  16. }
  17. void door_close() {
  18. // Linkslauf
  19. digitalWrite(relais1pin, LOW);
  20. digitalWrite(relais2pin, HIGH);
  21. Serial.println("Tür zu");
  22. }
  23. void door_stop() {
  24. // keine Bewegung
  25. digitalWrite(relais1pin, HIGH);
  26. digitalWrite(relais2pin, HIGH);
  27. Serial.println("Tür stop");
  28. }
  29. void callback(char *topic, byte *payload, unsigned int length) {
  30. Serial.print("Message arrived [");
  31. Serial.print(topic);
  32. Serial.print("] ");
  33. for (unsigned int i = 0; i < length; i++) {
  34. Serial.print((char) payload[i]);
  35. }
  36. Serial.println();
  37. if (strcmp(topic, "loxone/hof/Gansetorauf")==0) {
  38. if (payload[0] == '0') {
  39. door_stop();
  40. mqttClient.publish("Arduino-1/Hof/Gansetorauf", "0");
  41. } else {
  42. door_open();
  43. mqttClient.publish("Arduino-1/Hof/Gansetorauf", "1");
  44. }
  45. } else if (strcmp(topic, "loxone/hof/Gansetorzu")==0) {
  46. if (payload[0] == '0') {
  47. door_stop();
  48. mqttClient.publish("Arduino-1/Hof/Gansetorzu", "0");
  49. } else {
  50. door_close();
  51. mqttClient.publish("Arduino-1/Hof/Gansetorzu", "1");
  52. }
  53. }
  54. }
  55. void setup() {
  56. // Open serial communications and wait for port to open:
  57. Serial.begin(9600);
  58. while (!Serial) {
  59. ; // wait for serial port to connect. Needed for native USB port only
  60. }
  61. Ethernet.begin(mac);
  62. // Allow the hardware to sort itself out
  63. delay(1500);
  64. //mqttClient.setServer(server, 1883);
  65. mqttClient.setServer("192.168.11.35", 1883);
  66. mqttClient.setCallback(callback);
  67. if (mqttClient.connect("arduino-1", "loxberry", "OSVL0AMqISFXgr5g")) {
  68. // connection succeeded
  69. Serial.println("Connected ");
  70. boolean r = mqttClient.subscribe("loxone/hof/Gansetorauf");
  71. Serial.println("loxone/hof/Gansetorauf subscribe ");
  72. Serial.println(r);
  73. r = mqttClient.subscribe("loxone/hof/Gansetorzu");
  74. Serial.println("loxone/hof/Gansetorzu subscribe ");
  75. Serial.println(r);
  76. } else {
  77. // connection failed
  78. // mqttClient.state() will provide more information
  79. // on why it failed.
  80. Serial.println("Connection failed ");
  81. }
  82. // Relais
  83. pinMode(relais1pin, OUTPUT);
  84. pinMode(relais2pin, OUTPUT);
  85. door_stop();
  86. }
  87. void loop() {
  88. //
  89. while (true) {
  90. delay(1000);
  91. mqttClient.loop();
  92. }
  93. }