Luftdruck, Luftfeuchtigkeit und Temperatur mit UDP und Webserver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
5.0KB

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