Showing posts with label github. Show all posts
Showing posts with label github. Show all posts

Wednesday, May 10, 2017

Raspberry Pi 3 with Google AIY VoiceHat

I was lucky enough to get a copy of Issue 57 of The MagPi with the VoiceHat from Google on the cover.

This is a great bit of kit with Raspberry Pi Hat, speaker, microphone, and a cardboard box to put it in.
For a bit of fun I fitted it into a cardboard R2D2 that I had from before Christmas.

This is not the droid you're looking for
After doing the usual of asking it to tell a joke and give me the weather and details of what's nearby I was wondering what I could use the VoiceHat for.

We have an Amazon Dot and it's main purpose in the house is playing radio station.  I tried this out and nothing played.  Seems streaming radio support isn't built in.

Then keeping up with what others were doing Mike Redrobe posted on the Raspberry Pi forum that he had it playing YouTube audio. https://www.raspberrypi.org/forums/viewtopic.php?f=114&t=182665

With a bit of assistance I got this working.  If you're careful with the name of the video you can get it to play fairly much any song.  My kids loved this.

But I still wanted to play streaming radio.  You know, one command and it's off rather than needing to keep telling it new songs to play.

In a later post Mike mentions using VLC as the player and armed with his example I began looking to see if I could get streaming radio to work.

I did. Yeah! Then I went on the trail to add playing podcasts.  Again a success.
You need to install VLC

sudo apt update
sudo apt upgrade
sudo apt install vlc

Radio Stations supported are:
Absolute Radio
Absolute 80s
Absolute 90s
Absolute 00s
Eagle Radio
BBC Radio 1
BBC Radio 2
BBC Radio 3
BBC Radio 4
Capital FM


Podcasts Supported are:
Good Job Brain
No Such Thing As a Fish
Freakonomics


Here is a little video of it working.  I recorded it in Portrait as the cardboard R2D2 it is in is portrait shared so if viewing on a phone will fill the screen, while of viewing on a computer it will have the side bars which is probably better than seeing the messy table.




The action.py file that's needed with the commands built in is available on GitHub
https://github.com/winkleink/RPi_AIY


Other useful bits and pieces

Knowing what Google is actually hearing

While setting up the radio stations Absolute 90s was interpreted by Google as Absolute 90s, but Absolute 00s was interpreted as absolute noughties
It took me a while to figure this out and command that helped me was:

sudo journalctl -u voice-recognizer -n 20 -f

This shows what's happening when you issue a command,
Here's a small snippet showing what the voice command is converted to.
Absolute 00s or is that Absolute Noughties

Make sure to stop and start the service after each change to action.py

sudo systemctl stop voice-recognizer && sudo systemctl start voice-recognizer

Want to add more radio stations
This website has a list of streaming radio stations and their urls.  This is the UK list.

Stopping the playback

Since I'm using the button to stop the playback based on Mike Redrobe's code I decided to do the GPIO setup at the top so it is shared by all the commands rather than having it in each command.

The code is a while loop that keeps going until the button is pressed and then a kill statement to stop the process.  To be truthful I'm not sue how the kill bit works, I just know it does, so thank you to Mike for sharing.

while gpio.input(23):
    time.sleep(1)


pkill = subprocess.Popen(["/usr/bin/pkill","vlc"],stdin=subprocess.PIPE)
p.kill()


Doing the podcasts
Getting the most recent podcast to play is a bit more hacky than the radio stream as the radio stream is always the same while each podcast episode has a different url.  To achieve this I first had to find the relevant rss feed and then parse the file to find the start and end of the url to the mp3.  audioboom.com podcasts aren't too bad, but the Freakonomics one based off the feedburner rss was a little more complex as the url to the mp3 is actually a redirect url and they don't work.

https://www.podtrac.com/pts/redirect.mp3/audio.wnyc.org/freakonomics_podcast/freakonomics_podcast050317.mp3

You can see in the middle there is redirect.mp3 and then what looks like a path to a different website audio.wnyc.org
This meant the parser had to first find "/redirect.mp3/" and then from there find "mp3" to get the main bit for the audio.wnyc.org link.
Finally it needed to add back in "http://"
You can see how this is done in the code below.

url = 'http://feeds.feedburner.com/freakonomicsradio?format=xml' response = urllib.request.urlopen(url) data = response.read() # a `bytes` object text = data.decode('utf-8') startmp3 = text.find('/redirect.mp3/')+14 endmp3 = text.find('.mp3',startmp3+16)+4 if startmp3 > 0: command = "http://"+text[startmp3:endmp3]


Have a play and see what you can get the Raspberry Pi AIY to do.
Just be sure to backup any files you modify before changing them.
I didn't backup my action.py when I started playing but luckily google has made the original files available on GitHub https://github.com/google/aiyprojects-raspbian

