How to connect remotely to Raspberry Pi from a Mac using a mobile hotspot
It’s extremely convenient to be able to connect to a microcontroller wirelessly when working on an IoT or robotics project (or, really, anything with embedded systems). It makes it so you can quickly update code without plugging/unplugging, which is especially helpful if you’re building something that moves on boot. If you’re a beginner, setting this up can often be intimidating and confusing. Here’s a guide that hopefully helps!
To begin, you will need a mobile hotspot. You will need to know its name and password.
[Downloading Raspbian onto the RPi]
Raspbian is a free operating software (OS) designed for Raspberry Pi hardware. You need it to do pretty much anything with a RPi board. Go to https://www.raspberrypi.com/software/operating-systems/ and download an RPi OS image. I used the 32-bit Bullseye with recommended software. Optionally, you can rename the file (ex. “rpiimage.img”).
Plug a micro SD card into your Mac, it should show up in the “Locations” section of Finder.
Find the SD card location with the following terminal command (You will be able to tell which disk by the disk size and the description). To open terminal on a Mac, you can use the search button next to the WiFi symbol. You may need to change directory with ‘cd Downloads’, if you can see your image in the Downloads folder:
diskutil list
Unmount the SD card with the following terminal command (Replace ‘disk2’ with your disk):
sudo diskutil unmountdisk /dev/disk2
Write the image onto your SD card with the following terminal command (Replace ‘rpiimage.img’ with your file name):
sudo dd if=rpiimage.img of=/dev/rdisk2 bs=1m
The SD card should now show up in your locations as ‘/boot’.
[Editing and adding /boot files]
Note: Normally, we could tell the RPi how to connect to WiFi by plugging it into a monitor and editing a file titled ‘wpa_supplicant.conf’. We can get the same results by giving the SD card a file with the same name and the relevant WiFI info.
Create a file in TextEdit (TextMate, etc. — any text editor) and name it ‘wpa_supplicant.conf’. Copy and paste the following lines of code into the file (Replace SSID with the name of your mobile hotspot, replace psk with the password.):
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={ssid=”phonebot”psk=”botpass123"priority=10}
More networks can be added by copying and pasting the network={} section and adding new network information. Note that priorities can be added as well.
Create an empty file in TextEdit, and name it ‘ssh’. This is a shortcut and allows us to enable secure shell login without having to connect the RPi to a monitor.
Copy and paste both ‘ssh’ and ‘wpa_supplicant.conf’ into the /boot directory. You can do this with terminal, but I was getting a ‘disk is read only’ error and got around it with CTRL+C/V.
[Finding RPi IP address]
Eject the SD card from your laptop, and insert it into your RPi. Connect the RPi to a power supply using the micro USB port. The red light should be on, the green light should be flashing.
Connect your laptop to your mobile hotspot.
After a few minutes, your RPi should have connected to your hotspot with the information you gave it in the ‘wpa_supplicant.conf’ file (I would wait five or six minutes before thinking something is wrong, mine took a while to connect).
In terminal, type the following to get the RPi IP address:
arp -a | grep b8:27:eb
‘arp -a’ asks the terminal to look at the devices that are connected to each other, and ‘grep b8:27:eb’ asks terminal to look for devices with the MAC address b8:27:eb. MAC addresses show the maker of the device, and b8:27:eb corresponds to the MAC address of the RPi makers.
For information’s sake, you can find your laptop IP address by typing ‘ifconfig’ into terminal and looking for an ‘en#’ listed as active. I found my phone’s IP address with a network analyzer app. You can ping any device connected to the hotspot from your laptop with terminal command ‘ping’ followed by a space and an IP address. If you see timeout errors, something isn’t connected to your hotspot or there’s a typo somewhere.
[Logging into the RPi]
Now that we have the RPi’s IP address, we can log in using secure shell. Type the following line into terminal (Replace ‘172.20.10.8’ with your Rpi IP):
ssh pi@172.20.10.8
The username you just logged in with is ‘pi’, and the password for your RPi is default ‘raspberry’.
After entering the password, you are logged in and remotely connected to your RPi! Any terminal commands you write while logged in will be executed by the RPi’s terminal (Note the different colors).
[Graphical desktop]
It is useful to have a graphical desktop with our RPi, instead of executing everything with the RPi terminal. We can use a VNC viewer to do this.
Download the VNC viewer for Mac: https://www.realvnc.com/en/connect/download/viewer/macos/
We need to enable VNC on the RPi, which we can do by typing the following command into the RPi terminal:
raspi-config
This takes us to the RPi menu, where we can use the arrow keys to enable VNC in the System/Display options.
Launch VNC viewer and type the RPi IP address into the sign-in bar. You now have access to a graphical desktop!