Sunday, December 8, 2019

Cabling your Raspberry Pi-hole and minimizing power usage

In a previous blog post I showed you how to create a network-level ad blocker using a Raspberry Pi and Pi-Hole. Here is a link to that blog post

This blog post assumes you have already built your Pi-Hole device and want to switch it from WiFi to a cabled connection and reduce its power consumption to the lowest level possible.  If you don't already have a Pi-hole running then I suggest you go to the link above and follow my instructions to build one, otherwise this post won't be any use to you.

By now you've hopefully been using your ad blocker for a while and enjoyed the ad-free internet experience.  Here's my Pi-Hole dashboard.

As you can see, since I setup the Pi-hole a week ago, it has received 36,847 DNS requests from 4 devices on my network (two laptops and two phones).  Out of those, 9,651 requests were blocked because they were requests for known ad-serving domain names that are on the Pi-hole block list.  That's 26.2% of my total DNS requests that were blocked.  The number of domain names on the Pi-hole blocklist is 116,818.

If you see these numbers updating then your Pi-hole is working just fine to block ads on your network.  Now it's time to switch from WiFi to a cabled connection.  Why would you want to do that?  There are two reasons: 
  1. A cable connection is faster than a WiFi connection
  2. Having another wireless device on your network creates more interference and can slows your WiFi connection. 
Before we start with the switch to cable, open a command line window, and ping your Pi-hole device. In Windows you open the command line by typing "cmd" in the search box and pressing Enter.  On the command line type "ping pi.hole -4 -n 20". This will ping your Pi-hole 20 times using the IPv4 address.  You can leave out the "-4" if you want, the result will be the same but it will use the IPv6 address of your Pi.

In this example I pinged my Pi-hole 20 times and the average time to get a reply from the Pi-hole was 7 milliseconds.  We'll take a look at this again later once the device is cabled to see how much faster it is.

Hardware required

To cable up your Raspberry Pi you're going to need a micro-USB-to-ethernet adapter and a short ethernet cable.  Here are the ones I bought on Amazon.

Plugable micro-USB-to-ethernet adapter.  Other brands will also work. Be sure to get the micro-USB type so it can plug into your Raspberrry Pi.  This one only does 100Mb/sec internet and not Gigabit ethernet, but that's fine because the amount of data going over this connection is quite small and it works fast enough for this purpose. You can get a faster one if you want. There are also adapters like this that have a few USB ports if you want to add other peripherals to your Pi, for example if you have other services and projects running on the same Pi device. However, since my Pi is dedicated to running only Pi-hole I don't need that.

Ethernet cable, 6 inches long, Cat5E, with RJ45 connectors on each end. For some reason the blue one is the cheapest. This cable can support up to Gigabit speeds but is not certified for Gigabit speeds, whatever that means. Since my adapter can only do 100Mb/sec I have no need to use Cat6 or higher rated cable. Remember this is only going to be carrying DNS requests, so it's not much data and doesn't need to be super high speed.

Since my Google WiFi router is powered by a USB-C connector, I decided that I would power both my router and the Raspberry Pi from the same power supply. This saves me having two power supplies and two cables, and leaves one more power socket free on my power strip/surge suppressor. My Google Wifi router uses 9 watts of power and the Pi uses no more than 1.25 watts, which is 10.25 watts total.  The power supply from the kit can supply 12.5 watts, so it will be enough to power both devices.
To split the power cable I bought a 1ft USB cable with a USB-micro connector on one end and a USB-C connector on the other end.  I will cut this cable in half and solder the power wires to the power supply.

Once you have these new items, simply plug the ethernet adapter into the USB port of your Pi, plug the ethernet cable into the adapter, and plug the other end of the ethernet cable into one of the ports on your router.  My Google WiFi router only has one available port.

If your ethernet adapter has lights on it then these should now be lit and the activity light may be blinking. In my photo above you can also see that I've already made my USB Y-cable to power the router and Pi from the same power supply, and I'll go through making this cable at the end of this post.

Configuring the new cabled network interface

Now open the VNC connection to your Pi using VNC Viewer, just as I showed you in the previous blog post. Now open a Terminal window.

