nickoala
Posts: 7
Joined: Sat Apr 09, 2016 8:46 am

Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 3:23 pm

Hello,

I have been teaching a Raspberry Pi course for adults for 3 years now. The classroom, already equipped with computers, is not dedicated to Pi. It is impractical to remove the monitor, keyboard, and mouse from the back of computers and re-plug them to Pi. Short of using console cables (too hard for beginners), the only way to use Pi is to SSH in. To SSH in, Pi have to join the school's WiFi (because we have no physical access to router. Even if we do, I don't want LAN cables crisscrossing the classroom). After joining WiFi, each Pi has to have its IP discovered. Only after that can we SSH in and start working.

All Pi have the same hostname (SD freshly burned) and unknown MAC addresses. To cut the story short, the process of discovering (and distinguishing) multiple IPs has troubled me for 3 years. I make do with a variety of methods over the years. None are satisfactory.

In fact, the inconvenience of joining WiFi and discovering IPs happens every time I use my Pi away from home. The problem is exacerbated in a group setting where each person has their own Pi. There are ways to cope; all leave much to be desired.

I have detailed the problem and open-sourced my solution below. It's called Plug n Pi:

https://github.com/nickoala/pnpi

In essence, USB-connect your phone to your Pi. Use an app to join WiFi for the Pi, and see its IP. You need a "USB server" installed on the Pi, and an app installed on an Android phone (no iOS, sorry).

I am using it every time I use my Pi away from home. It's now a breeze to join a WiFi hotspot and look up the Pi's IP. I hope you find it useful, too.

Cheers,
Nick

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 4:25 pm

Excellent work :)

Worked 1st time for me on a Pi3B+


Does the server on the Pi take up much resources while waiting to see if a phone has been plugged in?

I tried something similar a few years ago (using network broadcasts) and what I did was to only run my Pi program for 3mins after boot so that it didn't take up any resources if it wasn't used

jbudd
Posts: 988
Joined: Mon Dec 16, 2013 10:23 am

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 4:59 pm

It must be a nightmare to be faced with a room full of newly burned Raspberry Pis all headless and with the default hostname.

I wish that you could change the hostname from default by a file in the boot partition, similar to enabling ssh and wpa_supplicant.conf:
If the Pi has the default hostname and the boot process finds /boot/hostname or /boot/hostname.txt, set the hostname, delete the file and reboot.
Now, if the network supports it, you can connect by ssh [email protected]

Edit: added .local
Last edited by jbudd on Sat Aug 18, 2018 7:53 pm, edited 1 time in total.

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

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 6:59 pm

jbudd wrote:
Sat Aug 18, 2018 4:59 pm
It must be a nightmare to be faced with a room full of newly burned Raspberry Pis all headless and with the default hostname.

I wish that you could change the hostname from default by a file in the boot partition, similar to enabling ssh and wpa_supplicant.conf:
I use my own custom install script. It takes a Raspbian image, unzips it (actually I download the images and recompress them with xz to save space on my local server Pi), resizes the FS to fit the card/device, does some custom tweaks to the FS and prompts for a hostname. Default is raspberrypi, but I can put in anything I like. That's how raspi7 to raspi10 were configured on my network. (Ok, I had to manually configure DNSMasq running on raspi5 to issue the correct IP addresses for those hostnames, but that isn't hard).

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 7:08 pm

I wish that you could change the hostname from default by a file in the boot partition, similar to enabling ssh and wpa_supplicant.conf:
If the Pi has the default hostname and the boot process finds /boot/hostname or /boot/hostname.txt, set the hostname, delete the file and reboot.
One of the most un-explained reluctance's, on RPi's part, has always been to give us the ability to pre-configure a Pi, before 1st boot.

It took years to get the wpa-supplicant.conf file ability

Why this is remains a mystery, as it would make life a lot, lot easier for educational classroom use

And quite convenient for a lot of hobbyist's as well

jbudd
Posts: 988
Joined: Mon Dec 16, 2013 10:23 am

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 7:18 pm

Why this is remains a mystery, as it would make life a lot, lot easier for educational classroom use

And quite convenient for a lot of hobbyist's as well
Agreed. It's not too difficult to tweek Raspbian to do first boot setup and save an image. That's no use in a lesson though where step 1 is "Download the latest Raspbian from raspberrypi.org"

User avatar
thagrol
Posts: 1844
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 7:31 pm

jbudd wrote:
Sat Aug 18, 2018 4:59 pm
It must be a nightmare to be faced with a room full of newly burned Raspberry Pis all headless and with the default hostname.

