Sunday, May 26, 2013

My RaspBerry Pi

(Hint: if you are new to the Raspberry Pi you might want to review my earlier posts)

Since an early disaster with the Pi's Linux filesystem I have been pretty careful about backing up any programming work over WIFI to my iMac. The last time I downloaded the full Linux image I also retained the file on the Mac. The one thing I had not done was to back the whole SD card. It turns out that my original stuff only took up about 2GB on my 16GB SD. But since then I have downloaded a lot of new software. This includes the the latest Linux updates, GPIO libraries, drivers for devices, and most recently, software to read weather reports as text from the Internet.

This AM when I booted the Pi up, my usual SSH (secure shell) from my iMac didn't work. Scary! Also, the activity light on my USB wifi dongle wasn't blinking. So I pulled the SD card out of the Pi, plugged it into a flash card hub on the Mac and immediately made a literal copy with the old UNIX DD command. I'm going to do that regularly from now on. Anyway, after I moved the wifi dongle to a different port on the USB hub everything worked again. So, no big deal. But now I'm fully insured against disaster.

I'm now waiting for some spring-clip connectors for breadboarding. Much less trouble than soldering pins on to flimsy wiring.


Saturday, May 25, 2013

If you are new to the Raspberry Pi you might want to start back to my earlier posts.

Got 3 things from Adafruit today:

  1. Many more assorted M/M jumper wires (you really want lots of lengths and colors).
  2. A DHT22 temperature/humidity sensor.
  3. A POS-type motion sensor.
In a mere hour I have the DHT working. I'm getting better at this! However, there were some wrinkles to work out.
  • The best info on how to use the Adafruit wiring plan and code was in French at http://www.manuel-esteban.com/lire-une-sonde-dht22-avec-un-raspberry-pi/ -- luckily I still remember enough francais from 60 years ago to get by.
  • The sample program did the hard part of reading the DHT22, but it didn't suit me otherwise. More luck: that program was written in C Language. While at the old Bell Labs and Clemson U. I programmed mostly in C from 1972 to the late '90s. I simplified the program down to what I needed and fixed a small bug: the code as I found it returned success even when the DHT22 was read incorrectly. 
I still have the motion sensor to conquer. But who's worried.

I'm still working toward a vegetable farm automation system. While pawing through Raspi references I found--

   http://rayshobby.net/blog/?page_id=562#ospi

Ray (assuming it's a person, probably male) has developed an add-on interface board for the Raspi that can control an 8-station irrigation setup. It costs about $150, plus the Pi, plus the irrigation parts. For another $50 you get 8 more hose valve controls. There is even code for controlling every valve from the Internet. Or -- it can be operated from a Google calendar. 

Here's my messy setup du jour:


Thursday, May 23, 2013


Things I've got working so far (Python programs):


  1. LEDs (probably always the first try).
  2. Buttons, button to turn LED on.
  3. Read the CPU temperature (and Halt the processor if it gets over 80C).
  4. Read an MD80C20 temperature sensor. This gave me some trouble because the waterproof version connected by a 30" cable came with no clues about which of the three colored wires corresponded with the pins on the bare MD80... I finally found it: red=3.3V, black=Ground, white=Data. Probably obvious to most Rpi users. Also, I had trouble connecting the 3 wires to the breadboard. The ends of the wires were tinned but only stripped back about 1/8". I stripped another 1/4" back but the wire was too flexible to make a decent connection to either the breadboard or to the female end of a jumper. So I cannibalized wire bits off an LED and soldered them to the flimsy MD80 wires. Voila, it worked.
I'm waiting for motion and humidity sensors. Also getting a larger selection of jumper wires. To keep sane you need a variety of colors and lengths. In addition, I ordered several multi-wire spring-clip connector blocks -- would have saved me some soldering on the MD80.

What next to buy?
  • Rpi camera board: $30
  • A stepper motor: $5 and up
  • A "power tail switch" to switch 110V/500W: $26
  • Controls for turning on garden/farm irrigation pipes.

Wednesday, May 22, 2013

6: Electricity?

My only exposure to electrical theory was 60+ years ago in 10th grade physics. My practical experience did not run to breadboards, jumpers, LEDs (see my previous post), etc. Internet content is great for ease of access but can occasionally be wrong. The breadboard images below make things pretty clear. The left side is less interesting than how the holes are really interconnected underneath.
The bad info I got stated that the vertical outer columns were NOT connected across the gaps that occur every 5 rows. I did some really dumb jumper-ing while I momentarily believed that.
I had not sprung for a DC voltmeter which could easily tested from top to bottom of a column. But then I realized I could make my own:
In around 1956 I bought a cheap soldering iron so I could assemble a Heathkit amplifier. It has been (literally) gathering dust in my workshop since then. The image above shows my crudely soldered voltage tester (cheap LED and matching resistor, pretty useful). This gimmick immediately disproved the false info I mentioned above. The underside breadboard diagram shows that the left and right sides of the board are a) totally separate, b) the outer 2 columns run the length of the board (and could care less which one is positive or negative), and c) the center 5-pin rows are horizontally connected. The red lines I drew on the left breadboard image above are to suggest that it might be cool to connect Pi GPIO pin 1 (3.3V) to the left-most column and GPIO pin 2 (5V) to the "+" column on the right side. I also drew a blue line to the left side minus column (from GPIO pin 6). This I also jumper on over to the "-" side of right edge. Just my way of keeping sane (-ish).

