|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- #include <Wire.h>
- #include <BME280I2C.h>
- #include <PubSubClient.h>
- #include <Dns.h>
-
-
-
- BME280I2C bme;
-
- float Temperatur;
- float Humidity;
- float Pressure;
-
- // Netzwerk-Einstellungen
- // MAC-Adresse darf nur einmal im Netz vohanden sein
- // Fuer jedes Geraet aendern!!
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x1A };
-
- // Port fur Daten-Empfang
- unsigned int localPort = 5010;
-
- char msg[20];
- // IP Adresse Loxone-MiniServer
- IPAddress RecipientIP;
-
- // Port Loxone-MiniServer... An diesen Port werden die Daten gesendet
- unsigned int RecipientPort = 5010;
-
- // Buffer fuer Daten-Empfang
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
-
- // Start der UDP-Instanz
- EthernetUDP Udp;
-
- // DNS
- DNSClient dnClient;
-
- // Initialize the Ethernet server library
- // with the IP address and port you want to use
- // (port 80 is default for HTTP):
- EthernetServer server(80);
-
- void sendUDP(String);
- void checkUDP();
- void clearAll();
- void MQTTcallback(char*, byte*, unsigned int);
-
- //MQTT
- const char *MQTT_BROKER = "loxberry";
- IPAddress mqttbroker(192,168,1,104);
- EthernetClient ethClient;
- PubSubClient MQTTclient(ethClient);
-
- void setup() {
- Wire.begin();
- Serial.begin(9600);
- //serset w5100
- Ethernet.begin(mac);
- if (!Ethernet.localIP()) {
- Serial.println("dhcp failed");
- Serial.println(Ethernet.linkStatus());
- } else {
- Serial.print("IP is ");
- Serial.println(Ethernet.localIP());
- }
- Udp.begin(localPort); // Start UDP
- Serial.println("Raumluft - aktiv"); // send UDP Ready
-
- // start the Ethernet connection and the server:
- server.begin();
-
-
-
- while (!bme.begin()) {
- delay(1000);
- Serial.println("No valid BME280 sensor, check wiring! retray");
- }
-
- Serial.println("1");
-
- //DNS Client starten
- dnClient.begin(Ethernet.dnsServerIP());
- Serial.print("dns server is at ");
- Serial.println(Ethernet.dnsServerIP());
- if (dnClient.getHostByName(MQTT_BROKER, mqttbroker,10) == 1) {
- Serial.print(F("loxberry = "));
- Serial.println(mqttbroker);
- } else {
- Serial.println("dns failed ");
- Serial.println( (MQTT_BROKER, mqttbroker,10));
- mqttbroker.fromString("192.168.1.127");
- Serial.println(mqttbroker);
- }
-
- // MQTT
- MQTTclient.setServer(mqttbroker, 11884);
- MQTTclient.setCallback(MQTTcallback);
-
- //Serial leeren
- clearAll();
-
- Serial.println("Warte auf UDP-Befehl");
-
- }
-
- void loop() {
- String temp = String(Temperatur, 2);
- String hum = String(Humidity, 2);
- String press = String(Pressure / 100, 2);
- char buff[10];
- bme.read(Pressure, Temperatur, Humidity);
-
- //MQTT
- if (!MQTTclient.connected()) {
- MQTTclient.connect("Arduino1");
- Serial.print("MQTT Client state:");
- Serial.println(MQTTclient.state());
- // Abonieren von Nachrichten mit dem angegebenen Topic
- //MQTTclient.subscribe("/Stall/arduino1/#");
- Serial.println("connected to MQTT");
- }
- temp.toCharArray(buff,temp.length());
- MQTTclient.publish("/Stall/Temperatur", buff);
- press.toCharArray(buff,press.length());
- MQTTclient.publish("/Stall/Pressure", buff);
- hum.toCharArray(buff,hum.length());
- MQTTclient.publish("/Stall/Humidity", buff);
-
- // MQTTclient.loop(); // Schleife fr MQTT
-
- // schaut on ein UDP Befehl empfangen wurde
- checkUDP();
-
- if (!strcmp(packetBuffer, "001")) {
- sendUDP(temp);
- }
-
- if (!strcmp(packetBuffer, "002")) {
- sendUDP(hum);
- }
- if (!strcmp(packetBuffer, "003")) {
- sendUDP(press);
- }
- clearAll();
-
- snprintf(msg, 50, "Alive since %ld milliseconds", millis());
- Serial.println("Publish message: ");
- Serial.println(msg);
- MQTTclient.publish("/home/data", msg);
-
- // listen for incoming clients
- EthernetClient client = server.available();
- if (client) {
- Serial.println("new client");
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- //Serial.write(c);
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == '\n' && currentLineIsBlank) {
- // send a standard http response header
-
-
- client.print("Feuchtigkeit = "); //Prints information within qoutation
- client.print(hum); //Prints the Humidity read from the DHT11 on PIN 5
- client.println("% ");
- client.print("Temperature = ");
- client.print(temp); //Prints the temperature read from the DHT11 on PIN 5
- client.println("C ");
- client.print("Luftdruck = ");
- client.print(press);
- client.println(" [hPa]");
-
- break;
- }
- if (c == '\n') {
- // you're starting a new line
- currentLineIsBlank = true;
- } else if (c != '\r') {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- Serial.println("client disonnected");
-
- }
- delay(1000);
- }
-
- //// Module ////
- // Serial-Speicher loeschen
- void clearAll() {
- // Paket-Buffer leeren
- for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++)
- packetBuffer[i] = (char) 0;
- }
-
- // empfangene UDP-Befehle auswerten
- void checkUDP() {
- // pruefen ob Daten vorhanden sind
- int packetSize = Udp.parsePacket();
- if (packetSize) {
- Udp.read(packetBuffer, packetSize);
- Serial.print("Packet Content: ");
- Serial.println(packetBuffer);
- RecipientIP = Udp.remoteIP();
- Serial.print("Remote IP: ");
- Serial.println(RecipientIP);
- }
- delay(10);
- }
-
- // UDP-Befehl senden
- void sendUDP(String text) {
- Udp.beginPacket(RecipientIP, RecipientPort);
- Udp.print(text);
- Udp.endPacket();
- delay(10);
- }
-
- void MQTTcallback(char *topic, byte *payload, unsigned int length) {
- Serial.print("Received message [");
- Serial.print(topic);
- Serial.print("] ");
- char msg[length + 1];
- for (unsigned int i = 0; i < length; i++) {
- Serial.print((char) payload[i]);
- msg[i] = (char) payload[i];
- }
- Serial.println("keine Ahnung");
-
- msg[length] = '\0';
- Serial.println(msg);
-
- if (strcmp(msg, "on") == 0) {
- digitalWrite(13, HIGH);
- } else if (strcmp(msg, "off") == 0) {
- digitalWrite(13, LOW);
- }
- }
|