Month: December 2018

How-To: DIY Custom Enclosure for Raspberry Pi NAS Media Server

How-To: DIY Custom Enclosure for Raspberry Pi NAS Media Server

In this post, I am building the first version of an enclosure for my Raspberry PI NAS and Media Server setup. It is built using Aluminium angle profiles and a plastic container. This is more like a POC build. It’s functional but not pretty. Eventually, I want to build an enclosure that’s as functional and also looks good in the living room, next to the TV.


Recently, I put my Raspberry PI Model 3B+ to good use as a NAS and Media server using OpenMediaVault and Plex (write-up coming soon). It’s a very good setup for casual file share operations between devices and streaming movie/music/pictures to various devices in the home wirelessly. While the setup did its job, I did not like all the hardware just lying there next to my router. Not to mention, the scalability of this setup wasn’t very good. I have plans to add another 2 TB 2.5″ but didn’t want it to add to the clutter. I also wanted to add some cooling for the PI and more importantly, the disks. So, I decided to make an enclosure that could fit it all in and also had provision for active cooling.

After going through some ideas for the shape for the enclosure, I settled on a cylindrical design. As always, going by the requirements, I ended up choosing aluminum for the internal frame encased in a cylindrical plastic box. I chose aluminum as it’s both an excellent heat conductor and pretty malleable to work with using my limited toolset. The plastic container idea came into the picture, as I could quickly source one from the kitchen. It did have a minor negative impact on the WAF score, but nothing I couldn’t handle with a little persuasion. 😜

 

Parts Used

  1. Aluminum Profiles (Right Angle) – It’s better to source this from your local hardware store. See images below.
  2. A non-powered USB Hub (Amazon INAmazon US)
  3. A SATA to USB adapter if you are using an internal hard drive (Amazon IN, Amazon US)
  4. Assorted nuts and bolts set (Amazon IN, Amazon US)
  5. Micro USB to DIP connector. (Amazon IN, Amazon US). This is required to convert non-powered USB hub to a powered one. If you are already using a powered hub then skip this.
  6. A Raspberry PI Model 3B+ (Amazon IN, Amazon US)

I yanked the Raspberry Pi 3B+ off of my last project – Raspberry Pi-Based DIY Automated Irrigation System which now happily runs on a Raspberry Pi Zero. As I mentioned in that post, that system did not need all the processing power that the 3B+ has. This project, however, can utilize as much processing power as the Pi can provide and then some.

The Design

I came up with this simple design for the enclosure in Sketchup. Click to interact and view 3D.

The above design can accommodate two hard drives, a cooling fan, a USB HUB, and, of course, the Raspberry Pi. Right now, I have a 1TB USB drive plugged in. The cooling fan is a 12V 60mm unit which I plan to run at 5V to reduce noise. I initially designed all four vertical aluminum bars to be of equal length. However, I accidentally cut one of the bars too short and liked it that way. So, I decided to make two of them smaller. The two larger bars measure 195mm and the smaller bars measure 180mm. The mounting bars for the hard drives and the Raspberry Pi measure 70mm each. The bottom two aluminum bars which are fixed to the base and hold the vertical bars measure 80mm each. There is enough space between them to fix the 60mm fan.

Here’s an image where I have hidden one of the aluminum bars to better show how everything is fixed.

DIY Enclosure for Raspberry PI based NAS and Media Server
DIY Enclosure for Raspberry PI based NAS and Media Server

I have made the 3D design available in SketchUp 3D Warehouse. You can either download it for editing in SketchUp desktop or import into SketchUp for Web and play with it there.

 

Mounting the Hard Drive

The image below shows the structure of the aluminum frame without the electronics. Notice the two aluminum bars attached to the two larger bars on the right. Those two are used for mounting and they keep the hard drive and the Raspberry PI in place as you can see in the 3D design above. I cut a small piece of sponge shaped like a “D” and glued it to one of the smaller bars to keep the drive firmly pushed in against the larger aluminum bar. The sponge is very useful since I am using a USB HDD with this setup right now which does not have any mounting screw holes. It also helps a lot in heat dissipation by keeping the drive pushed against the large aluminum bars that act as heat sinks for the drive.

