Pinging all the ip addresses on the local network

photon
Tags: #<Tag:0x00007fe22649db20>

#1

I got Wifi.ping() working
for a single
IPAddress remoteIP(192, 168, 5, 232); hardcoded in particle photon.

my particle photon is on the same network as other hosts are, I have 2 questions :

  1. is it possible to detect other hosts on the network ?
  2. even if its possible or not, I want to know how can I create a list of ip address and iterate over and ping each of the server to find the status .

I am a python programmer and this code I have written following examples.
any help will be greatly appreciated.

Here is some python version of what I want to implement in cpp for the 2nd quesiton:

hosts = {
	"host1":  IPAddress(192.168.1.2),
	"host2":  IPAddress(192.168.1.3),
	"host3":  IPAddress(192.168.1.112),
	"host4":  IPAddress(192.168.1.173),
}

# then I want to iterate over each of the items in the array above

for host, ip_address in hosts.iteritems():
	# do ping on ip_address
	# if ping succeed fine
	# else blink red LED and print status

#2

In C/C++ you’d use an array like this

IPAddress hosts[] = { IPAddress(192, 168, 1, 2), IPAddress(192, 168, 1, 3), ... };
const int hostsCount = sizeof(hosts) / sizeof(hosts[0]);
...
  for (int i=0; i < hostsCount; hostsCount++) {
    // do something with host[i]
  }

Instead of an array you could use a vector with its iterator syntax but dynamic memory management on embedded systems has its own caveats to consider and I’d also consider this overkill for a static list of possible IPs.


#3

Hello @ScruffR

thanks for a quick reply

apart from that, is it possible to do lookup for hosts from within particle photon.
I can do look if available hosts and store it in eprom instead of always searching for hosts.
and then ping and report in some way for host status.


#4

Depends on the “hosts”
If the make themselves known to the system (e.g. via mDNS) or respond to pings you may be able to, but I’d suspect a fair number of network connected devices may do any of that.


#5

oh no you miss-understood, I didn’t mean that my particle photon should be discoverable.
by lookup I meant, I want particle photon to look for connected devices on the local network.

I read that mdns allows register a device by a name soomething like photon.local or find ip address using the known host name .


#6

Nope, I didn’t misunderstand. I meant exactly that.
Any network client - Particle or not - can decide to make itself known to the rest of the network or not, respond to pings or not, …
Also mDNS is not a Particle invention it’s a common protocol - e.g. Apple Bonjour uses it


#7

oh ok, I am considering storing ip addresses in eprom of particle photon instead of having to discover it.