#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "lianderhall";
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
int i = 0;
while (replyPacket[i] != 0)
Udp.write((uint8_t)replyPacket[i++]);
Udp.endPacket();
}
}
there is no reading of Serial in your sketch
can you give me an example. i am really new to arduino please.
looks like you were following WiFiUdpSendReceiveString
but as I understand, didn't seem to send anything.
there are 2 udp.write methods, one takes a byte the other an array and length.
the original example seems broken a bit, need to add the length of array, so the correct method is called.
udp.write(array,size);
but i want to send a message from the app to serial monitor as well.
your serial monitor receives only if you have the sending device connected with a USB-cable to your computer.
Sending directly from your smartphone-app to the serial monitor does not work
The serial monitor expects a USB-cable between sending device and your computer
You must describe your project in much more details
You seem to use an ESP32concluded from
I had problems to receive too.
So I switched to AsyncUDP and this works for me
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// print only once when value has changed
#define dbgc(myFixedText, variableName) \
do { \
static long lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
} while (false);
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
#include "WiFi.h"
//#include "AsyncUDP.h"
#include <AsyncUDP.h>
const char* ssid = ""; // your network SSID (name)
const char* pass = ""; // your network key
const int localUdpPort = 4210;
AsyncUDP udp;
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__) );
Serial.print( F(" compiled ") );
Serial.print( F(__DATE__) );
Serial.print( F(" ") );
Serial.println( F(__TIME__) );
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
unsigned long MyTestTimer = 0; // Timer-variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
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) );
}
}
void udpRXCallBack(AsyncUDPPacket packet) {
Serial.print("UDP Packet Type: ");
Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
Serial.print(", From: ");
Serial.print(packet.remoteIP());
Serial.print(":");
Serial.print(packet.remotePort());
Serial.print(", To: ");
Serial.print(packet.localIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", Length: ");
Serial.print(packet.length());
Serial.print(", Data: ");
Serial.write(packet.data(), packet.length());
Serial.println();
String myString = (const char*)packet.data();
dbg("Rx", myString);
packet.printf("Got %u bytes of data", packet.length());
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (udp.listen(localUdpPort)) {
Serial.print("UDP Listening on IP: ");
Serial.print(WiFi.localIP());
Serial.print(" port:");
Serial.println(localUdpPort);
// define callback-function
udp.onPacket([](AsyncUDPPacket packet) {
udpRXCallBack(packet);
});
}
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 100);
if ( TimePeriodIsOver(MyTestTimer, 5000) ) {
udp.broadcast("Anyone here?");
}
}
best regards Stefan
it does the same thing. print the input on the serial monitor but when i input something in the serial monitor it wont show anything on the udp terminal.
Quoting you again:
smartphone-app --->--->-->--Data->-->-->-->-Arduino->-->-->-->-Computer_with_Serial_Monitor
What you are asking now
is the
opposite direction
smartphone-app ---<<<----Data--<<<---Arduino--<<<--Computer_with_Serial_Monitor
different direction requires different function calls
Can you see the
reversed
direction of the dataflow?
![]()
smartphone-app --->--->-->--Data->-->-->-->-Arduino->-->-->-->-Computer_with_Serial_Monitor
![]()
smartphone-app ---<<<----Data--<<<---Arduino--<<<--Computer_with_Serial_Monitor
working with microcontroller is a bit more complex than tapping the call-button on your smartphone
![]()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.