Andymator
Posts: 25
Joined: Sat Jun 30, 2018 9:38 am

Getting IP from headless RPi

Sun Jun 02, 2019 8:17 pm

Hello
I want my students that are about to use their RPIs at home with their laptops to not have to hook the RPi up to a monitor and use mouse/keyboard to get the IP that their home network has decided for their RPi. Browsing to their router is to complicated. Do you have any ideas on how I can set the RPIs up so that getting the IP will be easy for them? Maybe using bluetooth?

epoch1970
Posts: 3047
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Getting IP from headless RPi

Sun Jun 02, 2019 8:52 pm

"ping raspberrypi.local" works from Mac, linux, and even Windows if it has mDNS enabled.
Looking in the router (the DHCP server) will reveal the local IP address.
Otherwise an IP scanner will identify the machine as a Pi via its MAC address.

Installing a wifi access point (creating a secondary network) on the Pi is an option, but only if the Pi is not connected by wifi to the main network.
Text-to-speech (espeak) can be used to painfully spell the current IP address over audio out.
A service that reports the local IPs probably exists via HTTP (some Internet service) or via MQTT and a local broker. I think using IFTTT "webhooks" is no longer an option, I could be wrong.

The serial console can be moved over BT, that gives full access to the machine. Should work fairly well.
I suppose BT networking or other options are also available via BT.

EDIT: and obviously, sending an email to some address is an excellent option. Thanks, HawaiianPi
Last edited by epoch1970 on Mon Jun 03, 2019 1:22 pm, edited 1 time in total.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

Andymator
Posts: 25
Joined: Sat Jun 30, 2018 9:38 am

Re: Getting IP from headless RPi

Mon Jun 03, 2019 5:47 am

That is some great info.
Thank you very much.

User avatar
HawaiianPi
Posts: 4235
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting IP from headless RPi

Mon Jun 03, 2019 8:18 am

Look up the Fing app for Android and iOS. It's easy to use, and kids love apps.

It even puts a little Raspberry icon next to Pi computers it finds.
Fing_1.jpg
Fing_1.jpg (17.97 KiB) Viewed 1196 times

Or, you could have the Pi email or SMS the IP when it connects.

Serial console cables are yet another option (although a little more complicated). You can watch the shell output over a serial terminal on another computer and even login with no network connection. I use cheap CP2102 USB-TTL adapters from eBay (less than $2).
Image
The IP address is usually displayed near the end of the boot process (setting the wait for network boot option ensures it).
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

luckydog
Posts: 11
Joined: Sun Mar 03, 2019 7:07 pm

Re: Getting IP from headless RPi

Wed Jun 05, 2019 8:36 am

I recommend FING aswell, its super easy
You can label each Pi as it gets added to the network to help you keep track of them all if they do not have static IPs

User avatar
HawaiianPi
Posts: 4235
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting IP from headless RPi

Thu Jun 06, 2019 12:39 am

Andymator wrote:
Sun Jun 02, 2019 8:17 pm
Hello
I want my students that are about to use their RPIs at home with their laptops to not have to hook the RPi up to a monitor...
What OS on laptops?

What model of Raspberry Pi?

What OS on the Raspberry Pi?
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

DAI-X
Posts: 27
Joined: Fri Nov 27, 2015 11:19 pm
Location: sutton coldfield, uk

Re: Getting IP from headless RPi

Thu Jun 06, 2019 2:22 pm

What I've done on some pi-based 'mars rovers' I've built is write a script that gathers the pi's local IP address ("hostname -I")after boot-up, and flashes an LED on the rover according to the last 3 digits of the local IP (the 192.168.0. bit is a given)

so if the IP was 192.168.0.15 it would flash once (pause) five times (pause) (long pause) and repeat.

Simple and works well for me

User avatar
HawaiianPi
Posts: 4235
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting IP from headless RPi

Tue Jun 11, 2019 7:27 pm

DAI-X wrote:
Thu Jun 06, 2019 2:22 pm
... so if the IP was 192.168.0.15 it would flash once (pause) five times (pause) (long pause) and repeat. .
Interesting idea. Using a 2 color LED could avoid the pauses and possibly improve clarity. They actually produce 3 colors with one or the other or both on. So you could have different colors for hundreds, tens and ones.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

