A smart Raspberry Pi Zero DIY Text Clock

So this is the project I had in mind when I was experimenting with a NeoPixel strip on a Raspberry Pi Zero. The original text clock was invented a couple of years ago. With its elegant and timeless (yes, literally) design the QLOCKTWO is simultaneously a beautiful and useful piece of art.
It is not that I exactly needed yet another clock – but I got intrigued and wanted to create my own, smart version of a text clock.

Numerous examples of DIY versions and manuals on how to build a text clock are available on the internet. Some manuals involve soldering a lot of LEDs. I wanted to skip this step and went for a NeoPixel strip. In total I calculated 92 NeoPixels: one for each letter that can be alighted.

My version of the text clock should not only display the time in a unique way but should also indicate something more, it should be smart! This is why I chose a Raspberry Pi Zero instead of a microcontroller as a base. This way I’m able to easily get more information using a Python script along with some ready-made libraries.

My smart text clock indicates whether I have unread emails in my inbox by changing the colour of the LEDs. If desired the smart text clock is also able to indicate the weather developments depending on the outside temperature or any other criteria. The weather data is taken from openweathermap.org as in former projects.

One could also try to indicate whether a train one needs to catch regularly is on time. Or the smart text clock could be used as a traffic monitor for commuters (similar to this project idea).

Updating the smart text clock every 5 minutes should be precise enough for me. It is definitely more precise than a fuzzy clock which indicates bright and dark only.

Hardware

The hardware list of the last blog entry can be extended by the picture frame which is often used for DIY text clocks. A suitable one is sold by a well-known swedish furniture store.
Additionally some paper is useful for dispersing the light from the LEDs behind the letters.

Adhesive foil with precisely cut letters can be put on the glass to match the LEDs from the strip. Here I had professional help by friends owning a cutting plotter.

The LED strip is cut and soldered together appropriately to match the letters positions. The strip is glued with its adhesive back to the picture frame’s back plate.

The LEDs are separated by a grid behind the glass. It is printed with a 3D printer. This grid helps to avoid interferences between the different letters.

A piece of transparent paper between the glass and the grid is the possibility to make the letters look smooth. If it was missing the LEDs were directly visible. A bit of diffusion makes it look better…

Software

A straightforward python script is run automatically every five minutes. First the current time is determined. The time is translated into words with a five minute precision.

The words are mapped to the LED indices from the NeoPixel strip. These are the ones to alight to display the time.

Colour Definition

To determine which colour to use for the alighted LEDs some (optional) checks are built-in:

  • Approximately every hour the weather data is fetched from openweathermap.org using the python owm library. The temperature is extracted along with the weather code. The results are used for defining the colour of the LEDs. Other parameters can be taken into account as well.
  • The number of unread emails is checked using the Python imap library. If the number is greater than zero the LED color is changed.

During night time the brightness of the LEDs is lowered. That way the smart text clock serves as a convenient night light as well.

Source Code


#!/usr/bin/python
# -*- coding: cp1252 -*-

import time

import imaplib

import pyowm
import json
import pprint

from neopixel import *

########################CONFIG############################

OWM_APYKEY='get one from openweathermap.org'
OWM_ID = an ID number

# file to store weather state
fileName="/home/pi/textClockWeatherState.txt"

EMAIL_NAME = "user@googlemail.com"
EMAIL_PASS = "password"

# LED strip configuration:
LED_COUNT = 92 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 128 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)

######################END#CONFIG##########################

_start = "IT IS "
_end = " O\'CLOCK"
_numbers = ('ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN', 'ELEVEN', 'TWELVE')
_past = ' PAST '
_to = ' TO '
_fivepast = 'FIVE PAST '
_tenpast = 'TEN PAST '
_aquarter = 'A QUARTER '
_twenty = ' TWENTY'
_twentyfive = ' TWENTYFIVE'
_half = ' HALF'
_fiveto = 'FIVE TO '
_tento = 'TEN TO '

