Raspberry Pi 4 Configuration for IoT Projects: Quick and Easy Headless Setup Over Wifi

After doing this a few times for my project Plynth and writing up my own notes to keep track of things, I’d figured I’d share it for anyone else that might need it. Note that I’ve tested this on Raspberry Pi B 3+ and Raspberry Pi 4, though it should work on most Raspberry Pi boards with wifi. Note that these instructions are written for Mac, but you can find instructions for both Mac and Windows here.

Ok, so you want to use your Raspberry Pi for an IoT-type project, and you’re looking to set it up over Wifi without plugging it into a screen? If you’re someone who’s just getting into Raspberry Pi, you may be surprised to find that it’s not as intuitive as you’d expect. So here’s a quick and easy way to get your Raspberry Pi set up and ready to go–no HDMI or Ethernet cables required.

Steps are fairly short:

  1. Flash the OS to your SD card (same as most Pi setups)

  2. Enable SSH and add wifi credentials (before you plug in your SD card)

  3. Plug the SD Card into your Pi and connect over SSH

  4. Change the Hostname and Password

Flash Raspian to your SD card

I usually use Raspian Lite, since that’s as much as I need for my types of projects. But you can run whichever type of Raspian you prefer. I use Etcher from Balena to flash the image to an SD card:

  1. Install Etcher

  2. Download the image for whichever OS you’re using to run as a zip file

  3. Etch the image to the card using Etcher

Now grab a coffee or a snack or something, it might take a minute or two.

Enable SSH and add wifi credentials

If you were doing a traditional Raspberry Pi setup, now is when you’d pop the SD card into your Pi–but you’re not doing a traditional setup! Instead, you want to do two things. First, you’ll want to enable SSH, which is disabled in Raspian by default for security reasons. SSH lets you log into and manage the Raspberry Pi remotely.

Second, you’re going to want to add your wifi username and password, which will allow the Pi to connect to your wifi network as soon as it installs Raspian and reboots. This saves you the hassle of connecting a screen or keyboard to your Pi to manually type in the credentials, or connecting it via Ethernet to your router to connect to it over SSH. Bottom line: this is a lot easier.

Etcher ejects the SD card when it finishes, so remove the card and plug it back in. Then, you can open Terminal (on a Mac) and navigate to the boot volume on the SD card (on a Mac, the card appears as ‘boot’) and enter:

$ touch ssh$ touch wpa_supplicant.conf

This will create an empty and extensionless SSH file in boot, which will activate ssh by default, and a file to configure the wifi connection. Alternatively, you can use these links to download the ssh file and wpa_supplicant.conf file and drag them to the boot volume

After you’ve added the files to the card, you’ll need to input your wifi name and password so the Pi can connect. Open the wpa_supplicant.conf file and copy and paste this text (or, just update it if you downloaded the files):

country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={   
     ssid=”WIFI-NAME”
     psk=”WIFI-PASSWORD"
}

Change WIFI-NAME to the name of your wifi network and WIFI-PASSWORD to your wifi password.

Plug the SD Card into your Pi and connect over SSH

Eject the SD card from your computer and plug it into your Pi. Now plug in the Pi’s power supply and let it boot up. This will take a minute or two, but in the meantime, open Terminal on your computer.

This next step assumes you don’t have any other Pi’s on your network with the default Hostname (if this is your only Raspberry Pi, then you’re good). In Terminal, type:

$ ssh-keygen -R raspberrypi.local
$ ssh pi@raspberrypi.local

The first command just clears out any previous reference to raspberry.local, so ignore any errors. Once the Pi boots up, you should see a message prompting you to type Yes or No. Type Yes, and you’re in.

Change the Hostname and Password

Though not required, the next step is strongly encouraged. It’s best practice to change the Hostname and password. This makes your Pi more secure, and changing the Hostname means that if you set up a new raspberry pi, they would both have the same Hostname, which adds a few more steps to the setup process.

In the Raspberry Pi terminal, type:

$ sudo raspi-config

Select the first option in the menu, and then change your Password. Once you’re done, you can use the second option in the menu to change your Hostname.

Bonus: Get updates

Just always best practice to get updates once you set up a new Pi (or any device, really).

$ sudo apt-get update$ sudo apt-get dist-upgrade


Jono Matusky