OpenWRT: Start a python script at boot time

The following script will start a python weather parsing script at boot time (that script not included here). Place the script in

/etc/init.d/weather

 

#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org

START=99
start() {
        sleep 5   # make sure boot process is done, no more console msgs
        echo "Weather VFD App Started"
        . /etc/profile
        echo $PATH
        python /opt/scripts/wunderground_parse.py &
}

START=99 will create an entry in rc.d named S99weather when the script is enabled.

chmod +x /etc/init.d/weather

/etc/init.d/weather enable  #places entry S99weather in /etc/rc.d/

 

The profile command is [dot] [SPACE]/etc/profile. This will invoke the environment variables which otherwise wouldn't be included at boot time. Those variables will include the

LD_LIBRARY_PATH

and other env variables.

This can also be started with /etc/init.d/weather start. You can stop the init script from starting up with 

/etc/init.d/weather disable

You could optionally add other commands to this startup script.

See the entry at the OpenWRT wiki.

 

 

 

Python: Call a function from outside a Class (in another Class)

Warning! I'm a noob to python and the following may not be the best way to solve the problem. It may even contain errors!

I wanted to create a python class that would contain most of the functions I needed to interface a Samsung VFD to a raspberry pi. The VFD connects to the pi using SPI. To make things a little cleaner, I wanted to initialize SPI from within my main class, called SPI_VFD in the code shown below. I also needed to call the functions spi.writebytes and spi.xfer2 (from the module spidev) from within my SPI_VFD class.

In initial testing, here's what worked for me:

import spidev

class thisspi:
    spi = spidev.SpiDev()

class SPI_VFD:
     def __init__(self, callspi):
         self.myspi = callspi
         self.setspi()
     def setspi(self):
         self.myspi.spi.open(0,0)
         self.myspi.spi.mode=3

#instantiate spi connection
s = thisspi()

# initialize SPI_VFD class, passing the spi instance
vfd = SPI_VFD(s)

 

 

SPI for Samsung VFD on the raspberry Pi

TL;DR version - use spi.mode = 3

For an Internet radio project, I wanted to display "Now Playing" information on a beautiful Samsung VFD sourced from adafruit.com. Adafruit has provided a nice arduino library for this display which I used as a starter set to translate into/build a python SPI-VFD class for the raspberry Pi.

I'm a complete noob at python but I tackled the translation of C code to python without too much trouble. I got things going in fairly short order after some google searches, was able to print "Hello, World!" However, the VFD was behaving a bit oddly on a few commands, the cursor. Well, you have to have a cursor to get to the second line, right? 

Initially, I found a very useful thread on the raspberry Pi forum from poster bgreat on setting up a Nokia LCD to work with the Pi. That didn't fit my use case because it used more connections for SPI than the Samsung's three wires. It did give me a model of how to code for SPI on the Pi in general, however. The posting thread is also good for getting background on how to install/activate hardware SPI on the Pi.

But I hit a wall for a couple of weeks on fixing the cursor. I did a lot of searches based on guesses as to what the problem was. TTL voltage? Tried a 74HCT244 TTL up converter, same cursor result.  Under-current flakiness? Used a 2A power supply and separate, powered hub. Same. Some kind of SPI under-run, tried sending extra dummy bytes, still flakey. Think, think, think!

In my googling, I tripped over a posting reference to 3-WIRE mode in SPI. Hmn... The Samsung VFD uses only three SPI connections. Look at the data sheet, there's just a quick, passing reference to "three wire serial interface" and nothing else said.  OK, let's experiment!

I saw a test program on mitchtech.net that included source  code to set SPI mode with a variety of parameters. Those parameters triggered a memory of an answer on stackoverflow.com about some Py object bindings. OK. So first, not knowing what I'm doing, I stupidly try the command: 

spi.mode = 1

thinkng the mode is either on/off. The python interpreter accepts that, but I still get the same flakey results. Go back and look at the SPI test program again. There's a case statement for mode  that clearly says "3  #3-wire mode". Doh! Missed that.

Try:
spi.mode = 3

