Painless Wi-Fi for OctoPrint
We all love Raspberry Pi. But everywhere I go, I hear painful stories about setting up Wi-Fi on Raspberry Pi.
We all love OctoPrint. But everywhere I go, I hear painful stories about setting up Wi-Fi for OctoPrint.
If you are nodding your head, this post is for you. I’ll describe a few techniques that will make dealing with Wi-Fi in your OctoPrint as painless as it can get.
Set up Wi-Fi without monitor, keyboard, or mouse
When you set up OctoPrint on Raspberry Pi for the first time, or you move your Pi to a new place with different Wi-Fi network, what are the first things you scramble for? The answer: HDMI monitor, USB keyboard and mouse to set up Wi-Fi connection.
Not anymore. There are actually a mechanism (shameless brag: I did it) in latest OctoPi that allows you to add new Wi-Fi connection/password to your SD card without even booting into your Pi!
And here is how you do it.
- Download latest OctoPi image here. This link is a pre-release build of the latest version. Install it as if you were installing an original Raspbian image.
- In the
bootpartition of the newly-made SD card, you will find a text file called
octopi-wpa-supplicant.txt. Open it with your favorite text editor. Uncomment a
networkblock so that it reads:
ssid="Your Wifi SSID"
Of course, replace the Wi-Fi SSID and password with the real ones.
- As explained in the comments in
octopi-wpa-supplicant.txt, you can have as many
networkblocks as you want. Your Pi will try to connect to each of them until it has a successful connection or exhaust the list.
- If you don’t enjoy going down the rabbit hole to figure out why things don’t work as expected, double-check the file to make sure the quotes are
", not those “smart quotes”. This is true for all the text editing mentioned in this post.
- Do NOT use
Change Wi-fi Countryin
raspi-config. A subtle bug in
octopi-wpa-supplicant.txtin-effective. Instead, just open
octopi-wpa-supplicant.txtin an editor and directly change
Turns your Pi into a Wi-Fi hotspot, automatically
Admit it. There are just times when you can’t get hold of any Wi-Fi connection (yeah it’s 2017 but…), or you are too lazy to even edit that little
octopi-wpa-supplicant.txtfile. Fine. With a little bit of work upfront, you can tell your Pi to start a Wi-Fi hotspot when it can connect to none Wi-Fi networks. Now you can connect to OctoPrint anywhere and get your 3D printer to work — even there is no Wi-Fi whatsoever.
- Connect to your Pi on which OctoPrint runs. You can ssh, login through serial console or GUI, what have you.
- Install software needed to run Wi-Fi hotspot:
$ sudo apt-get update
$ sudo apt-get install hostapd dnsmasq iw
$ sudo systemctl disable hostapd dnsmasq
$ sudo nano /etc/hostapd/hostapd.conf
and copy-paste the following:
control-x to save the file.
$ sudo nano /etc/default/hostapd
control-x to save the file.
$ sudo nano /etc/dnsmasq.conf
to add to the bottom of the file:
#stop DNSmasq from using resolv.conf
#Interface to use
$ sudo nano /etc/network/interfaces
# in front of
wlan0so it looks like:
allow-hotplug wlan0iface wlan0-raspbian inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
- Finally, add a script to start Wi-Fi hotspot when failing to connect to any existing Wi-Fi networks.
$ sudo nano /usr/bin/autohotspot
ip link set dev wlan0 down
ip a add 10.0.0.5/24 dev wlan0
ip link set dev wlan0 up
systemctl start dnsmasq
systemctl start hostapd
while read ssid
if iw dev wlan0 scan ap-force | grep $ssid > /dev/null
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
if dhclient -1 wlan0 && iwconfig wlan0 | grep $ssid > /dev/null
echo "Not in range, WiFi with SSID:" $ssid
done <<< "$(grep -i '^[^#].*ssid=' /etc/wpa_supplicant/wpa_supplicant.conf | sed -n 's/.*ssid="*\([^"]*\)"*/\1/ip')"
if ! $connected; then
Important: Please make sure you copy-paste the content above, rather than typing it. Some long commands that have to be in the same line are displayed as if they were in multiple lines.
Create a new service file
$ sudo nano /etc/systemd/system/autohotspot.service
Description=Generates a non-internet Hotspot for ssh when no wifi network is in range.
Activate the service so that it’ll run the script when system boots up:
$ sudo chmod +x /usr/bin/autohotspot
$ sudo systemctl enable autohotspot.service
Whew, that’s it! Next time when you boot up OctoPrint in a new place with no known Wi-Fi networks, open up your computer and look for Wi-Fi with the name
OctoHotspot, which is the new Wi-Fi network your little Pi creates! Connect to it using password
1234567890. Once that is done, your old friend OctoPrint will be available at
http://10.0.0.5/. Fire up your browser and print away!