'''
I T L I S A S T I M E 0,1, 2,3
A C Q U A R T E R D C 4, 5,6,7,8,9,10,11
T W E N T Y F I V E X 12,13,14,15,16,17, 18,19,20,21
H A L F B T E N F T O 22,23,24,25, 26,27,28, 29,30
P A S T E R U N I N E 31,32,33,34, 35,36,37,38
O N E S I X T H R E E 39,40,41, 42,43,44, 45,46,47,48,49
F O U R F I V E T W O 50,51,52,53, 54,55,56,57, 58,59,60
E I G H T E L E V E N 61,62,63,64,65, 66,67,68,69,70,71
S E V E N T W E L V E 72,73,74,75,76, 77,78,79,80,81,82
T E N S E O C L O C K 83,84,85
'''
# map time to precise LED indices
_timeLightMap = {
'IT IS ' : (0,1,2,3),
' HALF' : (22,23,24,25),
' PAST ' : (31,32,33,34),
' TO ' : (29,30),
'FIVE PAST ' : (18,19,20,21, 31,32,33,34),
'TEN PAST ' : (26,27,28, 31,32,33,34),
'A QUARTER ' : (4, 5,6,7,8,9,10,11),
' TWENTY' : (12,13,14,15,16,17),
' TWENTYFIVE' : (12,13,14,15,16,17, 18,19,20,21),
' HALF PAST ' : (22,23,24,25, 31,32,33,34),
' TWENTYFIVE TO ' : (12,13,14,15,16,17, 18,19,20,21, 29,30),
' TWENTY TO ' : (12,13,14,15,16,17, 29,30),
'TEN TO ' : (26,27,28, 29,30),
'FIVE TO ' : (18,19,20,21, 29,30),
'ONE' : (39,40,41),
'TWO' : (58,59,60),
'THREE' : (45,46,47,48,49),
'FOUR' : (50,51,52,53),
'FIVE' : (54,55,56,57),
'SIX' : (42,43,44),
'SEVEN' : (72,73,74,75,76),
'EIGHT' : (61,62,63,64,65),
'NINE' : (35,36,37,38),
'TEN' : (83,84,85),
'ELEVEN' : (66,67,68,69,70,71),
'TWELVE' : (77,78,79,80,81,82),
' O\'CLOCK' : (0,1,2,3)
}

class SmartTextClock():

def run(self):
print "A SMART TEXT CLOCK"

def check_googlemail(self, login, password):
# if new mail return # emails
obj = imaplib.IMAP4_SSL('imap.gmail.com','993')
obj.login(login, password)
obj.select()
nofUnreadMessages = len(obj.search(None, 'UnSeen')[1][0].split())
print "Unread emails: " + str(nofUnreadMessages)
return nofUnreadMessages

def clock(self):
t = time.strftime("%H:%M")
print t
return t

def translateHour(self, hour, offset):
if hour == '00' or hour == '12':
if offset == True:
return _numbers[0]
else:
return _numbers[11]
if hour == '1' or hour == '13':
if offset == True:
return _numbers[1]
else:
return _numbers[0]
if hour == '2' or hour == '14':
if offset == True:
return _numbers[2]
else:
return _numbers[1]
if hour == '3' or hour == '15':
if offset == True:
return _numbers[3]
else:
return _numbers[2]
if hour == '4' or hour == '16':
if offset == True:
return _numbers[4]
else:
return _numbers[3]
if hour == '5' or hour == '17':
if offset == True:
return _numbers[5]
else:
return _numbers[4]
if hour == '6' or hour == '18':
if offset == True:
return _numbers[6]
else:
return _numbers[5]
if hour == '7' or hour == '19':
if offset == True:
return _numbers[7]
else:
return _numbers[6]
if hour == '8' or hour == '20':
if offset == True:
return _numbers[8]
else:
return _numbers[7]
if hour == '9' or hour == '21':
if offset == True:
return _numbers[9]
else:
return _numbers[8]
if hour == '10' or hour == '22':
if offset == True:
return _numbers[10]
else:
return _numbers[9]
if hour == '11' or hour == '23':
if offset == True:
return _numbers[11]
else:
return _numbers[10]
return ''

# time format: HH:mm
def translateTime(self, time):
t = time.split(':', 1)
print t
h = str(t[0])
m = str(t[1])
print h + ":" + m

indices = (1,2)

