Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Headless RPi4 moving to new location, how to adjust WiFi settings?

Mon Mar 02, 2020 8:35 am

I am preparing an RPi4 unit for use as a headless data backup server placed on a remote network and connecting via WiFi.
In order for this to work on location I need to enter the WiFi connect data into the wpa_supplicant.conf file and here is the problem:
I believe that I will not have the information until I arrive on location and then I will not be able to connect to the RPi4 until it has itself connected to the WiFi...
Similar to the Hen-and-egg problem.

Possible procedure?
I can extract the SD-card from the RPi4 and put it into a USB carrier and then use my Win10 laptop to access the boot partition on the SDcard.
So I wonder if there is something I can do to write the SSID/password info to the boot partition such that it will be transferred to /etc/wpa_supplicant/wpa_supplicant.conf file on boot, and then used to connect the WiFi?

As soon as the RPi4 has connected to a network with Internet acces I have a script on board that will post the ip address of the unit to my website where I can read it and use to connect SSH.

On a brand new install disk I know that I can create a /boot/wpa_supplicant.conf file with the WiFi settings and it will be used during the setup phase of the RPi4 to make it connect to WiFi, whereupon the file is removed from /boot by the install process. But what happens if I put such a file in a fully configured RPi4 boot partition?
Will it overwrite any existing wpa_supplicant.conf file???
In which case I hope it takes a backup first....
Bo Berglund
Sweden

User avatar
DougieLawson
Posts: 38882
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Mon Mar 02, 2020 8:49 am

Add the new credentials now.

Code: Select all

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
        ssid="Old_Network_SSID"
        priority=90
        psk="Password_for_OLD_network"
}
network={
        ssid="SSID_for_NEW_Network"
        priority=80
        psk="Password_for_new_network"
}
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Mon Mar 02, 2020 12:04 pm

Yes I can do this if I know it now, in fact I have done so for a number of different places I may go to.
However:
I believe that I will not have the information until I arrive on location and then I will not be able to connect to the RPi4 until it has itself connected to the WiFi...
This is the problem...
I do not yet know the logon and I have to prepare something...

Idea:
1) I create a file in /boot which is named something like wifilogin.txt and it contains the full section for the network to connect to.
2) I modify my /etc/rc.local script to contain:

Code: Select all

wififile="/boot/wifilogin.txt"
if [ -e "$wififile" ]; then
  cat "$wififile" >> /etc/wpa_supplicant/wpa_supplicant.conf
  rm "$wififile"
fi
Then presumably the entry will be added to wpa_supplicant.conf and then deleted during boot.
And it would be a one-time thing.
Bo Berglund
Sweden

achrn
Posts: 408
Joined: Wed Feb 13, 2013 1:22 pm

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Mon Mar 02, 2020 12:35 pm

you can just put wpa_supplicant.conf into /boot and the system will copy it across into the right place.
https://www.raspberrypi.org/documentati ... eadless.md

At least, that supposedly works on first boot, I'm not sure if it does it on subsequent boots. I'd test it first ( do a dummy run with a wifi network you do have acecess to).

User avatar
DougieLawson
Posts: 38882
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Mon Mar 02, 2020 12:57 pm

Create a new SDCard just for the move. When that's booted & connected insert the original SDCard in a USB reader and fix the WiFi settings. Then swap cards and boot normally.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Mon Mar 02, 2020 5:00 pm

achrn wrote:
Mon Mar 02, 2020 12:35 pm
you can just put wpa_supplicant.conf into /boot and the system will copy it across into the right place.
https://www.raspberrypi.org/documentati ... eadless.md
That is usually what I do when I build new headless RPi systems, then it will connect to the WiFi on first boot and I can check the router to see what IP address it got and go from there...

My issue here is:
If I put another wpa_supplicant in /boot and it is copied to /etc/wpa_supplicant/ then it will overwrite the existing one deleting the settings I want to keep, right? What I proposed was to append only one network setting into the file on boot.

But I think I will test if the /etc/rc.local script can do what I need as outlined above and if that works I will include it in my RPi builds.
I can test at home because then I can always hook up Ethernet to it during boot so I can connect to it and correct whatever went bad.

Edit:

TEST REPORT:
The wpa_supplicant.conf file was properly updated on boot when I had modified the /etc/rc.local as described.
So I will use this on my RPi4 units in the future to safeguard for an unknown/changed WiFi!
Bo Berglund
Sweden

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

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 4:52 am

Put credentials for your mobile phone hotspot into your standard wpa_supplicant.conf. Make this network the highest priority.
If the Pi can see the hotspot when it boots, it will connect and you can edit wpa_supplicant.conf over ssh from your laptop.

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 9:58 am

Thanks for the suggestion!
How can I make a hotspot the highest priority entry?
I did not know this existed but:

I found this: (edited this post whilst you were replying....)
Turns out that there is an optional "priority" setting to use in each network block.
So setting priority=1 forces these networks to be tested before those without any such setting.
The highest value block will be tested first and the ones without a priority last in the order they are listed in the conf file.

Note:
I have already populated the conf file with my own networks including my phone.
So if nothing else connects I guess it will fall back to the phone hotspot....
Last edited by Bosse_B on Tue Mar 03, 2020 10:23 am, edited 3 times in total.
Bo Berglund
Sweden

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

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 10:18 am

Just add a line to the network stanza
Priority = 1

A network with no priority line is priority 0 and a higher number is a higher priority ie 2 is higher priority than 1!

You don't really need to set priority but in theory it lets you insist that the pi connects to your phone rather than any other network it knows.