In the terminal window, type "ifconfig" and press Enter.  Now if you scroll up in the terminal window you will see that you now have a new network interface called eth0.  This is your new cabled connection to the router. You'll also see that it has a different IP address than the wlan0 interface. This is also shown in the box that appears if you hover the mouse pointer over the network icon at the top right of the screen.

The eth0 network interface has already been allocated an IP address by my router's DHCP service.  The new network interface will default to DHCP to get it's IP address from your router and that's fine.  However, remember that for Pi-hole to work, it needs to always have the same IP address.  So go into your router and reserve this address so the Pi always gets the same IP address from your router.  This will be the same or similar process you used before to reserve an IP address for your Pi's WiFi connection.  How to do this will vary by router.  Remember that your Pi now has two network interfaces connected to the router, one is the WiFi connection and the other is the cable connection. 

Here is how to reserve an IP address in a Google Wifi router. If the device uses DHCP then it's MAC address will always get the same IP address from DHCP.  If the device is set to static it will also work because the router will not allocate the Pi's address to another device. So it works fine both ways. Open the Google Wifi app.....

Click on Devices                                                              Click on raspberrypi Chrome OS Device
                                                                                         That's the cable connection, the other is the WiFi

   Click the three dots at top right and
   select Reserve IP                                                            Click NEXT to reserve

Now that you have your Pi-hole connected to your router by a cable, it's time to switch the Pi-hole ad blocker to start using that interface.

  1. Open the Pi-hole admin web page in your browser.  This was the browser bookmark you created earlier.  You can also type into the browser URL box "pi.hole/admin" or the IP address of your Pi, e.g.
  2. On the left side of the page click Login and login to the admin page using the password you changed earlier. I would assume you changed the web admin password to the same one you changed for the pi login, that would make sense.
  3. In the Pi-hole admin page, go to Settings on the left, then click the DNS tab.  On the right you will see options for Interface listening behavior select "Listen on all interfaces" and then be sure to scroll to the bottom of the page and press Save.

OK. Let's take a break for a moment and have a look at some things.

At the top left of the admin web page you will see the Status box.

It shows some basic info, for example that Pi-hole is active and the CPU temperature.  The next line below shows the system load averages for 1, 5, and 15 minutes. The way these values are calculated is actually quite complex, they are actually exponentially-damped moving sum averages that use 1, 5, and 15 minutes as constraints in the equation that calculates them. These are not just CPU load values, but also take into consideration other system resources such as disk access, number of CPU cores, and processes that are not actively running but are idle and waiting to run.  Because of this the numbers can have odd values.  The Pi Zero W only has one CPU core so it's load values are somewhere between 0 and 2. Values of zero means the system is idle. Values around 1 mean the system is busy.  Values closer to 2 mean your Pi is overloaded and you will probably notice it responding slowly to your inputs.  The actual values of the numbers are not all that useful, the important thing is that they allow you to compare relative values of system load under different conditions when the computer is doing different things, and therefore determine what is impacting system load.  For simplicity's sake you can think of these values as the average system load for 1, 5, and 15 minutes. In this way the three values allow you to see how the system load is changing over time. You can read more about system load values here:

The next line in the status box is memory usage.  I expect you to have a memory usage of about 30% right now if you're using a Pi Zero W and only running Pi-hole, the GUI, terminal window, and VNC server. The GUI you are using actually takes up about 10% of the Pi Zero W's memory.  It also uses some CPU cycles as well, even when it's not being used.  Another thing that takes some memory and CPU is the VNC connection.  If we get rid of the GUI and VNC, not only will you save some memory but also some electricity because the CPU won't need to manage them.

Rather than the system load values on the Pi-hole web page, a better way to look at CPU load is from the command line using "top".  Go back to VNC and open a terminal window in your Pi and type "top".

The important columns on this screen are %CPU, %MEM and COMMAND.  I've circled VNCserver in this example. As you can see, VNCserver and VNCagent together were using 14% of CPU time when I took this screenshot, and this was at idle. If you start moving the terminal window around inside VNC you'll see this value can shoot up as high as 75%.  VNC uses a lot of CPU time.  However it only uses about 5% of memory, so is not a memory hog. Memory is not a power concern because memory uses the same amount of power regardless of how full or empty it is. However memory might be a concern if you are running multiple services on a Pi and each one takes some memory. The Pi-hole ad blocker uses very little memory and CPU time.

