Unable to send UDP packets on NodeMCU

I have two NodeMCU 1.0 boards.

I am trying to send UDP packets with the string true or false from the serial monitor of the sender in client mode(STA) and send it to the receiver in access point mode (AP) and print the desired results on LCD.

Issue: The packet is sent successfully from the sender(STA) but it is not received by the receiver(AP).

Receiver in AP mode(receiver.ino):

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "ESPAsyncWebServer.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define ssid "Bike-Module-AP"
#define password "qwerty123"
#define PORT 4210   // local port to listen on
#define SDA D2
#define SCL D1

AsyncWebServer server(80);
WiFiUDP Udp;
LiquidCrystal_I2C lcd(0x27, 16, 2);

char incomingPacket[255];  // buffer for incoming packets
char replyPacket[] = "Got message successfully!";  // a reply string to send back
//unsigned long Interval = 20000;


void setup()  {
  Serial.begin(115200);
  Serial.println();

  Wire.begin(SDA, SCL);
  lcd.begin(16, 2);   // initializing the LCD
  lcd.backlight(); // Enable or Turn On the backlight
  lcd.home();

  Serial.print("Setting soft-AP ... ");
  Serial.println(WiFi.softAP(ssid, password) ? "Ready":"Failed!");
  Serial.print("AP IP address: ");
  Serial.println(WiFi.softAPIP());
  
  Udp.begin(PORT);
  server.begin(); 
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), PORT);

  // LED pin to show client status
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  lcd.clear();
  lcd.print("System ready!");
  delay(1000);
  
  lcd.clear();
}


void loop() {
  bool _client = client_status();
  lcd.setCursor(0, 0);
  lcd.print("Connection: ");
  lcd.setCursor(12, 0);
  if(_client)  {
//    Serial.printf("client: true\t");
    lcd.print("Okay");
    if(receive_packets()) {
//      Serial.printf("packet: true\n");
      lcd.setCursor(0, 1);
      lcd.print("All good");
      digitalWrite(LED_BUILTIN, LOW); // turn led ON
    }
    else  {      
//      Serial.printf("packet: false\n");
      lcd.setCursor(0, 1);
      lcd.print("Ignition off");
      digitalWrite(LED_BUILTIN, HIGH);  //turn led OFF
    }
  }
  else  {
    lcd.print("Lost");
    lcd.setCursor(0, 1);
    lcd.print("Ignition off");
  }
}

bool client_status() {
  unsigned char number_client;  
  number_client= wifi_softap_get_station_num();
//  Serial.printf("Total Connected Clients are = %d\n", number_client);

  if(number_client == 0)  {
    return false;
  }
  else if(number_client > 0) {
    return true;
  }
}


bool receive_packets()  {
//  digitalWrite(LED_BUILTIN, LOW); // client connected
  int packetSize = Udp.parsePacket();
  Serial.printf("packetSize = %d\n", packetSize);
  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);
  
    if(strcmp(incomingPacket, "true")==10)  {
      Serial.printf("incoming: TRUE\n");
      return true;
    }
    else if(strcmp(incomingPacket, "false")==10) {
      Serial.printf("incoming: FALSE\n");
      return false;
    }

    // send back a reply, to the IP address and port we got the packet from
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(replyPacket);
    Udp.endPacket();
  }
  else  {
//    digitalWrite(LED_BUILTIN, HIGH);  // client disconnected
  }
}

Serial Monitor:

Setting soft-AP ... Ready
AP IP address: 192.168.4.1
Now listening at IP 192.168.4.1, UDP port 4210

Sender in STA mode(sender.ino):

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#define ssid "Module-AP"
#define password "qwerty123"
#define IP "192.168.4.1"
#define PORT 4210   // local port to listen on

WiFiUDP Udp;

char senderPacket[100];


void setup() {
  Serial.begin(115200);
  Serial.println();

  // Connect to AP
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
}


void loop() {
  while (Serial.available() > 0 ) {
    String inputString = Serial.readString();
    inputString.toCharArray(senderPacket, 100);
    if(Udp.beginPacket(IP, PORT) &&
      Udp.write(senderPacket) &&
      Udp.endPacket())  {
      Serial.printf("Packet sent: %s\n", senderPacket);
    }
  }
}

Serial monitor:

Connecting to Module-AP .................. connected
19:08:05.107 -> Packet sent: true

I don't see a UDP.begin in the sender. I believe UDP.begin needs to be called before you can call UDP.beginPacket:
Arduino - EthernetUDPWrite

countrypaul:
I don't see a UDP.begin in the sender. I believe UDP.begin needs to be called before you can call UDP.beginPacket:
Arduino - EthernetUDPWrite

Thanks! That was silly mistake.
It's working now.
:slight_smile: