Luftdruck, Luftfeuchtigkeit und Temperatur mit UDP, Webservice und MQTT
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BME280MTTQ.ino 5.9KB

il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. 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. char msg[20];
  18. // IP Adresse Loxone-MiniServer
  19. IPAddress RecipientIP;
  20. // Port Loxone-MiniServer... An diesen Port werden die Daten gesendet
  21. unsigned int RecipientPort = 5010;
  22. // Buffer fuer Daten-Empfang
  23. char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
  24. // Start der UDP-Instanz
  25. EthernetUDP Udp;
  26. // DNS
  27. DNSClient dnClient;
  28. // Initialize the Ethernet server library
  29. // with the IP address and port you want to use
  30. // (port 80 is default for HTTP):
  31. EthernetServer server(80);
  32. void sendUDP(String);
  33. void checkUDP();
  34. void clearAll();
  35. void MQTTcallback(char*, byte*, unsigned int);
  36. //MQTT
  37. const char *MQTT_BROKER = "loxberry";
  38. IPAddress mqttbroker(192,168,1,104);
  39. EthernetClient ethClient;
  40. PubSubClient MQTTclient(ethClient);
  41. void setup() {
  42. Wire.begin();
  43. Serial.begin(9600);
  44. //serset w5100
  45. Ethernet.begin(mac);
  46. if (!Ethernet.localIP()) {
  47. Serial.println("dhcp failed");
  48. Serial.println(Ethernet.linkStatus());
  49. } else {
  50. Serial.print("IP is ");
  51. Serial.println(Ethernet.localIP());
  52. }
  53. Udp.begin(localPort); // Start UDP
  54. Serial.println("Raumluft - aktiv"); // send UDP Ready
  55. // start the Ethernet connection and the server:
  56. server.begin();
  57. while (!bme.begin()) {
  58. delay(1000);
  59. Serial.println("No valid BME280 sensor, check wiring! retray");
  60. }
  61. Serial.println("1");
  62. //DNS Client starten
  63. dnClient.begin(Ethernet.dnsServerIP());
  64. Serial.print("dns server is at ");
  65. Serial.println(Ethernet.dnsServerIP());
  66. if (dnClient.getHostByName(MQTT_BROKER, mqttbroker,10) == 1) {
  67. Serial.print(F("loxberry = "));
  68. Serial.println(mqttbroker);
  69. } else {
  70. Serial.println("dns failed ");
  71. Serial.println( (MQTT_BROKER, mqttbroker,10));
  72. mqttbroker.fromString("192.168.1.127");
  73. Serial.println(mqttbroker);
  74. }
  75. // MQTT
  76. MQTTclient.setServer(mqttbroker, 11884);
  77. MQTTclient.setCallback(MQTTcallback);
  78. //Serial leeren
  79. clearAll();
  80. Serial.println("Warte auf UDP-Befehl");
  81. }
  82. void loop() {
  83. String temp = String(Temperatur, 2);
  84. String hum = String(Humidity, 2);
  85. String press = String(Pressure / 100, 2);
  86. char buff[10];
  87. bme.read(Pressure, Temperatur, Humidity);
  88. //MQTT
  89. if (!MQTTclient.connected()) {
  90. MQTTclient.connect("Arduino1");
  91. Serial.print("MQTT Client state:");
  92. Serial.println(MQTTclient.state());
  93. // Abonieren von Nachrichten mit dem angegebenen Topic
  94. //MQTTclient.subscribe("/Stall/arduino1/#");
  95. Serial.println("connected to MQTT");
  96. }
  97. temp.toCharArray(buff,temp.length());
  98. MQTTclient.publish("/Stall/Temperatur", buff);
  99. press.toCharArray(buff,press.length());
  100. MQTTclient.publish("/Stall/Pressure", buff);
  101. hum.toCharArray(buff,hum.length());
  102. MQTTclient.publish("/Stall/Humidity", buff);
  103. // MQTTclient.loop(); // Schleife fr MQTT
  104. // schaut on ein UDP Befehl empfangen wurde
  105. checkUDP();
  106. if (!strcmp(packetBuffer, "001")) {
  107. sendUDP(temp);
  108. }
  109. if (!strcmp(packetBuffer, "002")) {
  110. sendUDP(hum);
  111. }
  112. if (!strcmp(packetBuffer, "003")) {
  113. sendUDP(press);
  114. }
  115. clearAll();
  116. snprintf(msg, 50, "Alive since %ld milliseconds", millis());
  117. Serial.println("Publish message: ");
  118. Serial.println(msg);
  119. MQTTclient.publish("/home/data", msg);
  120. // listen for incoming clients
  121. EthernetClient client = server.available();
  122. if (client) {
  123. Serial.println("new client");
  124. // an http request ends with a blank line
  125. boolean currentLineIsBlank = true;
  126. while (client.connected()) {
  127. if (client.available()) {
  128. char c = client.read();
  129. //Serial.write(c);
  130. // if you've gotten to the end of the line (received a newline
  131. // character) and the line is blank, the http request has ended,
  132. // so you can send a reply
  133. if (c == '\n' && currentLineIsBlank) {
  134. // send a standard http response header
  135. client.print("Feuchtigkeit = "); //Prints information within qoutation
  136. client.print(hum); //Prints the Humidity read from the DHT11 on PIN 5
  137. client.println("% ");
  138. client.print("Temperature = ");
  139. client.print(temp); //Prints the temperature read from the DHT11 on PIN 5
  140. client.println("C ");
  141. client.print("Luftdruck = ");
  142. client.print(press);
  143. client.println(" [hPa]");
  144. break;
  145. }
  146. if (c == '\n') {
  147. // you're starting a new line
  148. currentLineIsBlank = true;
  149. } else if (c != '\r') {
  150. // you've gotten a character on the current line
  151. currentLineIsBlank = false;
  152. }
  153. }
  154. }
  155. // give the web browser time to receive the data
  156. delay(1);
  157. // close the connection:
  158. client.stop();
  159. Serial.println("client disonnected");
  160. }
  161. delay(1000);
  162. }
  163. //// Module ////
  164. // Serial-Speicher loeschen
  165. void clearAll() {
  166. // Paket-Buffer leeren
  167. for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++)
  168. packetBuffer[i] = (char) 0;
  169. }
  170. // empfangene UDP-Befehle auswerten
  171. void checkUDP() {
  172. // pruefen ob Daten vorhanden sind
  173. int packetSize = Udp.parsePacket();
  174. if (packetSize) {
  175. Udp.read(packetBuffer, packetSize);
  176. Serial.print("Packet Content: ");
  177. Serial.println(packetBuffer);
  178. RecipientIP = Udp.remoteIP();
  179. Serial.print("Remote IP: ");
  180. Serial.println(RecipientIP);
  181. }
  182. delay(10);
  183. }
  184. // UDP-Befehl senden
  185. void sendUDP(String text) {
  186. Udp.beginPacket(RecipientIP, RecipientPort);
  187. Udp.print(text);
  188. Udp.endPacket();
  189. delay(10);
  190. }
  191. void MQTTcallback(char *topic, byte *payload, unsigned int length) {
  192. Serial.print("Received message [");
  193. Serial.print(topic);
  194. Serial.print("] ");
  195. char msg[length + 1];
  196. for (unsigned int i = 0; i < length; i++) {
  197. Serial.print((char) payload[i]);
  198. msg[i] = (char) payload[i];
  199. }
  200. Serial.println("keine Ahnung");
  201. msg[length] = '\0';
  202. Serial.println(msg);
  203. if (strcmp(msg, "on") == 0) {
  204. digitalWrite(13, HIGH);
  205. } else if (strcmp(msg, "off") == 0) {
  206. digitalWrite(13, LOW);
  207. }
  208. }