Create a portable battery and solar powered Raspberry Pi Zero web server

I asked the community on Reddit what tutorials people wanted me to write, and one, in particular, caught my imagination. ‘Thaslegendary’ said:

“A guide on how to power raspberry pi zero with 4 AA battery would help me a lot”

This really got me thinking. Can I build a portable web server that runs off battery alone, topped up with a solar panel? There’s only one way to find out. At the time of writing this, the Raspberry Pi Zero is running on a solar power bank and is therefore completely off the grid.

Step 1 – Choose your power supply and hardware

The Raspberry Pi choice had to be a Zero W. According to Jeff Greerling the Raspberry Pi Zero W uses around 0.4w, a super low consumption. The power bank choice was essential too, it had to fulfil the following criteria:

  • Has to have at least 10,000Mah battery
  • Has to have a solar panel for topping up the charge
  • Has to have a USB port capped at 1a to promote longer battery life

I bought an Aisla 20,000Mah power bank off Amazon for £16. Nice capacity, complete with a solar panel for a modest price.

Step 2 – Install Nginx

I’m going for Nginx as the web server for this project. Nginx is lightweight and uses minimal resources. Let’s first ensure that our installation of Raspbian is up-to-date (I’m assuming that your Raspberry Pi Zero is already working and you’re using Raspbian OS and you’ve connected to your Wifi network):

sudo apt-get update -y && sudo apt-get upgrade -y

Now for Nginx installation:

sudo apt-get install nginx -y

After a short while, Nginx should be installed. Go ahead and visit the IP address of your Raspberry Pi Zero. You should see something a little like this:

So that’s pretty much it for Nginx, add whatever site you want to or even install PHP. Next, we’ll take a look at some monitoring.

Step 3 – Add some monitoring by installing Rpi-Monitor

We will want to know the health status of our Raspberry Pi Zero battery-powered web server. This can be done using a package called Rpi-Monitor. I’m going to talk you through the installation and using Nginx as a proxy to access the monitoring page without an ugly port number.

Rpi-Monitor uses a private Debian repository, so we need to add this first. Install a couple of packages to get started:

sudo apt-get install apt-transport-https ca-certificates

Now we need to add more sources:

sudo wget http://goo.gl/vewCLL -O /etc/apt/sources.list.d/rpimonitor.list

Now let’s install a certificate so we can download from this repository:

sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 2C0D3C0F

At this point, we can install the package. We need to update our lists first so the following command will do both update sources and install the package:

sudo apt-get update && sudo apt-get install rpimonitor

Finally, we need to ensure our updatable packages are updated:

sudo /usr/share/rpimonitor/scripts/updatePackagesStatus.pl

Try going to your Raspberry Pi Zero’s IP address with the port 8888 and you should see the status page.

Step 4 – Configure the proxy in Nginx

We’re going to proxy our requests to Rpi-Monitor via nginx. This allows us to have our regular web traffic to Nginx and to Rpi-Monitor without an ugly port number on the URL. It also allows us to later add an SSL certificate to our monitoring page (see Adding SSL for free with Let’s Encrypt).

Edit the default site configuration by typing the following command:

sudo nano /etc/nginx/sites-available/default

In the configuration, we will see an entry for ‘location /’. Underneath that location block (so after the closing curly brace) add this:

location /monitor/ {
proxy_pass http://127.0.0.1:8888;
proxy_read_timeout 120s;
access_log off;
}

This will allow requests to example.com/monitor/ to show Rpi-Monitor. Your configuration will now look a little like this:

Exit and save by hitting CTRL + X. Finally, test that your configuration isn’t broken by typing the following command:

sudo service nginx configtest

You should have an OK message. Go ahead and restart Nginx:

sudo service nginx restart

In your browser, visit your Raspberry Pi Zero’s IP address and append /monitor/ to the URL. You should see something like this:

Conclusion

We’ve got our solar-powered, completely off-grid Raspberry Pi Zero, installed Nginx and Rpi-Monitor. It’s a truly portable device that can be used for many many different applications. How long the device can run off battery remains to be established. Below is an iframe of the actual monitoring page on my battery operated Raspberry Pi Zero. As always, any questions or feedback, feel free to add a comment and click a banner if you found this tutorial to be beneficial.

View the Rpi-Monitor on my Solar Powered Raspberry Pi Server.

Following the publishing of this article, I’ve had two comments and an email sent to me criticising me for asking people to click a banner if they found this to be beneficial. The banner adverts contribute towards the cost of the equipment I buy to write up tutorials like this but don’t come close to covering the full costs.

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

17 Responses

  1. Duncan says:

    I’m getting ready for a trip for charity so I’ve been looking into off-grid power for tablets etc. There’s Adafruit’s ‘mintyboost’ which uses batteries and boosts the voltage up to 5V (rather than rectify down to 5V). Not sure how you’d rig solar into this though. There’s also the water lily turbine (http://www.waterlilyturbine.com/) that’s just gone into production that can use wind or water power to produce power for USB devices and they also have plans to produce a 12V version.

  2. I am very interested on the usage of your set-up, how long does it last on the battery with solar when you put the PiZero under load? I have been running my weather station on solar for over a year and find it needs a bigger set-up to perform 24/7. or is this set-up just for as and when needed?
    still very good, but would like to know more about the power usage, lenght of time on load.

  3. eric says:

    you need to retitle that to ngix on pi 0 , or add documentation on everything between the solar array and the battery

  4. WOW! it works! but change the restart to “sudo service nginx restart”

  5. Nick Hartman says:

    This is very interesting! Being a complete noob to the Raspberry Pi world do you think the monitor would be able to be configured to check the status of the battery? % full, send notification if it got below certain amount?

  6. Nelson says:

    502 Bad Gateway lol

    • Ste says:

      Haha yep, it appears the solar panel charges after the battery has gone flat! I’m trying to source a better one at the moment. I’ll write something up about the uptime etc on a battery though

      • Joe C says:

        I came here to find out how you’d managed to sort out the pass-thru charging which most battery packs don’t support. Oh well 🙂
        Let us all know if you find one.

        • Ste says:

          Yeah, I’m trying to find a solution to this, I think using an Adafruit Powerboost 500 instead, that way if the battery fails, it uses a backup while the main power source is charging

  7. Glen says:

    Could this also power a camera attached to the Pi zero or too much current pull?

    • Ste says:

      I think a substantially better solar panel would be needed. The one I’ve used has been disappointing, as has the battery life. Typically getting 2.5 days from a 20,000mah battery, but I’ve got a sneaking suspicion that the battery capacity isn’t what it’s supposed to be.

      • Jacob says:

        That solar panel/battery pack looks like one I saw on Amazon. They are rated at 1 star by many users for the exact reasons you mentioned. I think they’re a scam.

  8. Andreas says:

    Nice Project. But the Solarpanel ist way to smal. I would guess for a stable 24/7/365 service a 20 Watt Panel would bee something to start. Depending where you live, of course.

  9. Javi says:

    Hi!.Great job!!
    it’s possible to monitoring information about battery? Hours of autonomie, general status without any external hardware?

  10. Hi Steve. What site do you have on this server?

  11. David says:

    Nice, looking to do something similiar. But this feels like more of a ‘how to’ on installing a web server on a Pi rather than a solar power project?

    Surely the most important information you’d want to monitor from this set up is the current state of the battery and how much power is being generated from the solar panel. Why not add a simple voltage test on the ADC pin and display that on your web page too?

    How has this performed?? Does it just run until the battery is flat. I doubt that small solar panel keep this thing running indefinitely, or does it?

Leave a Reply