Success!

Raspberry Pi SPI to Samsung VFD

P405

Did a crude translation of the adafruit SPI_VFD arduino library to python on the raspberry Pi, with the target being the Samsung 20T202DA2JA vacuum fluorescent display.

I'll do a longer write-up when I have time. You need to install spidev as a prerequisite.

The key to getting output is sending a series of integers in a call as below:

def text(string):
     L = [VFD_DATA]
     for char in string:
          L.append(ord(char))
     spi.writebytes( list(L) )

text("Hello, World!")

Parrot DF3120 to Raspberry Pi via VNC

Parrot_df3120_to_pi_vnc

The Parrot DF3120 is a small, cheap digital picture frame that at one point could be had for around $25 USD. About the time Hack-A-Day featured it for it's ability to run a small Linux, I picked up a couple of them. Nowadays (January 2013), they're hard to get.

The two Parrot's lay in inventory, waiting for a project. Now, I'm exploring a use for them. They might have some uses with raspberry pi as an auxiliary display.

I mostly used Nathan Chantrell's instructions for setting up the DF3120 for Linux and getting started with VNC.However, I couldn't get the VNC server started on the raspberry pi with Nathan's command, kept getting an error message:

vncserver: geometry 320×240 is invalid

Over on the cjb.im blog, there was a slight addition to the command, which worked for me:

vncserver -geometry 320x240 -depth 8 :1

The picture above is the result of the first connect to VNC on the Parrot.

Unfortunately, I couldn't get htop to work as Nathan indicated, kept getting invalid display error for xterm.

Below is a picture of console access (Serial?) from one of the original sources of information for hacking this device:

Df3120-console

 

Update: Makezine MonoBox Speaker

P399

In an earlier post, I showed the LM386 amp I made from the Makezine tutorial. This post covers the actual speaker build.

I used a small wooden "cigar" type box with a loose lid that held some kind of Tommy Bahama product. I followed the Makezine MonoBox instructions and used a dremel to cut out a circular hole for the speaker, after first tracing the speaker onto some paper. The result was pretty rough and ugly, with some wavy spots and some flat spots. Didn't like that at all.

I was casting about for some kind of covering to hide and smooth out the edge of the hole. First tried glueing split heat shrink but that was a disaster - it didn't take curves without kinking up and didn't glue well. What I finally hit on was using sugru, an old go-to tool for me. I shaped the sugru around the edges and gave the top a kind of a peak with a slightly inward tilt. While still not perfect, this gave the edges a more finished like, as if a gasket had been applied.

I liked the original build idea of using brass finishing washers. It turned out that when I drilled the holes for 4/40 screws to hold the speaker in, the exterior wood splintered horizontally. I needed something to cover that and the washers worked prefectly. I also found some size 4 brass acorn nuts that worked nicely on top of the washers and made it more interesting.

I drilled a hole for my 2.1mm ID barrel power connector and 1/8 inch audio input on the side of the box. I don't really know why I preferred the right side of the box instead of the back. Guess it's because I'm right-handed and hold the box steady with my left while I insert cables.

When I tested out cable inserts, I realized that I hadn't accounted for the thickness of the box walls, about 1/4 inch. This meant I had to attach a longer 2.1mm barrel connector to my battery holder to make good contact. It also meant I didn't have enough space to attach the nut for my 1/8 in audio connector. So, both the power connector and the audio connector were hot glued in. I held the pieces in place with their connectors inserted to get proper alignment for the holes.

With the speaker installed, the top fit loosely on the base and could easily tilt from one side to the other, wouldn't stay level. To seal the lid in level, I used hot glue, first applying it to the problem side and letting it set. Then I went after the opposite problem side (the tilting side) and did the same, letting it set. Finally, I glude the other two sides at the same time, taking care to lay down an even bead of glue and covering any gaps between the lid and the base.

I have yet to figure out how to put a handle on this box. I bought some nice satin nickel handles for $4 US but their screws were far too long to attach to this small box. I didn't have any shorter screws with the same diameter. I tried glueing the handle in place, but it just wouldn't take.

