Luftdruck, Luftfeuchtigkeit und Temperatur mit UDP, Webservice und MQTT
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.

BME280MTTQ.ino 6.2KB

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