#include #include #include #include // counter und define für die Sendeintervalle für MQTT #define loopsnd 1000 unsigned long loopcnt=0; // Netzwerk-Einstellungen // MAC-Adresse darf nur einmal im Netz vohanden sein // Fuer jedes Geraet aendern!! byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x1A }; // 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 MQTTcallback(char*, byte*, unsigned int); //MQTT const char *MQTT_BROKER = "loxberry"; IPAddress mqttbroker(192, 168, 1, 35); EthernetClient ethClient; PubSubClient MQTTclient(ethClient); void setup() { Wire.begin(); Serial.begin(9600); //Ethernet start w5100 Ethernet.begin(mac); if (!Ethernet.localIP()) { Serial.println("dhcp failed"); Serial.println(Ethernet.linkStatus()); } else { Serial.print("IP is "); Serial.println(Ethernet.localIP()); } // start the Ethernet connection and the server: server.begin(); //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.35"); Serial.println(mqttbroker); } // MQTT MQTTclient.setServer(mqttbroker, 1883); MQTTclient.setCallback(MQTTcallback); Serial.println("Warte auf UDP-Befehl"); } void loop() { char buff[10]; //MQTT if (loopcnt++ % 1000 == 0) { 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"); } temp.toCharArray(buff, temp.length()); MQTTclient.publish("Arduino/Aussen/Temperatur", buff); press.toCharArray(buff, press.length()); MQTTclient.publish("Arduino/Aussen/Pressure", buff); hum.toCharArray(buff, hum.length()); MQTTclient.publish("Arduino/Aussen/Humidity", buff); snprintf(msg, 50, "%ld", millis()); Serial.println("Publish message: "); Serial.println(msg); MQTTclient.publish("Arduino/Aussen/data/Alive", msg); loopcnt = 1; } // MQTTclient.loop(); // Schleife für MQTT // 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"); } } 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); } }