Let's look at a couple of other items in the list.  The next one down is lxterminal.  This is the terminal window you are using to view "top", and as you can see it is using 10.1% of CPU time and 11.1% of memory in this screenshot.  After that is Xorg, which is the GUI you are using, with all it's fancy windows and easy to use menus.  When I took this screen shot it was using 8.8% of CPU time and 9.1% of memory.  You will see these values update every few seconds.  Try moving the terminal window around in the GUI and you'll see how VNCserver and Xorg cause large increases in the CPU load.

When you're done press Ctrl-C to exit out of "top".

So as you can see there are some services running on your Pi which use a lot of CPU time and they are mostly things to do with the graphical user interface (GUI) you're using.  Since your Pi is only going to be running Pi-hole without you logged into it most of the time, it makes sense that to reduce the electricity used by the CPU, you can turn the GUI and VNC services off. Actually, since you're no longer using the HDMI to connect to a display, you can turn that off as well. And since you've cabled your Pi to your router you can also turn off the WiFi too.  How about Bluetooth and audio?  We've never used those in this setup, so they can also be turned off to save power, memory, and CPU.

How will you connect to your Pi if you turn off VNC?  You will use a service called SSH.  SSH stands for Secure SHell.  SSH provides a secure command line connection to your Pi over the network, just like the terminal window. The service that listens for and makes SSH connections to your Pi uses only a tiny amount of CPU and other resources, so it is much more efficient than the GUI and VNC. If you remember from the first blog post, I had you turn on SSH when you turned on VNC, so SSH should already be running.

However, before we turn off VNC, let's use the GUI to turn off the other things first.  Let's turn off the HDMI display, WiFi, Bluetooth, audio, and setup Pi-hole to listen only on the wired connection to your router.

Your Pi Zero W uses no more than 250mA, which is very little to begin with, but every little bit counts, especially since this device will be running 24/7/365.  Turning off the HDMI chip probably saves about 30mA.  I don't know how much are saved by turning off WiFi, Bluetooth, and audio, but we know we don't need them so they can be turned off.

Turning off these parts is done by editing the boot config file:   /boot/config.txt
This can be done from the terminal window by typing "sudo mousepad". This will open the text editor called Mousepad and give you root permissions so you can edit the system file /boot/config.txt.  Don't worry if some warnings appear in the terminal window when you run Mousepad.
If you're familiar with command line text editors like vi or nano, you can use those as well, but I'll give the Mousepad example here because it's a lot easier to work with.

In Mousepad, use the File menu to open a file. Click the boot folder at left and then scroll down to config.txt and open it.

Near the top of the file you're going to add a few new lines. These are the lines you'll add to the file to turn off the HDMI, WiFI, and Bluetooth.

     # These lines added by Dave to turn off WiFi, Bluetooth, and the HDMI display

If you bought the black case for the Pi Zero W then you can also turn off the power & activity light on the board. I prefer to leave it on because I have the clear case and it's useful to know if the Pi is actually turned on. However if you turn it off you will save another 30mA of power. If you want to turn off the LED then add these two lines to the three lines above.


You can find out more about turning off components of your Pi here.

Here is my /boot/config.txt file with the new lines I've added.

Now scroll to the bottom of the file and find the line for the audio, dtparam=audio=on.
Change the setting from on to off.

Now you can save the file and exit Mousepad.

Important step: Remember not long ago that we set Pi-hole to listen on both the cabled and WiFi interfaces. Now we can make the switch to the cabled connection in your router. Go into your router's settings and change the DNS addresses to the ones for the wired interface.   You can find these by typing "ifconfig" into the terminal window and looking at the eth0 interface.  In the example below the IPv4 address is and the IPv6 address is the long one that has eight blocks of four numbers.  If you don't do this, then when you turn off your Pi's WiFi interface your connection to the internet will go down. This is because your router will not be able to resolve DNS requests because your Pi is your DNS and you turned off the WiFi link to it.  If you make a mistake and need to recover your internet connection, just set your router's DNS address back to default, e.g. Google's DNS server address,

