Luftdruck, Luftfeuchtigkeit und Temperatur mit UDP, Webservice und MQTT
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ů.

271 řádky
6.5KB

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