Because of age-related presbyopia I could really use a "large print" version of the breadboard. But that's never going to happen so I keep the magnifying glass close.

Friday, May 10, 2013

5: I have added some gear:
1. a full-sized breadboard
2. some F/M jumper wires
3. some LEDs (with matching resisters) and button switches

I added the GPIO software to my Pi (via the dangerous "sudo apt-get install ..." method, but this time backing everything up).

My first attempt was the obvious "make an LED blink" program. My first four attempts didn't work. Guess why-- nobody told me that LEDs care which wire is positive. Four tries and I never guessed right! Finally got the hint from a Youtube lesson. The longer wire on the LED has to be the positive side. Duh.

I got some button switches too. They work (sort of) with my Python code but not reliably. I've tried testing button depressed, button edge down, button edge up. Also, different sleeps between tests. Not reliable yet.

Here's a confusion: GPIO pin numbering. There are at least 3 schemes. Two are shown below.

If your Python shows:
import RPi.GPIO as GPIO
. . .
GPIO.setmode(GPIO.BOARD)

You will use actual pin numbers (they aren't numbered on the 26-pin connector, you have to count). Those numbers are in the 2 center columns, above. But if you replace the 2nd line of code above with:
GPIO.setmode(GPIO.BCM) 

Then you are using the"GPIO" numbers shown in the outer 2 columns. I'm not getting into the third scheme, yet.

In the meanwhile, I've ordered more jumpers (can't get by with M/F) a wide assortment of resisters, plus temperature and motion sensors.

Sunday, May 5, 2013

4: Since my last post:

I tried adding software by the--

  sudo apt-get ...

method. And I learned (again) that doing things as super-user (i.e., "sudo") is inherently dangerous. The last such download (wish I remembered what it was, exactly) corrupted my flash filesystem. I only found out when I rebooted. Anyway, it was back to downloading a new copy of the OS. This time I am doing a better job of backing up the Pi to a folder on my Mac.

When I initialized the system, this time I set up the US keyboard the easy way and added SSH at the same time. Now my Pi sessions go as follows:

1. Power up the Pi.
2. Start SSH from my iMac Terminal app:
      ssh pi@192.168....
3. Mount the Pi "Home Folder" on the iMac.
4. Play with the Pi
5. Copy any new Python code to a backup folder on the iMac. 
6. Shutting down:
      sudo shutdown 1
7. Pull the power plug.

Here's a little Python program that prints the CPU temperature:

#cputemp.py
import commands
import re

str = commands.getoutput('/opt/vc/bin/vcgencmd measure_temp')

