Friday, October 26, 2018

Raspberry Cookbook

February 2015 I wrote that I purchased a book which made me very happy: the Arduino Cookbook from O'Reilly. http://lucstechblog.blogspot.com/2015/02/the-bible-arrived-i-have-been-looking.html




Well recently a bought another book from O'Reilly publishers that made me equally happy. And this is also from the cookbook series being the Raspberry Cookbook.

I am aware that the Raspberry Pi in all its variations is basically a Linux computer and that it is impossible to describe in detail all things you can do with a Linux machine. The author however manages to cover (in detail) many aspects of this popular micro computer.

The book starts with the process of installing the operating system, then covers the Linux commands you probably will use most. It descibes how to use these commands from the command line and also covers how to use most aspects from the Graphical User Interface.

Next the Python language is covered. Python is important because it is flexible, free, included with the Raspberry operating system and can be used for almost any Raspberry project you want to realise.

The book goes on with briefly describing some programs you can use for image manipulation (Gimp), Office programs, Media player software, Retro computing emulators and packages like OpenCV which is used for objects and person recognition.

It even describes building an internet radio although it uses a different approach from what I did: http://lucstechblog.blogspot.com/2018/09/raspberry-internet-radio-part-1.html

After that the (for me) most interesting part comes: interfacing the Raspberry Pi's with all, kinds of sensors. And lastly building webpages (with Bottle) that will show your sensor readings to the world.

At this moment I already have several Raspberry Pi's fully oprational in my home working as:
- a media centre (playing movies and series on my TV)
- a printer server (making possible that anyone in my house can print from any location)
- controller for my 3D printer
- Domoticz home automation system
- Internet radio

Nevertheless I learned a lot from this book and I am really glad I bought it.

And yes I know that you can find anything you need on the internet. And yes I am even aware that you can download this book as a PDF from the internet. However buying a book now and then stimulates the writers to write more interesting books and stimulates publishers to keep publishing interesting stuff for us. Besides that I prefer holding a real book in my hands over reading on a screen.

So if you are interested in starting projects with a Raspberry Pi do yourself a favor and spend a little money on this book. It is not expensive (I paid 26 euro) and packs a ton of information.


Maybe an idea for a Christmas present.....
Highly recommended !!

Till next time.
Have fun

Luc Volders

Friday, October 19, 2018

Raspberry Pi Internet Radio Part 3

For a complete index to all my stories click this text

This is the third installment in a series of 3 stories on how to build a cheap internet radio with a Raspberry Pi Zero.

The first story described what accessories you need and how to write a small Python program that actually playes one radio station. You can find that story here: https://lucstechblog.blogspot.com/2018/09/raspberry-internet-radio-part-1.html

In the second story I expanded the Python program to give it a nice looking menu that gives the possibillity to choose from a number of radio stations. You can re-read that story here: https://lucstechblog.blogspot.com/2018/09/raspberry-pi-internet-radio-part-2.html

An intermezzo was story 2B that showed how to find MP3 Broadcasts on the internet that you can incorporate in the program made in story 2. Read it here: https://lucstechblog.blogspot.com/2018/09/raspberry-pi-internet-radio-part-2-b.html

In this last part we are going to make the Internet Radio headless.
This means that it is a stand-alone system. The Pi Zero will start when you press a button and shuts-down again when you press a button. Next to that there will be buttons for choosing the radio stations. No monitor, keyboard or mouse will be needed to operate the internet radio.

To achieve this there are several steps to be made:

1) the hardware
2) the Python Internet Radio program
3) assure that the Python Internet Radio program is started automatically at booting the Pi

The hardware

I am giving you the breadboard layout for an Internet Radio control with an on/off button and 2 buttons for Radio stations. Just have a good look at the setup and you can expand this easily.


First let us have a look at the Raspberry I/O pins.

GPIO3 (pin 5) has a special ability. When you momentarily connect it to ground the Pi Zero will boot. Now that is a nice feature we happily will use to set the Internet Radio ON.

Several I/O pins have multiple functions. So best is (in this case) not to use them for our Internet Radio. That is no problem as there are my pins left that can be used. I will use GPIO20 (pin 38) and GPIO21 (pin 40) as the pins that choose the radio stations.

I used one extra GPIO being GPIO18 (pin 12) to attach through a 1K delimiting resistor to a led.
This led will go ON as the Internet Radio program is running and OFF when the Internet Radio program stops.



So the above breadboard layout shows you how to wire the buttons. As there are many I/O pins freely available you can expand this with as many buttons as you like.



Above you can see my setup in real life. I just altered one thing.




The button in the picture is not an ordinary push button. It is a pushbutton with a build-in led. So what I did is connect the button to GPIO3 and connect the LED part to GPIO18. You can use one ground lead that can be attached to the two connectors on the switch. Don't forget to attach a 1k delimiting resistor to the LED. If you don't you will damage your Raspberry PI

This way I have a switch with a build in LED that goes on when I start my internet radio and the LED goes off when I shut down the Raspberry Pi. Neat !!!

