First steps with the Adafruit Trinket

I recently picked up a Adafruit Trinket (3.3v), simply because they’re so cheap (about $8).  I like the idea of a tiny small Arduino-ish board.  Since I’m forgetful, below documents the overall process I got to get it working.

End result:  Franken-servo!

End result: Franken-servo!

Resource Links:

First Steps:

For all the documentation on the web, I really wasn’t sure where to start.  There’s all this talk of installing AVRDUDE, etc, but as it turns out it’s really not needed (if using the Arduino IDE). Here’s the streamline approach I finally took:

  • Setup my pre-existing Arduino IDE for use with the Trinket, following these Adafruit docs.
  • Since I’m on a Mac, I didn’t have to worry about installing any drivers, but the “Mac Arduino IDE” app download they provided didn’t work.  Mac said it was “corrupted”.  So instead I had to use the “Slow Way” example they gave.  And other than a few path differences it worked just fine.

Programming:

  • I was able to successfully run all their examples from the page “Programming with Arduino IDE“.
  • Earlier in the day I had done some servo programming on an Arduino, and I wanted to emulate it on the Trinket.  Thanks to the “Trinket Servo Control” tutorial, and their “Adafruit SoftServo” library, I was able to make it happen.  However, I was unable to create two separate servo objects:  I think I don’t quite grasp the Trinket pinouts.  The above image has two servo’s hooked up to the same pin, so they move the exact same way.

So a successfully first attempt.  Next up, I really need some batteries to make a standalone project with it…

Scrolling the Adafruit 16×2 LCD+Keypad

I wanted to add additional functionality to my Raspberry FM project by having any long station names \ song names scroll on the Adafruit 16×2 LCD+keypad for Raspberry Pi: While the lcd Python module Adafruit provides has methods to scroll the text (scrollDisplayLeft, scrollDisplayRight, autoScroll), I was unable to get them to work nor find any good examples.  Maybe it’s completely possible with what they provide, but I had no luck with it.  If anyone does know how, please comment! :)

Why not write my own?  That’s a fun thing to do on a Saturday afternoon, right? 😉  Find a snapshot of the Python source below, and a link to the most current version on Bitbucket here: lcdScroll.py

To see it in action:

It’s a standalone module I designed to work with any sized lcd:  It’s really just a text formatter:  All the drawing to the LCD would be handled by some other application.  A very simple example can also be found on Bitbucket here: lcdScrollTest.py Or of course you could check out the Raspberry FM source here:  raspberryFm01.py

For either the top or bottom line, if they are longer than 16 characters (which is completely adjustable based on the type of lcd used), they will auto-scroll.  If less than 16 characters, no scrolling happens.  So you can have completely independent scrolling on any line based on their length.

#!/usr/bin/python
"""
lcdScroll.py
Author             :  Eric Pavey 
Creation Date      :  2014-02-08
Blog               :  http://www.akeric.com/blog

Free and open for all to use.  But put credit where credit is due.

OVERVIEW:-----------------------------------------------------------------------
Create scrolling text on a LCD display.  Designed to work on the the 
Adafruit LCD  + keypad, but it's not tied to any specific hardware, and should
work on a LCD of any size.

See lcdScrollTest.py for simple example usage.
"""

class Scroller(object):
    """
    Object designed to auto-scroll text on a LCD screen.  Every time the scroll()
    method is called to, it will scroll the text from right to left by one character
    on any line that is greater than the provided with.
    If the lines ever need to be reset \ updated, call to the setLines() method.
    """
    def __init__(self, lines=[], space = " :: ", width=16, height=2):
        """
        Instance a LCD scroller object.

        Parameters:
        lines : list / string : Default empty list : If a list is passed in, each 
            entry in the list is a  string that should be displayed on the LCD, 
            one line after the next.  If a string, it will be split by any embedded 
            linefeed \n characers into a list of multiple lines . 
            Ultimately, the number of entries in this list must be equal to or 
            less than the height argument.
        space : string : Default " :: " : If a given line is longer than the width
            argument, this string will be added to the end to help designate the
            end of the line has been hit during the scroll.
        width : int : Default 16 : The width of the LCD display, number of columns.
        height : int : Default 2 : the height of the LCD, number of rows.
        """
        self.width = width
        self.height = height
        self.space = space
        self.setLines(lines)

    def setLines(self, lines):
        """
        Set (for the first time) or reset (at any time) the lines to display.
        Sets self.lines

        Parameters:
        lines : list : Each entry in the list is a string
            that should be displayed on the LCD, one line after the next.  The 
            number of entries in this list must be equal to or less than the 
            height argument.
        """
        # Just in case a string is passed in, turn it into a list, and split
        # by any linefeed chars:
        if isinstance(lines, basestring):   
            lines = lines.split("\n")
        elif not isinstance(lines, list):
            raise Exception("Argument passed to lines parameter must be list, instead got: %s"%type(lines))
        if len(lines) > self.height:
            raise Exception("Have more lines to display (%s) than you have lcd rows (%s)"%(len(lines), height))            
        self.lines = lines
        # If the line is over the width, add in the extra spaces to help separate
        # the scroll:
        for i,ln in enumerate(self.lines[:]):
            if len(ln) > self.width:
                self.lines[i] = "%s%s"%(ln,self.space)

    def scroll(self):
        """
        Scroll the text by one character from right to left each time this is
        called to.

        Return : string : The message to display to the LCD.  Each line is separated
            by the \n (linefeed) character that the Adafruit LCD expects.  Each line
            will also be clipped to self.width, so as to not confuse the LCD when
            later drawn.
        """
        for i,ln in enumerate(self.lines[:]):
            if len(ln) > 16:
                shift = "%s%s"%(ln[1:], ln[0])
                self.lines[i] = shift
        truncated = [ln[:self.width] for ln in self.lines]
        return "\n".join(truncated)

