OK I had some first rudimental success:
I found a pretty short python-code that creates a UDP-listener
# very simple and short upd-receiver found here
# https://www.studytonight.com/network-programming-in-python/working-with-udp-sockets#
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # For UDP
udp_host = socket.gethostname() # Host IP
udp_port = 4210 # specified port to connect
#print type(sock) ============> 'type' can be used to see type
# of any variable ('sock' here)
sock.bind((udp_host,udp_port))
while True:
print ("Waiting for client...")
data,addr = sock.recvfrom(32) #receive data from client
Msg = data.decode('ascii')
print ("Received Messages: #",Msg,"# from",addr)
that receives code from a UDP-Demo-sender running on a ESP8266 nodeMCU
Though know I have to analyse and learn why the python-UDP-listener spits out all these
/x00 's
Received Messages: b'787\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' from ('192.168.178.167', 49400)
Msg = data.decode('ascii')
does the trick
here is the ESP8266 Demo-code
This code includes some of my basic tool-functions like heartbeatblinker with onboard LED giving visual feedback code is running
on startup telling compiled-file to the serial monitor
I insert these functions into every code I write through modifying the bareminimum.ino
This also inlcudes PString for easier String-handling and non-blocking-timer-function based on millis()
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <PString.h>
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod )
{
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
const byte OnBoard_LED = 2;
int BlinkTime = 500;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
unsigned long TestTimer;
unsigned long UDP_SendTimer;
int myCounter = 0;
char UDP_Msg_AoC[16 + 1]; // always remember one extra-char for terminating zero
PString UDP_Msg_PS(UDP_Msg_AoC, sizeof(UDP_Msg_AoC));
char *ssid = "";
char *password = "";
// receiver-IP
IPAddress remoteIP(192, 168, 178, 157);
unsigned int remotePort = 4210; // remote port to listen on
WiFiUDP Udp;
void PrintFileNameDateTime() {
Serial.print("Code running comes from file ");
Serial.println(__FILE__);
Serial.print(" compiled ");
Serial.print(__DATE__);
Serial.println(__TIME__);
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Setup-Start");
PrintFileNameDateTime();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
BlinkHeartBeatLED(OnBoard_LED, 200);
delay(200);
Serial.print(".");
}
Serial.print("\n connected.");
Serial.println(WiFi.localIP() );
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, BlinkTime);
if (TimePeriodIsOver(UDP_SendTimer, 2000) ) {
UDP_Msg_PS = myCounter++;
Serial.print("Send UDP_Msg #");
Serial.print(UDP_Msg_PS);
Serial.println("#");
Udp.beginPacket(remoteIP, remotePort);
Udp.write(UDP_Msg_PS, sizeof(UDP_Msg_PS) );
Udp.endPacket();
}
}
@JOHI do you have a ready to run demo-code for an ESP8266 /ESP32 just sending a counting up number and a configuration-file that I just import into WaWiLib that writes the UDP-received data into a textfile?
Without such an working example WaWiLib seems to need some hours of reading the manual and adapting code.
best regards Stefan