The Software

To install the software I always enable VNC on the Raspberry Pi and download the VNC viewer on my PC (https://www.realvnc.com/en/download/vnc/windows/) so I can install everything remotely and I do not need to attach an extra  keyboard, mouse and monitor to the Raspberry.

Start with installing and updating the Raspberry OS as described here:
https://www.raspberrypi.org/documentation/installation/installing-images/README.md

Make sure Real-VNC is active and connect to your PC. Not only can you operate the Raspberry from your PC but as a bonus it is easy to copy files between the PC and Raspberry by the normal copy and paste commands.



When the Raspberry has booted start with opening a terminal window by clicking on its icon on the top-left of the raspberry window. That's where the arrow is pointing.

Now first make sure you are working in the home/pi directory by typing the following command:

CD ~

Next open the nano editor and make sure it starts a new file with the name radio3.py by giving the following command:

sudo nano radio3.py

When the nano editor has started copy the following program from this weblog and paste it into the editor.



#!/usr/bin/python3

import os
import sys
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.OUT)

SOUND = 'mpg123 http://icecast.omroep.nl/radio1-bb-mp3 >/dev/null 2>&1 &'
SOUND5 = 'mpg123 http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-einws >/dev/null 2>&1 &'

os.system('clear')
os.system(SOUND)

def station1(channel):
 os.system('sudo killall mpg123')
 os.system(SOUND)
 
def station2(channel):
 os.system('sudo killall mpg123')
 os.system(SOUND5) 
 
def stationq(channel):
 GPIO.output(18, GPIO.LOW)
 os.system('sudo killall mpg123')
 os.system('sudo shutdown now') 
 
GPIO.add_event_detect(20, GPIO.FALLING, callback=station1, bouncetime=300)
GPIO.add_event_detect(21, GPIO.FALLING, callback=station2, bouncetime=300)
GPIO.add_event_detect(3, GPIO.FALLING, callback=stationq, bouncetime=300)

while True:
 GPIO.output(18, GPIO.HIGH)
   


When everything is pasted in the editor close the Nano Editor with CTRL-x and make sure the file is saved by answering yes when asked and look if the right filename is used.

A closer look at the Internet Radio program

The import commands make sure the necessary libraries are present.

GPIO.setmode(GPIO.BCM)

This command makes sure that we can use the GPIO names instead of the pin numbers.
Next the GPIO's are defined as input with a pull-up resistor except GPIO18 on which the led is attached.

SOUND = 'mpg123 http://icecast.omroep.nl/radio1-bb-mp3 >/dev/null 2>&1 &'

The SOUND variables are filled with the links of the MP3 streams and the & at the end of the command makes sure that the MPG123 playes is opened in the background so that the radio3.py program keeps the focus and does not open a terminal window.

Next the screen is cleared and the radio station that is defined in the variable SOUND is activated. This way the station strats playing direct when the program is started.

The def station routines first make sure anything that MPG123 is playing is shutdown and then open the new choosen radio station defined in the accompanying SOUND variable.

The last def routine called stationq is activated when the button attached to GPIO3 is pressed. This will shut down the Raspberry Pi Zero. This is the ON/OFF button that has been described in this story:

GPIO.add_event_detect(20, GPIO.FALLING, callback=station1, bouncetime=300)

These commands test wether a button has been pushed, and incorporate a waiting time to stabilise button bouncing.

The last commands:

while True:
    GPIO.output(18, GPIO.HIGH)

Are an endless loop and make sure the program keeps running.

Expanding.

Just as easy as expanding the hardware with more buttons is it to expand the software.

Just add some SOUND variables. Add some GPIO.add_event_detect commands with the right GPIO numbers and attach them to some extra def station1(channel): routines. This should not give you any trouble.

Autostarting the program.

This is the last step. We have to make sure the Internet Radio program will start immediately after the Raspberry Pi has booted. If that would not be the case we would need a screen, mouse and keyboard to start the program and that is just the thing we want to avoid.

Before we make things permanent test if the program works as expected. Type the next command in the terminal window:

python3 radio3.py

The default radio station should be playing immediately. Pressing one of the pre-set buttons will change the radio station and pressing the ON/OFF button will shut the Raspberry Pi Zero down.

Re-booting the Pi is now easy as you just have to press the ON/OFF button. Remember what I said previously: that connecting GPIO3 shortly to ground will boot the Raspberry.

The Pi will re-boot but the radio3.py program will not start automatically. That is the last step we need to implement.

Open again the terminal window and make sure you are working in the /home/pi directory by typing the following command:

cd ~

Next step is to write the script. Open the Nano editor with:

sudo nano ~/.config/lxsession/LXDE-pi/autostart

The Nano editor opens and displays the autostart file which looks (in my case) like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi

Now alter the autostart file so that it looks like the next lines:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
python3 /home/pi/radio3.py
@xscreensaver -no-splash
@point-rpi


When done press CTRL-X to stop the editor and make sure you answer yes to the question wether the changes must be saved. Also like usual check if the right filename is used at saving: ~/.config/lxsession/LXDE-pi/autostart