NB it won't drop an existing connection to join a different network, you have to reboot.

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

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 10:34 am

Bosse_B wrote:
Mon Mar 02, 2020 5:00 pm

My issue here is:
If I put another wpa_supplicant in /boot and it is copied to /etc/wpa_supplicant/ then it will overwrite the existing one deleting the settings I want to keep, right? What I proposed was to append only one network setting into the file on boot.
Append the new network to the /boot/wpa_supplicant.conf
Then you don't lose anything.

You can start by making a copy of your live wpa_supplicant

Edit the copy to append other networks when you know them then save to /boot

User avatar
bensimmo
Posts: 4577
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 10:40 am

Another option I have used before is to set the Pi up so that it switched to be an Access Point if no know SSID is found.

Not tried it with a Pi4, just the earlier ones.

Edit to add link
https://www.raspberryconnect.com/projec ... connection

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

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 10:49 am

Another option that I looked at but didn't try yet is to have the Pi open it's own hotsport / soft AP if no other network can be found.

I was going to use that with a photobooth to configure it in various new locations.

See if this helps you:

https://raspberrypi.stackexchange.com/q ... -available

In a new location you should find the Pi creates an SSID you can connect to configure it, maybe by a config web page or just SSH.

[ETA]bensimmo's looks good.

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 10:57 am

Android phone Hotspot usage test
I started the hotspot on my Samsung Galaxy S7 phone.
Then I tested the connection to my RPi4 via the hotspot...

What happens is this:
After a reboot of the RPi4 with the hotspot priority set to 2 in the conf file and the hotspot running
1) It does connect to my hotspot according to the report in my phone
2) But it does not report its IP address to the website as expected, should happen 60 s after /etc/rc.local is executed...
3) I also connected my laptop to the hotspot and it did receive an IP address 192.168.43.121

Now I am stuck. Cannot talk to the RPi4 without knowing its address.
How can I find the IP address of my RPi4? Can't find anywhere this info on my phone (the client list only shows the MAC addresses).
Without the IP address I cannot really get any connection set up to the RPi4 such that I can edit the wpa_supplicant.conf file properly.

So I guess my original idea of creating a file on the SDcard and then boot the RPi4 from it is still the only way...
Bo Berglund
Sweden

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 11:10 am

bensimmo wrote:
Tue Mar 03, 2020 10:40 am
Another option I have used before is to set the Pi up so that it switched to be an Access Point if no know SSID is found.

Not tried it with a Pi4, just the earlier ones.
raspberry-pi-auto-wifi-hotspot-switch-direct-connection
I have had trouble before trying to set up an AP on the RPi4 so much so I had to scrap the whole Raspbian installation and start over again.
Not fun. And when reading the link I see that there are a LOT of stuff to install and configure for this, so I do not dare doing it.
I only have a few days left until I have to go to the site...
Bo Berglund
Sweden

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

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 11:12 am

You should be able to see the IP of connected devices from your Android hotspot.

Go to Mobile Hotspot and scroll down to Connected devices. Click on the device and you should see its IP Address.


You could try Fing Android app. It searches the local network for devices and should report the Pi and its IP address.

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 11:30 am

PiGraham wrote:
Tue Mar 03, 2020 11:12 am
You should be able to see the IP of connected devices from your Android hotspot.
Go to Mobile Hotspot and scroll down to Connected devices. Click on the device and you should see its IP Address.
Well, that was what I did and it only showed the MAC address of the RPi4...
Maybe it is possible to ask for the IP address of a MAC addressed device, but I don't know how...

Anyway I shut down the RPi and then the hotspot and restarted the RPi again.
Amazingly now my website got two posts, one with the hotspot address from the earlier start and one with its normal home IP address!

So I repeated the process again:
-shut down RPi4
-switch on Hotspot
-start RPi4
-check on the phone that it has connected
-connect my laptop to the hotspot
-use the IP address from the earlier report to connect PuTTY

And now it did work!
Maybe the first connection attempt to my phone hotspot somehow was only "half" done and therefore networking was not functioning properly?
Anyway, now it seems like it is working. :D :D :D

PS: When I switched off the hotspot on my phone the RPi4 auto-connected to the home wifi without any restart. DS
Bo Berglund
Sweden

Bosse_B
Posts: 966
Joined: Thu Jan 30, 2014 9:53 am

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 2:08 pm

PiGraham wrote:
Tue Mar 03, 2020 11:12 am
You should be able to see the IP of connected devices from your Android hotspot.
Go to Mobile Hotspot and scroll down to Connected devices. Click on the device and you should see its IP Address.
Reply #2 after more testing:
You are right, but I need to go the following route after the RPi4 has connected:
-Settings
-Connections
-Mobile hotspot
-Mobile Hotspot (same name again, new location)
-In "connected devices" click name of device

Now a list with the IP Address, MAC, Connection time and Connection duration is shown.
Bo Berglund
Sweden

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

Re: Headless RPi4 moving to new location, how to adjust WiFi settings?

Tue Mar 03, 2020 10:20 pm

PS: When I switched off the hotspot on my phone the RPi4 auto-connected to the home wifi without any restart.
Yes, that's what it should do. But the Pi (or most wifi devices) will not drop an existing connection because a preferred one is available. So if you needed grab it from the wifi onto your hotspot you'd have to reboot it.

ps - IP address:
On my Windows laptop I use IPScan24 to find connected devices.
On my phone I use Fing - but I don't know if it will work on the same phone that's creating the hotspot.

Return to “General discussion”