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.

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