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.

277 lines
6.8KB

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