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

254 lines
6.0KB

  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. bme.read(Pressure, Temperatur, Humidity);
  76. // MQTT
  77. MQTTclient.setServer(mqttbroker, 1883);
  78. MQTTclient.setCallback(MQTTcallback);
  79. //Serial leeren
  80. clearAll();
  81. Serial.println("Warte auf UDP-Befehl");
  82. }
  83. void loop() {
  84. String temp = String(Temperatur, 2);
  85. String hum = String(Humidity, 2);
  86. String press = String(Pressure / 100, 2);
  87. char buff[10];
  88. bme.read(Pressure, Temperatur, Humidity);
  89. //MQTT
  90. if (!MQTTclient.connected()) {
  91. MQTTclient.connect("Arduino","loxberry","OSVL0AMqISFXgr5g");
  92. Serial.print("MQTT Client state:");
  93. Serial.println(MQTTclient.state());
  94. // Abonieren von Nachrichten mit dem angegebenen Topic
  95. //MQTTclient.subscribe("/Stall/arduino1/#");
  96. Serial.println("connected to MQTT");
  97. }
  98. temp.toCharArray(buff,temp.length());
  99. MQTTclient.publish("Arduino/Stall/Temperatur", buff);
  100. press.toCharArray(buff,press.length());
  101. MQTTclient.publish("Arduino/Stall/Pressure", buff);
  102. hum.toCharArray(buff,hum.length());
  103. MQTTclient.publish("Arduino/Stall/Humidity", buff);
  104. snprintf(msg, 50, "%ld", millis());
  105. Serial.println("Publish message: ");
  106. Serial.println(msg);
  107. MQTTclient.publish("Arduino/Stall/data/Alive", msg);
  108. // MQTTclient.loop(); // Schleife für MQTT
  109. // schaut on ein UDP Befehl empfangen wurde
  110. checkUDP();
  111. if (!strcmp(packetBuffer, "001")) {
  112. sendUDP(temp);
  113. }
  114. if (!strcmp(packetBuffer, "002")) {
  115. sendUDP(hum);
  116. }
  117. if (!strcmp(packetBuffer, "003")) {
  118. sendUDP(press);
  119. }
  120. clearAll();
  121. // listen for incoming clients
  122. EthernetClient client = server.available();
  123. if (client) {
  124. Serial.println("new client");
  125. // an http request ends with a blank line
  126. boolean currentLineIsBlank = true;
  127. while (client.connected()) {
  128. if (client.available()) {
  129. char c = client.read();
  130. //Serial.write(c);
  131. // if you've gotten to the end of the line (received a newline
  132. // character) and the line is blank, the http request has ended,
  133. // so you can send a reply
  134. if (c == '\n' && currentLineIsBlank) {
  135. // send a standard http response header
  136. client.print("Feuchtigkeit = "); //Prints information within qoutation
  137. client.print(hum); //Prints the Humidity read from the DHT11 on PIN 5
  138. client.println("% ");
  139. client.print("Temperature = ");
  140. client.print(temp); //Prints the temperature read from the DHT11 on PIN 5
  141. client.println("C ");
  142. client.print("Luftdruck = ");
  143. client.print(press);
  144. client.println(" [hPa]");
  145. break;
  146. }
  147. if (c == '\n') {
  148. // you're starting a new line
  149. currentLineIsBlank = true;
  150. } else if (c != '\r') {
  151. // you've gotten a character on the current line
  152. currentLineIsBlank = false;
  153. }
  154. }
  155. }
  156. // give the web browser time to receive the data
  157. delay(1);
  158. // close the connection:
  159. client.stop();
  160. Serial.println("client disonnected");
  161. }
  162. delay(1000);
  163. }
  164. //// Module ////
  165. // Serial-Speicher loeschen
  166. void clearAll() {
  167. // Paket-Buffer leeren
  168. for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++)
  169. packetBuffer[i] = (char) 0;
  170. }
  171. // empfangene UDP-Befehle auswerten
  172. void checkUDP() {
  173. // pruefen ob Daten vorhanden sind
  174. int packetSize = Udp.parsePacket();
  175. if (packetSize) {
  176. Udp.read(packetBuffer, packetSize);
  177. Serial.print("Packet Content: ");
  178. Serial.println(packetBuffer);
  179. RecipientIP = Udp.remoteIP();
  180. Serial.print("Remote IP: ");
  181. Serial.println(RecipientIP);
  182. }
  183. delay(10);
  184. }
  185. // UDP-Befehl senden
  186. void sendUDP(String text) {
  187. Udp.beginPacket(RecipientIP, RecipientPort);
  188. Udp.print(text);
  189. Udp.endPacket();
  190. delay(10);
  191. }
  192. void MQTTcallback(char *topic, byte *payload, unsigned int length) {
  193. Serial.print("Received message [");
  194. Serial.print(topic);
  195. Serial.print("] ");
  196. char msg[length + 1];
  197. for (unsigned int i = 0; i < length; i++) {
  198. Serial.print((char) payload[i]);
  199. msg[i] = (char) payload[i];
  200. }
  201. Serial.println("keine Ahnung");
  202. msg[length] = '\0';
  203. Serial.println(msg);
  204. if (strcmp(msg, "on") == 0) {
  205. digitalWrite(13, HIGH);
  206. } else if (strcmp(msg, "off") == 0) {
  207. digitalWrite(13, LOW);
  208. }
  209. }