|
|
@@ -23,9 +23,6 @@ DNSClient dnClient; |
|
|
|
// (port 80 is default for HTTP): |
|
|
|
EthernetServer server(80); |
|
|
|
|
|
|
|
void sendUDP(String); |
|
|
|
void checkUDP(); |
|
|
|
void clearAll(); |
|
|
|
void MQTTcallback(char*, byte*, unsigned int); |
|
|
|
|
|
|
|
//MQTT |
|
|
@@ -37,7 +34,8 @@ PubSubClient MQTTclient(ethClient); |
|
|
|
void setup() { |
|
|
|
Wire.begin(); |
|
|
|
Serial.begin(9600); |
|
|
|
//serset w5100 |
|
|
|
|
|
|
|
//Ethernet start w5100 |
|
|
|
Ethernet.begin(mac); |
|
|
|
if (!Ethernet.localIP()) { |
|
|
|
Serial.println("dhcp failed"); |
|
|
@@ -46,18 +44,12 @@ void setup() { |
|
|
|
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()); |
|
|
@@ -72,25 +64,19 @@ void setup() { |
|
|
|
mqttbroker.fromString("192.168.1.35"); |
|
|
|
Serial.println(mqttbroker); |
|
|
|
} |
|
|
|
bme.read(Pressure, Temperatur, Humidity); |
|
|
|
|
|
|
|
// MQTT |
|
|
|
MQTTclient.setServer(mqttbroker, 1883); |
|
|
|
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 (loopcnt++ % 1000 == 0) { |
|
|
@@ -99,7 +85,7 @@ void loop() { |
|
|
|
Serial.print("MQTT Client state:"); |
|
|
|
Serial.println(MQTTclient.state()); |
|
|
|
// Abonieren von Nachrichten mit dem angegebenen Topic |
|
|
|
//MQTTclient.subscribe("/Stall/arduino1/#"); |
|
|
|
MQTTclient.subscribe("/Stall/arduino1/#"); |
|
|
|
Serial.println("connected to MQTT"); |
|
|
|
} |
|
|
|
temp.toCharArray(buff, temp.length()); |
|
|
@@ -117,20 +103,6 @@ void loop() { |
|
|
|
|
|
|
|
// MQTTclient.loop(); // Schleife für 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(); |
|
|
|
|
|
|
|
// listen for incoming clients |
|
|
|
EthernetClient client = server.available(); |
|
|
@@ -178,36 +150,6 @@ void loop() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//// 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 ["); |
|
|
|