What is the best way to link my own hardware with my application on a LAN network?

133 Views Asked by At

I have hardware based on a microcontroller with Ethernet communication.

There is a TCP server in the microcontroller.

To configure the hardware, I have an application made in C# for android.

The application, in turn, has a TCP client.

For the app to find my hardware, what I'm doing is pinging each of the addresses of the same network segment of my mobile. That is, if the IP address of my mobile is 192.168.0.xx, I ping from the address 192.168.0.1 to the address 192.168.0.255.

Those addresses that respond, I try to open a socket and send a data frame, if the answer is correct, I assume that I have found a hardware in my local network (there could be more than one connected)

Obviously those IP addresses that don't respond, or that the socket cannot be opened or that they respond to something wrong are discarded.

Those valid addresses are displayed in a list for the user to choose with which to interact.

Also, these valid addresses are saved in the application so that the next time the app is opened, it will automatically connect to the stored addresses, avoiding the scanning of the IP addresses.

This seems correct to me the first time the user installs the hardware and configures with the app

The problem is that I was informed that there are users that their routers are configured to renew their IP addresses once a month.

If this happens, the app should again perform a scan of all the IP addresses again, and this is somewhat cumbersome, since scanning all the IPs takes some time, I don't think users are happy configuring their app and hardware once a month.

Another cumbersome solution could be to use static IP addresses, but I don't think that's a good idea either.

Any suggestions on how to improve this?

2

There are 2 best solutions below

0
On

You need to take subnets into account. You are assuming that the subnet you are connected to is a /24. You are also pinging the broadcast address (.255) which is unnecessary. A host doesn't reside there.

DHCP assignments will renew their lease halfway through the lease period. If your hardware is still on the network it will most likely get the same IP assigned as it did prior from most modern DHCP implementations.

Finally, consider lowering any timeout values in your scan. Scanning a /24 for hosts to respond on a specific port should complete in seconds.

0
On

The solution is explained by @Charlieface in the comments of the question.

I'm going to implement a UDP server on the microcontroller with an IP address within the range of 224.0.0.0-239.255.255.255 (Multicast IP addresses).

When connecting to said server and sending a data frame, the ip and other parameters will be returned so that the app can connect directly to the hardware without having to perform a scan of all the IP addresses.

More details of the solution, in the following freeRTOs thread:

How can I create a UDP server with static IP?