Chat freely about anything...

User avatar
By Syafee
#76451 Hello,

I set esp8266 as access point (AP) and also to wait for udp packets (see code down here).

When I use PC to send/receive UDP packets it work fine.
But, when I use Mobile the esp8266 not receiving (and not sending back) the packets.

The esp8266 AP is showing the ip address of PC and Mobile so they exists (see report down here).

With external router the Mobile and PC works fine with esp8266 (not as AP) I can send/receive UDP packets.

Why Mobile can't send/receive UDP packets to/from esp8266 AP?

Arduino 1.8.5.

**************************************************************************************************
Code:
Arduino 1.8.5.
SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)

**************************************************************************************************

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

extern "C" {
#include<user_interface.h>
}
const char* ssid = "AP";
const char* password = "123456789";

IPAddress local_IP(192,168,4,22);
IPAddress gateway(192,168,4,9);
IPAddress subnet(255,255,255,0);

WiFiUDP Udp;
unsigned int localUdpPort = 5000; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Received Packets From Client"; // a reply string to send back


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


Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");

Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP(ssid,password) ? "Ready" : "Failed!");

Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());

while (WiFi.softAPgetStationNum()== 0)
{
delay(1000);
Serial.println("Wait for clien");
}

Serial.printf("Stations connected to soft-AP = %d\n", WiFi.softAPgetStationNum());
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
Serial.printf("MAC address = %s\n", WiFi.softAPmacAddress().c_str());

client_status();
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}


void loop()
{
delay(2000);
client_status();
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());
Udp.write(replyPacket);
Udp.endPacket();
}
}

void client_status() {

unsigned char number_client;
struct station_info *stat_info;

struct ip_addr *IPaddress;
IPAddress address;
int i=1;

number_client= wifi_softap_get_station_num();
stat_info = wifi_softap_get_station_info();

Serial.print(" Total Connected Clients are = ");
Serial.println(number_client);

while (stat_info != NULL) {

IPaddress = &stat_info->ip;
address = IPaddress->addr;

Serial.print("client= ");

Serial.print(i);
Serial.print(" IP adress is = ");
Serial.print((address));
Serial.print(" with MAC adress is = ");

Serial.print(stat_info->bssid[0],HEX);Serial.print(" ");
Serial.print(stat_info->bssid[1],HEX);Serial.print(" ");
Serial.print(stat_info->bssid[2],HEX);Serial.print(" ");
Serial.print(stat_info->bssid[3],HEX);Serial.print(" ");
Serial.print(stat_info->bssid[4],HEX);Serial.print(" ");
Serial.print(stat_info->bssid[5],HEX);Serial.print(" ");

stat_info = STAILQ_NEXT(stat_info, next);
i++;
Serial.println();
}

delay(500);
}


**************************************************************************************************
Report:
Mobile: client= 1 IP adress is = 192.168.4.121 with MAC adress is = D8 63 75 2B EB 53
PC : client= 2 IP adress is = 192.168.4.122 with MAC adress is = 54 35 30 D6 C1 29
UDP Packet:"test1"

**************************************************************************************************
SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)

Setting soft-AP configuration ... Ready
Setting soft-AP ... bcn 0
del if1
usl
add if1
dhcp server start:(ip:192.168.4.22,mask:255.255.255.0,gw:192.168.4.9)
bcn 100
Ready
Soft-AP IP address = 192.168.4.22
Wait for clien
Wait for clien
Wait for clien
.
.
.
add 1
aid 1
station: d8:63:75:2b:eb:53 join, AID = 1
Wait for clien
Stations connected to soft-AP = 1
Soft-AP IP address = 192.168.4.22
MAC address = B6:E6:2D:28:AB:11
Total Connected Clients are = 1
Now listening at IP 192.168.4.22, UDP port 5000
Total Connected Clients are = 1
client= 1 IP adress is = 192.168.4.121 with MAC adress is = D8 63 75 2B EB 53
Total Connected Clients are = 1
client= 1 IP adress is = 192.168.4.121 with MAC adress is = D8 63 75 2B EB 53
Total Connected Clients are = 1
.
.
.
add 2
aid 2
station: 54:35:30:d6:c1:29 join, AID = 2
Total Connected Clients are = 2
client= 1 IP adress is = 192.168.4.121 with MAC adress is = D8 63 75 2B EB 53
client= 2 IP adress is = 192.168.4.122 with MAC adress is = 54 35 30 D6 C1 29
Total Connected Clients are = 2
client= 1 IP adress is = 192.168.4.121 with MAC adress is = D8 63 75 2B EB 53
client= 2 IP adress is = 192.168.4.122 with MAC adress is = 54 35 30 D6 C1 29
Total Connected Clients are = 2
.
.
.
Received 5 bytes from 192.168.4.122, port 60142
UDP packet contents: test1
Total Connected Clients are = 2
client= 1 IP adress is = 192.168.4.121 with MAC adress is = D8 63 75 2B EB 53
client= 2 IP adress is = 192.168.4.122 with MAC adress is = 54 35 30 D6 C1 29
Total Connected Clients are = 2
client= 1 IP adress is = 192.168.4.121 with MAC adress is = D8 63 75 2B EB 53
client= 2 IP adress is = 192.168.4.122 with MAC adress is = 54 35 30 D6 C1 29
Received 5 bytes from 192.168.4.122, port 60142
UDP packet contents: test1
Total Connected Clients are = 2
.
.
.