Month: November 2016

Piader meets ESP and events

Some time ago we made a WiFi keyboard on ESP 8266. Long time ago we made a Piader game. From their combined powers comes Piader controlled by WiFi keyboard 😀

We need to change Piader a little, to be precise, event listener and keyboard server needs to use JSON packet not string. Why ? Because wifi keyboard is using it.

Piader will also move from TCP to UDP and this allows us to use broadcast.

What about keyboard changes ? Yey! No changes!

Piader source code


Code example – simple clock

Code that shows how to use libraries to display a clock on 20×4 lcd screen via i2c bus.

import time
from charlcd.buffered import CharLCD
from charlcd.drivers.i2c import I2C
from lcdmanager.manager import Manager
from lcdmanager.widget.label import Label
from lcdmanager.display import Display

i2c_20x4 = I2C(0x3b, 1)
i2c_20x4.pins = {
    'RS': 6,
    'E': 4,
    'E2': None,
    'DB4': 0,
    'DB5': 1,
    'DB6': 2,
    'DB7': 3

First connection to screen is defined as I2C on port 0x3b and bus 1. Next we set pins.

lcd = CharLCD(20, 4, i2c_20x4, 0, 0)

Here we initialize CharLCD with width 20 chars, height 4 chars, defined connection, without cursor.

lcd_manager = Manager(lcd)

label1 = Label(8, 1)
label1.label = "--------"

Now we create manger for given LCD. Create label widget on which we will display time. Label is positioned at (8, 1). Next we add this label to manager.

disp = Display(0.5, True)
disp.add(lcd_manager, 'one')

Almost done. This will create a display server that will keep refreshing at 0.5 fps. See that we send True as second parameter. This will render manager before flushing it.
Next we add our manager to display server and run it.

while True:
    label1.label = str(time.strftime('%H:%M:%S'))

This is a loop that is setting label to current time. We don’t have to call render manually. We are just changing label and all is done automatically.

LCDManager- fault protection

Imagine situation, you have a perfect LCD that displays a perfect content on a perfect day. On this perfect day some imperfect being trip and unplug power from a display. It goes off, RPi tries to send packet to it and a program explodes with:

IOError: [Errno 5] Input/output error

And the perfect day is back to a typical day. What can we do about it ? We may write a fault protection.
During usage I met mostly with two problems: one power was lost and second there were a some kind of interference and screen goes wild. How wild ? With each tick content was shifted or a gibberish was displayed. We can do something with both cases.

Download source from Bitbucket