standart Framework für HTTP und MQTT implementierung
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <Ethernet.h>
  2. #include <Wire.h>
  3. #include <Dns.h>
  4. #include <PubSubClient.h>
  5. // counter und define fr die Sendeintervalle fr MQTT
  6. #define loopsnd 1000
  7. unsigned long loopcnt=0;
  8. // Netzwerk-Einstellungen
  9. // MAC-Adresse darf nur einmal im Netz vohanden sein
  10. // Fuer jedes Geraet aendern!!
  11. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x1A };
  12. // DNS
  13. DNSClient dnClient;
  14. // Initialize the Ethernet server library
  15. // with the IP address and port you want to use
  16. // (port 80 is default for HTTP):
  17. EthernetServer server(80);
  18. void MQTTcallback(char*, byte*, unsigned int);
  19. //MQTT
  20. const char *MQTT_BROKER = "loxberry";
  21. IPAddress mqttbroker(192, 168, 1, 35);
  22. EthernetClient ethClient;
  23. PubSubClient MQTTclient(ethClient);
  24. void setup() {
  25. Wire.begin();
  26. Serial.begin(9600);
  27. //Ethernet start w5100
  28. Ethernet.begin(mac);
  29. if (!Ethernet.localIP()) {
  30. Serial.println("dhcp failed");
  31. Serial.println(Ethernet.linkStatus());
  32. } else {
  33. Serial.print("IP is ");
  34. Serial.println(Ethernet.localIP());
  35. }
  36. // start the Ethernet connection and the server:
  37. server.begin();
  38. //DNS Client starten
  39. dnClient.begin(Ethernet.dnsServerIP());
  40. Serial.print("dns server is at ");
  41. Serial.println(Ethernet.dnsServerIP());
  42. if (dnClient.getHostByName(MQTT_BROKER, mqttbroker, 10) == 1) {
  43. Serial.print(F("loxberry = "));
  44. Serial.println(mqttbroker);
  45. } else {
  46. Serial.println("dns failed ");
  47. Serial.println(dnClient.getHostByName(MQTT_BROKER, mqttbroker, 10));
  48. mqttbroker.fromString("192.168.1.35");
  49. Serial.println(mqttbroker);
  50. }
  51. // MQTT
  52. MQTTclient.setServer(mqttbroker, 1883);
  53. MQTTclient.setCallback(MQTTcallback);
  54. Serial.println("Warte auf UDP-Befehl");
  55. }
  56. void loop() {
  57. char buff[10];
  58. //MQTT
  59. if (loopcnt++ % 1000 == 0) {
  60. if (!MQTTclient.connected()) {
  61. MQTTclient.connect("Arduino", "loxberry", "OSVL0AMqISFXgr5g");
  62. Serial.print("MQTT Client state:");
  63. Serial.println(MQTTclient.state());
  64. // Abonieren von Nachrichten mit dem angegebenen Topic
  65. MQTTclient.subscribe("/Stall/arduino1/#");
  66. Serial.println("connected to MQTT");
  67. }
  68. temp.toCharArray(buff, temp.length());
  69. MQTTclient.publish("Arduino/Aussen/Temperatur", buff);
  70. press.toCharArray(buff, press.length());
  71. MQTTclient.publish("Arduino/Aussen/Pressure", buff);
  72. hum.toCharArray(buff, hum.length());
  73. MQTTclient.publish("Arduino/Aussen/Humidity", buff);
  74. snprintf(msg, 50, "%ld", millis());
  75. Serial.println("Publish message: ");
  76. Serial.println(msg);
  77. MQTTclient.publish("Arduino/Aussen/data/Alive", msg);
  78. loopcnt = 1;
  79. }
  80. // MQTTclient.loop(); // Schleife fr MQTT
  81. // listen for incoming clients
  82. EthernetClient client = server.available();
  83. if (client) {
  84. Serial.println("new client");
  85. // an http request ends with a blank line
  86. boolean currentLineIsBlank = true;
  87. while (client.connected()) {
  88. if (client.available()) {
  89. char c = client.read();
  90. //Serial.write(c);
  91. // if you've gotten to the end of the line (received a newline
  92. // character) and the line is blank, the http request has ended,
  93. // so you can send a reply
  94. if (c == '\n' && currentLineIsBlank) {
  95. // send a standard http response header
  96. client.print("Feuchtigkeit = "); //Prints information within qoutation
  97. client.print(hum); //Prints the Humidity read from the DHT11 on PIN 5
  98. client.println(" % ");
  99. client.print("Temperature = ");
  100. client.print(temp); //Prints the temperature read from the DHT11 on PIN 5
  101. client.println(" C ");
  102. client.print("Luftdruck = ");
  103. client.print(press);
  104. client.println(" [hPa]");
  105. break;
  106. }
  107. if (c == '\n') {
  108. // you're starting a new line
  109. currentLineIsBlank = true;
  110. } else if (c != '\r') {
  111. // you've gotten a character on the current line
  112. currentLineIsBlank = false;
  113. }
  114. }
  115. }
  116. // give the web browser time to receive the data
  117. delay(1);
  118. // close the connection:
  119. client.stop();
  120. Serial.println("client disonnected");
  121. }
  122. }
  123. void MQTTcallback(char *topic, byte *payload, unsigned int length) {
  124. Serial.print("Received message [");
  125. Serial.print(topic);
  126. Serial.print("] ");
  127. char msg[length + 1];
  128. for (unsigned int i = 0; i < length; i++) {
  129. Serial.print((char) payload[i]);
  130. msg[i] = (char) payload[i];
  131. }
  132. Serial.println("keine Ahnung");
  133. msg[length] = '\0';
  134. Serial.println(msg);
  135. if (strcmp(msg, "on") == 0) {
  136. digitalWrite(13, HIGH);
  137. } else if (strcmp(msg, "off") == 0) {
  138. digitalWrite(13, LOW);
  139. }
  140. }