DIY Enclosure for Raspberry PI based NAS and Media Server
The aluminum frame without the electronics.

 

DIY Enclosure for Raspberry PI based NAS and Media Server
Aluminum frame with the HDD in place. The sponge keeps the drive firmly pushed against the aluminum helping in heat dissipation.

If you use a 2.5″ internal drive, you can easily drill mounting holes in the vertical aluminum bars and fix the drive with screws. With internal drives, use a cheap SATA to USB adapter to connect it to the USB hub. Just make sure that your adapter does not require external power to work.

DIY Enclosure for Raspberry PI based NAS and Media Server
Fixing a 2.5-inch internal drive using screws.

Mounting the Raspberry Pi

The other smaller bar has holes drilled into it to fix two standoffs to mount the Raspberry Pi. This means that the Pi gets mounted between the two drives and will get plenty of airflow when the fan is running. I plan to write a script for the Pi to monitor its own and the drives’ temperature and run the fan if anything gets too hot for comfort.  Given that the Pi has to do a fair amount of media transcoding, good cooling would definitely prevent any thermal throttling and slow streaming performance.

DIY Enclosure for Raspberry PI based NAS and Media Server
Mounting the Raspberry PI using standoffs between the two drive holders.

 

I mounted the USB hub on the back of the large aluminum bars using some hot glue. I am using a cheap non-powered USB hub which I modded to add external power to it. There is an excellent instructable by “msaleiro” that demonstrates how it can be done. The whole aluminum frame is fixed to the base using just two M4 screws. With everything mounted, it was time to connect the cables and route them nicely. Right now, there are three cables running out of a hole I drilled in the base: A CAT-6 LAN cable, and two USB cables. One USB cable powers the Raspberry Pi and the other powers the USB hub.

That’s all about the design. Now, some pictures of my build.

Build Pictures

 

Closing Thoughts

As you can see in the pictures above, the fan is missing because it’s on its way from China. I will update this post with new pics once I have that installed. But, before that, I would have to figure out whether to use it as an intake or exhaust. In any case, I would have to make some holes at the top of the outer shell. Also, the cable situation needs improvement. There are 3 cables coming out of the base right now. I want to keep it to a maximum of two: one for power and one for LAN. I am also exploring the possibility of fixing female LAN and micro USB connectors to the base. The base also needs some decent legs.

Even with all the shortcomings, I am pretty satisfied with how this build turned out. I initially wanted to wrap the outer casing with a carbon fiber vinyl wrap to hide the internals. However, now I kinda like the rugged industrial look of the whole setup. This enclosure is going to be running my raspberry pi media server until I get some time to work on the second version. One thing’s for sure, I like the cylindrical design and I’m going to keep it. I might decide to change the orientation though. I also plan to add LED lighting to the second version.

What do you think about this build?

If you liked this project, please share this post and leave your comments below. If you too decide to build something like this yourself, I’d really love to know more about it. Please share your story.

I regularly make and write about DIY projects. If you like what I share, please consider subscribing.

More Raspberry Pi Projects

Raspberry PI Based DIY Automated Irrigation System [With Pictures]

How-To: Make a Raspberry PI Based DIY Automated Irrigation System

How-To: Make a Raspberry PI Based DIY Automated Irrigation System

In this post, I will show you how to build an automated irrigation system using a Raspberry PI. It waters my garden automatically every 24 hours. And I can even control it over the web for on-demand watering. I will walk you through the three main components of this system: the plumbing, wiring the Raspberry PI and programming it.

I live in an apartment complex and like any average apartment dweller, I and my wife have built ourselves a little balcony garden. Because who doesn’t like waking up to a little bit of green, right? But, the flip side to keeping plants is that your travel plans are affected. In the past, we have tried more traditional options, with varying levels of success. Sure, you can leave a key with a helpful neighbor, but that might not be an option for everybody. We recently moved to a new house and did not have anyone to rely on, when we decided to go on a week-long holiday. In all the planning leading up the to trip, we almost forgot about this problem. About a week before the trip, we realized that we had no backup this time around. So I decided to do something about it.

