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.

117 line
2.6KB

  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 mqttreconnect() {
  56. if (mqttClient.connect("arduino-1", "loxberry", "OSVL0AMqISFXgr5g")) {
  57. // connection succeeded
  58. Serial.println("Connected ");
  59. boolean r = mqttClient.subscribe("loxone/hof/Gansetorauf");
  60. Serial.println("loxone/hof/Gansetorauf subscribe ");
  61. Serial.println(r);
  62. r = mqttClient.subscribe("loxone/hof/Gansetorzu");
  63. Serial.println("loxone/hof/Gansetorzu subscribe ");
  64. Serial.println(r);
  65. } else {
  66. // connection failed
  67. // mqttClient.state() will provide more information
  68. // on why it failed.
  69. Serial.println("Connection failed ");
  70. Serial.println(mqttClient.state());
  71. }
  72. }
  73. void setup() {
  74. // Open serial communications and wait for port to open:
  75. Serial.begin(9600);
  76. while (!Serial) {
  77. ; // wait for serial port to connect. Needed for native USB port only
  78. }
  79. Ethernet.begin(mac);
  80. // Allow the hardware to sort itself out
  81. delay(1500);
  82. //mqttClient.setServer(server, 1883);
  83. mqttClient.setServer("192.168.11.35", 1883);
  84. mqttClient.setCallback(callback);
  85. mqttreconnect();
  86. // Relais
  87. pinMode(relais1pin, OUTPUT);
  88. pinMode(relais2pin, OUTPUT);
  89. door_stop();
  90. }
  91. void loop() {
  92. //
  93. while (true) {
  94. if (!mqttClient.loop()) {
  95. mqttreconnect();
  96. }
  97. delay(1000);
  98. }
  99. }