Raspberry FM Part 2, the sequal

Nearly a year ago I wrapped up a project I called “Raspberry FM“:  A Raspberry Pi based internet radio streamer coupled with a MaKey MaKey as the interface.   Why the name Raspberry FM?  It’s a mashup of a Raspberry Pi, and my favorite internet radio station, Soma FM.   It worked, but had a few problems:

  • There was no visual feedback, the MaKey MaKey was purely an input.
  • I was having a hard time getting Python to interface with with MPlayer (probably due to my own ignorance), the audio player I had chosen, so everything was done via bash.

Fast forward nearly a year:  After learning about the Mpd audio player/server, and (one of) its clients Mpc, it re-piqued my interest in programming a Python app to play music on the Pi.  Second time around, it all turned out really well:  New software combined with new hardware and a 3d-printed case turned it into a good looking compact unit.

Overview of the whole process below (pic on top, video on bottom).  During the development I decided to write my own Python music player around mpc & Adafruit’s LCD library.  At this point there are several others online, but I enjoyed coding it from scratch.

adaLcdCase

Raspberry FM Features:

  • Auto-on with the Pi
  • If no internet is present or it drops out, app will go into holding pattern until it returns.
  • Can change between any number of stations. (left\right buttons).
  • Stations are easy to add via the commandline:  No need to update Python source: SSH into the Pi, and add\remove what you need.
  • Increase\decrease volume (up\down buttons).
  • Station and song info is displayed and auto-scrolls.
  • Shutdown Pi  (select+left) or turn off program (for debugging, select+right)
  • Lots of color changing when buttons are pressed!

Hardware needed:

  • Raspberry Pi (I used a B model)
  • Adafruit RGB 16×2 LCD+Keypad Kit : Solder and install!
  • Optional:  Custom 3D printed case I designed (well, I designed the top part), download on Thingiverse.  Print & install!  Took me about an 1:20 on my Makerbot Replicator (1).
  • I stream the internet radio over cat5, but I’ve also had success with wifi.
  • I use the headphone jack for audio out.

Software needed:

  • This was all done on the Raspbian distro via NOOBS.
  • My “Raspberry FM” Python program.  Find on Bitbucket.
  • You’ll need Adafruits CharLCDPlate library.
  • FYI, I coded this all via the Adafruit WebIDE, I’d recommend anyone else to use it as well to help manage the various Python modules on the Pi.
  • MPD & MPC:  sudo apt-get install mpc mpd

Steps:

  • I presume you already have your Pi setup.  If not, see my notes here on the general steps needed to get a Pi live and kicking.
  • Setup Pi to auto-login.  See notes here.
  • Download the Raspberry FM Python program to a folder of your choosing.  Since I coded this via the WebIDE, both the creation of my code and the integration of the Adafruit LCD modules was all handled via the WebIDE.  Make sure you download all the Adafruit CharLCDPlate modules as well and put them in the same directory.
  • Install MPD & MPC.
  • Add stations to MPC.  This is super easy on the commandline.  May I recommend anything from SomaFM?
    mpc add http://ice.somafm.com/groovesalad
  • Setup Pi to auto-run a program on start.  See notes here.  You will point that script to wherever you saved the Raspberry FM Python script.  For example, my startup.sh script looks like:
  • #!/bin/bash
    echo startup.sh : Launching raspberryFm.py
    sudo python /usr/share/adafruit/webide/repositories/my-pi-projects/Adafruit_CharLCDPlate/raspberryFm01.py
  • Restart the Pi and listen to the music!