if float(m) >= 0.0 and float(m) <= 2.5:
#IT IS X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[self.translateHour(h, False)] + _timeLightMap[_end]
if float(m) > 2.5 and float(m) <= 7.5:
#IT IS FIVE PAST X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_fivepast] + _timeLightMap[self.translateHour(h, False)] + _timeLightMap[_end]
if float(m) > 7.5 and float(m) <= 12.5:
#IT IS TEN PAST X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_tenpast] + _timeLightMap[self.translateHour(h, False)] + _timeLightMap[_end]
if float(m) > 12.5 and float(m) <= 17.5:
#IT IS A QUARTER PAST X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_aquarter] + _timeLightMap[_past] + _timeLightMap[self.translateHour(h, False)] + _timeLightMap[_end]
if float(m) > 17.5 and float(m) <= 22.5:
#IT IS TWENTY PAST X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_twenty] + _timeLightMap[_past] + _timeLightMap[self.translateHour(h, False)] + _timeLightMap[_end]
if float(m) > 22.5 and float(m) <= 27.5:
#IT IS TWENTYFIVE PAST X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_twentyfive] + _timeLightMap[_past] + _timeLightMap[self.translateHour(h, False)] + _timeLightMap[_end]
if float(m) > 27.5 and float(m) <= 32.5:
#IT IS HALF PAST X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_half] + _timeLightMap[_past] + _timeLightMap[self.translateHour(h, False)] + _timeLightMap[_end]
if float(m) > 32.5 and float(m) <= 37.5:
#IT IS TWENTYFIVE TO X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_twentyfive] + _timeLightMap[_to] + _timeLightMap[self.translateHour(h, True)] + _timeLightMap[_end]
if float(m) > 37.5 and float(m) <= 42.5:
#IT IS TWENTY TO X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_twenty] + _timeLightMap[_to] + _timeLightMap[self.translateHour(h, True)] +_timeLightMap[_end]
if float(m) > 42.5 and float(m) <= 47.5:
#IT IS A QUARTER TO X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_aquarter] + _timeLightMap[_to] + _timeLightMap[self.translateHour(h, True)] + _timeLightMap[_end]
if float(m) > 47.5 and float(m) <= 52.5:
#IT IS TEN TO X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_tento] + _timeLightMap[self.translateHour(h, True)] + _timeLightMap[_end]
if float(m) > 52.5 and float(m) <= 57.5:
#IT IS FIVE TO X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[_fiveto] + _timeLightMap[self.translateHour(h, True)] + _timeLightMap[_end]
if float(m) > 57.5 and float(m) <= 59.9:
#IT IS TO X O'CLOCK
indices = _timeLightMap[_start] + _timeLightMap[self.translateHour(h, True)] + _timeLightMap[_end]
return indices

def selectColor(self, weatherCondition):
# Email: Lime Green 50-205-50
# http://www.tayloredmktg.com/rgb/
color = Color(255, 222, 173)
# weatherCondition = 'fair' # 'good', 'fair, 'bad'
if weatherCondition == 'fair':
color = Color(255, 222, 173) # Navajo White 255-222-173 # Lemon Chiffon 255-250-205
if weatherCondition == 'good':
color = Color(255, 127, 80) # Coral 255-127-80 # Light Salmon 255-160-122
if weatherCondition == 'bad':
color = Color(70, 130, 180) # Steel Blue 70-130-180
return color

def getWeatherFromOWM(self):
owm = pyowm.OWM(OWM_APYKEY, version='2.5')
# Search for current weather
print "Weather @ID"
obs = owm.weather_at_id(OWM_ID)
w1 = obs.get_weather()
print(w1)
print w1.get_status()

weatherCondition = 'fair' # 'good', 'fair, 'bad'

# get general meaning for weather codes https://openweathermap.org/weather-conditions
weatherCode = w1.get_weather_code()
print weatherCode
print w1.get_sunset_time('iso')
temperature = w1.get_temperature('celsius')['temp']
print str(temperature) + " C"

# simple: judge weather on temperature
if temperature<=10.0:
weatherCondition = 'bad'
if temperature>10.0 and temperature<20.0:
weatherCondition = 'fair'
if temperature>=20.0 and temperature<35.0:
weatherCondition = 'good'
if temperature>=35.0:
weatherCondition = 'bad'

return weatherCondition

def readSavedWeatherCondition(self):
weatherCondition = 'fair' # default
try:
print 'Read file ' + fileName
target = open(fileName, 'r')
weatherCondition = target.read()
print weatherCondition
target.close()
except IOError:
print fileName + " does not exist yet. Creating a default file."
self.saveWeatherConditionToFile(weatherCondition)
pass
return weatherCondition

def saveWeatherConditionToFile(self, weatherCondition):
try:
print 'Write file ' + fileName
target = open(fileName, 'w')
target.write(weatherCondition)
target.close()
except:
print 'File ' + fileName + ' could not be written.'

