Mostly about my amusement

Tag: Raspberry Pi (page 1 of 1)

Octoprint and Reverse Proxies

I have two active 3D printers, each hooked up to their own Raspberry Pi 3 running Octoprint. I like to manage them from my iPhone when I’m about. I don’t want to expose my IoT devices to the Internet without some precautions.

Here’s how my Internet connected house is setup.

Simple network diagram

The diagram was created and edited in https://www.draw.io/ Free online tools FTW.

My FIOS router on the left listens on port 80 and 443 and forwards that traffic to my Ubuntu Linux Server. On that server I run Apache2 with mod_proxy enabled. 

I run ddclient to update a DNS name with my floating IP address. I use virtual hosts on the Ubuntu Linux Server to receive all external http/https requests. All http requests on port 80 get 301’ed to https on the same host. 

First setup https on the virtual host as you normally do. Before trying to reverse proxy, have a default index.html file and make sure that works. I use Let’s Encrypt for the TLS certificate as it’s free and easy to setup.

Here’s reverse proxy configrution. 10.1.1.17 is the internal IP address of my first Octopi. Make sure the DNS name is working first before trying to test anything.

ProxyPreserveHost On
ProxyPass "/"  "http://10.1.1.17/"
ProxyPassReverse "/"  "http://10.1.1.17/"

ServerName pi1.valid-dns-name.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/vhosts/pi1.valid-dns-name.com

<Directory /var/www/vhosts/pi1.valid-dns-name.com>
        # Options Indexes FollowSymLinks MultiViews
        Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
        Options -Indexes
        AllowOverride All
        Order allow,deny
        allow from all
</Directory>
<Location />
        AuthType Basic
        AuthName "Wrapper Auth"
        AuthBasicProvider file
        AuthUserFile "/var/www/external.htpasswd"
        Require valid-user
</Location>

That <Location /> section on the bottom? That’s important. That’s the section that says “You need a password to access this URL” and protects the Octopi setup from passerby’s on the Internet.

What I tell you three times is true.

  1. Do not expose any IoT devices on the Internet without encryption and passwords.
  2. Do not expose any IoT devices on the Internet without encryption and passwords.
  3. Do not expose any IoT devices on the Internet without encryption and passwords.

It’s just a bad idea. The wonderful Gina Häußge who writes and drives the Octoprint software knows this and has an excellent guest post on her blog about that access. This post is how I accomplished the Reverse Proxy method.

The password is created using the htpasswd command.

$ sudo su - www-data -s /bin/bash -c "htpasswd -c /var/www/external.htpasswd bob"
New password:
Re-type new password:
Adding password for user bob
$

I sudo as the www-data user so that the ownership of that file will be set as I want it. This creates a file with bob and his hashed password in it. The AuthUserFile directive will use that. If you have a valid user ID and password, you get in. If not you don’t get access.

The configuration gets copied for a new Pi. Just change the IP and ServerName and you can re-use this for other Octopi installations. By having an encrypted password protected access to your Octoprint setups, you can monitor and control your 3D printers from anywhere you have Internet access.

Pi Zero W makes for a compact Retropie box

Of course I did this with a Pi Zero W. 😉

One of the reasons I like the Raspberry Pi is that it lets you take hardware and turn it into a dedicated purpose based appliance. Cheaply! And one of those uses is to make a retro game system. I've used the Raspberry Pi Zero before but I was hampered by the fact that it lacks network and Bluetooth access. To get that you need to purchase a USB hub, wifi dongle and if you want it an Bluetooth fob.

The new Pi Zero W addresses that and adds both wifi and Bluetooth on the small package. Once it's setup all you need is an HDMI cable and power. That's it.

Here's how I did it with an 8Bitdo Bluetooth SNES style controller.

  1. Download Retropie for the Zero onto another Pi Zero (non-wireless one, I have a few). I could not get the image to boot on the new device so I setup a hub with a wifi dongle and keyboard on an old version.
  2. After the image reboots to resize the file system then F4 to get out of Emulationstation and on the command line run this command.
 sudo apt-get update ; sudo apt-get -y upgrade ; sudo apt-get -y dist-upgrade ; sudo apt-get -y autoremove 