The final result in action:

Playing with the Raspberry Pi’s camera

I recently picked up a camera module for the Raspberry Pi from Adafruit.  This post will serve to be notes to myself on how to use the dang thing.  There’s a lot of info on the web, so I’m going to collect what’s applicable to myself here.  This post will continue to evolve over time.

Note, I’m using a Macbook Air, so all software and commands are centric to OSX (10.8.5).

First off, you need some sort of stand for it.  I made one that should survive the Zombie Apocalypse out of some MicroRAX:

SAMSUNG

First Time Setup:

Based on the latest installation of Rasberian via NOOBS, the hardware installed easily, and was auto-detected by the Pi.  First time setup can be found here on raspberrypi.org.

Documentation:

Official documentation can be downloaded off of Github.  The first time setup above covers many basics.

Camera forum can be found here.

Capturing and Viewing:

Important note:  You can’t view anything over VNC, and obviously you can’t do it via a ssh terminal.  This post explains the reasons behind it.  This is however, a bummer:  To run any of the “demo” code, you need to be viewing the Pi directly over hdmi.

Super Simple Commands:

You’ll find these on all the sites:

$ raspistill -o image.jpg
$ raspivid -o video.h264
$ raspivid -o video.h264 -t 10000

1 : Capture an image
2 : Capture a (5 second, default) video, at 1920×1080 (1080p)
3 : Capture a 10 second video (in milliseconds)

Viewing a video stream from the Pi on your Mac:

Need to instal mplayer on the Mac so you can access it from the command-line.  The best luck I had was to install it via macports.

$ sudo port selfupdate
$ sudo port install mplayer

Now, thanks to a post by spudnix from this thread, here’s how you can stream video from the Pi to your Mac:

Mac shell code to start netcat listening to port 5001, and piping that to mplayer:

$ nc -l 5001 | mplayer -fps 31 -cache 1024 -

Pi shell code to start streaming vid and pipe it to netcat on port 5001, shooting it to the mac’s local ip:

$ raspivid -t 999999 -o - | nc 192.168.2.15 5001

There’s a few second lag, but it worked right away.  My guess is the lag is because of the full 1080p signal being broadcast.  Dropping the resolution down had some lag at first, then caught up after a few minutes, odd:

$ raspivid -t 999999 -o -w 640 -h 480 - | nc 192.168.2.15 5001

Record raw video, convert to mp4, play:

The h264 video the camera records is “raw”.  To make it easily viewable by the Pi or Mac (or other PC’s) it needs to be converted.  Thanks to this post, here’s how you can do it:

First, you need to install gpac on the Pi, then run MP4Box (part of that install) to do the convert:

$ sudo apt-get update
$ sudo apt-get install -y gpac
$ MP4Box -fps 30 -add myvid.h264 myvid.mp4

To play video on the Pi, you need omxviewer.  I think it may come installed iwth NOOBS now(?), but if not:

$ sudo apt-get install omxplayer

Then play in a window (again, this doesn’t work over VNC, need to be on a monitor connected to the Pi) or to the HDMI port:

$ omxplayer myvid.mp4
$ omxplayer -p -o hdmi myvid.mp4

Copy data from the Pi to the Mac:

Once you record a nice video, how do you get it to your PC?  Presuming you have a ssh connection, execute this from a shell on your Mac, to copy the data from the Pi, to the Mac:

$ scp user_name@the_pi_ip:/path/to/source/file/on/pi /path/to/destination/file/on/pc

For example:

$ scp pi@192.168.2.27:~/piVid.mp4 ~/macVid.mp4

Broadcast video to the Internet:

Using VLC

Thanks to this post, I was able to get the Pi cam streaming to the web, and viewable on my Mac (via the “middle” option they described).  It wasn’t entirely straight forward though, so these are the steps I went though:

Install VLC:

On the Pi, it’s easy:

$ sudo apt-get install vlc

On the Mac, at first, I tried to install it via Macports like so:

$ port install vlc

It took forever.  And while vlc was then available at the command line, it kept giving me missing plugin errors.  I found a App for it in my downloads folder (which I moved to Applications) and tried via the gui to “File -> Open Network…” : But it wouldn’t recognize the stream from my Pi (info below).  Soooo, I went to the official download page here, installed the App that way, and it started working!

Port Forward the Router:

I accessed my routers web page 192.168.x.x and via the “virtual servers” option, opened up port 8554 for outside listening.  I’m sure this process can vary widely per router.

Stream from the Pi:

After ssh’ing into the Pi, I executed this to start the video stream (note I knocked down the resolution from the default 1080p):

$ raspivid -w 640 -h 480 -o - -t 9999999 |cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8554}' :demux=h264

View via VLC:

