Wednesday, December 18, 2013

My 7-Segment Display

I ordered the device from Adafruit. The main reason was to check out getting a second I2C device to work. Here's how I wired it from the Pi:

To get that far I followed the directions at--

http://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack

I found soldering the 24 tiny pins to connect the display to the "backpack" a trial, but surprise, surprise -- it worked with their example 24-hour clock program first try. I had previously downloaded --

Adafruit-Raspberry-Pi-Python-Code-master/Adafruit_LEDBackpack

(Do you think they could make the directory names any longer?)

I wanted my clock to display 12-hour/AM/PM time. Here's my rework of the software:

#!/usr/bin/python
import time
import datetime
import signal
import sys
from Adafruit_7Segment import SevenSegment

def signal_handler(signal, frame): # to stop the clock
    print 'You pressed Ctrl+C!'
    SevenSegment(address=0x70) #clear display
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

segment = SevenSegment(address=0x70)

lastHour = -1

print "Press CTRL+C to exit"

# Continually update the time on a 4 char, 7-segment display
while(True):
  now = datetime.datetime.now() # get 24-hour time values
  hour = now.hour
  pm = False
  # Set hours
  if hour != lastHour:   # don't change display
    lastHour = hour
    if hour > 11: # change to 12 hour clock
      hour -= 12
      pm = True
      if hour == 0: # just after midnight
        hour = 12
      if int(hour / 10) == 0:  # turn off leading zero
        segment = SevenSegment(address=0x70)  # turns all digits off
    if int(hour / 10) != 0:    
      segment.writeDigit(0, int(hour / 10))     # Tens
    segment.writeDigit(1, hour % 10)          # Ones
  # Set minutes
  minute = now.minute
  segment.writeDigit(3, int(minute / 10))   # Tens
  segment.writeDigit(4, minute % 10, pm)        # Ones + PM dot
  # turn on colon
  segment.setColon(1)

  time.sleep(60)    # update in 1 minute -- may be 1 minute slow 

One of the irritations of Python is that a program like this can only be executed from the directory its local "import" file is in. There are ways around this but it's enough to make C language attractive. 

Most importantly, both of my I2C-connected devices still work. And I can add more. From two GPIO pins (3=SLA, 5=SCL), many devices!

Monday, December 16, 2013

More about my Raspberry Pi's Web Site

See

http://raspi-online.info/

It now allows someone (with the password) to operate the 8 relays connected by I2C from the Pi. At the moment the relays themselves aren't connected to anything much --  but they could be. It looks like this:
I have one 8-switch Sain relay, but I think that two 16-relay boards would work, too. I adapted code in C language that I found at--

http://www.skpang.co.uk/blog/archives/637

I would have preferred to work in Python but code I've found in that language didn't work for me and I was more interested in results than I was in discovering the problem.

The web site does not send data or commands to the Pi. Rather the Pi uploads data on a scheduled basis and checks for change requests. So whenever the relays are changed (whether directly on the Pi or requested from the web) the then-current settings are uploaded to raspi-online.info for display as above. When a change is recorded from the web form (above) a temporary file is written and then queried every 2 minutes from the Pi.

Sunday, December 1, 2013

Housekeeping (Drat!)

In Linux it is seductively easy to install new software packages. Of course, it's also a great convenience. Keeping track of what you've installed (and when) is mostly up to you. I say mostly because the following shell expression is a help:


  dpkg --get-selections | grep install

Unfortunately, this gives you the list in alphabetical rather than date order and it lists all the sub-packages -- not just the ones you actually typed in. My list since June is over 1000 lines long. I mentioned June because just before that I did --

  sudo apt-get -y ??? install

And whatever that was (the "???" bit) corrupted the filesystem on my 16gb SD card ("sudo" can scribble anywhere). So I had to re-download the whole system (using my iMac) on to a spare SD card. I then tried to patch the original filesystem using the fsck command. But it didn't help. Since then I have been pretty good about backing things up (covered in earlier posts). The thing I didn't do was write down each time I did an install or update. Duh. And if you DO keep this record, don't just leave the file on the Pi where it could get lost.