That will take a while to run and you will need to read part of the upgrade and press Q at a point. That annoys me as those commands should run unattended. Meh.

The purpose of this is to get the underlying Raspbian OS up-to-date.

  • When you’re done run sudo shutdown -h now and swap the old Pi Zero for the Pi Zero W. Unplug the wifi dongle, you don’t need it anymore. Boot up the Pi Zero W with the keyboard attached via the USB adapter.
  • Once it boots up press F4 to get back to the CLI and run these commands.
  •  cd RetroPie-Setup sudo ./retropie_setup.sh 

    And select "U Update all installed packages" with your arrow keys. I don't know if that step is necessary but I do it out of habit. I like making sure I have the latest updates. That will take a long while too and reboot when it's finally done.

  • Update your new 8Bitdo controller to the latest firmware. On my Windows PC I updated my SFC30 to firmware 2.71.
  • Boot your Pi Zero W and press F4 exit Emulationstation and manually run the retropie-setup script again. Follow these instructions to connect your 8Bitdo controller and don’t forget to do the UDEV part. Reboot as needed.
  • Transfer ROMs via the wifi to your new device. I find that NES, SNES and Genesis games work well. MAME is a bit of dicey but older games played satisfactorily.
  • Connect to HDMI TV and have lots of fun.
  • The final parts list came out to this.

    Pi Zero W $10
    8Bitdo SFC30 Bluetooth controller $30
    Pi Zero Case I used one I 3D printed
    Mini-HDMI cable adapter $20
    2A micro USB power adapter $8

    I already had the cables and power supply but I'm trying to be complete. Not counting the case it's ~$70 for this one player setup.

    It's not a Raspberry Pi 3

    The overall experience is good and if you limit your retrogames to the older 8bit systems then you'll like it. But the Pi Zero W doesn't have all the CPUs and RAM of the bigger Raspberry Pi 3. The good Neogeo games won't play well. Sometimes sound suffers due to CPU use. Lag is a thing.

    But if you have the technical know how this beats the Nintendo Classic by a mile.

    So! You like retro game systems?

    One of my recent hobbies (aside from breaking my WordPress site) is anything related to the Raspberry Pi. I use it as a network media player in my living room, USB computer on a stick and as a retrogaming console.

    Nintendo recently released the NES Classic Edition which comes with 30 built in games. This holiday season it will be a big hit and it sold out almost as quickly as it was released. With a Raspberry Pi 3 running Retropie you can accomplish almost the same thing. It just takes a little geeky work and some parts.

    Here’s the parts and links. I usually just drive out to Micro Center in Westbury and get it in person.

    Raspberry Pi 3 $29.99 Micro Center
    Raspberry Pi 3 case $8.99 Micro Center
    USB charger (5 volts, 2.4 amps) $8.99 Micro Center
    Microsoft Xbox 360 wireless controller for Windows 2 x $35.99 Best Buy
    32GB microSD Class 10 $10.99 Micro Center
    6′ HDMI cable $6.99 Amazon
    Total: $137.93

    OK, that added up quickly. The NES Classic Edition is $59.99. Add another $9.99 for a second wired controller and you’re only in for $69.98.

    I begin to see what Lily means. Grownup’ing is a pain.

    The XBox 360 wireless controllers that I linked to also include a USB transceiver. That part number JR9-00011 is cheaper than just a controller alone. I don’t know why.

    Just about any USB game controller that works with your PC will work with Retropie. I picked these because I had a spare and I like being able to sit on the couch while playing.

    Download and burn the Retropie image

    Visit Retropie’s download page and make sure you get the one for Raspberry Pi 2/3. At the time of this post that’s version 4.1

    I’ve never tried burning the image using a Mac. On a PC I just use 7Zip to expand the retropie-4.1-rpi2_rpi3.img.gz and use Win32 Disk Imager to write it to the microSD card.

    There are some really good detailed instructions for installing and configuring Retropie here.

    https://github.com/retropie/retropie-setup/wiki/First-Installation

    When it boots you will be asked to configure your controller so you’ll need to get your XBox 360 controllers working.

    think the Xpad driver ships and is activated by default. I don’t recall activating it myself. If that’s not the case then I’ll update this post.

    1. Put batteries into your XBox 360 controller, and plug the XBox 360 Wireless USB transceiver into any free port on your Pi.
    2. Press the stylized XBox button on your controller to turn it on and then press the button on the transceiver.
    3. The front of your controller has a not quite recessed wireless sync button. Press that and your controller will connect to your USB transceiver.
    4. Press a button on your controller to begin configuring it.

    From the Retropie Git docs, you’ll configure the buttons like so.

    The image is from that Retropie installation page. I didn’t want to hotlink the image as that’s rude. If it’s a problem I’ll remove the image.

    You will get some of the button assignments wrong. Don’t worry, as long as you get the D-PAD, START button and A button assigned then you can redo it later on.

    That USB transceiver supports up to 4 controllers. When you use PS2 emulation for games it not only works but so does the rumble part. That’s cool.

    Network your Raspberry Pi 3

    I happen to have an Etherenet connection onto my Raspberry Pi 3.  It also comes with a built in 802.11n wifi adapter. You want your Retropie on your network and being able to ssh to it is very useful.

    https://github.com/retropie/retropie-setup/wiki/Wifi

    Borrow a USB keyboard from your PC or Mac so you can enter your wifi key. You won’t need it afterwards. I do all my Linux admin via ssh from my PC or Mac.

    Your Raspberry Pi 3 running Retropie is a Linux server. It’s running a Debian based distribution called Raspbian. If you’ve spent time administering an Ubuntu LTS VPS then this will feel very similar if not downright identical.

    The reason for getting your Retropie on the network is simple: once you do you will find a new Windows share at \\RETROPIE and you can deposit the NES ROMs you obtain in \\RETROPIE\roms\nes as easy as drag-n-drop.  You’ll have to do some research where to get them yourself. They’re not hard to find.

    When you do obtain NES ROMs make sure you keep them in individual ZIP archive files. Don’t extract them, just from them as is into your nes directory. Once you’ve gotten your roms onto your new system, press the “start” button on your controller and restart emulationstation.

    So many emulators to use

    Retropie supports many retro arcade systems. My favorite are MAME, SNES, NEO GEO and of course NES. I don’t play a lot of Atari 2600 games though I should. That’s one of the systems I had as a kid.

    The emulators are easy to use. Generally you just drag the ROM zip file into it’s directory. Use \\RETROPIE\roms\nes and \\RETROPIE\roms\snes for the  right one. You’ll see many more directories there but for now ignore them. You can explorer them later.

    This is not a game system for everyone

    If you are just looking for the classic NES games and can get your hands on one even with the small controller cables, then do so.

    This illustrative YouTube video can explain the mindset of people who do this sort of thing.

    You’ll either immediately understand where I’m coming from or you wont. That’s OK, some people just enjoy the nerdy aspects of things.

    Using a Raspberry Pi 3 with Retropie is purely a geeky exercise. It works, it works well. It’s easy to maintain provided you are willing to learn the Zen of the Debian Based Linux Server™

    Part of the appeal of the Raspberry Pi 3 is that it is a server with a quad core ARM CPU running at 1GHz. It has 1 GB of RAM built in. With a 32GB microSD card, case, and A/C adapter it’s a full on Linux server for less than $70.

    Setting up a small PC with similiar stats will run you at least $200. The small size of the Raspberry Pi 3 shouldn’t take away from the fact that it is a Linux server. It has a default user ID and password. You should change that if you’re concerned.

    Here’s an example of what I mean. Yesterday I did the following.

    1. ssh’ed to the retropie as the pi user.
    2. Ran sudo apt-get update ; sudo apt-get -y upgrade ; sudo apt-get -y dist-upgrade
    3. Ran cd Retropie-Setup then sudo ./retropie_setup.sh
    4. Selected “Update all installed packages” and skipped the OS ones because I already did those.
    5. Had coffee. See illustrative video above.
    6. Ran sudo reboot to reboot box.

    If you read that, stopped at step 2 and said “Are you kidding me?” then it’s alright. You’re OK. The NES Classic Edition is for you, it’s $60 and it is fire and forget. It does not have any network capabilities, it will never be updated. And there’s no legal question about using one either.

    If you want to roll your own and don’t mind getting up to your neck in Geeky Nerdy things then maybe the Raspberry Pi 3 is for you.