Tagged: rpi

Hakology Project Notes : Python, RedditAPI, Arduino, RaspberryPi thing.

Over the last three weeks I’ve been playing with various breakout boards, microcontrollers and embedded linux. I’ve been trying to take time to learn how the technology works and how best to combine them to teach and produce something useful, different and entertaining. So this project initially started out when I was playing with the RTLSDR dongle, I thought it might be cool to display the messages as they got demodulated on a separate device that was dedicated to grabbing POCSAG messages, I slapped it all together, then when my son was over the other weekend we decided to modify the code to grab the top 25 articles from a given subreddit.

Eventually when I get time ill put together a video tutorial on setting this all up but in the meantime here are the notes …

You will need:

1 x Raspberry Pi model B.
1 x Wifi dongle or ethernet cable.
1 x Arduino deumilinove.
1 x USB lead (for serial communication with the arduino)
1 x USB lead (to power the RPi)
1 x USB Charger
1 x LED Matrix 8×8 x4 (I used the horizontal style layout)

So how does it work?
Its pretty simple really, the raspberry pi runs a small python script that grabs the top 25 articles from a predefined subreddit using the api (most of reddits api can be accessed just by post fixing .json to a lot of their URLS the data structures are also well documented and available on github.) The script strips out any weird characters as the JSON is returned in utf-8 format. A string is built containing the articles rank, total amount of upvotes and the article title. If the string is over 300 characters it gets trimmed and “…” appended to the end. The script creates a serial connection to the arduino (the device is usually addressable using /dev/ttyUSB0) The string is then sent to the arduino over serial. When the arduino receives a new string it displays it to the LED matrix after having scrolled any current messages.

What’s the python code?

Install and configure raspbian in the normal way. Before running the script make sure you have internet access otherwise the python script will fail when it trys to communicate with reddit. (todo: add better error handling.)
Python code – http://pastebin.com/shXyry5C
Copy the code in to a file called reddit.py to your home directory.
Edit the file so it points to the correct serial device.
nano reddit.py
If you’re unsure about the arduino serial port run
sudo ls /dev
Look for something like …
ttyUSB0
Edit the line in reddit.py where the serial communication is initialised ensure it matches the port for the arduino.
ser = serial.Serial('/dev/ttyUSB0','57600')
Save the file using …
ctrl+x
Make sure the file has execute permissions by issuing the following command …
chmod +x reddit.py
You can test the script by running it from the command line using the following command (*this needs to be run with superuser privs to access the serial port) …
sudo python reddit.py
Once you’re happy with the way the script is running you can make it autoboot with the pi by using the following command …
sudo crontab -e
If you are prompted which editor to use and are unsure use nano (option 2)
At the bottom of the file add the following line …
@reboot /local/bin/python /home/user/reddit.py
Save the file by pressing ctrl+x
Reboot and test your script is autoloading.

To display the messages on the matrix you’ll need to flash your arduino with a little code …

Wiring the arduino …
To connect the arduino to the matrix you will need to ensure you have the correct SPI pins as they differ between arduino boards the ones on the deumilinove are pins A4 and A5, A4 SDA and A5 SCL, you will also need to attach the CS (chip select) pin to pin number 13 on the arduino and also connect VCC to 5v and GND to GND on the arduino.

A4 to SDA
A5 to SCL
13 to CS
5v to VCC
GND to GND

(todo : will post a pic when i get time)

What’s the arduino code?
Arduino code – http://pastebin.com/pvkM2DUK
Copy and paste the arduino code in to your arduino IDE, you’ll also need to grab the md_max29xx library from git hub. (Download the zip file and unzip the files to your arduino IDE library folder usually /Documents/arduino-x.x.xx/libraries) This library is used to drive the LED matrix. Once you’ve installed the library you’ll have to specify how many matrices you are using in the arduino code. (How many LED matrix panels you will be displaying to my board had 4 8×8 LED panels.) Here’s the important bit though depending on what/where/who you bought your led matrix board from you might find you’ll have to edit way the arduino draws to the matrix otherwise you could be in for some very interesting results. This can be done in the md_max29xx lib folder. There are a few variables that define the matrix rotation and draw direction IE. right to left, left to right, top to bottom etc. You will need to play with these variables until you get the right combination for your matrix.

