hobby raspberry_pi, productivity

The Problem

Snoozing is the worst possible thing you can do to yourself! You start the day with a feeling of guilt and being late for everything from the start. To avoid this I’ve been working on improving my toolkit and techniques. I posted this as a list of my current methods and tools.

Whilst these methods worked for me a while I’ve started having an issue with Alexa playing my playlist from Spotify: It always starts with the same song. My idea was to have a different song every morning so this bug in Spotify impeded me from achieving my goal.

The Idea

As the saying goes: If all you have is a hammer, everything looks like a nail!

Even though I have other means of developing tools and software, I wanted to use another Raspberry Pi to replace Alexa morning routine (it still turns on the light though).

The Execution

So I decided to dust off my OG Raspberry Pi 1 Model B and implement my own custom solution for playing a random song every morning to help wake me up.

Here’s how I implemented it from start to finish:

Step 01: Download Raspbian image and prepare SD/MicroSD card

I downloaded Raspbian Buster Lite as I don’t need desktop and other extra software for this project.

I followed the official guide from raspberrypi.org to burn the image. It’s basically just a few commands:

Insert the card and run the following to identify the card:

diskutil list

Then

sudo dd bs=1m if=path_of_your_image.img of=/dev/rdiskn conv=sync

where n is the number of disk which I found from the first step.

If you get “resource is busy” error, then run the following

diskutil unmountDisk /dev/diskn

Step 02: Carry Out Necessary Configuration Updates

It’s a good practice to change the default passwords so I ran

passwd

and updated the password.

Also for remote installations I enabled SSH:

sudo systemctl enable ssh
sudo systemctl start ssh

And I like to give a specific name to all my devices so that I can keep an eye on the machines connected to my network more easily.

To achieve that I ran:

sudo hostname NEW_HOSTNAME

I used raspi-config to connect to WiFi network

sudo raspi-config

and enter the details and rebooted to connect to the network.

In this MVP implementation I don’t even need Internet connectivity (after the initial installations below) as all my MP3 files are on the device already. But in my tests I noticed that when the Pi is disconnected the system time wasn’t updating so scheduled job wasn’t being triggered. This is probably because I’m using a Pi 1. I’m guessing the newer version can work offline as well.

Step 03: Install MP3 Player Software

In this simple project I’m going to install mpg123 and run it via a Python script

Installation is as simple as this:

sudo apt-get install mpg123 -y

Step 04: Develop the script

The following script points to a folder which supposedly have all the local MP3 files, picks one randomly and starts playing.

import os
import random

path ='/path/to/mp3/folder' 
files = os.listdir(path)
index = random.randrange(0, len(files))

os.system(f'/usr/bin/mpg123 {files[index]}')

Step 05: Copy music and script to Raspberry Pi

Save the script above in a USB drive along with the MP3 files. To copy the files from USB drive to Pi microSD card, I inserted my USB and ran the following:

sudo fdisk -l
sudo mkdir /media/usb
sudo mount /dev/sda1 /media/usb

Then