We have about 10 large potted plants and 5 small ones. I wanted all of them to survive our week-long trip. I dived right into scoping the problem and designing a system to solve it.

The idea was to have a Raspberry PI control a relay board which could in-turn control a submersible AC water pump.

Parts needed

There can be innumerable ways to solve a problem. I chose my design based mostly on what I already had available in my parts bin. I have included Amazon links here to buy these items:

Electricals

  1. A Raspberry PI. I used a model 3B+, but even a Raspberry PI Zero W is more than enough for something like this. (Raspberry PI Zero W – Amazon IN, Amazon US)
  2. An 8G (16G recommended) microSD card. (Amazon IN, Amazon US)
  3. A 4 channel isolated relay module. (Amazon IN, Amazon US)
  4. A 5v 2A / 2.5A power supply. (Amazon IN, Amazon US)
  5. A portable charger with 2 outputs. (Amazon IN, Amazon US)
  6. Some jumper cables. (Assorted Jumper Wires 120pcs – Amazon IN, Amazon US)
  7. A 3/4 outlet power strip. (Amazon IN, Amazon US)
  8. A 12v fan. (Generic PC case fan 80/120mm – Amazon IN, Amazon US)
  9. A female micro USB plug. (Generic USB plugs set – Amazon IN, Amazon US)
  10. A plastic box to put everything in.

Plumbing

  1. An AC submersible pump. (Amazon IN, Amazon US)
  2. A container that can hold enough water. (Amazon IN)
  3. A garden hose long enough to cover the required area. (I used a PVC pipe because I wanted a permanent setup)
  4. Length of drip pipe and connectors. (Amazon IN, Amazon US and Amazon US)

The plumbing

I moved the 5 small indoor plants to the balcony to easy up plumbing. My initial thought was to hang a small plastic container from the ceiling and run drip pipes from it to each of the pots. To control the amount of water that flows through each drip pipe, they could be attached at different heights in the container. This would look something like below:

Plumbing design for an automatic garden watering system.
The original idea for the plumbing.

While this would have worked, there were two main issues with it:

  1. I wanted something a little more permanent, which I could activate in the future whenever need be.
  2. A plastic bottle with pipes coming out of it sticks out like a sore thumb. And it’s not good for a healthy WAF score.
DIY Project Feasibility Graph
Make sure you gauge your DIY project on this scale for financial feasibility.

So, I decided on a different approach: Running a 1-inch PVC pipe around the balcony rails, fitted permanently, and connecting the smaller drip pipes directly to it. This eliminated the need to have anything hanging from the ceiling, which guarantees a big improvement in the overall look and the WAF. With the plumbing plan finalized, I took measurements of my balcony and noted everything down. This is what the new arrangement looks like:

Plumbing design for a raspberry pi based automatic garden watering system.
New plumbing setup

I bought the necessary supplies as listed above and quickly fitted everything. Having a few tools helped. I cut the pipes to size using a handsaw and drilled the holes required for drip pipes. I attached the PVC pipe to the balcony using zip ties.

Once I had the plumbing setup finalized, it was time to move on to the second stage.

How to Wire the Raspberry Pi

I had a Raspberry Pi 3 Model B+ lying around collecting dust. So, I was planning to use that to implement the brains of this system. Raspberry Pi 3 B+ is a complete overkill for this project and something as humble as even a Raspberry Pi Zero W is more than enough to handle this. The plan was to use the PI’s GPIO pins to drive a relay board which in turn could control the Pump. All I needed now was to wire up the Pi with the Relay and think of how I am going to power both. I came up with a simple circuit as shown below:

Connecting a Raspberry PI GPIO to a relay module to control an AC appliance
Figure 1. Connecting Raspberry PI to a 4-channel relay module

A word of caution: Working with AC mains voltages can be dangerous if you are not careful. It might result in damage to your components or an electric shock. Seek qualified help if you are not comfortable doing this yourself.