OK, now you have edited your boot config file and set your router to use the new DNS addresses of your Pi's cable connection. The next step is to reboot your Pi.  To reboot simply type "reboot" in the terminal window and press enter.  Your VNC connection will be lost.  You can close the VNC window.

Now go back to VNC and open a new connection using the new IP address you got for the eth0 interface. You will now be connecting via the wired interface instead of the Wifi, because the Wifi has been turned off.  In the above example the address is Simply subsitute your address.

When you reconnect VNC it will give you a warning about a Duplicate VNC Server Identity.
Simply click Continue to accept and store the new identity.

Login again as user pi with the password you changed earlier. Check the box "Remember password".

When the GUI opens in VNC you will see in the top right corner that the audio icon is grayed out and has a red cross on it. You will also see the WiFi and Bluetooth icons are no longer there and have been replaced with a pair of arrows that represent the wired connection.

OK, let's go back to the Pi-hole web interface. Use yor browser to go to the new address of your Pi-hole.  You should edit the bookmark you created earlier with the new address.

Login to the admin interface, go to Settings, and go to the DNS tab. In the interface listening behavior you will see that you are still listening on all interfaces. The second option is to listen on wlan0, but we just turned this WiFi interface off.  

This is not really a problem, but since I'm being obsessive about this, I feel I need to fix it. You can leave it if you want.  The only way I've found so far to fix this is to repair the Pi-hole install.  To do this, simply open a terminal window and type "pihole -r". When asked if you want to Repair or Reconfigure Pi-hole, select Reconfigure and then press TAB and Enter to select Ok.  I'm sure there must be a config file somewhere you can edit to make this change without reconfiguring the entire Pi-hole install, but I haven't found it yet.

  1. Press return three times to get past the first few screens.
  2. Use the down arrow key to select Quad9 filtered DNSSEC as your upstream DNS, then press TAB and Enter for OK.
  3. For protocols press TAB and Enter for OK.
  4. For the static IP selection press TAB and Enter for Yes.
  5. Press Enter at the IP Conflict screen for OK.
  6. Press Enter at the IPv6 screen for OK.
  7. For the web admin interface press TAB and Enter for OK.
  8. For the web server screen press TAB and Enter for OK.
  9. Log queries, I prefer not to log my web activity, so press the down arrow to move to Off, press the space bar to select it, then press TAB and Enter to OK. You can read more about Query logging here:
  10. For Privacy levels, select your privacy level according to this web page. If you choose higher levels of privacy you will not be able to view statistics like Top Permitted and Blocked Domains, and Top Clients. I actually like these statistics so I'm leaving them enabled for now. Just be aware that they require another set of logs to be recorded. :

The install will now continue. When it completes it should show the same IPv4 and IPv6 addresses you used for DNS in your router already, and the web admin page password will be unchanged.  

Now when you go to the Pi-hole admin web page, Login, go to Settings, and click the DNS Tab.  Under Interface listening behavior you can now select Listen only on interface eth0. Be sure to scroll to the bottom of the page and click Save.  

That was a lot of crap just to change that, and it wasn't all that necessary, but now it's fixed. One day I'll try to find out where that Listening interface info is stored and just change it there. There is a file at /etc/dnsmasq.d/01-pihole.conf that changes its last line when you change the listening interface in the web page, but changing the file manually and restarting Pi-hole does not change the listening interface is the web page.

Now we have your Pi-role running on a cable to your router, and the WiFi, Bluetooth, audio, and HDMI have been turned off.  

The next step is to turn off VNC.  Once you do that you will be connecting to your Pi by SSH.  You'll remember in the last blog post that I had you turn on SSH when you turned on VNC, so it should already be running on your Pi.  Before we turn off VNC however, we must test to make sure SSH works, because if it doesn't and you turn off VNC, your Pi will become uncontactable on your network and you'll probably have to start the entire OS install process from the beginning again. (Actually, I think there is a serial service on the USB port that you could use for a terminal connection).

Technical Note: For SSH to work it needs to use port 22, and your router and firewall will need to allow traffic on port 22.  If that sounds like gobbledygook to you then don't worry about it.  In most cases your firewall and router will allow traffic on port 22 automatically because it's the standard port for SSH. I'm only mentioning it here in case you have problems with SSH, which I certainly hope you don't. If so then you may need to open up port 22 on your firewall or router and make sure SSH is working before you disable VNC.

