|
- #include <SPI.h>
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- #include <Wire.h>
- #include "I2Cdev.h"
- #include "IAQ2000.h"
- #include <PubSubClient.h>
- #include <dht.h>
- #include <Dns.h>
-
- #define dht_apin 5
-
- IAQ2000 iaq;
-
- DHT dht(dht_apin, DHT11);
-
- unsigned long loopcnt=0;
-
- // DNS
- DNSClient dnClient;
-
- uint16_t airQuality;
- int val = 0;
- float Temperatur;
- float Humidity;
-
- // 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;
-
- // 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;
-
- //MQTT
- const char *MQTT_BROKER = "loxberry";
- IPAddress mqttbroker(192, 168, 11, 35 );
- EthernetClient ethClient;
- PubSubClient MQTTclient(ethClient);
-
- // 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 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
- SPI.begin();
- sendUDP("Raumluft - aktiv"); // send UDP Ready
-
- // start the Ethernet connection and the server:
- server.begin();
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
-
- // initialize device
- Serial.println("Initialisierung des Sensor...");
- iaq.initialize();
-
- Serial.println(
- iaq.testConnection() ? "iAQ-2000 erfolgreich" : "iAQ-2000 Fehler");
-
- dht.begin(); //DHT11 Sensor starten
-
- //Serial leeren
- clearAll();
-
- // //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(dnClient.getHostByName(MQTT_BROKER, mqttbroker, 10));
- // mqttbroker.fromString("192.168.1.127");
- // Serial.println(mqttbroker);
- // }
-
- // MQTT
- MQTTclient.setServer(mqttbroker, 1883);
- //MQTTclient.setCallback(MQTTcallback);
-
- //Serial.println("setup done");
- dht.read();
- airQuality = iaq.getIaqpred();
- Temperatur = dht.readTemperature();
- Humidity = dht.readHumidity();
-
- }
-
- void loop() {
- String aq = String(airQuality);
- String temp = String(Temperatur, 2);
- String hum = String(Humidity, 2);
- char buff[20];
-
-
- dht.read(); //read data from pin 5 (DHT11)
- airQuality = iaq.getIaqpred();
- Temperatur = dht.readTemperature();
- Humidity = dht.readHumidity();
- //MQTT
- if (!MQTTclient.connected()) {
- MQTTclient.connect("Arduino", "loxberry", "OSVL0AMqISFXgr5g");
- 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");
- }
-
- if (loopcnt++ % 1000 == 0) {
- temp.toCharArray(buff, temp.length());
- MQTTclient.publish("Arduino/Stall/Temperatur", buff);
- aq.toCharArray(buff, aq.length() + 1);
- MQTTclient.publish("Arduino/Stall/CO2", buff);
- Serial.println("CO2 buff");
- hum.toCharArray(buff, hum.length());
- MQTTclient.publish("Arduino/Stall/Humidity", buff);
- snprintf(buff, 19, "%ld", millis());
- Serial.println("Publish message: ");
- Serial.println(buff);
- MQTTclient.publish("Arduino/Stall/data/Alive", buff);
- Serial.println("MQTT done");
- loopcnt = 1;
- }
- // MQTTclient.loop(); // Schleife für MQTT
-
- // schaut on ein UDP Befehl empfangen wurde
- checkUDP();
-
- if (!strcmp(packetBuffer, "000")) {
- Serial.print("CO2 = ");
- Serial.print(airQuality);
- Serial.print(" ");
- Serial.println("[ppm]");
-
- // Wert wird auf 3000ppm begrnezt
- if (airQuality > 3000) {
- aq = "3000";
- } else {
- aq = airQuality;
- }
- sendUDP(aq);
- }
-
- if (!strcmp(packetBuffer, "001")) {
- sendUDP(temp);
- }
-
- if (!strcmp(packetBuffer, "002")) {
- sendUDP(hum);
- }
- clearAll();
- //Serial.println("UDP done");
-
- // 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.println("Current Humidity = "); //Prints information within qoutation
- client.println(hum); //Prints the Humidity read from the DHT11 on PIN 5
- client.println("% ");
- client.println("Temperature = ");
- client.println(temp); //Prints the temperature read from the DHT11 on PIN 5
- client.println("C ");
- client.println("CO2 = ");
- client.println(aq);
- client.println(" ");
- client.println("[ppm]");
- 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");
-
- }
- //Serial.println("HTTP done");
- }
-
- //// 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);
- // }
- //}
|