I have a Raspberry Pi hooked up to a RGB LED strip. On the Pi, is a Node.JS server that hosts a webpage, and that webpage lets me pick a color for the LED strip. All that works.
The problem is, I want to be able to dynamically find the IP address (I don't want to do static IPs. I want to give these out to some family, and I want them to "just work" once I join them to the wifi).
What I would like to do is to have a phone app that can find the Raspberry Pi on the network. I was thinking about this, and I remembered reading about broadcast IP addresses for the LAN. I've never used them before, but seems to be what I need. The app would start up, send a specially crafted message on broadcast that says "Where's the Raspberry Pi?" and the Pi would answer "Here I am! I'm at X address!"
I've never tried to use a broadcast IP address before, so I decided to try a proof of concept. I started up a Node.JS server on my Pi that looks like this:
var http = require('http');
var port = 8081;
var server = http.createServer(function(request, response) {
response.writeHead(200);
response.end("Pong");
});
server.listen(port, function() {
console.log((new Date()) + ' Server is listening on port ' + port);
});
Then from my PC, curl 192.168.1.XXX:8081
(the known IP address) and boom. Expected response.
But curl 192.168.1.255:8081
gets me a 502
error.
From what I'm reading online, that should be the broadcast address. Is my router likely to be blocking this? Am I just doing something fundamentally wrong?
Thanks in advance. :)
You're doing something fundamentally wrong :)
You can not send a broadcast message using TCP/HTTP (see Can I use broadcast or multicast for TCP?) but you could send an UDP datagram (see Send Broadcast datagram) instead. It should work if your router doesn't filter that kind of traffic.
Many operating systems come with a tool called arp-scan
. You could rely on ARP (Address Resolution Protocol) and use this tool to list the IP-MAC associations on your network. You could simply use the IP that belongs to a Raspberry Pi.