How to find all the esp8266 connected to a router - android

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.

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.

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.

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.

Related

Android udp server cannot receive data via SIM card(4G)

I created a UDP server on android app, and this UDP server is reachable via wifi (Local network). I can receive and send data. However when I change to 4G network. The UDP server on Android not able to receive anything. I use ip chicken to check my cell phone IP address and my UDP server port is 2004. Is there anything wrong? or my public ip is not correct?
Assuming that your phone IP address is correct, and it looks like you have checks this, it is important to know that mobile network providers may not support UDP inbound connections on their networks.
While it's not true to say that all don't, it is true to say that not all do so if you want your solution to be portable across networks you may need to look at alternatives anyway.
There are examples and guides on traversing network address translation boundaries on networks - one good example, which is looking at peer to peer applications, is here: http://bford.info/pub/net/p2pnat/

Android/iOS set IP-address of device hotspot

Is there a way to "fix" or set up a static ip address of the hotspot? I need to guarantee a peripheral can connect to my server socket on that ip always.
I am trying to connect a wifi peripheral device to a server socket running on my android device. I have the peripheral connected to my android's hotspot with the ssid and ssid password.
Right now as a work around I am using ip 192.168.43.1. Sources here and here say that 192.168.43.1 will is the default. However, I am under the impression that it's possible this ip address could change.
I am also wondering about doing this for iOS but one step at a time.
Many thanks in advance!
This IP Address is hardcoded for Android as you can see in the source.
if (ifcg != null) {
/* IP/netmask: 192.168.43.1/255.255.255.0 */
ifcg.setLinkAddress(new LinkAddress(
NetworkUtils.numericToInetAddress("192.168.43.1"), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);
}
However a better implementation is for your peripheral device to get the default gateway of the local network as that will work regardless of hotspot implementation.

Server on Android reachable from devices not on local network?

I have been trying to set up an app that allows communication between two devices, a server and a client. I have established the connection and can send messages between the two as long as they are connected to the same source of internet. When I connect the devices to two different sources, I am not able to establish a connection between the two. It works by entering the IP address of the server you want to connect to.
For example, if my server device is connected to my home Wi-Fi and my client device is connected to the same Wi-Fi router, communication works as supposed to. But, when I connect the server to Wi-Fi and my client to cellular data (or different Wi-Fi router), the client is not able to find the server.
From all the searching I've done, I found that it is because my server is bound to my local host. So my question is, How can I make my server reachable from anywhere regardless of what network you are connected to?
I used this tutorial to get my server running:
http://android-er.blogspot.com/2014/02/android-sercerclient-example-server.html
The very first comment is a question that asks why it only works on a local network and someone answered saying you have to set up port forwarding on your router. From what I understand, this will work only if the server device is always connected to the same router. This will not be the case for my app. The server will have a new IP address as the device changes networks. I want this IP address to always be reachable no matter what network the server is connected to.
You will need to create a relay server. This would be set up using a java application on the PC and it would manage virtual "rooms" that then relay the messages to the clients. The computer you run your relay server off of will need port forwarding but the clients will not. Much of the code from that tutorial could be applied to this concept.
See: How to create a java Server that accepts client connections and then build a relay connection for a client pair
If your server is behind a firewall, you will not be able to reach it unless that port is open on the firewall. The only way to avoid this is to have a non-firewalled server.

Adding an android phon to a DNS server?

I want to know if it is possible and how to, make an application, that allows me to put my android phone in a DNS server, as we do in a Windows PC. I want to identify my android phone by a name in a DNS network, obviously in a network in which there is a DNS server.
Thanks in advance...
Generally, you have three options:
Static IP. Sometimes it's possible to get a fixed IP for your phone, in this case you need to setup a DNS record only once, and it requires no software nor configuration on the phone itself. All you need is just create a regular DNS address record. (Consult your DNS administrator (or ask on superuser.com if your need help))
Home/office network (DHCP). If you want to access your phone via LAN, and this LAN have a DHCP server under your control, DHCP server can be configured to update a DNS record whenever your phone connects. In this case too, the phone does not need any special configuration/software - it's a DHCP server which needs to be configured.
Dynamic DNS. Your phone can submit it's address via dynamic DNS. This will work for any case - LAN (including NAT) and cellular connection. You usually need to subscribe to some dynamic DNS service for this and yes, there are dynamic DNS clients for Android. Try net-me.net guide on setting up dynamic DNS under Android (I am affiliated).
(For what purpose you want access your phone, which software will listen to incoming connections, and if the phone IP will be accessible from anywhere - it's all a completely different story.)
If your DNS and DHCP servers support it, you can get the DHCP server to automatically update the DNS server. See http://technet.microsoft.com/en-us/library/cc787034(v=ws.10).aspx for a description of how to do it with Microsoft's servers.

need to get IP address for client server in Android, Is there a better way?

I used sockets to connect two Android tablets as a network over wifi using the same router. one is a server and the other is a client.
the problems is that every time I want to connect the client and server tablets, I have to type in the server tablet's IP address in the client tablet. Is there a way to avoid this?
I have not figured out how to send the server IP address to the client tablet before they are networked.
I wish there was a way to network the two tablets without having to know the IP address of the server tablet.
One idea is to have the server tablet connect to a website written in php and have the website get the IP address and store it in a database. next i would have the client tablet get the IP address from that website. Then the client tablet having the server's IP will be abble to make the connection. All of this will avoid the user having to know the IP and typing it in every time they want to connect.
I am sure that others have done something like this before and solved this problem. Any ideas?
I had this kind of android app. What I did is by using a Multicasting network. See an example here
Since Multicast Network uses a group IP, here's my suggested idea
The server tablet will act as a receiver
The clients will be a sender (They will send their local IP to the receiver)
If the server Android cannot be assigned a fixed public IP address, which means it gets LAN IP using DHCP, it can ask for specific IP address from the DHCP server (Maybe you need some configuration for the router), which is the router here.
Another method is using Reverse Address Resolution Protocol (RARP), with which you can ask the switch for the IP address of a host with a specific MAC address. The MAC address is fixed for your server Android phone.
These are just theoretical feasible. You need to look into the APIs to call in Android phones.
After the connection, you can retrieve the socket address
using the new client
( Socket client = server.accept(); )
from
client.getLocalAddress().toString() //ip server
client.getRemoteSocketAddress().toString() //ip client
and store them in a HashMap list, for example:
String name=client.toString();
name=name.substring(name.indexOf("/")+1,name.indexOf(","));
List<String> index = new ArrayList<String>(list.keySet());
if(index.indexOf(name)<0)
list.put(name,client.getRemoteSocketAddress().toString());

Categories

Resources