# NEOPIXEL: GRB
#strip.setPixelColor(i, Color(0,0,120)) # B
#strip.setPixelColor(i, Color(0,120,0)) # R
#strip.setPixelColor(i, Color(120,0,0)) # G
def alight(self, LEDindices, color):
print 'Alight indices ' + str(LEDindices)
for i in LEDindices:
strip.setPixelColor(i, color)
strip.show()

if __name__ == "__main__":
app = SmartTextClock()
app.run()

# check for unread emails
unreadEmails = app.check_googlemail(EMAIL_NAME, EMAIL_PASS)

# get time and determine LED indices
time = app.clock()
indices = app.translateTime(time)
print "Indices " + str(indices)

# investigate weather data
weatherCondition = app.readSavedWeatherCondition()

# update weather data every hour
theTime = time.split(":")
# this range should be met from time to time
if int(theTime[1])>=0 and int(theTime[1])<=7:
weatherCondition = app.getWeatherFromOWM()
app.saveWeatherConditionToFile(weatherCondition)

# create NeoPixel object with appropriate configuration
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
# intialize the library (must be called once before other functions)
strip.begin()

# low light during 19-8 o'clock
if(8 < int(theTime[0]) > 19):
strip.setBrightness(200)
else:
strip.setBrightness(50)

stripColor = Color(120,120,120)

# select color depending on weather condition
if weatherCondition == 'bad':
stripColor = Color(0, 120, 0)
if weatherCondition == 'fair':
stripColor = Color(120, 120, 120)
if weatherCondition == 'good':
stripColor = Color(0,0,120)

if unreadEmails > 0:
stripColor = Color(205,50,50)

app.alight(indices, stripColor)

 

NeoPixels Strip on Raspberry Pi Zero

Looking into my desk’s drawer I found the remainder of an Adafruit NeoPixel strip I used in another project. And an unused, last years Raspberry Pi Zero. Does that work together? Well, yes, it does! At least after fiddling a bit with hard- and software and circumventing some common traps.

Searching the web I found a tutorial for steering a NeoPixel strip with a first generation Raspberry Pi. Technically it should work with an exemplary of a more recent version, but it did not initially.
Here is the description of how it all worked out in the end:

Hardware

Raspberry Pi Zero with up-to-date Raspbian Jessie Pixel
Mini USB WiFi Adapter (if the brand new Raspberry Pi Zero W is not used)
Raspberry Pi Zero adapter cables + power supply
Adafruit NeoPixel strip
1000 μF capacitor
330 Ω resistor
1N4001 diode
5 V breadboard power supply
breadbord, cables

Connections

  • 5V power supply GND : 1000 μF capacitor (short leg)
  • 5V power supply 5V : 1000 μF capacitor
  • 5V power supply GND : NeoPixel strip GND
  • 5V power supply 5V : NeoPixel strip 5V via 1N4001 diode (side with stripe goes to 5V input of the strip)
  • 5V power supply GND : Raspberry Pi Zero GND (physical pin 6)
  • Raspberry Pi Zero (physical pin 12) : NeoPixel strip data line via 330 Ω resistor

The available pins of the Raspberry Pi Zero are listed here. GPIO #1 correlates to physical pin 12 which is BCM #18. The latter is used in the Python software.

Don’t!

It is not recommended to use the 5V output of the Raspberry Pi Zero directly to power the NeoPixel strip. The pixels might draw too much current and might therefore damage the pin. It would have been way too convenient…so: an additional 5V power supply is strongly recommended.

Software

Running Headless: Setting up WiFi

To run the Raspberry Pi Zero headless (without display), set up the WiFi connection. For this step an HDMI display and a keyboard is required. Open the file

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Put the network configuration at the end of the file:

network={
ssid="WiFi network name"
psk="password"
}

Use raspi-config to allow SSH connections and to adjust the Pi’s hostname, the password, the time settings etc. .

SSH to Zero

When attaching the Mini USB WiFi Adapter instead of the keyboard and rebooting the Raspberry Pi Zero the desired WiFi network is used and it is possible to SSH to the Pi Zero. To find the IP adress in the local network check which devices are logged into the network at your routers access point. Or kindly ask your network admin to check. 😉

GPIO Checks

To see the available GPIO pins on the Raspberry Pi Zero run

gpio readall

NeoPixel Python Library

To set up the Python library for driving NeoPixels on a Raspberry follow this tutorial. Jeremy Garff’s Python library for NeoPixels is working like a charm.