The above circuit diagram connects physical pins 28, 3, 5 and 7 (GPIO 1, 2, 3, 4 respectively) on the Raspberry PI to pins IN1, IN2, IN3, IN4 on the relay module respectively (It’s a 4-channel relay module). I connected the AC water pump to the third relay. Notice, that the ground line of the mains supply connects to the relay module before reaching the water pump. This is how the relay controls the activation of the pump. The circuit between the two pins is completed when the relay is activated, and the pump turns on. Also connected to the relay module is a female micro-USB connector. This is so that I can power the relay using a standard micro USB cable, such as the one that charges your phone.

You can get detailed information on the pins of the Raspberry Pi by typing “pinout” on a terminal in Raspbian or going to www.pinout.xyz

Note: Relays handle high voltages on one end and are connected to low-voltage sensitive electronics on the other. The recommended way to connect a relay to a Raspberry PI is to use an optocoupler to isolate the two sides of the relay. You don’t see one in the above diagram because this relay module has one built-in. When buying a relay module, make sure to read the description to find if the relay is suitable for direct connection to a logic circuit such as Raspberry PI / Arduino. Most relay modules being sold on Amazon / AliExpress nowadays are.

I made the above GPIO connections using jumper cables and added two 5v power supplies (phone chargers) – one for the Raspberry PI and one for the relay module. I then wrote a small python script on the PI to test out the circuit, substituting the water pump for a bulb.

Connecting the Raspberry PI to a relay module
Raspberry PI connected to the relay module

Once this jerry-rigged proof of concept setup was tested, it was time to put this all in a bigger box and have all the components fixed securely, so they don’t bump into each other and set my house on fire. One of my major focus areas on this whole project was the idea of redundancy and fail-safes. I wanted this system to have a backup for some of the most common failures that I could think of.

One very common scenario was a power outage. Not that the device could do much if there was a power outage, but I still wanted it to have backup power at least for the Raspberry Pi. This was mostly driven by my fear of the SD card failing due to repeated reboots. So, I threw in an old, but functioning battery pack I had, in the mix and ended up with something looking like this in a new box:

Raspberry PI all connected up with battery pack

The battery pack has two outputs to connect two devices simultaneously. It was perfect for this purpose as I could connect both the Raspberry Pi and the relay module to the battery pack and did not have to worry about running 2 charging wires into the box. This also made the box a self-contained unit which could run itself for several hours to outlast any power outage. Now, at this point, you know that I have a 4-channel relay module, but the pump is only going to need one relay. So, I started thinking about uses for the other three.

All this while, one thing had me worried: all these electronics in a cheap plastic box could be a recipe for fire. So, I wanted to fix the whole setup outside the home in the balcony. Outside meant more heat during the daytime, which could mean that the Raspberry PI will run very hot in a closed box. So, I wired up one of the relays with a 5v power supply (from the battery pack) and connected it to a CPU fan that I fixed to the box lid. I also cut up various holes using a box cutter to pass wires and also for ventilation.

I scripted the fan to turn on when the CPU temperature hits 55*C. Once triggered, it keeps running till the CPU cools down to 45*C. With the fan attached, I now had 3 relays available to handle higher power AC currents. So I decided to hook up a generic extension board to it. The setup looked something like this after everything was wired up:

Full setup with the extension board hooked up
Full setup with the extension board hooked up

 

raspberry pi automatic watering system block diagram
Block diagram showing all components and connections (Click for high resolution)

A few things to note in the above diagram:

  1. I connected the wall charger to Slot 1 in the Extension board. I did not show this in the above diagram to reduce clutter.
  2. The cooling fan is a typical 12v PC cooling fan running on 5v. This does not harm the fan, it runs slow and is more than enough for the PI.
  3. Relays 1-3 handle high AC voltage and relay 4 handles 5V DC.
  4. I wired the setup in such a way that GPIO 1 on the PI controls Relay 1, which controls slot 1 on the extension board. Similarly, GPIO 2, 3 control slots 2 and 3 respectively and GPIO 4 controls the fan. This is to make identification easier during coding.
  5. The water pump can be connected to either slot 2 or 3.
  6. Slot 1 is an Active Low slot. i.e. when the relay signal is 0, this slot will have AC power. Other slots are wired the other way around – they only have power when the relay signal is 1 (high). This is a failsafe measure – in case the battery runs out (more on why that might happen in the software section), and the relay board and PI are turned off, the charger is turned on automatically and the system boots up again.

