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

How to connect WiFi on RPi4 and Raspbian Buster on startup?

Wed Oct 16, 2019 7:49 pm

I have a RPi4 which runs Buster and is connected by wire to the netywork.
I am trying to configure it to also connect via WiFi in preparation for moving it to a location where wired network is not available.
The RPi is headless, the only access is via PuTTY command line.

What I have done is to edit the /etc/wpa_supplicant/wpa_supplicant.conf file:

Code: Select all

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

network={
        ssid="currentlocation-ssid"
        psk="currentlocation-pwd"
}

network={
        ssid="remotelocation-ssid"
        psk="remotelocation-pwd"
}
Then I have used this to restart wlan0:

Code: Select all

sudo ifconfig wlan0 down
sudo ifconfig wlan0 up
Now I expected the RPi4 to connect to the WiFi network currentlocation-ssid, but this is what I see:

Code: Select all

$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.65  netmask 255.255.255.0  broadcast 10.0.0.255
         .....
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
     ......
wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether dc:a6:32:05:6c:db  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
So apparently it is not connecting even though I have done what I think I should.
What am I doing wrong here?

I need to configure this before I travel to the remote location so that the RPi4 will connect there when it is powered on.

I also edited the /etc/wpa_supplicant/wpa_supplicant-wlan0.conf file andf entered the same network data there...
Still no change....
Bo Berglund
Sweden

User avatar
TideMan
Posts: 234
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

Re: How to connect WiFi on RPi4 and Raspbian Buster on startup?

Wed Oct 16, 2019 8:26 pm

Have you tried shutting down, disconnecting the ethernet cable, then powering up?
This will remove eth0 from the equation.

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

Re: How to connect WiFi on RPi4 and Raspbian Buster on startup?

Wed Oct 16, 2019 9:14 pm

Problem:
If I do this and it actually does connect to WiFi, then I won't be able to know its IP address.
So I cannot connect to it with ssh (PuTTy) as I normally do.
I do have a script that runs 1 min after boot and which sends me an email containing the IP addess it is running on.
But that of course will not go out unless it does connect within the 1 min timeframe.

Now I am preparing a fresh installation SDcard with the Buster Lite version and where I have put a wpa_supplicant.conf file on the boot partition. Also created an empty ssh.txt file on that partition.
So that would perhaps result in a WiFi connection, but still I won't know the IP address...
Bo Berglund
Sweden

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

Re: How to connect WiFi on RPi4 and Raspbian Buster on startup?

Wed Oct 16, 2019 9:45 pm

I usually pre-configure the card with ssh and wpa_supplicant.conf files in the small FAT32 "boot" partition. No need for a keyboard, screen or Ethernet connection (completely headless system setup).

  1. Write a Raspbian image to the SD card with Etcher.
  2. Remove and reinsert the card, so your Windows/Mac OS PC can see the "boot" partition (ignore/canel format warnings).
  3. Add an empty file named ssh (or ssh.txt).
  4. Add a pre-configured wpa_supplicant.conf file.
  5. Safely eject the card and use it to boot the headless Pi computer.

The wpa_supplicant.conf file should contain the following.

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 your information and save the file (use the 2 letter country abbreviation in CAPS).

When Raspbian finds a wpa_supplicant.conf file in the "boot" partition it will move it to its correct location and use it to connect to the network. If there was a previous wpa_supplicant.conf file, it will be overwritten, so this can also be used to update WiFi connections if you get a new router or change your router settings.


Notes:
Multiple network sections can be included for different networks/locations.

Use a pure text editor (not a word processor) to create/edit the wpa_supplicant.conf file.

I have used this technique for years configuring many completely headless Pi computers. It works with the built-in WiFi on Pi 3, 4 and Zero W models, as well as non-WiFi models with a Raspbian compatible USB WiFi dongle. I just re-tested it on a Pi4 with the latest Raspbian Lite image available at the time of this post (2019-09-26), and it worked as expected.
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: 4728
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: How to connect WiFi on RPi4 and Raspbian Buster on startup?

Wed Oct 16, 2019 9:51 pm

Bosse_B wrote:
Wed Oct 16, 2019 9:14 pm
Problem:
If I do this and it actually does connect to WiFi, then I won't be able to know its IP address.
You can find the IP with a network scanner app like Fing for smartphones (Android/iOS).

So I cannot connect to it with ssh (PuTTy) as I normally do.
You can SSH into pi@raspberrypi.local (or pi@raspberrypi) for the initial connection. Could be problematic if you have multiple Pi computers on the same network with the default hostname, so I usually change the hostname after connecting to a new 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?

Return to “Raspbian”