Raspberry Pi Setup
last updated: 2020-02-11
This covers setting up a Raspberry Pi (RPi) to connect to WiFi and run a basic web server using Python and Flask.
First... Why Would Anyone Do This?
Why would you want to set up a web server on such a little computer... over Wifi no less?
- Bragging Rights - The Raspberry Pi Zero W is a computer that about half the size of a credit card and can be powered by 6 AA batteries or any USB power bank capable of delivering more than 2A (which is plenty of them these days). And since setting up a web server on any hardware is a pretty cool accomplishment, setting one up on a system you can literally put in your pocket is automatically an even cooler accomplishment.
- Temporary Autonomous Web? - While one of the big strengths of the WWW is that it is the (ahem) "world wide" web and you can share information all over the world, there's something to be said for the ability to quickly add a web server to a local area network (LAN) like your home WiFi network (or the one at the local coffee shop or conference). Sure, you could use an old laptop or some other small form factor PC, but...
- Hardware - The RPi is not just a really small computer, it's also designed to be extremely functional in terms of the ability to hook up to the wider world of electronics. With a laptop/PC you're really limiting your I/O game to monitors, keyboards, mice, and things that have pre-built USB interfaces (and drivers!). With RPi those limits are gone. Web server on wheels anyone? Your robot can communicate via web page and receive commands over HTTP! And that's just the tip of the iceberg.
Is your RPi Capable of This?
If you're going to connect your RPi to a LAN via WiFi, it has to be one of the models that has WiFi. 😃
RPi's that have WiFi are: Pi Zero W, Pi 3A+, Pi 3B, Pi 3B+, Pi 4.
If your RPi is not one of the above models, you could still use a USB WiFi dongle. Not sure why would do that though. You can buy a Pi Zero W for $10 from AdaFruit. I think I got a few of these for just $5 at MicroCenter when they were running a promotion. And starter kits with Pi Zero, power supplies, MicroSD cards, and various other goodies are all over the place. You can usually find a good bundle for $25-$35. I don't know which is worse, though: having to get a bunch of dongles and attachments for older hardware, or watching the older hardware collect dust.
If you have one of the Ethernet-only Pi's, and you don't care where the Pi is as long as it participates in your local WiFi, you could probably just plug it in directly to the WiFi router (assuming you have access/permission).
- Get a Raspbian image from the Raspbian image site — just use the desktop + recommended image, your MicroSD card is at least 32GB right?
- Put the image on an SD card using Balena Etcher.
Boot Raspberry Pi
- Put the MicroSD card into the RPi and boot it up. You should get a desktop and a "Welcome..." window. Click "Next".
- Set your country, language, and time zone, then click "Next".
- Change your password.
- Set up your screen. "Next".
- Set up your WiFi. "Next".
- Update the software. "Next".
Configure Raspberry Pi Host Name and enable SSH
- When the Pi has finished rebooting, select "Preferences > Raspberry Pi Configuration" from the Raspberry menu.
- On the system tab, change the hostname.
- On the interface tab, enable SSH.
- Click "OK" to save your configuration changes and then reboot the Pi (again).
- From your PC: Configure password-less SSH access
Install Windows Networking
Wow. After changing the hostname, I'm able to SSH into the machine from a DOS prompt using my configured hostname. If you don't have SSH, it is part of the Git for Windows package. I'm not able to SSH in from my WSL Ubuntu terminal at this point, though, the hostname is unknown (although using IP address works). If step 15 below works for you, you may not really need any additional networking.
- Put the following Python script somewhere useful (like
/home/pi/python/server.py) and run it.
import SimpleHTTPServer import SocketServer PORT = 8000 Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", PORT), Handler) print "serving at port", PORT httpd.serve_forever()
- Go to
http://[hostname]:8000in your favorite web browser. If you're as lucky as I've been this time around, you'll get a directory listing for
/containing a link to your
server.pyfile. If you click the link you should see the actual text of your Python script.