NB. When initially getting this running use the example grapicstest to make sure the leds are drawing as expected then flash the arduino reddit serial code once you have the library configured correctly.

Now all the hard work’s done
Once you’ve installed the library, edited the arduino code, updated the variables for the draw direction and successfully flashed the code, plug the arduino in to the raspberry pi and boot. There should be enough of a delay in the script to ensure the RPI has connected to your network before it makes its initial request.

If you have the arduino setup correctly you should see scrolling text on the matrix. “Waiting for serial data!” after a short while the rpi should connect and start displaying articles from reddit.

Concluding
Still yet to build an enclosure or decided how / where im going to use this yet but i’ve had fun making it. Initially getting the libraries setup and running correctly was by far the most frustrating part of this project writing / editing the code was pretty straight forward. There’s plenty of scope for improvement / modification too, if I get time I was thinking I’d get it to cycle multiple subreddits, but for the time being I’ve spent enough hours on this project.

NB. The original code running on the arduino was limited to 25 characters I’ve modified this to allow for messages up to 300 characters in length (the size of an article title from reddit) Since doing so some of the serial messages appear to glitch intermittently. I’ve been checking the code for errors but haven’t managed to pinpoint the problem yet. As far as I can tell the python code runs fine but there might be a problem when the arduino is receiving the serial messages. Possible solutions maybe change the baud rate and send the data slower or add a checksum. (*As suggested by advancednewbie) Example code an library for implementing a checksum. https://github.com/AdvancedNewbie/serialChecksum

IF YOU HAVE ANY QUESTIONS REGARDING THIS PROJECT PLEASE PUT THEM IN THE COMMENTS ON THE YOUTUBE VBLOG EPISODE AT THE TOP OF THIS POST.

Hakology Day 45 : Blog Entry

[1523]
Woke up very late today but respectively I had a very late night last night, tweaking the rpi/reddit/python/arduino project. Im having issues with serial communication somewhere there’s a few bits / bytes being lost? It very irregular and after a little bit of debugging I think its either the serial message get out of sync and missed part of the message or the arduino is rejecting certain characters. The reddit API JSON comes utf-8 encoded and needs converting to ASCII before I send it to the arduino, I’m processing the string to remove and replace irregular characters, I’m going to try changing the baud rate later see if this has an impact.

RE. Wardriving idk if it was where I had the GPS dongle positioned in the car but the GPS co-ords I converted to KML where ALL OVER THE PLACE so maybe its down to kismet and the –use-nmea-gps option this is the only thing I’ve really changed since I last went out. (Regardless of GPS I still managed to get some good data.)

The LED matrix project is looking very slick now, apart from the serial message issues I’m pretty darn happy with it.

Managed to get my keyboard in the post this morning on its way back to the manufacturer for the second time, think I’ve mentioned this before but, really hope they get it right this time (I’ve even requested they send me a different model this time round.) *fingers crossed*
[/1523]

[1813]
Chores done thinking about doing a vblog assembling a small FM transmitter I purchased from ebay. Will try and get the next main hakology video done first though.
[/1813]

[1958]
Im only rendering the sdr# video! … still dont have the perfect solution for screen capture but its done now … the audio is terrible near the start next time will record on another device as noise supression appears to be playing hell with my laptop capture, meh, its done now time to move on and not make that mistake in future.
[/1958]

[2105]

[/2105]

[2324]
Todays VBLOG I made an FM transmitter from a small kit I purchased on ebay. Just rendering the video and preparing to upload it. This by far was the most enjoyable vblog I’ve done so far may be more to come like it.
[/2324]

[0043]

[/0043]

Hakology Day 37 : Blog Entry

[1543]
Good afternoon hakologists, I’ve finally got the LED matrix ‘working’ I’ve managed to find one library that draws correctly to the screen, with a little bit of effort I think I could rewrite it for multiple matrices. Today I’m going to get a quick vblog together then later tonight hopefully record the SDR# video. Then we’ll take a look at multimon-ng and demodulating pager messages.

Need to send a massive shout out to gir, for sending hakology 2 RPi Zeros from the US as I couldnt get any at a reasonable price anywhere in the UK. Ridiculous considering they are made less than 50/60 miles from my house.
[/1543]