mkdir /home/pi/music
cp /media/usb/*.mp3 /home/pi/music

and copy the script to home folder

cp /media/usb/alarm-pi.py /home/pi

Step 06: Schedule the script

crontab -e

Then add to run at a certain time. The example below runs at 7:30am everyday:

30 7 * * * /usr/bin/python3 /home/pi/alarm-pi.py

If you want to run the script multiple times you can another one (like another one after 10 minutes as a snooze-prevention mechanism!)

Step 07: Test the schedule

Update your schedule temporarily and set it to a few minutes later and start playing the waiting game! Once you hear a random song playing make sure to set it back to your actual alarm time.

Conclusion

I meant to run this in a Docker container and develop the application with dotnet core but turns out playing MP3 with dotnet core in Linux is not easy. Also playing sound from inside a container has its own issues. So for those reasons I chose the easiest possible path to achieve the goal. If I find time I’ll improve this and make it more flexible but for the time being it does the job so I’ll assume MVP is done now.

Resources

security pi_hole, raspberry_pi

For some time I’d been wanting to set up my own Pi Hole in my network.

In this previous post I implemented a Docker registry on Raspberry Pi. I was planning to install it using Docker on that Pi but then I decided to set up a new Pi from scratch due to performance concerns. After I get the hang of it I might migrate it to Docker.

Preparations: Download Raspbian image and prepare Micro SD card

As of this writing, the latest Raspbian release is Buster but in the official supported OS list the latest supported Raspbian is Stretch. So I went to this page to download Raspbian Stretch Lite.

I downloaded Raspbian Buster Lite as I don’t need desktop and other extra software for this project.I followed the official guide from raspberrypi.org to burn the image. It’s basically just a few commands:

I inserted the card and run the following to identify the card:

diskutil list

Then

sudo dd bs=1m if=path_of_your_image.img of=/dev/rdiskn conv=sync

where n is the number of disk which I found from the first step.

If you get “resource is busy” error, then run the following

diskutil unmountDisk /dev/diskn

Carry Out Necessary Configuration Updates

It’s a good practice to change the default passwords so I ran

passwd

and updated the password.

Also for remote installations I enabled SSH:

sudo systemctl enable ssh
sudo systemctl start ssh

And I like to give a specific name to all my devices so that I can keep an eye on the machines connected to my network more easily.

To achieve that I ran:

sudo hostname NEW_HOSTNAME

No need to set a static IP address at this point as Pi Hole setup takes care of that:

Install Pi-Hole

There is an easy one-step installation:

curl -sSL https://install.pi-hole.net | bash

I changed my IP to a different static IP and rebooted the Pi.

After that I could go to the admin page via http://{Pi Hole Static IP}/admin/ and see the dashboard:

Test

Now it’s time to see if it’s working at all. I have 2 MacBooks connected to the same network. I changed one of the laptop’s DNS server to point to Pi Hole and left the other one intact.

In the laptop that’s still using the old DNS settings, I opened a Chrome window in incognito mode and visited a site and at top it shows an ad:

And on the other laptop I repeated the process and I got the same page without ads this time:

When I go to the admin page I can see what calls are blocked:

Also a nice thing about it you can choose to Whitelist certain providers. For instance in the image below it looks like Google Analytics is blocked and I can whitelist it easily:

Resources

machine_learningaws deeplens

I recently bought this device in anticipation of getting accustomed with AWS Machine Learning services. I thought it would be a nice and fun introduction to build a DIY project using ML. This weekend I had a chance to play around with it.

Unboxing

Registering

First thing you need to do is register your DeepLens which can be done here

Alternatively, you can log in to AWS Console, go to AWS DeepLens service and click Register Device. As of this writing this service is only available in Tokyo, N.Virginia and Frankfurt. As closest to me is Frankfurt I chose that one.

Creating a Project

Now that the devices is ready, I went to Projects section and created a new project based on a template. For this initial exploration mission I chose Bird Classification template.

When you choose your template it shows a description of the template and how it works

Once you create the project it creates the necessary resources. For example in my template it created 2 Lambda functions:

Now we are ready to deploy the project to the device so we can simply click on Deploy to Device button on top right-hand corner in the project page:

After that you choose your DeepLens from the avialble device list (in my case I had one device) and click Review then Deploy

Testing the project

First I followed the steps to install custom certificate so that I can start viewing the video feed in my browser:

Because DeepLens doesn’t know if there’s a bird or not in the picture it constantly makes a guess. Even an empty carton box looks like a Long Tailed Jaeger with 2.1% confidence apparently!

When I presented an image I found from the Internet the results significantly improved though (thankfully, otherwise this would all be for nothing!)

According to Google the bird in the picture is indeed a Kingfisher.

Next test is the bird I saw on the roof and identified as a Mallard. Let’s see what DeepLens thinks about it:

So with 17.5% confidens DeepLenms agrees that it is a Mallard. One thing to note is that the angle of the image significantly changes the prediction. Even a slight change in the angle makes the prediction shift from 3% to 45%. But it’s just a test project template anyway so I’m not looking for too much accuracy at this point.

Final test: I downloaded the dataset used to train the algorithm. It’s a 1.1GB download full of images. To compare with the last Mallard test I got one picture from the Mallard folder and showed it to DeepLens:

As shown above the confidence increased to 55%. So probably if I use the pictures I took to train the algorithm I would be able to get much better results.

Conclusion

This was just a first step to set up and explore the device. Hopefully in the comin days I’ll be doing more meaningful projects with it and post those too.

Resources