Even without the handle, the box looks good. And, it ROCKS!

HTC EVO 4G Digitizer & Screen Replacement

Before_and_after
I find a lot of discarded technology on my daily walks with my dog. So far, I've found one camera, a Samsung BlackJack 2 phone and most recently, an HTC EVO 4G cell phone.

I found the EVO on the street next to a restaurant with the heavily cracked digitizer as shown in the before ("From this...") picture above. I replaced the screen at a cost of about $8.00 with install kit, shipped from ebay using the video show here as a guide. Unfortunately, I wasn't careful enough pulling the old digitizer away from the bezel. Too much pressure from the broken screen caused cracks in the LCD under the screen.

So, ordered a new LCD at about $22.00 shipped. I repeated the same procedure and resused the digitizer screen, being more careful to go very slowly with the nylon removal tool around the edges. I also learned from the first install attempt not to use the removal tool around the very bottom of the screen. That's where the fine copper cable slides through a small hole to attach to the phone mainboard. You can bend and crack this cable easily with too much force.

If you are doing this repair, make sure that you get the correct size of cable for the LCD. There are two sizes: narrow and wide. The narrow ones are relatively rare. You can check which one you need by taking the phone apart and looking at the back of the LCD. If the model shown is 2WCA, you need the wide flex version.

Depending on your experience level, this repair should take about an hour. I worked very slowly and carefully, having never done this before, and took about an hour and 45 minutes to make sure everything was correct.

One thing to be careful of is removing the flex cables from the mainboard. The referenced video made the removal of the cables seem very straightforward. I found this was more difficult than the video indicated. Also, the clamps holding the tiny cables in I thought were quite fragile. I made efforts to be careful and I broke the camera cable clamp off completely and the LCD cable clamp slightly in one corner. However, both of these still work.

When I powered up the device and tested the camera, I got an odd sepia/brown tone to photos, even on the LCD.Thinking this was some kind of photo effects filter, I started poking around in settings. Didn't see any obvious filters, but I did see a setting to "Reset to defaults". This fixed the problem!

For $30 in repair costs and a little time, I have a fairly nice extra device with WiFi (but no cell coverage, don't need it), Android 2.3, Pandora, Google Maps, Google Mail, Music player and quite a few goodies. It's kind of like getting an iTouch-class device for cheap.

Nokia 5110 LCD on raspberry Pi

I'm just taking baby steps here, trying to duplicate the work of others. I compiled a C program from binerry.de that talks to the 5110 from the GPIO's on the Pi.

These Nokia graphical screens are cheap, about $10 at adafruit and eBay, and they're used enough that there's starter sets of C and python out there.

Eventual goal is to have a display powered by python. Idea is to output current song from mpd, along with other interface info, for a wifi radio.

Photo

Sent from my iPhone

Controlling pianobar Pandora client with keyboard shortcuts

I've been wanting to get pianobar, a CLI for Pandora, installed on a raspberry pi as a stand-alone player. This post talks about "baby steps" on the way there, a work-in-progress.

First, since I'm running Ubuntu 10.04, I had to install from source. Make sure you have the following dependencies:

sudo apt-get install install build-essential

sudo apt-get install libjson0-dev

Download from the pianobar github and untar/unzip.

cd to the pianobar download folder

Do the following:
make

sudo make install


Test it out just by running:
pianobar

This should bring up your stations and start playing, if you have your login configured properly.

You can find a list of commands by pressing the "?" key. Obviously, you can enter these commands in the same terminal session as pianobar.

But, as a step toward getting a raspberry pi interface going, I wanted to be able to send the commands from some other terminal session. That would sort of emulate sending from a python program and/or serial connection.

To do this, you need to create a FIFO file.  Here's how:

cd ~/.config/pianobar

mkfifo ctl

Now, when you issue the commands (from another terminal session):

echo "i" > ~/.config/pianobar/ctl       # for example, song title

You will get back info on the playing song, although this will be in the original terminal session.

More on this at the Copper Thoughts blog, and a better way to do actual keyboard shortcuts.