[0015]
Family stuff over, vblog recorded, introduction video for vblog channel recorded, vblog rendering, time to get the kettle on and make a brew, thinking I might have a look at an old netgear router I have, see if I can connect using those new USB2UART boards. If I’m feeling really productive I might even get the sdr# video done.
[/0015]

[0133]

[/0133]

Hakology – Retropie HDMI audio fix

No audio over HDMI on retropie? No problem, here’s your solution i’ve seen a few fixes on the web lots varied some were overly complicated so I decided to write up a really simple fix. Tested and working on the RPi3MB.

Boot retropie
Go to the retro pie menu
Go to the audio menu and press ‘A’
Ensure HDMI is selected, also check the mixer volume is at 100%
Save settings and return to the retropie menu
Quit emulation station
Run …
sudo nano /boot/config.txt

uncomment the following …
#hdmi_drive=2
so it looks like this …
hdmi_drive=2

also uncomment …
#hdmi_force_hotplug=1
so it looks like this …
hdmi_force_hotplug=1

Press ctrl+x to save the file.
now run …
amixer cset numid=3 2
and ..
sudo reboot

Hopefully you should now have audio over HDMI

Hakology Day 11 : Blog Entry

[1712]
Just doing some RL stuff will be on this evening. SD card for RPi3 has arrived and whiteboard pens. Multimon-ng running on linux logging data. 🙂
[/1712]

[1904]
Munching food just about to get down to business.
[/1904]

[1909]
TODO:
-SDR# notes / audio / video
-POCSAG in PDW
-FLEX in multimon-ng
-Test RPi3 (with retropie of course)
-Check corsair ticket.
-VBLOG
-Audio working on RPi3 HDMI
[/1909]

[1920]
Whoot! looks like corsair have updated the ticket. Awaiting RMA confirmation swapping this brown mx for a new spangly red mx keyboard hopefully. That would be most awesome.
[/1920]

[2014]
Distracted by the internet again! … also reluctant to stop logging POCSAG messages … but I need the dongle.
[/2014]

[2102]
Rendering vblog
[/2102]

[2116]
Looking at demodulating FLEX using multimon-ng but IDK if I have the latest version compiled. Time for some updates.
[/2116]

[2124]
RPi3B vs RPiB there is no comparison, first impressions very impressed!
[/2124]

[2148]
RPi3 still updating & uploading vblog.
[/2148]

[2206]

[/2206]

[2224]
Rebuilt and installed latest version of multimon-ng. Now have FLEX messages demodulating whoot!
[/2224]

[2325]
Audio working on RPi3 Whoot!!! 😀
[/2325]

[0034]
Ready to record SDR# tutorial tomorrow.
On mumble with luc, hexon and gir.
Signing off for today, its been fun. 🙂
[/0035]

[0147]
Worth an update thank you gir for offering to send a RPi zero for hakology to have a play with … can’t wait 😀
[/0147]

[0234]
Thanks hexon been after this link for a long time … http://playdosgamesonline.com
Ran by the creator of textfiles.com
[/0234]

Hakology – Retropie steam controller setup guide

Notes to accompany the following video showing you how to setup and configure the steam controller for retropie. Tested and verified as working with python 2.7 on the rpi model B and rpi3 model B.

sudo apt-get install python-pip
sudo pip install libusb1
sudo pip install enum34
git clone https://github.com/ynsta/steamcontroller.git
cd steamcontroller
sudo python setup.py install
sudo nano /etc/udev/rules.d/99-steam-controller.rules
Add these lines :
SUBSYSTEM==”usb”, ATTRS{idVendor}==”28de”, GROUP=”games”, MODE=”0660″
KERNEL==”uinput”, MODE=”0660″, GROUP=”games”, OPTIONS+=”static_node=uinput”

sudo udevadm control –reload
sudo crontab -e
@reboot /usr/local/bin/sc-xbox.py start

Hakology Day 6 : Blog Entry

[15.31]
Really late night last night, time to get the kettle on and wake up. Need to do some shopping then I think ill look at re-recording the steam controller tutorial without the animations and just do a straight forward install video, I’m thinking that’s the best plan of action. Wasted too much time on this video and want to get busy with SDR and some other bits and bobs.
[/15.31]