First of all you will need a program to connect to your Pi with SSH.  There are many SSH clients out there but the one I use is Tera Term.  Download and install Tera Term from this page:

In Tera Term, put in the IP address of your Pi and press OK.

You will get a message saying that your Pi is not in the list of known hosts.  Simply press Continue to add it to the list of known hosts.
You will now be logging into as "pi" using the password you set before. 
Once you've entered the username and password, click OK.

You will now be logged into your Pi with SSH.  If Tera Term doesn't connect then the connection may have timed out while you were reading these instruction.  Simply close Tera Term and try again.

This window behaves in exactly the same way as the terminal windows we've used before in the GUI.  For example, type "top" and press Enter to see CPU load.

Now that we have tested that SSH works, we can turn off VNC.  Go back to the VNC connection to your Pi.  Click on the Raspberry icon at top left, select Preferences, and select Raspberry Pi Configuration.  In the configuration select the Interfaces tab and Disable VNC and press OK.  If your VNC screen is too small go into Pi settings and change the appearance settings to those for a Small screen.

Your VNC connection will now be lost.

Go back to the Tera Term and type "top" into the terminal. You'll notice in "top" that VNC, Xorg, and lxterminal are no longer present because we turned those off when we turned off VNC. As you can see, "top" is using the most resources now, with pi-hole in second place. Overall your processor is idle 98.4% of the time.  Nice!
Use Ctrl-C to exit top.

Remember at the beginning of this blog post I had you ping your Pi to see how fast the connection was?  The average reply time from the Pi was 7 milliseconds.  Let's have a look what it is now.  Go to a command line window on your PC and ping the Pi using "ping pi.hole -4 -n 20".

As you can see the average ping reply time is now 2 milliseconds, or less than one third of the time it took through the WiFi connection.  Now you can see why cabling the connection is a good idea, it's always faster than a wireless connection.  Using a cable also reduces the amount of radio interference on your WiFi network, especially since this device is going to be sitting very close to your WiFi router.  

To be honest, this reduction in time isn't actually a big thing.  You saw at the beginning of this blog post that my Pi-hole had received 36,847 DNS requests in the 24 hours since I'd got it running.  If we multiply that by the time difference between a WiFi and a cable connection (5ms) it only amounts to 3 minutes over that whole day.  It's not a whole lot of time wasted while I'm waiting for a web page to load.  Also the WiFi modems on the Pi and router should turn down the radio power to communicate with each other, reducing unwanted interference.  So this is really all just an exercise of diminishing returns and more about learning some Linux.

OK, now go back to the Tera Term SSH window.

We are nearly done.  Now we can get rid of the last few things.  In the terminal window. Type "sudo raspi-config" and press Enter. This will open the Raspberry Pi configuration where we can make changes to the system.

One of the first things we can do is change the hostname of your Raspberri Pi. Currently it's called raspberrypi.  You can change it to whatever you want as long as you follow the rules that are shown: use only lower case characters, numbers, and hyphens.  

First of all select option 2 Network Options using the arrows and press Enter.

Select N1 Hostname and press Enter. Enter your new hostname.

Press Tab and Enter to select OK.

The Desktop GUI is still running on your Pi so it's time to turn it off.  We're also going to turn off the splash screen that appears at bootup. So, on the main screen again, select Boot Options.  Select option B1 and then select option B1 again so that your Pi boots to a terminal console where you log in, just the same as the terminal you're using now.  After this change you will be logging into that terminal console when you log in with SSH using Tera Term.

When set, you will be taken back to the main screen.  Select Boot Options again and option B3 Splash screen. Use the arrow keys to select No and press enter to turn off the bootup Splash screen.

If you need to turn on VNC again, go to Interfacing options and select VNC and turn it on again.  This is good to know if you ever need to go back to the GUI again.  However you will also need to turn on the Desktop GUI in the Boot Options, otherwise when you VNC to the Pi you will get the command line terminal that your Pi now boots into.

Select Finish on the main menu and when asked to reboot, select Ok.