Finished !!!!

You can now reboot your Raspberry Pi with the command:

sudo reboot now

Or reboot from the menu.

The Pi will shutdown / restart and when the GUI is started the radio3.py program is automatically started in the background. No terminal window will be opened and just the plain desktop is shown. If everything goes well the default radio station is playing. Sometimes that does not happen, or it just plays for a few moments and then stops. No problem just push one of the pre-set radio buttons and that station begins to play.

Pressing the ON/OFF button will shut down the Pi and pressing it again will re-boot the Pi and the Internet Radio program will instantly be working again.

Shutting down manually.

If you need to make alterartions to the program like adding radio stations or altering stations you can shut down the program manually even if it is running in the background.

Open the terminal window and type the following command

sudo killall python3 mpg123

This kills any running Python3 program and also kills any MPG123 stream.

Expanding further

Like said above it is easy to expand the hardware with more butoons and it is equally easy to expand the software to add more radio stations.

You can expand further.

- Add some MP3 music files to your SD card and dedicate a button to randomly playing songs. 
- Dedicate 2 buttons and call them next and previous to step through your music files.
- Let the internet Radio play songs from your NAS
- Alter the software so that it can not only play headless but that it can also be controlled in a terminal window

- Add an LCD screen or Oled screen that displays what's playing.
- Exchange the pushbuttons for a rotary switch

This way you could build a complete media system.


Casing

Off course you can build any casing to your liking even from cardboard. You could also check the flea markets and find some vintage radio in which you can build-in the Pi-Radio. 

Luckily for me I have a 3D printer so I designed a simple box and put my radio in it. 



Till next time. Have fun

Luc Volders

Friday, October 5, 2018

Eindhoven Maker Faire

For an index to all my stories click this text



Last weekend I visisted the Maker Faire in Eindhoven (Netherlands). For those who are not aware about what a Maker Faire is a short introduction.

Maker Faires are initiated by Make magazine. You probably may only use the name Maker Faire when you meet their conditions. These are probable the number of stands,  variety etc. So far for the legal mumbo jumbo. Besides this it really is great fun.

Maker faires are a place where all kinds of creative people meet and show their tinkerings, give workshops and demonstrations and have fun in general. The Eindhoven Maker Faire has grown from one to three halls and an outdoor happening.





At the entrance we were visited by some enormous scorpions and Robots.



As you can see the halls are big and there were a lot of visitors.



One of the stands that really impressed me was that of http://enablingthefuture.org/
This organisation which thrives on volunteers brings together tinkerers with a 3D printer and people that are disabled. What they do is design 3D printed hands and make them freely available to anyone who needs one.
So if someone (mostly children as the dutch health care does not help them) needs an artificial hand, or finger you visit the website and they will bring you in contact with someone who has a 3D printer and wants to help.
If you do have a 3D printer and want to help you are asked to print a sample for approval and when approved you will get in contact with someone who needs help.
As a printed hand or finger just costs a few euro's to make all is based on voluntarely base.
Go take a look at their website and do not be ashamed if you get emotional when you see the video's and read the stories there. Highly recommended. http://enablingthefuture.org/



Next stop. Model railway hobbyists.
Well it is not exactly my 'thing' but I was amazed at the amount of work these guys put into replicating buildings, sceneries and old trainstations. The building in the photo is a 3D printed replica of ancient Eindhoven station. It was printed and then painted. Just look at the quality !!



Then there was a stand from Inmoov. Inmoov is an open source project to build a robot. Loads of electronics involved and ages of 3D printing. The result is getting better and better.
The website4 can be found here: http://inmoov.fr/



Outside there were all kinds of crazy vehicles and contraptions. This one was a autonomous vehicle that drew all kinds of figures by dropping coloured sand.



And a gigantic horse.



And this took my particular attention. A stand of the Things Network. The Dutch Lora association. They demonstrated all kinds of Lora applications. For those in the blind. Lora is a LOng RAnge network system with frequencies that are open for everyone. So there are commercial networks and free networks like the Things network. You can send short messages over the network which can be accessed from anywhere. A bit like wifi but then for outdoors and over long distances. I will be doing some Lora projects myself so keep following this weblog.

Next to that there were 3D printers everywhere, workshops on Lora, programming etc, lectures about a new sattelite communication system for hobbyists, a hall dedicated to tinkering for kids with marble runs, building with carton, educational electronic projects, starting with programming etc etc etc. There were Micro:bits, DIY plastic recycling projects, simple robot building workshops, paper plane building, robots playing football, robots fighting eachother, robots, robots, build your own miniature car and race it against others. Just too much.

I arrived there at 10 o'clock just before opening and left at 5 o'clock just before closing time and had the idea that I still had not seen and tried everything I wanted to see and try. I really am sorry that I lacked time to attend sokme very interesting workshops....

If there ever is a maker faire which you can attend do yourself a favour and go there !!!

Find maker fairs in your country through this link: https://makerfaire.com/

Till next time
have fun.

Luc Volders