[16.40]
RPI3 arrived whoot

[/16.40]

[20.20]

More info on RPI hardware and chips
[/20.20]

[20.31]
CHARGE EVERYTHING!!!!
[/20.31]

[21.06]
SSH sorted to the pi for screen cap. (Thanks for the tip luc)
[/21.06]

[00.26]
Rendering test footage.
[/00.26]

[00.56]
Rendering the actual footage, get in there! Hopefully uploading soon. Then time for a little relaxation as I need to be up early tomorrow.
[/00.56]

[1.20]
Uploading hakology video and rendering VBLOG.
[/1.20]

[1.40]
Uploading zee files.
[/1.40]

[2.44]
Retropie steam controller installation tutorial. Guide, blog post and script coming soon.

[/2.44]

[2.46]
Day six vblog.

[/2.46]

Hakology Day 4 : Blog Entry

[15.13]
Chores done and I’m online. Going to get busy, goals for today, get steam controller working on retro pie, re-record retropie audio, and re-render using better stock footage including steps to configuring. Then if I have any time get back on the SDR work.
[/15.13]


[16.21]
Spent as much time working on retropie as I want for now. Just uploading WIP on the hakology VBLOG channel. Tea, food & SDRs.
[/16.21]


[16.32]

[/16.32]

[16.45]
Background noise.

[/16.45]


[17.57]
Steam controller working in retropie … whoot.
[/17.57]


[19.23]
Written an automated installation script. Finding bash in raspbian has some unusual defult settings regarding echo. If any one knows how to get echoes to display …
[/19.23]


[20.05]
Testing and debugging script. Being a noob. Making silly mistakes. Correcting them. NTS: tput setaf 1; is a great way to set echo output in bash 🙂
[/20.05]


[20.10]
Finished …. taking a break. – Managed to brick emulationstation in the process, now have to recover script and reimage.
[/20.10]


[20.24]
Turns out if you boot the pi with a cronjob for the steam pad drivers it’ll crash emulationstation if you dont have the dongle inserted.
[/20.24]


[21.01]

#!/bin/bash
echo This script is designed intended for retro pie running raspbian and python 2.7
echo This script will now install the drivers and files required to use the steam controller with retropie
echo Ensure your pi has an internet connection prior to running.
echo Moving to home directory
cd /home/pi
echo Installing dependancies Python package manager and git
sudo apt-get install python-pip git
echo Installing – libusb1
sudo pip install libusb1
echo Installing – enum34
sudo pip install enum34
echo Cloning steam controller files.
git clone https://github.com/ynsta/steamcontroller.git
echo Moving to steam controller files directory
cd steamcontroller
echo Intsalling steam controller files
sudo python setup.py install
echo Setting up udev rules
echo SUBSYSTEM==\”usb\”, ATTRS{idVendor}==\”28de\”, GROUP=\”games\”, MODE=\”0660\” >> /etc/udev/rules.d/99-steam-controller.rules
echo KERNEL==\”uinput\”, MODE=\”0660\”, GROUP=\”games\”, OPTIONS+=\”static_node=uinput\” >> /etc/udev/rules.d/99-steam-controller.rules
echo Reloading udev
sudo udevadm control –reload
tput setaf 1;echo All operations complete
echo PLEASE READ THE FOLLOWING
echo
echo Update crontab …
echo crontab -e
echo Add the following to crontab …
echo @reboot /usr/local/bin/sc-xbox.py start
echo Ensure the steam usb dongle is inserted when you reboot otherwise emulationstation might not load.
echo
echo If you need to run the script more than once besure to clean the udev rules found in:
echo /etc/udev/rules.d/99-steam-controller.rules
Will post a better a link to the file later when i’ve finished tinkering.

[/21.01]


[21.22]
Random pic for the day, great tool all round esp when it comes to cabling new pc builds or finding that tiny screw you just dropped on the floor, cree LED with recharagable 18650s and adjustable focus.

[/21.22]


[23.49]
Final retropie video posted to the hakology main youtube channel.
http://youtube.com/hakology

[/23.49]

[00.33]

[/00.33]

[4.16]
Signing off.
[/4.16]