Your Pi will now reboot with the new setup.

That's It!!!  You have now configured your Pi-hole device for minimal power usage.  

You will always be connecting to your Pi with SSH from now on.  However there's really no need to log into the Pi because it will run fine on its own and update itself without any interaction from you.

Note that the Pi-hole admin web page still works exactly the same as before, so you can see statistics and manage your Pi-hole.

That's all folks.  Enjoy your low power ad-free internet experience.  If you have any difficulties just leave a comment below.  Also, don't be a bragger about your Pi-hole. If everyone had one of these things the internet wouldn't be free and you'd have to pay for most web sites.  Be happy there are other people out there who endure ads so you don't have to. Welcome to our little secret, you non-conformist!! 😄

The End

Look out for a future a future blog post where I'll show you how to install Pi-hole on Android so you 
can have it on your mobile devices as well.

Thanks for reading.  Subscribe to my blog post at the top right of this page for regular updates.

If you're looking for the first blog post in this two part series, here it is:

-Dave Bad Person

Epilogue: USB power

In this final section I'll quickly show you how to cable up USB power.  You don't have to do this, you can just use the power supply that came with the Raspberry Pi Zero W kit.  However, since my Google WiFi router uses a USB-C connection for power, and the Pi will be sitting right next to it, I decided to take the power supply from the kit and turn it into a Y-cable with a USB-C on one branch and a USB-micro on the other.  That way I have one less cord and one more power outlet available.

According to Google, my router takes 9 watts and comes with a 15 watt power supply.  The Pi Zero W uses no more than 250mA, or a quarter of an amp, which multiplied by 5 volts gives 1.25 watts.  So the router and the Pi together are a total of 10.25 watts. The power supply in the Pi kit is rated at 2.5A at 5V, which means it can deliver 12.5 watts, so it will be able to deliver enough power to run both these devices.

As I showed at the beginning of this blog post, I bought a 1ft cable with a USB-C on one end and a USB-micro on the other.  I simply cut this cable in half.  I also took off the fancy braiding with a razor knife because once you snip the cord the braiding starts to fray and misbehave.
With the power supply cord from the kit, simply snip off the USB-micro on the end.  I keep the micro connector in case I might want to use it later, so I leave a couple of inches of cord on the connector for future use.

Strip off the outer layers of the cables.  The USB cable has a layer of braided shielding wire, and under that is a layer of shielding foil.  You can tease these out and expose the wires underneath.

Below is a picture of the wires inside the USB cable.  Starting from left and going clockwise, the wires are:
  1. The first wire is the braided shielding, which I've teased out and twisted into a single wire.
  2. Next is a layer of shielding foil.
  3. The red wire is the positive power wire.
  4. Next there are some fibers and a piece of string. These give the cable strength so it doesn't stretch or crush so easily.
  5. The green and white wires carry data through the cable. These are a differential signalling pair and represent one data channel. Differential signalling makes the data connection more resistance to electromagnetic noise from external sources outside the wire.
  6. Finally there is a bare wire which appears to be the power negative wire.  If you use a multimeter you'll see this wire is connected with the outer shielding of the cable and can therefore be tied together with those.

For the bare metal wires including the shielding braid, foil, and the negative power wire, you can twist them together.  The green and white data wires can be cut off short and will not be used. The red power wire can be stripped back a little for soldering.

Here I have joined the power wires of two USB cables together and soldered them.

Now I've soldered the red and black wires from the power supply to the positive and negative power wires of the USB cables. As you vcan see my soldering isn't the tidiest. If you have some heat shrink remember to put that on before you solder the wires.

Shrink the heat shrink tubing and/or wrap the cable with electrical tape so the power wires don't touch and short out. Here's the Y-cable I made.

Tadaa!  Here is the Y-cable providing power to both the router and Pi.  Dang, now that I look at that ethernet cable, it's obvious that it needs to be 3 inches long instead of 6. Doh!

The End

Look out for a future a future blog post where I'll show you how to install Pi-hole on Android so you can have it on your mobile devices as well.

Thanks for reading.  Subscribe to my blog post at the top right of this page for regular updates.

If you're looking for the first blog post in this two part series, here it is:

-Dave Bad Person

No comments:

Post a Comment