Pages

Showing posts with label Raspberry Pi. Show all posts
Showing posts with label Raspberry Pi. Show all posts

Friday, August 10, 2018

OpenSCAD: Camera case for the Raspberry Pi camera module

Introduction

At the end of 2015 I placed a DIY security camera in our house with a view on our backyard. The camera consists of a Raspberry Pi model B and a 5MP camera board module (It's been replaced by a 8MP version). I used Motion, a very flexible program that monitors video signal, on top of Raspbian. It worked all these years without problem but over time the wooden case that I made was starting to fall apart. I therefore decided to create a 3D printed camera case to replace it. I also wanted to use this opportunity to eliminate the problem of glare on sunny days.


Design of the Camera Case

The camera case consist of five parts. A back and a front of the case, a camera holder and lens to protect the camera module and easily insert it into front of the case, and a lens hood to prevent glare.  I used OpenSCAD, a free and open source program to design 3D CAD models. The Constructive Solid Geometry properties of OpenSCAD are excellent for these kind of designs. The back case has mounting points for the Raspberry Pi. I use the Raspberry Pi B but the mounting points are easily changed for other Raspberry Pi models, in fact I already added the coordinates for some other models.

I updated the design with stop ring for the lens module to prevent the lens to slide into the camera a little so this is a link to version 2. If you want to make the case yourself or just want to tinker with it here is a link to the OpenSCAD file: https://drive.google.com/open?id=1qJ7picRoSDI_HNSntF7nptrMazSRV3FF


Thursday, April 5, 2018

Maintaining the Raspberry Pi Squeezebox server (aka Logitech Media Server)

Introduction

I wrote earlier about the Logitech Media Server (LMS), open source software that I've used to turn a Raspberry Pi into a music server for my Squeezebox devices. I'm using my Raspberry Pi based Logitech Media Server a couple of months and I've grown very fond of it. What started as an experiment now has become a device I'm starting to rely on. I guess that eventually I have to replace it with something like the WD PiDrive Node Zero. In the meantime however I do need to maintain the LMS. In this blogpost I'll explain how I add and remove music files from the LMS and create a simple backup of your music files. Finally I provide a tip to debug problems with the Logitech Media Server. All software that I used for this project is free and open source.

My Raspberry Pi turned into an LMS with 3D printed case on my desk.

Adding, changing and deleting music files

I partly ripped my CD collection and still add new albums. All the ripping is done on my Thinkpad T40 laptop using Asunder, a low resource ripper for Linux, on the Puppy Linux operating system. In the beginning I unmounted the USB hard disk from the LMS, removed it and connected it to the laptop and copied the new albums. This proved to be be a clumsy method and I quickly found myself looking for an alternative.

I decided to use FTP to transfer files over the network to the Raspberry Pi. FTP is a network protocol to transfer files between a client and a server. The software that I'm using is either FileZilla or gFTP. They have basically the same functionality but gFTP is not available for Windows while FileZilla is available for GNU/Linux, OSX and Windows. Working with both is easy enough the only problem that I had was to set the ownership for the USB hard disk on the Raspberry Pi. Only root was able to write to the disk. The reason is a bit technical and maybe confusing but I had used the Windows FAT filesystem for the USB hard disk and when I mounted it on my Linux laptop (Puppy Linux distro), ownership of all files was changed by Puppy Linux to root. At this point it was impossible to change ownership and permissions with chmod and chown from the media server. To solve this problem I had to unmount and mount the USB hard disk using the following general command:
mount device mount-point -o uid=foo -o gid=foo
or in my case
mount  /dev/sda1 /media/usb-drive -o uid=pi -o gid=pi
This command sets the ownership of all files on the USB hard disk to pi instead of root enabling me to use FileZilla and gFTP.

Creating a backup of the music collection

I invested a lot of time in ripping my CD collection so it would be a shame if I lost my music files due to a hard disk crash. I therefore use a very simple backup method, largely copied from the Raspberrypi.org website to ensure that my files are safe.
cd /home/media/usb-drive
sudo tar czf usbdrive.tar.gz
This creates a file 'usbdrive.tar.gz' that contains files in /media/usb-drive, the mounting point of my USB drive and places it in the same map. Next I use (again) Filezilla to move the backup file to my desktop computer.

Debug problems with the Logitech Media Server (tip)

I didn't encounter much problems with the LMS but if you do take a look into the log file of the server. This can be done opening a terminal on your PC and login into the Rasperry Pi with:
ssh pi@<your_network_ip_address_of_the_server>
cat /var/log/squeezeboxserver/server.log
The sudo ssh command prompts for the password of the Raspberry Pi.  Now look for anything suspicious in the log, copy it and do an internet search with your preferred search engine. Chances are someone already encountered the same problem.

Earlier posts about the Logitech Media Server:

Setting up a Raspberry Pi Logitech Media Server
https://eribuijs.blogspot.nl/2017/08/creating-raspberry-pi-squeezebox-server.html

3d printed enclosure for the Raspberry Pi Logitech Media Server
https://eribuijs.blogspot.nl/2017/11/raspberry-pi-squeezebox-server-with-3d.html

Wednesday, November 1, 2017

Raspberry Pi Squeezebox server with 3d printed enclosure

The need for a enclosure

Back in August I created a Squeezebox server using a Raspberry Pi 2 and a USB harddrive I had both lying around. I quickly had the server up and running and I'm using it ever since but I didn't have a enclosure. A 3D printed enclosure would enable me to give the server a more permanent place and at the same time I'm getting rid of these components and cables lying around on my desk.

The Squeezebox server sitting nicely into the 3d printed enclosure that I designed for it.

Solvespace for the enclosure

As a 3D CAD tool to design the enclosure I choose Solvespace. Solvespace is free and open source available for Windows, OSX and Linux. I use Solvespace a lot and it has it's limitations but I felt it was fully capable for this job and indeed it didn't disappoint. Before starting I made a couple of simple drawings with pen and paper, I measured the size of the harddisk and the Raspberry Pi. I also used a template for the Raspberry Pi mounting holes that I found online.

With all this information I started drawing with Solvespace. The power of Solvespace (and other 3D CAD programs) is that's it's very easy to make changes to the 3D design. While working on the design I noticed that USB harddisk can become hot so I figured it needed to be cooled. In the drawing I elevated the harddisk a bit by adding four supports one on each corner of the disk and made four ventilation slots in the bottom of the enclosure thus allowed air to flow freely around the harddisk. The Solvespace files of this enclosure are here. Make sure to adjust the size to the USB HDD you're using.

The 3D design I came up. The printed parts, bottom and lid, are grey, the green part is the Raspberry Pi and the red part is the USB harddrive.

3d printing and assembly

I printed all the parts on my Hephestos 2 printer using black PLA filament and the result looks pretty good. Assembly of the parts is simple. First I used 2.5mm bolts and nuts to mount the Raspberry Pi upside down to the lid of the enclosure. Next I insert the USB HDD into the bottom placing it on the elevated supports.  The enclosure is fastened with four bolts 5x60mm and 5mm nuts. The nuts fall nicely into the hexagonal cavity of the feet. Finally the USB cable of the HDD is connected with the Raspberry PI and the ethernet cable is connected from my router to the Pi. The server has run for several weeks in the 3d printed enclosure without any problem. For those that want more information I made a YouTube video about this project.

Wednesday, August 16, 2017

Spotify silently and remotely deleted app from Squeezebox devices

Spotify removed

Spotify silently and remotely deleted the Spotify app from the Squeezebox devices I found out after returning from vacation (Yes, they did post an article on their help pages I found out but I wasn't notified). I own a Squeezebox Radio and a Squeezebox Touch that I both use heavily to listen to music with the Spotify app. I even have a Spotify Premium Family account that enables me to play Spotify on multiple devices. I understand that support for hardware, such as the Squeezebox, is limited and will end some day. However what outrages me is that Spotify remotely removes software on a device that I own. Spotify makes it very clear that they control the software that they provide for the device and not the user. With that they control the functionality of my devices.

The Logitech Squeezebox Radio, one of several types of Squeezebox devices, now without Spotify (photo from https://en.wikipedia.org/wiki/Squeezebox_(network_music_player)).

The Squeezebox music player

The Squeezebox device is a network music player from Logitech. The devices are versatile and have excellent hardware. The Squeezebox can be used in two different ways; as a player that works with an online service, mysqueezebox.com, or with a private server called Logitech Media Server. I preferred to use my Squeezebox devices with the online service mainly for its ease of use and because some streaming services such as Spotify and Deezer are available as an app. The apps are very convenient to stream music just with a remote control. The Squeezebox hardware was discontinued in 2012 but  but, to it's credit, Logitech maintains the online service to this very day.

I didn't mention the best thing about Squeezebox, both the server and player software are non-proprietary and can be installed on several different operating systems. This enables all users to make build their own Squeezebox clients and servers.

Choices

With the removal of the Spotify app I have a choice to make. Will I continue to be using the my Squeezebox devices with the online server using Deezer instead of Spotify or will I set up my own private server? The latter is of course more of a hassle since it involves installing and maintaining the Logitech Media Server, ripping CD's to mp3's and storing them on the server. Nevertheless I may choose to create my own private server. I recently decided to move the software on my iMac from proprietary to non-proprietary so creating the private server for my Squeezebox would also make sense. I happen to have a Raspberry Pi 2 lying around and in the coming weeks I'll try to set it up as a Logitech Media Server.


Monday, May 9, 2016

Adding WiFi and audio streams to the cooler

Introduction

In my previous blog post I finished my audio cooler. It's a small cooler with a tiny audio system that nevertheless sounds good. The only way to control the audio is through a wired connection. It would be a nice addition to have some kind of remote control either by WiFi or Bluetooth. While testing the cooler I've got the idea to connect a Raspberry Pi A+, that was still unused, to the cooler and stream audio over WiFi to the Pi,with the Pi wired with the cooler. This could be useful for a garden party or BBQ where WiFi is available and I don't want to attach the smartphone to the cooler. In this blog post I'll share my experience with installation and operating the software needed for this project on the Pi.

Raspberry Pi A+ (in it's Pimoroni Pibow case)  connected with the headphone jack to the cooler. The Pi has a small Edimax Wifi adapter.

Installing MPD and MPC on the Raspberry Pi

Since I run the Raspberry Pi headless I use SSH login to the Pi. SSH is available for most operating systems with the notable exception of Windows. I already had Raspbian installed on the Pi so first I updated the OS and rebooted.

sudo apt-get update
sudo reboot -h now

Next I created a .conf file for the Edimax WiFi USB adapter. I learned from a previous project that, for a good remote connection, the sleep mode of the Edimax needs to be disabled. In the /etc/modprobe.d folder on the Raspberry Pi a file needs to be created with the name 8192cu.conf (8192cu is the name of the Realtek WiFi chip where the Edimax is based on).

cd /etc/modprobe.d
sudo nano 8192cu.conf

Add the following line in the nano editor and save the file

options 8192cu rtw_power_mgnt=0 rtw_enusbss=0

reboot again

sudo reboot -h now

Now I needed software on the Pi that allows me to remotely play (and organize) music. I installed the MPD (music player daemon) and the very lightweight MPC (music player client) and this server/client combination seemed perfect for this purpose. There are many other clients for MPD, even for Android and iOS, but for now MPC is good enough. To install MPD and MPC on the Pi:

sudo apt-get install mpd mpc

According to some sources on the internet a sound driver (sudo modprobe snd-bcm2835) should be installed on the Pi however I found it was already installed, probably with Raspbian.  This can be checked with the lsmod command.

Adding streams to a playlist

With MPD and MPC installed I'm finally able to stream a radio station from my Pi to the cooler but I needed to add stream urls of my favorite stations. In the example below I add the stream of a Dutch radio station to the current playlist.

mpc add http://icecast.omroep.nl/radio1-bb-mp3

In order to play the stream just type and the added stream starts playing

mpc play

To save the current playlist type

mpc save aplaylist

The file with the name aplaylist.m3u file is created in the folder /var/lib/mpd/playlists with the above url of a stream in it. Of course other urls can be added to the playlist. I've found this list very helpful if you're looking for (Western) European streams.

Adding music to a playlist

To create a playlist of music that is stored somewhere as mp3's just add these files to the folder /var/lib/mpd/music on the Pi. Before the files are copied however the permission of this folder has to be changed. That is done by adding write permission to the group and by changing the group to audio (which is currently root).

sudo chmod g+w /var/lib/mpd/music
sudo chgrp audio /var/lib/mpd/music

Now the mp3's can be copied. To create a playlist of these mp3's first clear the current playlist, create a new one by adding mp3 files and save the playlist.

mpc clear
mpc add /var/lib/mpd/music/my_cool_music.mp3
mpc save cool_playlist
mpc update

The last command updates the database of MPD. By adding more files the playlist increases in length. To play the playlist use the same command as above (mpc play).

Conclusion

A music or internet stream over WiFi to the cooler is a nice addition. MPD is a very stable music player with lots of different clients, perfect for these kind of projects. With MPC I can easily add streams, create playlists, play music or even store mp3's on the Pi. Unfortunately WiFi with a Pi has some downsides too. The Raspberry Pi takes up space in the cooler and it will drain the small battery fast. Therefore as a next step I'll try bluetooth because it probably adds less weight to the cooler and has less power consumption.

Yes, the Raspberry Pi with MPD is a nice addition to the cooler but it has it's drawbacks such as power consumption and added occupied space.


Wednesday, November 18, 2015

Plywood housing for the homemade security camera

In the last two weeks I have built a Raspberry Pi security camera. For the time being I used a crude housing made out of MDF. This was ok for testing purposes but now I wanted something more permanent. Something that I could attach to my window frame and that is able to pan and tilt to get the best angle at my backyard. I could buy of camera housing such as the ModMyPi Nwazet Pi Camera or buy a dummy camera and build the security camera in it. However building it myself would be more fun. I decided on a housing made out of plywood since it is cheap, strong and durable.

The Raspberry Pi security camera keeping an eye on my backyard.

The housing is rectangular box that nevertheless looks like a camera due to the round placeholder of the camera. I used 1/2 inch thick plywood for the body of the housing and 1/4 inch thick plywood for the lid and bottom plate. The camera module is placed between two pieces of plywood, that keep it in place. The two pieces are tight together with two machine screws. I used spacers to prevent the module from being squeezed when tightening the machine screws.

Housing of the security camera
The front of the housing lifted showing the Raspbery Pi B mounted on the 1/4 inch plywood bottom.  
The camera module is easily removed from the front.

I left an opening in the camera housing to have easy access to the micro-USB connector and the SD-card.
the Raspberry Pi camera module between two pieces of 1/4 inch plywood kept together with two machine screws.
The camera housing is connected to a plywood frame that enables me to pan and tilt it. The frame is also made out of 1/2 inch thick plywood. The three pieces are glued together and two 1/4 inch machine screws (one on the top of the housing and one on the bottom) connect the frame with the housing. Two wing nuts are used to tighten the camera enabling the camera to pan. The frame is connected to the wall with cylindrical piece of wood (from a broom stick). Two pieces of plywood at the end of this pieces of wood enable the camera to tilt (see image below).

The finished security camera. The mount will be fitted to our window frame. The camera can be panned and tilted to get the best view of my backyard.

With the housing and mount complete my Raspberry Pi security camera is finished. Motion has been very stable on the Pi. I haven't had a crash or malfunction in three weeks. And thanks to crontab the camera doesn't need a lot of maintenance. Is there something left to be desired? Yes. The camera is not usable at night (I need the NoIr camera and IR lights for that) and I have false positives e.g. when it rains (the raindrops on the window are causing this) other than that I'm pleased with the result.

Friday, November 13, 2015

Raspberry Pi security camera, first experience

Introduction

(EDIT: updated on 4 May 2018). Last week I wrote about the Raspberry Pi, the camera module and Motion acting as a security camera. I've been testing my Raspberry Pi security camera for a week now and the results are satisfying although there are also some problems. I think much of the success of the camera comes down to finding the best settings of the /etc/motion.conf file of Motion for a given scenario. The motion.conf file enables you to setup the Motion program. Everything from camera settings to motion detection can be changed in this file.

Image from a motion video. If someone (in this case my son) is entering our backyard it is perfectly recorded by Motion.

Threshold

I want my security camera to record anyone entering my backyard but this sounds easier than it is. If the threshold parameter in motion.conf is too low all kinds of events are recorded that are completely irrelevant. E.g. on a rainy day raindrops falling on the window are recorded. With higher threshold values I might miss something relevant. After experimenting with different values of the threshold I found that a value of 1500 works best in my case.

Lens flare

On a sunny day bright sunlight can fall onto the camera lens causing lens flare. This reduced contrast and color saturation (see image below). More importantly the Motion program detect changes in lens flare as motion and records them. I think problem can easily be solved with some kind of lens hood.

Lens flare caused by sunlight reducing contrast and color saturation.

Night

A whole different problem is motion detection at night. My camera, the regular Raspberry Pi camera module, is just not up to this task. For detection at night I'll need the NoIR Pi camera. This camera has no infrared filter. As a consequence colours at daylight look odd but you're able to record at night, but only with infrared light illuminating the subjects. Several add-on boards for the NoIR Pi camera are on the market that do just that. These boards fit right over the camera module.

The regular Raspberry Pi camera module is useless at night.

Proper file permissions

I created a directory /home/pi/Camera on the Raspberry Pi. I want all my video files to be stored in this place however before Motion is able to do this I had to change the permissions. Motion (the program) creates it's own user motion so this user needs proper permission in the home/pi/Camera folder (that is created by the user pi). To do this I first changed the group ownership of the folder:
chgrp motion /home/pi/Camera
And next give the group the proper permission:
chmod g+rwx /home/pi/Camera 

Scheduled tasks

The number of motion video's that are stored on the Pi increase rapidly over the days. My backyard is quiet but not that quiet (not to mention the false positive that I described above). Since the Raspberry Pi stores the files on an SD card I will easily run out of storage space. So I need a method to remove the files automatically e.g after five days. Cron is perfect for this since it let me schedule commands or scripts periodically. I wrote a shell script that removes avi files created by Motion older than five days and added this to my crontab file to be executed daily. The key command in this script is:
find /home/pi/Camera -mtime +5 -name "*.avi" -exec rm {} \;
This shell script is then added to the crontab. With crontab -e the crontab file can be edited. If you're looking for a good article on Cron and how to create a scheduled task look here.

Copying files to a PC

Whenever I want to check the motion files (e.g after one day) I copy them to my iMac using scp from the OSX terminal and then created a playlist in VLC by dragging all the files into it. The harvest of that one day is then displayed.
scp pi@192.168.178.27:Camera/*.avi to/destination/folder

What's next

Next I'll test the security camera further until I'm satisfied with the settings and build a proper housing for the camera. Possibly I will switch from the regular camera module to the NoIR camera.

Saturday, November 7, 2015

Homemade security camera with a Raspberry Pi

Introduction

(EDIT: updated on 4 May 2018) One week ago we've had a burglar in our house. Of course I immediately started improving the security of our home. After improving the usual stuff I felt the need for some kind of security camera. This camera could provide me a good view of the backyard which is a very quiet place and therefore preferred by burglars. Because I like to make things myself I thought it was a good idea to use a Raspberry Pi and it's camera module. I used them in the past for time lapse video's of scenery but I hadn't used it much lately.

The goal that I had set was a camera that detects and records motion and that I could access through our local network preferably a browser. I also wanted the camera to give decent images by day and night.


Hardware.

The heart of the system is a Raspberry Pi model B with the Raspberry camera module (5MP). Furthermore I use an Edimax dongle for my WiFi connection and a micro-USB power supply (1A). Initially I use an MDF case that I made to fit the camera and the Pi. It isn't pretty but it protects both the camera and the Pi. If everything works as planned I probably built it into a dummy camera such as this one or may make a wooden enclosure myself.

Initially I had some problems connecting to the Pi through SSH. I discovered that this was caused by the Edimax dongle (8192cu WiFi chip) that apparently goes into sleep mode after a period of inactivity. This was solved with a command line fix that disables power saving (see here on page 17 how to fix this).

Raspberry Pi B and camera module in an MDF enclosure.

Software

On top of Raspbian (I installed Wheezy with Desktop but nowadays I'd suggest using Stretch Lite) I installed Motion. Motion is a Linux program that monitors the video signal from camera's and, very important to me, is able to detect motion. Motion is widely used and there is plenty of good information on it on the internet. A simple tutorial how to install Motion specifically on the Raspberry Pi and get it to work with the Raspberry camera module can be found on here (go to step 7 for software installation). However Motion has far more possibilities and it is worthwhile to explore these once you start using it.

Configuring and testing the system

Motion can support multiple camera's but I'll stick to one camera for now. Configuration of Motion for the Raspberry Pi and it's camera module is done in the /etc/motion.conf file (not in the /etc/motion/motion.conf file). There is a very good YouTube tutorial on configuring Motion for Linux here and here. At this point I made only a few changes to the motion.conf file such as camera width and height, directory where the video is stored on the Pi and some camera specific variables.

The camera works great. The image quality is good and the system appears to be stable. I can open a stream of the security camera in my browser by entering the URL of the Raspberry Pi and the selected port (default 8081). My motion files are stored as avi's on the Raspberry Pi. I can play them with VLC media player on my iMac. Next I'll experiment with the settings of Motion (e.g. sensitivity of motion detection, resolution of the camera), test in under different circumstances (indoor, outdoor, night and day) and build a proper housing for the security camera.

It doesn't look pretty but the security camera works great.