r = re.match(r'.*=(\d+\.\d)', str)
c = r.group(1)
c = float(c)
f = int(( c * 1.8) + 32.0)

print "Pi CPU temp=", f, "F, ", c, "C"
if c >= 80.0:
    print("Don't fry your Pi -- 80C is the max. safe CPU temp!")


My Pi runs at a little over 40C in its plastic case.

Thursday, April 18, 2013

Devices

I have looked into GPIO programming in Python. Looks ok so I ordered some basic (and cheap) stuff:

a full-sized breadboard
50 F/M patch wires
assorted colored LEDs with resisters
assorted colored push-button switches

After I figure out programming those simple devices, I plan to move on to controlling the LEDs from my iPhone.

Once that works, I plan to move on to more challenging (and expensive) devices.

Friday, April 12, 2013

My Pi chapter 2

Post 2

Something I forgot about initial setup: U.S. users -- be sure to change the default keyboard from UK to US. Otherwise, lots of luck finding important special characters.

Here's my Python program (copy/try it if you wish):
===================
# Compute Pi to 10,000 places
# recoded using the algorithm from "ABC Programmers Handbook", Geurts, et al
def arccot(x, unity):
    sum = xpower = unity // x
    n = 3
    sign = -1
    while 1:
        xpower = xpower // (x*x)
        term = xpower // n
        if not term:
            break
        sum += sign * term
        sign = -sign
        n += 2
    return sum

def pi(digits):
    unity = 10**(digits + 10)
    pi = 4 * (4*arccot(5, unity) - arccot(239, unity))
    return pi // 10**10

ans = str(pi(10000))
ans = ans[0:1]+'.'+ans[1:] # insert decimal point
print ans
=====================
BTW: this takes 4 seconds on the Pi and .6 second on my iMac. Same answer, though.

What I'd like to do with the Pi: (sounds like a 3rd-graders essay)
My grand-daughter & husband have a vegetable farm. They have built a cold storage building and need to trick a window AC unit into keeping it cooled to 40 degrees F. They also could use other automation aids: a) alarm on the lock, motion detector, soil moisture reading, plus lots more. I'd like them to be able to control the Pi via a local WAN by an iPhone. This will be a challenge!

More as I progress.

Dick Haight

Rpi: getting started

Post 1: First of Who knows?

I ordered my Raspberry Pi in February. I resisted paying any more than the legendary $35. That made it take longer. I also ordered a fast class 10 16GB SD card so it would have plenty of free space ($28).

This took so long that I swiped an SD card from my camera and downloaded a Linux image onto my iMac. To copy it on to the chip in proper format I had to learn about the ancient Unix "dd" command (I worked on Unix at Bell Labs in the 1970s -- never thought I use dd again).

I had just finished all of this when Fedex guy arrived with the other chip in a tiny padded envelope (4-ton truck to deliver a half-ounce package).

I have also bought an HDMI-to-VGA cable so I could use a borrowed old LCD monitor. You could program from a VGA monitor but it was fuzzy on any resolution greater than 640x480, which now days is useless on the Web. This was a waste of $20+ and a lot of time messing with the config file.

Also, I acquired a plastic case and a tiny USB WIFI plugin ($13). WIFI setup was pretty easy but the device caused a headache. When I typed on my old left-over keyboard I got a bunch of repeated keystrokes. It wasn't obvious to me that the WIFI device was taking too much current but the problem stopped the instant that I plugged the cords into a powered USB hub (another $16).

I borrowed an HDMI-to-DVI cable & monitor. They work fine. Here's how my Pi looks now.
The little blue light is on the WIFI adapter. The keyboard/mouse (when needed) go on the powered hub.

I mentioned "when needed" because I have worked out how to access the Pi from my iMac. I set up "ssh" (remote login program) on the Pi and use the same thing from Terminal on the Mac. Now I only need a monitor/keyboard when I screw something up.

Next issue: a Python Pi program for your bemusement and what I'd like to do with the Pi.

Dick Haight