I wish that you could change the hostname from default by a file in the boot partition, similar to enabling ssh and wpa_supplicant.conf:
If the Pi has the default hostname and the boot process finds /boot/hostname or /boot/hostname.txt, set the hostname, delete the file and reboot.
Now, if the network supports it, you can connect by ssh [email protected]
Not quite what you want but I've a script here that sets hostname from a Pi's serial number: https://github.com/thagrol/usb-gadget. Call it @reboot in root's crontab or from /etc/rc.local
This space unintentionally left blank.

jbudd
Posts: 988
Joined: Mon Dec 16, 2013 10:23 am

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sat Aug 18, 2018 7:44 pm

nikoala's post is addressing the problem of a bunch of headless Pi's with newly burned OS, unknown Mac and ip addresses and all with the same hostname. Probably without access to the router admin page too.

There's many ways to tackle it if one can use a customised Raspbian image. His/her Android app seems like an innovative approach that should work with Raspbian out of the box. (I've not tried it yet)

Edit
Maybe not Raspbian out of the box:
"You need a usb server on the pi"

nickoala
Posts: 7
Joined: Sat Apr 09, 2016 8:46 am

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sun Aug 19, 2018 6:17 am

To make it easy for students, a customized SD image is almost a must. In my situation, at least I have to enable SSH server on the SD cards, so students can SSH in right away.

However, there are practical limits to customization. jbudd's and thagrol's suggestion to modify hostname per card is doable. But the next question comes: how do I remember which SD card has which hostname? Stick a label to each SD card? In thagrol's case, how do I know the serial number beforehand?

Furthermore, hostname.local is not a reliable means to reach Pi, in my opinion. It depends on what software you have installed on the computer. Network scan is also not guaranteed to return the hostnames.

The nice thing about Plug n Pi is, once you have it installed (or pre-installed on a customized image), it works the same everywhere, regardless of the outside environment. That is the biggest appeal to me.

In response to mattmiller's question of how much resources the USB server consumes, I just checked and it's using about 1% CPU on Pi 3B+. Not as low as I would like, but acceptable I assume.

Cheers,
Nick

nickoala
Posts: 7
Joined: Sat Apr 09, 2016 8:46 am

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sun Aug 19, 2018 6:24 am

Regarding the RPi Foundation's reluctance to implement the aforementioned tweaks to Raspbian, I tend to empathize with them. I imagine, with Pi's popularity, they have to deal with a lot of requests from a lot of users. They have to decide where to devote resources.

I certainly would like to see Plug n Pi included in Raspbian. I believe it can benefit many users, especially in a group setting. But that's not my decision. So far, I am glad to receive positive responses from the community.

User avatar
thagrol
Posts: 1844
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sun Aug 19, 2018 12:09 pm

nickoala wrote:
Sun Aug 19, 2018 6:17 am
In thagrol's case, how do I know the serial number beforehand?
You don't :(. But my script writes the new hostname to /boot/hostname.txt so you could pull the SD card and read it from there. It also prionts it to the console.

Alternatively, if my script runs at every boot not just at first boot, you can label the Pi with the generated hostanme and let the script update the SD card to match the Pi it's currently in. There is no need to make sure the correct SD card is in the correct Pi.

I decided to generate the hostname from the serial number precisely so the hostname is tied to the Pi not to the SD card.
This space unintentionally left blank.

nickoala
Posts: 7
Joined: Sat Apr 09, 2016 8:46 am

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sun Aug 19, 2018 3:12 pm

I see. You make the hostname tied to the CPU instead of the SD card. That's a new way of thinking!

User avatar
thagrol
Posts: 1844
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Sun Aug 19, 2018 7:18 pm

nickoala wrote:
Sun Aug 19, 2018 3:12 pm
I see. You make the hostname tied to the CPU instead of the SD card. That's a new way of thinking!
Not really. It's similar to how raspbian generates the MAC address for ethernet and WiFi. My script grew out of a need I had with a Pi zero for loading the g_ether module with the same MAC addresses each time and with minimal chance of a conflict when doing so. Figured doing the same for the hostname made sense while I was at it.

I wouldn't use this method in a server farm (where you want hostname to be independent of the hardware) but for a collection of Pi that need to be set up with minimal effort configuring one Pi then cloning its SD card seemed the way to go.
This space unintentionally left blank.

User avatar
thagrol
Posts: 1844
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Wed Sep 05, 2018 1:38 pm

Just a heads up.

I've posted an upate to the script I mentioned above. It now needs just one .py file and allows the use of arbitary hostnames via alookup table.

https://github.com/thagrol/usb-gadget
This space unintentionally left blank.

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

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Wed Sep 05, 2018 7:39 pm