User avatar
DougieLawson
Posts: 35364
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Getting IP from headless RPi

Tue Jun 11, 2019 7:38 pm

If I were displaying 15. That would be one short four long five short. (. ---- .....)

Morse code removes any ambiguity and needs five pulses for each digit.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
HawaiianPi
Posts: 4235
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting IP from headless RPi

Wed Jun 12, 2019 3:02 am

Morse code, eh? I used to know that. I doubt many kids do. Only thing I remember now is my name - ** --

How about binary? Bi-color or red & green LED with one color representing zero and the other one. Only 8 pulses for 0-255.

Red/Green LED: red, red, red, red, green, green, green, green = 00001111 = 15

Of course, then you'd need to teach the kids binary, which is not a terrible idea for kids learning computers and programming.

But my original suggestion of email is probably the simplest (or text message via email if their phone carrier supports it).
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

User avatar
rpdom
Posts: 14451
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Getting IP from headless RPi

Wed Jun 12, 2019 4:33 am

If we're going to start talking about attaching hardware, what I have done in the past is connect a cheap 2-line LCD display and have the Pi display the IP on that.

Andymator
Posts: 25
Joined: Sat Jun 30, 2018 9:38 am

Re: Getting IP from headless RPi

Thu Jun 20, 2019 12:31 pm

HawaiianPi wrote:
Thu Jun 06, 2019 12:39 am
Andymator wrote:
Sun Jun 02, 2019 8:17 pm
Hello
I want my students that are about to use their RPIs at home with their laptops to not have to hook the RPi up to a monitor...
What OS on laptops?
Windows 10

What model of Raspberry Pi?
3 B+

What OS on the Raspberry Pi?
Rasbian Noobs(?)

Andymator
Posts: 25
Joined: Sat Jun 30, 2018 9:38 am

Re: Getting IP from headless RPi

Thu Jun 20, 2019 12:43 pm

The email/text-message idea sounds great, but doesnt that require the pi to actually be logged on to the home network?
I was unclear when I described this situation. This is the scenario:

1. Student brings home laptop and rpi from school
2. Student wants to get the rpi on to the home network without having to configure router or hook rpi up to a monitor
3. Student wants to get the ip of the rpi to ssh to it with MobaXTerm with laptop (just like in school)

The answers you so nicely gave me all takes care of point nr 3, right?
So the challenge now is point nr 2. Any suggestions?

Andyroo
Posts: 3320
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Getting IP from headless RPi

Thu Jun 20, 2019 1:00 pm

Assuming the student has WiFi at home than I would look at https://github.com/balena-io/wifi-connect

This turns the Pi into an access point with a captive portal as its only page.
1) Connect a mobile / computer to this access point
2) Enter details of your home network
3) Pi reconfigures and connects to the home network

If they do not have WiFi then issue a 3B+ with Ethernet cable or a Zero with USB connection configured.

Thinking about it - the latter maybe the best bet as it should suit people without broadband as well...
Need Pi spray - these things are breeding in my house...

hippy
Posts: 5353
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Getting IP from headless RPi

Thu Jun 20, 2019 1:12 pm

I thought I had posted here but must have been a similar thread - My solution is to attach a small speaker and have the Pi speak its IP address -

Code: Select all

echo "My IP Address is" `hostname -I` | espeak --stdout | aplay
That can be improved on by including that functionality in a Python program which inserts spaces between the digits of the IP address and replaces "." with the word "dot".

Code: Select all

def IpAddress():
  s = os.popen("hostname -I").read().strip()
  result = ""
  for c in s:
    if   c == " "   : result = result + " and"
    elif c == "."   : result = result + " dot"
    else            : result = result + " " + c
  return result
os.popen( 'espeak "My IP address is'+IpAddress()+'" --stdout | aplay 2> /dev/null' )
One can have that activated by a button connected to GPIO.

In fact, one can have IP address reporting, shutdown and rebooting all safely controlled by button push and speech feedback which is extremely useful for when the Pi is headless but isn't on a network or cannot be connected to.

User avatar
Douglas6
Posts: 4672
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Getting IP from headless RPi