I'm really looking forward to seeing how this grows and expands.

Just saw that https://twitter.com/k_tinkerer has been doing some great things with the AIY as well.  Worth checking out if you want to do more.

http://ktinkerer.co.uk/

I'll definitely be adding the shutdown and reboot commands from here.


Thursday, February 16, 2017

Making Games with Scratch and Sense Hat on Raspberry Pi


I'm a big fan of the Raspberry Pi Sense Hat and Scratch for doing interesting things and learning to code.
Previously I made a Sense Hat Cave Game using Python where you tilt the Sense Hat to move around as well as a Crane controller programmed in Scratch.
This time the goal was to create game that uses the Sense Hat as the display while programmed in Scratch.  The best of both world.  The goal for me was to lean to control the pixels a bit better in Scratch.

The result is Collect the Dots a simple game where you're a blue dot collecting red dots.  It's kind of like snake without the tail.



You control your dot using either the small joystick on the Sense Hat or use the keyboard on the computer.  Press Down to start the game.  You'll see in the video it tells you this on the Sense Hat, but if no Sense hat is attached and you're running on screen there is no prompt.


As can be seen in the video the on screen display mirrors on the Sense Hat dots, so either can be used.
Strangely even though the screen display is bigger I found it easier to play if I only looked at the Sense Hat display.
The game can also be played without the Sense Hat. Uploaded to Scratch website https://scratch.mit.edu/projects/144708660/

Embedded from the Scratch Website



You can download the code from GitHub

TIP: As the wonderful Cat Lamin points out in her blog post about using doing an animation on the Sense Hat using Scratch you have to run the code once to enable the sensors and capability of the Sense Hat.  So, the first time you run the program on a Raspberry Pi with a Sense Hat it may not work correctly.  Just stop it and start again and all will be perfect

A thought for an enhancement would be to control your dot by tilting the Sense Hat rather than using the joystick or keys.

Wednesday, June 22, 2016

Spider Chart maker using Python and Pygame

16th May 2017 - Updated to make the output an A4 landscape page.
Also, provided Inkscape created SVG file so you can prepare you can add your own School or Club name.  Make sure the final PNG resolution for __spider.png is 3508 x 2481 as the dot locations are hand coded for the spider to be in the location it is on this size of image.

Example of improved chart.



A few days ago a call went on out on Twitter for how to make lots of Spider Charts, also known as Radar charts in a programmatic way.  I decided to take up the challenge.


The code and associated files are available to download from GitHub

Below is a sample completed chart

To do this I started with a blank version of the chart and added the person name top left and the red dots and lines for the individual scores.

The program is written using Python and Pygame and was developed on the Raspberry Pi.
Since it doesn't use any other external libraries I'd expect it to work on other systems with Pygame installed.

To achieve this 2 external CSV files and a large (3508 x 2481) blank chart image are used.

The image file is: __spider.png

spider_data_points.csv is a comma separated variable list of the co-ordinates on the 3508 x 2481 image for the individual points.  By using a separate lookup if this was to be adapted to a different chart then you can just use a new master image and a different set of co-ordinates.

Student_Scores.csv is the file with the individual student (in this case) names and scores for the different skills.  There is no limit to the number of students that can have charts created as the code just goes through the csv line by line and creates a chart for each one.

Rather than creating the csv files manually it is so much easier to use a spreadsheet for data entry and then [Save As] a .csv
To facilitate this I've include .xlsx versions of the files.
These can be open and saved on the Raspberry Pi using OpenOffice.

Meaning if you needed to make 100 of these charts it would be a simple matter of:
Place all the code in a single directory
Enter the data in the spreadsheet.
Save as a .csv
execute spiderchart.py
Watch as all the charts are created automatically with the student name as the filename

Don't worry if the image is mainly off screen it is still being done correctly.

In the code there is a 1 second sleep after each chart is created. If you are doing lots then it might be worth commenting out the sleep.  It's useful for seeing what's going on.

As a complete side note: I was one of the backers of the Indiegogo pi-topCEED an All-in-One form factor for a Raspberry Pi desktop and it was delivered and set up on Monday.  It came with a Raspberry Pi 3.  This was the set up I used to develop and run the code.  I was very impressed by the quality of the 14" screen on the CEED.  This is now set up as the permanent home computer that my kids can use for playing Minecraft and exploring the CEEDuniverse, a game that teaches you about computers, coding and electronics.

Tuesday, March 29, 2016

Testing Gamepad on Raspberry Pi using Pygame

I bought a really small Bluetooth gamepad/media remote on eBay and needed a small program to test the gamepad buttons on the Raspberry Pi 3 once I set up Bluetooth.

So I created a short pyton program using Pygame.
It's available to donwload from GitHub.
https://github.com/winkleink/pygame_test_gamepad