standart Framework für HTTP und MQTT implementierung
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

4 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 sendUDP(String);
  19. void checkUDP();
  20. void clearAll();
  21. void MQTTcallback(char*, byte*, unsigned int);
  22. //MQTT
  23. const char *MQTT_BROKER = "loxberry";
  24. IPAddress mqttbroker(192, 168, 1, 35);
  25. EthernetClient ethClient;
  26. PubSubClient MQTTclient(ethClient);
  27. void setup() {
  28. Wire.begin();
  29. Serial.begin(9600);
  30. //serset w5100
  31. Ethernet.begin(mac);
  32. if (!Ethernet.localIP()) {
  33. Serial.println("dhcp failed");
  34. Serial.println(Ethernet.linkStatus());
  35. } else {
  36. Serial.print("IP is ");
  37. Serial.println(Ethernet.localIP());
  38. }
  39. Udp.begin(localPort); // Start UDP
  40. Serial.println("Raumluft - aktiv"); // send UDP Ready
  41. // start the Ethernet connection and the server:
  42. server.begin();
  43. while (!bme.begin()) {
  44. delay(1000);
  45. Serial.println("No valid BME280 sensor, check wiring! retray");
  46. }
  47. Serial.println("1");
  48. //DNS Client starten
  49. dnClient.begin(Ethernet.dnsServerIP());
  50. Serial.print("dns server is at ");
  51. Serial.println(Ethernet.dnsServerIP());
  52. if (dnClient.getHostByName(MQTT_BROKER, mqttbroker, 10) == 1) {
  53. Serial.print(F("loxberry = "));
  54. Serial.println(mqttbroker);
  55. } else {
  56. Serial.println("dns failed ");
  57. Serial.println(dnClient.getHostByName(MQTT_BROKER, mqttbroker, 10));
  58. mqttbroker.fromString("192.168.1.35");
  59. Serial.println(mqttbroker);
  60. }
  61. bme.read(Pressure, Temperatur, Humidity);
  62. // MQTT
  63. MQTTclient.setServer(mqttbroker, 1883);
  64. MQTTclient.setCallback(MQTTcallback);
  65. //Serial leeren
  66. clearAll();
  67. Serial.println("Warte auf UDP-Befehl");
  68. }
  69. void loop() {
  70. String temp = String(Temperatur, 2);
  71. String hum = String(Humidity, 2);
  72. String press = String(Pressure / 100, 2);
  73. char buff[10];
  74. bme.read(Pressure, Temperatur, Humidity);
  75. //MQTT
  76. if (loopcnt++ % 1000 == 0) {
  77. if (!MQTTclient.connected()) {
  78. MQTTclient.connect("Arduino", "loxberry", "OSVL0AMqISFXgr5g");
  79. Serial.print("MQTT Client state:");
  80. Serial.println(MQTTclient.state());
  81. // Abonieren von Nachrichten mit dem angegebenen Topic
  82. //MQTTclient.subscribe("/Stall/arduino1/#");
  83. Serial.println("connected to MQTT");
  84. }
  85. temp.toCharArray(buff, temp.length());
  86. MQTTclient.publish("Arduino/Aussen/Temperatur", buff);
  87. press.toCharArray(buff, press.length());
  88. MQTTclient.publish("Arduino/Aussen/Pressure", buff);
  89. hum.toCharArray(buff, hum.length());
  90. MQTTclient.publish("Arduino/Aussen/Humidity", buff);
  91. snprintf(msg, 50, "%ld", millis());
  92. Serial.println("Publish message: ");
  93. Serial.println(msg);
  94. MQTTclient.publish("Arduino/Aussen/data/Alive", msg);
  95. loopcnt = 1;
  96. }
  97. // MQTTclient.loop(); // Schleife fr MQTT
  98. // schaut on ein UDP Befehl empfangen wurde
  99. checkUDP();
  100. if (!strcmp(packetBuffer, "001")) {
  101. sendUDP(temp);
  102. }
  103. if (!strcmp(packetBuffer, "002")) {
  104. sendUDP(hum);
  105. }
  106. if (!strcmp(packetBuffer, "003")) {
  107. sendUDP(press);
  108. }
  109. clearAll();
  110. // listen for incoming clients
  111. EthernetClient client = server.available();
  112. if (client) {
  113. Serial.println("new client");
  114. // an http request ends with a blank line
  115. boolean currentLineIsBlank = true;
  116. while (client.connected()) {
  117. if (client.available()) {
  118. char c = client.read();
  119. //Serial.write(c);
  120. // if you've gotten to the end of the line (received a newline
  121. // character) and the line is blank, the http request has ended,
  122. // so you can send a reply
  123. if (c == '\n' && currentLineIsBlank) {
  124. // send a standard http response header
  125. client.print("Feuchtigkeit = "); //Prints information within qoutation
  126. client.print(hum); //Prints the Humidity read from the DHT11 on PIN 5
  127. client.println(" % ");
  128. client.print("Temperature = ");
  129. client.print(temp); //Prints the temperature read from the DHT11 on PIN 5
  130. client.println(" C ");
  131. client.print("Luftdruck = ");
  132. client.print(press);
  133. client.println(" [hPa]");
  134. break;
  135. }
  136. if (c == '\n') {
  137. // you're starting a new line
  138. currentLineIsBlank = true;
  139. } else if (c != '\r') {
  140. // you've gotten a character on the current line
  141. currentLineIsBlank = false;
  142. }
  143. }
  144. }
  145. // give the web browser time to receive the data
  146. delay(1);
  147. // close the connection:
  148. client.stop();
  149. Serial.println("client disonnected");
  150. }
  151. }
  152. //// Module ////
  153. // Serial-Speicher loeschen
  154. void clearAll() {
  155. // Paket-Buffer leeren
  156. for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++)
  157. packetBuffer[i] = (char) 0;
  158. }
  159. // empfangene UDP-Befehle auswerten
  160. void checkUDP() {
  161. // pruefen ob Daten vorhanden sind
  162. int packetSize = Udp.parsePacket();
  163. if (packetSize) {
  164. Udp.read(packetBuffer, packetSize);
  165. Serial.print("Packet Content: ");
  166. Serial.println(packetBuffer);
  167. RecipientIP = Udp.remoteIP();
  168. Serial.print("Remote IP: ");
  169. Serial.println(RecipientIP);
  170. }
  171. delay(10);
  172. }
  173. // UDP-Befehl senden
  174. void sendUDP(String text) {
  175. Udp.beginPacket(RecipientIP, RecipientPort);
  176. Udp.print(text);
  177. Udp.endPacket();
  178. delay(10);
  179. }
  180. void MQTTcallback(char *topic, byte *payload, unsigned int length) {
  181. Serial.print("Received message [");
  182. Serial.print(topic);
  183. Serial.print("] ");
  184. char msg[length + 1];
  185. for (unsigned int i = 0; i < length; i++) {
  186. Serial.print((char) payload[i]);
  187. msg[i] = (char) payload[i];
  188. }
  189. Serial.println("keine Ahnung");
  190. msg[length] = '\0';
  191. Serial.println(msg);
  192. if (strcmp(msg, "on") == 0) {
  193. digitalWrite(13, HIGH);
  194. } else if (strcmp(msg, "off") == 0) {
  195. digitalWrite(13, LOW);
  196. }
  197. }