Thu Jun 20, 2019 7:12 pm

Just as a proof of concept, this code works to set up a BLE service on the Pi with one characteristic providing the Pi's IP address. Advantages: should work from any modern Android or iPhone. Disadvantages: many, but it's a little more modern than Morse code. You'll need to grab the imports from my github: https://github.com/Douglas6/cputemp.

Code: Select all

#!/usr/bin/python3
 
import dbus
from subprocess import check_output
 
from advertisement import Advertisement
from service import Application, Service, Characteristic, Descriptor
 
GATT_CHRC_IFACE = "org.bluez.GattCharacteristic1"
 
class IPAddressAdvertisement(Advertisement):
    def __init__(self, index):
        Advertisement.__init__(self, index, "peripheral")
        self.include_tx_power = True
 
class IPAddressService(Service):
    IPADDR_SVC_UUID = "00000001-810e-4a5b-8d75-3e5b444bc3cf"
 
    def __init__(self, index):
        Service.__init__(self, index, self.IPADDR_SVC_UUID, True)
        self.add_characteristic(IPAddressCharacteristic(self))
 
class IPAddressCharacteristic(Characteristic):
    IPADDR_CHARACTERISTIC_UUID = "00000002-810e-4a5b-8d75-3e5b444bc3cf"
 
    def __init__(self, service):
        Characteristic.__init__(
                self, self.IPADDR_CHARACTERISTIC_UUID,
                ["read"], service)
 
    def ReadValue(self, options):
        value = []
 
        ipaddrs = check_output(["hostname", "-I"])
        ipaddr = ipaddrs.split()[0]
        for c in ipaddr:
            value.append(dbus.Byte(c))
 
        return value
 
app = Application()
app.add_service(IPAddressService(0))
app.register()
 
adv = IPAddressAdvertisement(0)
adv.register()
 
try:
    app.run()
except KeyboardInterrupt:
    app.quit()
    print("\nGATT application terminated")

deepo
Posts: 25
Joined: Sun Dec 30, 2018 8:36 pm

Re: Getting IP from headless RPi

Thu Jun 20, 2019 9:14 pm

Douglas6 wrote:
Thu Jun 20, 2019 7:12 pm
Just as a proof of concept, this code works to set up a BLE service on the Pi with one characteristic providing the Pi's IP address. Advantages: should work from any modern Android or iPhone. Disadvantages: many, but it's a little more modern than Morse code. You'll need to grab the imports from my github: https://github.com/Douglas6/cputemp.

Code: Select all

#!/usr/bin/python3
 
import dbus
from subprocess import check_output
 
from advertisement import Advertisement
from service import Application, Service, Characteristic, Descriptor
 
GATT_CHRC_IFACE = "org.bluez.GattCharacteristic1"
 
class IPAddressAdvertisement(Advertisement):
    def __init__(self, index):
        Advertisement.__init__(self, index, "peripheral")
        self.include_tx_power = True
 
class IPAddressService(Service):
    IPADDR_SVC_UUID = "00000001-810e-4a5b-8d75-3e5b444bc3cf"
 
    def __init__(self, index):
        Service.__init__(self, index, self.IPADDR_SVC_UUID, True)
        self.add_characteristic(IPAddressCharacteristic(self))
 
class IPAddressCharacteristic(Characteristic):
    IPADDR_CHARACTERISTIC_UUID = "00000002-810e-4a5b-8d75-3e5b444bc3cf"
 
    def __init__(self, service):
        Characteristic.__init__(
                self, self.IPADDR_CHARACTERISTIC_UUID,
                ["read"], service)
 
    def ReadValue(self, options):
        value = []
 
        ipaddrs = check_output(["hostname", "-I"])
        ipaddr = ipaddrs.split()[0]
        for c in ipaddr:
            value.append(dbus.Byte(c))
 
        return value
 
app = Application()
app.add_service(IPAddressService(0))
app.register()
 
adv = IPAddressAdvertisement(0)
adv.register()
 
try:
    app.run()
except KeyboardInterrupt:
    app.quit()
    print("\nGATT application terminated")
Works fine!
Thumbs up for this solution!

