Esp8266 UDP Unicast senden als Arduino Tab.
UnicastClient.ino
// ****************************************************************
// Sketch Esp8266 UDP Unicast Client Modular(Tab)
// created: Jens Fleischer, 2021-02-05
// last mod: Jens Fleischer, 2021-02-05
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp8266
// Software: Esp8266 Arduino Core 2.6.0 - 2.7.4
// Getestet auf: Nodemcu, Wemos D1 Mini Pro
/******************************************************************
Copyright (c) 2021 Jens Fleischer. All rights reserved.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*******************************************************************/
// Diese Version von UDP Unicast Client sollte als Tab eingebunden werden.
// #include <ESP8266WebServer.h> und #include <ArduinoOTA.h> muss im Haupttab aufgerufen werden.
// Die Funktion "setupUdpUnicast();" muss im Setup eingebunden werden.
// Die Übergabe der Nachricht an "sendUnicast("Nachricht");" erfolgt als String-Object.
// Die "WiFiUdp.h" wird durch "ArduinoOTA.h" im Webserver Tab includiert.
/**************************************************************************************/
WiFiUDP Unicast;
void setupUdpUnicast() {
Unicast.begin(1042); // Lokaler UDP Port.
}
void sendUnicast(const String &message) {
IPAddress receiverIP(192, 168, 178, 43); // Senden an diese IP
constexpr uint16_t PORT = 1043; // und diesem UDP Port.
Unicast.beginPacket(receiverIP, PORT);
Unicast.print(message);
Unicast.endPacket();
}
Der Aufruf der Funktion UDP senden ist nach ausführen von "Connect();" möglich.
Du kannst "sendUnicast();" überall in deinem Sketch einbinden.
****************** UDP senden **********************
sendUnicast(localTime()); // Datum und Uhrzeit
sendUnicast(String(millis() / 1000)); // Laufzeit des Esp... in Sekunden
Beispiel:
***************** UDP im Setup *********************
void setup() {
Connect();
.........
sendUnicast("Start Esp8266 mit der IP: " + WiFi.localIP().toString());
......
}
Beispiel:
********** Esp8266 sendet Lebenszeichen ************
void loop() {
......
.........
const unsigned long interval = 1000UL * 5; // Interval in Sekunden einstellen
static unsigned long letzteMillis = 0;
unsigned long aktuelleMillis = millis();
if (aktuelleMillis - letzteMillis >= interval) {
const char* vivit = "Ich bin noch da!";
sendUnicast(vivit);
letzteMillis = aktuelleMillis;
}
......
}
Esp8266 UDP Unicast empfangen als Arduino Tab.
UnicastServer.ino
// ****************************************************************
// Sketch Esp8266 UDP Unicast Server Modular(Tab)
// created: Jens Fleischer, 2021-02-05
// last mod: Jens Fleischer, 2021-02-05
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp8266
// Software: Esp8266 Arduino Core 2.6.0 - 2.7.4
// Getestet auf: Nodemcu, Wemos D1 Mini Pro
/******************************************************************
Copyright (c) 2021 Jens Fleischer. All rights reserved.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*******************************************************************/
// Diese Version von UDP Unicast Server sollte als Tab eingebunden werden.
// #include <ESP8266WebServer.h> und #include <ArduinoOTA.h> muss im Haupttab aufgerufen werden.
// Die Funktion "setupUdpUnicast();" muss im Setup eingebunden werden.
// Um zu empfangen muss die Funktion "unicastServer();" im loop(); aufgerufen werden.
// Die "WiFiUdp.h" wird durch "ArduinoOTA.h" im Webserver Tab includiert.
/**************************************************************************************/
char unicastBuffer[255]; // Puffer für eingehendes Paket.
void unicastServer() {
uint16_t packetSize = Unicast.parsePacket(); // Eingehende UDP-Pakete empfangen.
if (packetSize) { // Prüfen ob UDP-Pakete empfangen wurden.
Unicast.read(unicastBuffer, sizeof(unicastBuffer)); // Einlesen des UDP Paket in den Bufffer.
unicastBuffer[packetSize] = 0; // String Ende hinzufügen.
Serial.println(unicastBuffer); // Visualisierung des empfangenen Packets.
// Otional eine Antwort an den UDP Client senden.
Unicast.beginPacket(Unicast.remoteIP(), Unicast.remotePort());
Unicast.print(WiFi.hostname() + " bekam das Unicast Packet!");
Unicast.endPacket();
}
}
Esp8266 UDP Unicast senden und Antwort vom Server auswerten.
Unicast.ino
// ****************************************************************
// Sketch Esp8266 UDP Unicast Client Server Modular(Tab)
// created: Jens Fleischer, 2021-02-05
// last mod: Jens Fleischer, 2021-02-05
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp8266
// Software: Esp8266 Arduino Core 2.6.0 - 2.7.4
// Getestet auf: Nodemcu, Wemos D1 Mini Pro
/******************************************************************
Copyright (c) 2021 Jens Fleischer. All rights reserved.
This file is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*******************************************************************/
// Diese Version von UDP Unicast Client Server sollte als Tab eingebunden werden.
// #include <ESP8266WebServer.h> und #include <ArduinoOTA.h> muss im Haupttab aufgerufen werden.
// Die Funktion "setupUdpUnicast();" muss im Setup eingebunden werden.
// Die Übergabe der Nachricht an "sendUnicast("Nachricht");" erfolgt als String-Object.
// Um zu empfangen muss die Funktion "unicastServer();" im loop(); aufgerufen werden.
// Die "WiFiUdp.h" wird durch "ArduinoOTA.h" im Webserver Tab includiert.
/**************************************************************************************/
WiFiUDP Unicast;
char unicastBuffer[255]; // Puffer für eingehendes Paket.
void setupUdpUnicast() {
Unicast.begin(1042); // Lokaler UDP Port.
}
void sendUnicast(const String &message) {
IPAddress receiverIP(192, 168, 178, 43); // Senden an diese IP
constexpr uint16_t PORT = 1043; // und diesem UDP Port.
Unicast.beginPacket(receiverIP, PORT);
Unicast.print(message);
Unicast.endPacket();
}
void unicastServer() {
uint16_t packetSize = Unicast.parsePacket(); // Eingehende UDP-Pakete empfangen.
if (packetSize) { // Prüfen ob UDP-Pakete empfangen wurden.
Unicast.read(unicastBuffer, sizeof(unicastBuffer)); // Einlesen des UDP Paket in den Bufffer.
unicastBuffer[packetSize] = 0; // String Ende hinzufügen.
Serial.println(unicastBuffer); // Visualisierung des empfangenen Packets.
}
}