Luftdruck, Luftfeuchtigkeit und Temperatur mit UDP und Webserver
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

220 satır
5.0KB

  1. #include <Ethernet.h>
  2. #include <EthernetUdp.h>
  3. #include <BME280I2C.h>
  4. BME280I2C bme;
  5. uint16_t airQuality;
  6. int val = 0;
  7. float Temperatur;
  8. float Humidity;
  9. float Pressure;
  10. // Netzwerk-Einstellungen
  11. // MAC-Adresse darf nur einmal im Netz vohanden sein
  12. // Fuer jedes Geraet aendern!!
  13. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x1A };
  14. // Port fur Daten-Empfang
  15. unsigned int localPort = 5010;
  16. // IP Adresse Loxone-MiniServer
  17. IPAddress RecipientIP;
  18. // Port Loxone-MiniServer... An diesen Port werden die Daten gesendet
  19. unsigned int RecipientPort = 5010;
  20. // Buffer fuer Daten-Empfang
  21. char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
  22. // Start der UDP-Instanz
  23. EthernetUDP Udp;
  24. // Initialize the Ethernet server library
  25. // with the IP address and port you want to use
  26. // (port 80 is default for HTTP):
  27. EthernetServer server(80);
  28. void setup() {
  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. }
  36. else {
  37. Serial.print("IP is ");
  38. Serial.println(Ethernet.localIP());
  39. }
  40. Udp.begin(localPort); // Start UDP
  41. sendUDP("Raumluft - aktiv"); // send UDP Ready
  42. // start the Ethernet connection and the server:
  43. server.begin();
  44. Serial.print("server is at ");
  45. Serial.println(Ethernet.localIP());
  46. if (!bme.begin()) {
  47. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  48. while (1);
  49. }
  50. //Serial leeren
  51. clearAll();
  52. Serial.println("Warte auf UDP-Befehl");
  53. }
  54. void loop() {
  55. String aq = String (airQuality);
  56. String temp = String (Temperatur, 2);
  57. String hum = String (Humidity, 2);
  58. String press = String(Pressure/100,2);
  59. bme.read(Pressure, Temperatur, Humidity);
  60. // schaut on ein UDP Befehl empfangen wurde
  61. checkUDP();
  62. if (!strcmp(packetBuffer, "000"))
  63. {
  64. Serial.print("CO2 = ");
  65. Serial.print(airQuality);
  66. Serial.print(" ");
  67. Serial.println("[ppm]");
  68. // Wert wird auf 3000ppm begrnezt
  69. if (airQuality > 3000)
  70. {
  71. aq = "3000";
  72. }
  73. else
  74. {
  75. aq = airQuality;
  76. }
  77. sendUDP(aq);
  78. }
  79. if (!strcmp(packetBuffer, "001"))
  80. {
  81. sendUDP (temp);
  82. }
  83. if (!strcmp(packetBuffer, "002"))
  84. {
  85. sendUDP (hum);
  86. }
  87. if (!strcmp(packetBuffer, "003"))
  88. {
  89. sendUDP (press);
  90. }
  91. clearAll();
  92. // listen for incoming clients
  93. EthernetClient client = server.available();
  94. if (client)
  95. {
  96. Serial.println("new client");
  97. // an http request ends with a blank line
  98. boolean currentLineIsBlank = true;
  99. while (client.connected()) {
  100. if (client.available()) {
  101. char c = client.read();
  102. //Serial.write(c);
  103. // if you've gotten to the end of the line (received a newline
  104. // character) and the line is blank, the http request has ended,
  105. // so you can send a reply
  106. if (c == '\n' && currentLineIsBlank) {
  107. // send a standard http response header
  108. client.println("HTTP/1.1 200 OK");
  109. client.println("Content-Type: text/html");
  110. client.println("Connection: close");
  111. client.println();
  112. client.println("<!DOCTYPE HTML>");
  113. client.println("<html>");
  114. // add a meta refresh tag, so the browser pulls again every 5 seconds:
  115. client.println("<meta http-equiv=\"refresh\" content=\"10\">");
  116. // output the value of each analog input pin
  117. client.println("Current Humidity = "); //Prints information within qoutation
  118. client.println(hum); //Prints the Humidity read from the DHT11 on PIN 5
  119. client.println("% ");
  120. client.println("Temperature = ");
  121. client.println(temp); //Prints the temperature read from the DHT11 on PIN 5
  122. client.println("C ");
  123. client.println("Luftdruck = ");
  124. client.println(press);
  125. client.println(" ");
  126. client.println("[hPa]");
  127. client.println("</html>");
  128. break;
  129. }
  130. if (c == '\n') {
  131. // you're starting a new line
  132. currentLineIsBlank = true;
  133. }
  134. else if (c != '\r') {
  135. // you've gotten a character on the current line
  136. currentLineIsBlank = false;
  137. }
  138. }
  139. }
  140. // give the web browser time to receive the data
  141. delay(1);
  142. // close the connection:
  143. client.stop();
  144. Serial.println("client disonnected");
  145. }
  146. }
  147. //// Module ////
  148. // Serial-Speicher loeschen
  149. void clearAll() {
  150. // Paket-Buffer leeren
  151. for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++) packetBuffer[i] = (char)0;
  152. }
  153. // empfangene UDP-Befehle auswerten
  154. void checkUDP()
  155. {
  156. // pruefen ob Daten vorhanden sind
  157. int packetSize = Udp.parsePacket();
  158. if (packetSize)
  159. {
  160. Udp.read(packetBuffer, packetSize);
  161. Serial.print("Packet Content: ");
  162. Serial.println(packetBuffer);
  163. RecipientIP=Udp.remoteIP();
  164. Serial.print("Remote IP: ");
  165. Serial.println(RecipientIP);
  166. }
  167. delay(10);
  168. }
  169. // UDP-Befehl senden
  170. void sendUDP(String text)
  171. {
  172. Udp.beginPacket(RecipientIP, RecipientPort);
  173. Udp.print(text);
  174. Udp.endPacket();
  175. delay(10);
  176. }