I'm using LightBlue for iOS.
The IP address is displayed as 0x3139322E3136382E302E3636 for 192.168.0.66.
It makes it a little hard to read, but it works.

/Mogens

User avatar
Douglas6
Posts: 4672
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Getting IP from headless RPi

Thu Jun 20, 2019 9:26 pm

deepo wrote:
Thu Jun 20, 2019 9:14 pm
It makes it a little hard to read, but it works
Ah, sorry about that. I use nRF Connect on my Android, and it displays the data both in hex and ascii.

User avatar
HawaiianPi
Posts: 4235
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting IP from headless RPi

Thu Jun 20, 2019 9:43 pm

Andymator wrote:
Thu Jun 20, 2019 12:43 pm
The email/text-message idea sounds great, but doesnt that require the pi to actually be logged on to the home network?
Yes, but fortunately that part is easy.

I'm going to assume the students know the details of their own home WiFi (otherwise, how would they use their laptops at home).

Raspbian has a provision for configuring WiFi prior to booting by creating a file named wpa_supplicant.conf on the small FAT32 "boot" partition of a Raspbian imaged SD card. When Raspbian finds that file it will move it to its correct location and connect to the network. The file should contain the following information.

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
	ssid="WiFi SSID"
	psk="WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}
Edit country=, ssid= and psk= with the correct information and save the file.

So now you're probably wondering about going back and forth between school and home. Well it's possible to configure multiple networks in wpa_supplicant.conf by including multiple network sections.

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
	ssid="School WiFi SSID"
	psk="School WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}

