Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

230 linhas
5.3KB

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