Disabling Audio

To be able to use the PWM pins as data pins for the NeoPixel strip I disabled audio by commenting the line

# Enable audio (loads snd_bcm2835)
#dtparam=audio=on  # disable audio for PWM pin usage

in the file /boot/config.txt.

Whether audio is disabled can be checked using

aplay -l

. If audio is disabled properly the result is an error message („aplay: device_list:268: no soundcards found…“).

Examples

Once the NeoPixel library is set up and the hardware is connected properly run strandtest.py or any other example code from the rpi_ws281x/python/examples section.

Result

That’s it! The NeoPixel strip finally can be driven by a Raspberry Pi Zero.

neopixeltest

While this example is working I definitively have a new project in mind…

Links

https://learn.adafruit.com/neopixels-on-raspberry-pi/overview

https://github.com/jgarff/rpi_ws281x

blinking bike helmet

A bike helmet could also light signal left and right on the push of a button. In the dark visibility is improved. As more and more car drivers don’t use their blinkers when changing directions it is also a nice statement.

components used
Adafruit Gemma
4 Neopixel
conductive thread, needle for sewing
small LiPo
two mini switches
hook-and-loop fastener
fabric
for testing: alligator clips

make it happen
Sew the components on fabric as shown on the pictures. Alternatively test everything using the alligator clips before sewing.

blinkinghelmet_circuitblinkinghelmet_blink

Important: the ‚data‘ thread of the neopixels should not continue below the pixels.

To place this on a helmet hook-and-loop fastener may be the right choice.

the source code
Load this program on the Adafruit Gemma. When the LiPo is powering the microcontroller push the switches to see either the left or the right LEDs blinking.

leftright.ino

The source code is based on the works of Leah Buechley, 2008.

how it works

When one of the switches is pressed the two pixels beside blink 10 times (adjustable in the source code).

blinkinghelmet_blink2

Please keep in mind that such an add on is never a replacement for proper lighting of the bike or showing your directions using your hands!

The Gemma and the Neopixels are also available in starter kits.

Of course there is no guarantee this description is free of bugs!

enlightened bag

Why not „enlighten“ a bag when opening it? This comes in useful especially in the dark.

components used
Adafruit Gemma
4 Neopixel
conductive thread, needle for sewing
small LiPo
soldering kit, small plate, cables
bag with metal zipper
for testing: alligator clips

making it happen

Sew the components in a bag as shown on the pictures. Alternatively test everything using the alligator clips before sewing.

enlightenedbag

enlightenedbag_inside

bag_plate

Realize the zipper switch to turn on the Gemma MicroController when the bag is open. A small plate with soldered cables is used to make the connection only when the zipper is open (=switched). Ensure that there is no connection on the plate for the bridged cable! Transparent nail polish helps to keep the knots from undoing. Needs several hours to dry!

the source code

Load this program on the Adafruit Gemma.

enlightenedbag.ino

how it works

When the zipper switch is used the microcontroller is powered on and turns on the LEDs one by one.

The Gemma and the Neopixels are also available in starter kits.

Of course there is no guarantee this description is free of bugs!

garden watering system

Since I have a garden regular watering is essential. But this can easily be automated with a micro controller. Here is a short description how the system is realized.

components used

Arduino Uno (or replacement)
12 V magnet valve
pressure sprayer, 5 l
flexible hoses, matching adapters
PIR sensor
IR receiver
TO 220 MOSFET
TIP 120 transistor
(red) LED
1 kOhm resistor
12 V power supply
old (plastic) box
wooden lath, screws
soldering kit, small plate, cables

making it happen

Bring the components together as briefly depicted below.

wetcat_circuit_wateringsystem_inside

wateringsystem_

The most time consuming is to find the proper hoses and adapters so that no water is leaking and the pressure lasts.

For the water output simple bend the end of a hose and prepare some holes with a hot needle.

the source code

Prepare this program for upload on the Arduino.

wetcat.ino

Adapt the code for your preferred IR remote control. It is possible to find out which codes they are with this example from an Arduino IR library.

how the system works
The red LED blinks several times when the system is powered on. The red LED is lighted when the water is running. The water runs when either the PIR sensor is activated for 5 s or as long as the matching remote control button was pressed.

The beautiful side effect of this watering system is that the neighbour cats suddenly decide to find another loo.

Of course there is no guarantee this description is free of bugs!