network={
	ssid="Home WiFi SSID"
	psk="Home WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}

FYI:
NOOBS is not recommended for headless systems, since it's an interactive installer and not a functional OS. For headless systems use the Raspbian images written to SD cards with Etcher.

Based on your post, here's an example of how this could be done.
1. Student brings home laptop and rpi from school
Before doing so, copy the existing wpa_supplicant.conf file to the SD card "boot" partition and shutdown the Raspberry Pi.

Code: Select all

sudo cp /etc/wpa_supplicant/wpa_supplicant.conf /boot/wpa_supplicant.conf && sudo shutdown now
2. Student wants to get the rpi on to the home network without having to configure router or hook rpi up to a monitor
  • At home, put the card into the Windows PC.
  • Edit the wpa_supplicant.conf file to add the home network info (boot partition is accessible from Windows).
  • Safely eject the card from the PC and use it to boot the Pi.
3. Student wants to get the ip of the rpi to ssh to it with MobaXTerm with laptop (just like in school)
  • A startup script or program would email or text the IP to the student.

Of course the wpa_supplicant.conf file could be configured for both networks at school, if you believe the students would need help with that. My example above was responding to your numbered points directly.

It might help to know the age and experience level of the students, but the most difficult part of my email/text scenario would be the startup script or code that emails the IP address, which could be prepared in advance for the students (or could be a class exercise to create).

Text messaging can be achieved through email with many phone carriers. For example, someone can text me from their computer by emailing the message to my phone [email protected] ([email protected]), but I have no idea of that's available in all countries.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

User avatar
HawaiianPi
Posts: 4235
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting IP from headless RPi

Fri Jun 21, 2019 3:15 am

If the email/text thing seems overly complicated, they could use wpa_supplicant.conf to connect to their network then use ping to get the IP address (as suggested by epoch1970).

Assuming they are using an up-to-date version of Windows 10 with working mDNS (it was buggy in older versions), they could open Windows PowerShell and ping the Pi's hostname (raspberrypi or raspberrypi.local), which will return the IP address in the response (add -4 for IPV4).

Example finding the IP of my Creeper Pie Minecraft SMP server.

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\hawaiianpi> ping -4 creeperpie.local

Pinging CreeperPie.local [192.168.0.123] with 32 bytes of data:
Reply from 192.168.0.123: bytes=32 time=1ms TTL=64
Reply from 192.168.0.123: bytes=32 time=1ms TTL=64
Reply from 192.168.0.123: bytes=32 time=1ms TTL=64
Reply from 192.168.0.123: bytes=32 time=1ms TTL=64

Ping statistics for 192.168.0.123:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 1ms, Maximum = 1ms, Average = 1ms
PS C:\Users\hawaiianpi>
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

Andymator
Posts: 25
Joined: Sat Jun 30, 2018 9:38 am

Re: Getting IP from headless RPi

Mon Jun 24, 2019 9:21 am

Thank you all for your input.
I ended up adding my home network info to the wpa_supplicant-file and making a mail-script autorun by adding a line to rc.local.

Another issue has turned up.

After doing these changes to the wpa_supplicant- and rc.local-file and then commenting them away - the pi does no longer auto-connect to my home router as it did before all this. I had added the mac-adress of the pi to my router and assigned it a specific ip-adress. This worked before, but not anymore and the only way I can get the pi to connect now is by putting back the network info to my wpa_supplicant-file.

I need the pi to auto connect to the schools network (which has the mac-adress added to its client list), but also auto connect to a home network when the pi is there. I dont want to abandon the auto connect solution to the schools network that is currently in play, that uses the mac-adress.

Any ideas?

User avatar
HawaiianPi
Posts: 4235
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting IP from headless RPi

Mon Jun 24, 2019 8:07 pm

As I mentioned somewhere above, you can have multiple networks in wpa_supplicant.conf and you'r Pi will connect to whichever network it finds when it boots. There is no need to comment out anything. Just leave all the networks in the file.

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
	ssid="School WiFi SSID"
	psk="School WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}

network={
	ssid="Home WiFi SSID"
	psk="Home WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}

You can optionally assign priorities, but I have not needed to myself. For example, I have the following in mine:

Code: Select all

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
	ssid="2.4GHz WiFi SSID"
	psk="2.4GHz WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}

network={
	ssid="5GHz WiFi SSID"
	psk="5GHz WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}

network={
	ssid="Mobile hotspot WiFi SSID"
	psk="Mobile hotspot WPA/WPA2 passphrase"
	key_mgmt=WPA-PSK
}
Even though the 2.4GHz network signal is stronger in my bedroom, the slightly weaker 5GHz WiFi is faster, and the 3B+ always picks that one. However, the same wpa_supplicant.conf file works great on my older Pi 3B and Pi0W, which don't support 5GHz WiFi.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

Andymator
Posts: 25
Joined: Sat Jun 30, 2018 9:38 am

Re: Getting IP from headless RPi

Tue Jun 25, 2019 8:03 am

But the fact that the pies get their ip from the school network from the router having their mac-adresses is something I cant change.
I understand that having both the school-network and the students home-network in the wpa_supplicant-file would be ideal, but I cant do that.

Is it possible to have the pi connect to the home network with help from the wpa_supplicant-file and the pi connect to the scools network by having its mac-adress added to the schools routers?

PiGraham
Posts: 3553
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Getting IP from headless RPi

Tue Jun 25, 2019 8:49 am

Andymator wrote:
Tue Jun 25, 2019 8:03 am
But the fact that the pies get their ip from the school network from the router having their mac-adresses is something I cant change.
I understand that having both the school-network and the students home-network in the wpa_supplicant-file would be ideal, but I cant do that.

Is it possible to have the pi connect to the home network with help from the wpa_supplicant-file and the pi connect to the scools network by having its mac-adress added to the schools routers?
The MAC address doesn't change so it the router the Pi connects to looks for the specific MAC address that should still work.
At school and at how the router assigns an ip address. You are likely to get different iP addresses in each location unless you configure the routers to assign the same addresses. That shouldn't be a problem as long as you know with the address is in each case.

Put both networks in wpa_supplicant.conf and the Pi should automatically connect at home and at school.

Andymator
Posts: 25
Joined: Sat Jun 30, 2018 9:38 am

Re: Getting IP from headless RPi

Tue Jun 25, 2019 12:00 pm

PiGraham wrote:
Tue Jun 25, 2019 8:49 am

Put both networks in wpa_supplicant.conf and the Pi should automatically connect at home and at school.
But the problem is that I dont have access to the school network settings and I cant count on getting them.
The solution the school-tech-guys provided was putting all 90 pi-mac-adresses into their routers. That has worked great this past year. I believe they put the pies on a subnet that does not require any login or acitivity to stay on. I cant change the way the pies logon to the school network.

Return to “General discussion”