I accessed “What’s My Ip” to find the external IP of my router.

Launching VLC, I accessed “File -> Open Network…”, and entered:

http://<ip of my router>:8554

And hit “open” :  Next popped up a (delayed by about 5 seconds) stream from my Pi’s cam.  Awesome.

Using MJPG-Streamer

It’s slightly more involved, but this tutorial shows how to broadcast video straight to a web page via MJPG-Streamer. All things considered, it’s really easy to setup.  I followed the tutorial my Miguel Mota, and it worked the first time I tried.  Nice!  :  “Raspberry Pi camera board video streaming

Miguel made two shell scripts, start_stream.sh & stop_stream.sh that handle all the heavy lifting of starting and stopping all the services:  Make a copy of them to your home dir for easy execution.  Note, I changed them to up the resolution, jpg quality, and add a password to the site.  I only made one change:  Since I previously port-forwarded port 8554, I also changed their code to use that port, rather than 9000.

To add your own password, edit start_stream.sh and change the line including the block of code below to include the “-c” stuff shown here, changing myUserName:myPassword appropriately.  Note, the -c argument must be inside the quotes, after the www, or things won’t work so well.

-o "output_http.so -p 8554 -w /opt/mjpg-streamer/www -c myUserName:myPassword"

Then browse to:

http://<ip of your router>:8554/stream_simple.html

To login and start watching from the auto-generated web page!  Looks like I’m getting around 1fps.

raspistill Image Formats

--encoding <format>

The default is jpg, but you can change it, where <format> is jpg, bmp, gif, & png.  From the docs: “Note that unaccelerated image types (gif, png, bmp) will take much longer to save than JPG which is hardware accelerated.”

If using jpg, you can set the quality via:

--quality #

Where # is a value from 1 -> 100.  They say that 75 is a good number.

Python Bindings

 picamera

Here on PyPi.  Official documentation here.  Source on Github here.  Forum discussion here.

Easy to install with pip:

$ sudo pip install picamera

I’ve successfully ran the quickstarts via Adafruit WebIDE successfully (while having the Pi hooked up over HDMI to preview the results).

picam

Homepage here.  Source on over on Github.  It returns PIL Image objects.

$ sudo pip install https://github.com/ashtons/picam/zipball/master#egg=picam

Project Links:

Control your Arduino via Python with your Raspberry Pi

I recently ran across the nanpy library for Python when looking at different internet radio projects using the Raspberry Pi.  It allows you to easily control your Arduino from Python, and it installs on the Pi in a snap:

First, install Arduino:

$ sudo apt-get install arduino

Next, install the nanpy source:  This is needed to later build the new Arduino firmware:

$ cd ~
$ curl -O https://pypi.python.org/packages/source/n/nanpy/nanpy-v0.8.tar.gz
$ tar xvf nanpy-v0.8.tar.gz
$ rm nanpy-v0.8.tar.gz

Now install the required Python libs:

$ sudo pip install nanpy
$ sudo pip install pyserial

Hook up your Arduino to one of the Pi’s USB ports, and create/upload the new firmware (using an Arduino Uno as an example):

$ cd ~/nanpy/firmware
$ export BOARD=uno
$ make
$ make upload

From there, programming my Pi via the Adafruit WebIDE (and with my Arduino hooked up to a breadboard with the required led’s and resistors),  I recreated a few basic Arduino sketches to see how it worked.  It worked as expected, simple and easy.

Here is a port of the basic Arduino Blink sketch:

from nanpy import Arduino as A
led = 13

# SETUP:
A.pinMode(led, A.OUTPUT)

# LOOP:
while True:
    A.digitalWrite(led, A.HIGH); # turn the LED on (HIGH is the voltage level)
    print "blink on"
    A.delay(1000); # wait for a second
    A.digitalWrite(led, A.LOW); # turn the LED off by making the voltage LOW
    print "blink off"
    A.delay(1000);

And a port of the basic Fade sketch:

from nanpy import Arduino as A
led = 9
brightness = 0 
fadeAmount = 5

# SETUP:
A.pinMode(led, A.OUTPUT)

# LOOP:
while True:
    # set the brightness of pin 9:
    A.analogWrite(led, brightness)
    # change the brightness for next time through the loop:
    brightness += fadeAmount
    # reverse the direction of the fading at the ends of the fade: 
    if brightness == 0 or brightness == 255:
        fadeAmount = -fadeAmount
    # wait for 30 milliseconds to see the dimming effect 
    A.delay(30)

Only a few core libraries are currently supported.  To see the list, you can visit this page.

On a side note I should point out I went to great lengths to get this working on my Mac, without a lot of success.  You can check out my Python Wiki post on it, under the “Mac Notes” section.