Android Question Discover clients on wifi

poya10

Member
Hello, I am planning to use a part in my program that will discover the IP address of the clients connected to the network and display it in a list.
Let me be more clear, I'm trying to find esp8266 wifi modules that are connected to the local network, the modules have dynamic ip and this is not ideal.
 
Solution
Now after checking, I have completely fixed the problem and the sender and receiver are communicating well, I have set the data sending format in json format, but you can send the data in any way you like.
At the end, I will put my codes for you, I hope it will help others in similar work:

Arduino side [ESP8266]:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoWiFiServer.h>

const char *ssid = "yourSSID";
const char *password = "yourPassword";

StaticJsonDocument<2000> myjson;
WiFiUDP udp;

IPAddress broadcast_ip(255, 255, 255, 255);

unsigned long timerC = 0;
const int intervalC = 2000;

String DeviceName = "MyDevice";
char packet[255];

void setup() {

    WiFi.mode(WIFI_AP_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() !=...

mhk1368

Member
Hello, I am planning to use a part in my program that will discover the IP address of the clients connected to the network and display it in a list.
Let me be more clear, I'm trying to find esp8266 wifi modules that are connected to the local network, the modules have dynamic ip and this is not ideal.
hi
you can use a fixed IP in Esp, you will get a faster answer.
 
Upvote 1

mhk1368

Member
But, I'm confused, how to send a packet with the esp module without the destination IP address?
poya, the easiest way is to set a single ID for it and then check the IP range up to 255, each one responds to your sent data, save the ID and IP and use it.
 
Upvote 0

poya10

Member
poya, the easiest way is to set a single ID for it and then check the IP range up to 255, each one responds to your sent data, save the ID and IP and use it.
This is true, but if you assume that we have a router that assigns an address in the format 192.168.x.x, then we have to check 65536 states and this is unreasonable.
 
Upvote 0

poya10

Member
Now after checking, I have completely fixed the problem and the sender and receiver are communicating well, I have set the data sending format in json format, but you can send the data in any way you like.
At the end, I will put my codes for you, I hope it will help others in similar work:

Arduino side [ESP8266]:
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoWiFiServer.h>

const char *ssid = "yourSSID";
const char *password = "yourPassword";

StaticJsonDocument<2000> myjson;
WiFiUDP udp;

IPAddress broadcast_ip(255, 255, 255, 255);

unsigned long timerC = 0;
const int intervalC = 2000;

String DeviceName = "MyDevice";
char packet[255];

void setup() {

    WiFi.mode(WIFI_AP_STA);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
    Serial.println(WiFi.localIP());
 
      udp.begin(8192);
}

void loop() {

counter = millis();

    if (counter - timerC >= intervalC) {
        timerC = counter;
        myjson.clear();
        myjson["AP"] = WiFi.softAPSSID();
        myjson["STA"] = WiFi.SSID();
        myjson["STAIP"] = WiFi.localIP().toString();
        myjson["DeviceName"] = DeviceName;

        String output;
        serializeJson(myjson, output);

        udp.beginPacket(broadcast_ip, 8084);
        udp.print(output);
        udp.endPacket();
     }

}

B4A side:
Sub Process_Globals

    Dim autodiscover As UDPSocket

End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("layout")

    If FirstTime=True Then
        CallSub(Starter,"disconnect")
        autodiscover.Initialize("AutoDiscover",8084,8192)
    End If

End Sub

Private Sub AutoDiscover_PacketArrived (Packet As UDPPacket)
     
    Dim data As String=BytesToString(Packet.Data,Packet.Offset,Packet.Length,"UTF8")
     
    Dim jp As JSONParser
    jp.Initialize(data)
 
    Dim jp_map As Map
 
    jp_map.Initialize()
    jp_map=jp.NextObject

    log(jp_map.Get("STAIP"))
 
End Sub
 
Upvote 0
Solution
Top