Can't send/receive Unicast UDP message through softAP

My project is for an outdoor race timer. I'm using three NodeMCU esp8266's, one for the softAP and the other two for timing beam-break gates. An apple, iPad is used to track the results and also connects to the esp8266. In the "real" application, the esp8266's send out a broadcast request and the iPad uses the MAC address to bind the timing gate to the iPad and sends out its IP address to the timer gate (esp8266). So I need transmission to complete the broadcast (IP address 255.255.255.255) and the unicast (IP address 192.168.4.2) through the softap.

The following is trimmed down software of an access point, transmitter (XMIT), and receiver (RCVR)

THE ACCESSPOINT CODE
/*

  • WiFi.ino
  • This code is running the WiFi softap using code developed on Arduino cloud.
  • The hardware is a NodeMCU esp8266
  • It should pass UDP packets between two NodeMCU esp8266 runung transmit and
  • receiver codefrom one to the other. Source code submitted later.
    */
    #include <ESP8266WiFi.h>
    #include <WiFiUdp.h>
    const char *ssid = "testnet";
    const char *password = "password";
    WiFiUDP udp;
    bool toggle = true;
    void setup() {
    Serial.begin(9600);
    WiFi.softAP(ssid, password);
    delay(1000);
    pinMode(LED_BUILTIN, OUTPUT);

udp.begin(2277);
Serial.println("UDP connected");
digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
delay(1000);
}

THE XMIT CODE

/*

  • XMIT.ino
  • This code is running the WiFi station code developed on Arduino cloud.
  • The hardware is a NodeMCU esp8266
  • This program transmits a single char in a UDP packet, alternately
  • sending from a udp unicast to the address 192.168.4.2 and
  • broadcast 255.255.255.255
  • The broadcast packet works. The unicast doesn't
  • Used with alternative WiFi access points, both work
    */
    #include <ESP8266WiFi.h>
    #include <WiFiUdp.h>

// Set WiFi credentials
#define WIFI_SSID "testnet"
#define WIFI_PASS "password"

bool toggle = true;
IPAddress compDestIP;
WiFiUDP udp;
uint8_t ping[2];
uint8_t assoIpAddr[4];

void setup() {
Serial.begin(9600);
delay(50);

pinMode(LED_BUILTIN, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASS);

// Connecting to WiFi...
// Loop continuously while WiFi is not connected
while (WiFi.status() != WL_CONNECTED)
{
delay(2000);
Serial.print(".");
if ( toggle ) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
toggle = ! toggle;
}
// Connected to WiFi
Serial.println();
digitalWrite(LED_BUILTIN, LOW);
// delay(3000);

udp.begin(8496);
Serial.println("UDP connected");
}

void loop() {
if ( toggle ) {
assoIpAddr[0] = 255;
assoIpAddr[1] = 255;
assoIpAddr[2] = 255;
assoIpAddr[3] = 255;
ping[0] = 1;
} else {
assoIpAddr[0] = 192;
assoIpAddr[1] = 168;
assoIpAddr[2] = 4;
assoIpAddr[3] = 2;
ping[0] = 2;

}
toggle = ! toggle;
compDestIP = IPAddress(assoIpAddr[0], assoIpAddr[1], assoIpAddr[2], assoIpAddr[3]);
Serial.print("Sending packet to: ");
Serial.println(compDestIP);
udp.beginPacket(compDestIP, 8496);
udp.write(ping, 1);
udp.endPacket();
delay(2000);
}

AND THE RCVR CODE

/*

  • RCVR.ino
  • This code receives and prints a UDP message send by the transmitter XMIT

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

// Set WiFi credentials
#define WIFI_SSID "testnet"
#define WIFI_PASS "password"
bool toggle = true;
WiFiUDP udp;
uint8_t inbuf[80];

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
// Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Connecting to WiFi...
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{
delay(2000);
Serial.print(".");
if ( toggle ) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
toggle = ! toggle;
}
udp.begin(8496);
Serial.println("UDP connected");
}

void loop() {

int packetSize;
packetSize = udp.parsePacket();
if ( packetSize >= 1 ) {
Serial.print("Incoming message that's ");
Serial.print( packetSize );
Serial.println(" big ");
udp.read( inbuf, 1);
uint8_t lclbuf;
lclbuf = inbuf[0];
Serial.print("The message is: <");
Serial.print(lclbuf);
Serial.println(">");
delay(2000);

}
}

you can read the DHCP clients of SoftAP so the addresses of the iPad are available in the SoftAP esp8266.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.