Relays and active-high vs active-low

A relay usually has 3 pins on the AC side. I have numbered them 1-3 in the image below for easy identification:

single relay module

The symbol denoted by K1 to the right of these pins explains how the internal circuitry is wired up in the relay. The white line connects Pin 1 and 2 in the symbol. This is the default state of the relay: pins 1 and 2 are connected when the relay is off.  This is the active low state of the relay. If you connect a power source and a load in series to these pins, the circuit is complete and the load would get power.

When the relay is turned on though, the connection between pins 1 and 2 will be broken and instead, pin 2 will be connected to pin 3. Now, pin 2 and 3 make up an active high circuit. Any load connected to pin 3 would turn on only when the relay signal is high.

Note that not all relays follow the same convention, so, before wiring anything up, take a look at its description and the switching diagram.

 

How to Program the Raspberry Pi

With everything wired up and packed decently, it was time to move to the next stage – the software to control everything. Just like with the electricals and plumbing, I had decided on the scope of the software too and come up with a list of functions I wanted:

  1. Run the pump reliably: once every 24 hours for 15 seconds.
  2. Ability to trigger the pump run remotely over the web.
  3. Keep a log of all times the pump was run in the past and serve that over the web.

Given that I had a little time and only really needed very basic features, I decided to code a solution myself instead of looking for something ready made. This might seem counter-intuitive at first, but it’s not. With any ready-made solution, I’d first have had to spend time searching for it. And even if I found something that fits my requirement, it would most likely have many other features that would add to the complexity and require testing. A tailor-made solution was not only easy to implement, given the relatively easy requirements, but it also fit better and more importantly, I knew the innards of it which provided me the confidence that I can leave this unattended for 10 days.

I decided to implement all the functionality as separate Python scripts. You can find the code and more details on how it works in this GitHub repository GardenPI. Clone the repository to play with the source code:

git clone https://github.com/ankitr42/gardenpi.git

I also wrote some crontab entries to run controller scripts either during boot-up or at regular intervals. For example, the web server and temperature run at boot up. While the pump controller runs every 30 minutes to poll the elapsed time since the last run. To view the crontab commands I used, see the crontab.md file in the repository. You can set up the same crontab on your PI by running crontab -e on a terminal.

I plan on further improving the GardenPI software and make it more generic so that it can fit a wide variety of use cases. At the time of writing this post, the software had the following features.

  1. Separate scripts to implement services that control pump, battery charger, and cooling fan.
  2. A small DB that keeps a log of each event happening in the system, such as pump runs, battery charger toggle, and fan toggle.
  3. The battery charge controller service toggles the charger on/off every 4 hours to avoid over-heating the battery and maintain a charge-discharge cycle. Note that this meant that there was a risk that the battery ran out before the service could turn the charger on. If that happens, the PI and relay would shut down and the slot 1 being an active-low slot, would automatically turn on the charger.
  4. A small web server that reports the status of each of the above service.
  5. The web server also allows running the pump on demand with the help of the pump controller script.

Please see the GitHub repository for more information on how you can use the code.

Admittedly, the software took the longest and the most work. It took 2 all-nighters to achieve the bare minimum functionality I was shooting for, work reliably. Managing a full-time job and DIYing on a time-table is hard. Finally, I was able to test the complete set up for 2 days before leaving and it worked flawlessly.

If you have any questions, let me know in the comments below. Or, if you decide to make this, share your story. If this post helped you, please share.

 

More Raspberry Pi Projects

DIY Custom Enclosure for Raspberry Pi Media Server