1
votes

Hi all I want to connect multiple esp8266 devices to my router and create a mobile app that can find those devices and send messages to them using udp. Currently my plan is to let esp devices listen to a port and then my app would send a message on that port, esp would respond and the app will store the IP.

Is there any better way to do that? A friend of mine told me that this approach will fail if routers's gateway is changed. Is it true?

I am just calling WiFi.begin(Ssid, Password); to connect to wifi without doing any changes with wifi.conf().

I am using arduino SDK.

3
Is the mobile app going to be inside the network or out on the internet?leetibbett
@leetibbett The mobile app will be inside the network.Manprit Singh

3 Answers

2
votes

Give your ESP8266 devices a static IP addresses so the mobile app will know in advance where they could be 'found':

IPAddress ip(192,168,1,xx);               // desired static IP address
IPAddress gateway(192,168,1,yy);          // IP address of the router
IPAddress subnet(255,255,255,0);

WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);

while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}

and use dynamic DNS service (unless you have a static IP address) with port forwarding to access each of the ESPs. You can choose ports 8266, 8267, 8268, 8269, ... or any other above 1024 and then set the port forwarding in your router settings so port 8266 will be forwarded to the IP address of your first ESP and port 80 (or any other), port 8267 will be forwarded to the IP address of your second ESP and port 80 etc.

Afterwards you can access your ESPs from mobile app (or internet) using URLs looking like http://xx.xx.xx.xx:8266, http://xx.xx.xx.xx:8267, http://xx.xx.xx.xx:8268, http://xx.xx.xx.xx:8269, ... or by using dynamic DNS service with: http://myhome.something.com:8266, http://myhome.something.com:8267 etc. Or you can access them from your local network by known static IP addresses. That is - your mobile app will have to determine if it is inside local network or not. Or you can always access ESPs using proxy - in that case the URLs will not depend upon being inside or outside the local network.

1
votes

Your friend may have more of the details of your solution, but IMO the gateway has nothing to do with it. If all clients and the server are on the same subnet inside the router's local network then the gateway doesn't come into play.

I would do a UDP multicast. Your mobile app would just need to send one request and then listen for replies from the ESPs for a few seconds.

0
votes

One way is to check all the Mac addresses on the network. The first 6 digits is the unique code for the company that made the wifi dongle.

This is assuming you do not have any other devices on the network using the same dongle and aren't what you're trying to search for.

You can find all the Mac addresses on a network by performing an ARP request on each IP.

But all the devices would have to be on the same subnet.

If you devices are remote, then I would go with your original plan, however your friend is correct if the gateway changes this would require something a little more robust. Use a Dynamic DNS service along with a domain name. The Dynamic DNS will update your DNS records in almost real time if your gateway address changes.