nickoala wrote:
Sat Aug 18, 2018 3:23 pm
...
Short of using console cables (too hard for beginners)
Curious as to why you think using a console cable is hard?
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?

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Wed Sep 05, 2018 11:44 pm

Curious as to why you think using a console cable is hard?
I'm with @nickoala - console cables are not beginner/muggle devices - particularly not in an educational setting :)

jbudd
Posts: 988
Joined: Mon Dec 16, 2013 10:23 am

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Wed Sep 05, 2018 11:57 pm

Curious as to why you think using a console cable is hard?
What is a console cable?

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

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Thu Sep 06, 2018 2:10 am

mattmiller wrote:
Wed Sep 05, 2018 11:44 pm
I'm with @nickoala - console cables are not beginner/muggle devices - particularly not in an educational setting :)
Again, why? They are very easy to use. How is burn, boot, login harder for beginners?

jbudd wrote:
Wed Sep 05, 2018 11:57 pm
What is a console cable?
A serial cable that you connect to a host PC, and the output of the CLI is then displayed in a serial terminal on the PC.
Image
Note the IP address a few lines above the login prompt. You can even login without a network connection.

I use CP2102 USB-3V3TTL adapters purchased from ebay for less than 2 bucks apiece,
Image
but there are more Pi specific cables available as well.
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?

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Thu Sep 06, 2018 8:31 am

Again, why? They are very easy to use.
Getting a beginner to plug it into the right pins the right way around would be challenge enough :)

An app on a phone is a beginner way :)

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

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Thu Sep 06, 2018 10:17 am

mattmiller wrote:
Thu Sep 06, 2018 8:31 am
Getting a beginner to plug it into the right pins the right way around would be challenge enough :)

An app on a phone is a beginner way :)
Not really that hard to plug in a cable, and I thought we were talking about a classroom environment, where I assume such things would already be done. But whatever...

Every time I bring up console cables I get kickback from the forum. People seem to think they're some kind of evil voodoo magic or something. They are actually extremely simple to use, really inexpensive, and a great tool for troubleshooting. But if you guys would rather develop apps and write complicated scripts, go for it. I can't imagine why anyone would think that's easier than using an existing tool the developers of the Pi put in place to help you, but it seems like you all want to do things the hard way. :?
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?

mattmiller
Posts: 2106
Joined: Thu Feb 05, 2015 11:25 pm

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Thu Sep 06, 2018 11:31 am

‍They are actually extremely simple to use, really inexpensive, and a great tool for troubleshooting.
For us geeks :)

I've got one (don't use it much but it's there if I need to)

But i sometimes work in education and its not a tool for the classroom

Unless the lesson is how to use a console cable :)

We just want to SSH/VNC in and get going.

We really want our Pi to automagically connect to the schools locked down tight WiFi network and light up an in-built display telling us its Ip address, hostname and how its feeling today :)

But that's not the case but we want to get as close to that as possible

User avatar
thagrol
Posts: 1844
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Thu Sep 06, 2018 12:12 pm

Console cables (aka USB to TTL serial cables) definitely have their uses and aren't difficult to use provided all you want is terminal access. If you want/need more than that, while it can be done it isn't something that's easy for a beginner to setup. And won't perform well. Anyone else remember dial-up internet?

That said, they do have their uses. Debuging USB gadget stuff and watching boot message on a headless Pi for two.

As for complicated scripts, the one I linked to grew out of a need I had for fixed MAC addresses and unique hostnames for Pi zeros running as USB ethernet gadgets without needing to manually edit every SD card. Sure, I could have just printed the Pi's MAC address, hostname, and IP address to the console but that still needs a script and needs an additional connection to the host along with additional software steps on the host to see it.
This space unintentionally left blank.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Plug n Pi: USB-connect your phone to look up Raspberry Pi's IP address

Thu Sep 06, 2018 12:55 pm

Every time I bring up console cables I get kickback from the forum. People seem to think they're some kind of evil voodoo magic or something. They are actually extremely simple to use, really inexpensive, and a great tool for troubleshooting. But if you guys would rather develop apps and write complicated scripts, go for it. I can't imagine why anyone would think that's easier than using an existing tool the developers of the Pi put in place to help you, but it seems like you all want to do things the hard way.
Part of the problem is the use of the term "console cable" - a term I've never heard before and which means next to nothing to me, despite my having used serial cables to connect to Unix machines for decades.

But, terminology aside, the world divides into two groups, with almost null set as the intersection:

1) People who understand and would use a serial cable.

2) People who think smartphones are the be-all and end-all of existence and that everyone has one and that you get one assigned to you at birth.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Return to “General discussion”