Luftdruck, Luftfeuchtigkeit und Temperatur mit UDP, Webservice und MQTT
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

253 lines
6.1KB

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