Page 1 of 1

Pi Clock

Posted: Thu Jun 14, 2012 5:41 pm
by kghunt
I am just posting because I'm on a high after my first real program. My pi is not due until the end of the month ( I hope) but I have been doing some python in preparation for it.

I am making a proof of concept clocking in machine using a pi a USB keypad and a USB LCD off eBay. I call it the pi clock. I have managed to write (after doing a few tutorials) a program that presents a menu (clock in, out, start break , end break). The asks for a pin number. Then it builds the data into an XML document and posts it to an existing web service.

I am well impressed with myself. I have covered quite a bit of file IO, http post, XML parsing and config.

Re: Pi Clock

Posted: Thu Jun 14, 2012 5:43 pm
by Joe Schmoe
Well, done!

(And here I thought this was going to be yet another complaint about the lack of an RTC on the Pi)

Re: Pi Clock

Posted: Thu Jun 14, 2012 6:11 pm
by liz
Well done! Hope you have a nice evening basking in the sense of accomplishment.

Re: Pi Clock

Posted: Fri Jun 15, 2012 11:07 pm
by kghunt
Now I am basking in frustration.

I have a sure Electronics 16*2 USB LCD display and I am trying to control it without using LCD proc /LCD4 linux etc.

You are supposed to be able to write directly to the serial port of the device using hex commands (see below). Someone has actually written a python driver for said display, but try as I might I cannot get anything I do to effect the display. But I can get it working in LCDproc I have done it a few times.

Here is the driver

http://pastebin.com/XsSfvXzi

If I import this Driver and they attempt to call any functions with the correct com port, I get no errors but nothing happens to the LCD display.

So I tried doing the serial comms myself

Code: Select all

#!/usr/local/bin/python

import time
import serial
input = "0xFE,0x47,0X01,0X01TEST"

def display():

	ser = serial.Serial("/dev/ttyUSB0", 9600,
							serial.EIGHTBITS,
							serial.PARITY_NONE,
							serial.STOPBITS_ONE,
							timeout = 5,
							rtscts = False)
	

	ser.open()
	ser.isOpen()
	ser.write(input)

display
The serial connection details I borrowed from the driver mentioned above and the hex commands from the devices manual.

http://www.sure-electronics.net/downloa ... 1.0_EN.pdf

(see at the end of the file the hex to send via serial.)

I am stumped. I dont really want to use LCDproc as it adds another layer of complexity to what should be simple serial comms.

Any ideas....

oh yeah and the other thing the code above I have to run as root or it returns permission denied on /dev/ttyUSB0

Re: Pi Clock

Posted: Sat Jun 16, 2012 12:29 am
by buxtronix
You are sending the hex strings as ascii representations of them, but the LCD expects actual binary values represented by the hex.

Change your line specifying the data to send from:

input = "0xFE,0x47,0X01,0X01TEST"

to

input = "\xFE\x47\x01\x01TEST"

For more info lookup "python string literals".

As for the permissions, you can try "chmod 666 /dev/ttyUSB0" before you run the program (but this also needs to be done as root, once per usb insertion or system boot).

To do this permanently, check this thread: https://bbs.archlinux.org/viewtopic.php?id=86088

Re: Pi Clock

Posted: Sat Jun 16, 2012 6:57 am
by kghunt
I'll give this a try in bit. You are a leg-end..

Re: Pi Clock

Posted: Sat Jun 16, 2012 9:47 am
by kghunt
I did what you suggested but still no dice :-(. Its weird as the guy who wrote the driver has the same lcd as me so he must of had it working like this.

Re: Pi Clock

Posted: Sat Jun 16, 2012 4:05 pm
by sharpapotheosis
Are you sure you have the contrast correctly set and everything? I know it sounds like a noob question, but I had this exact problem :P

The other problem might be the voltages: most LCDs expect a 5V input, and the Pi only outputs 3V3. Some LCDs have a 3V setting, so I would recommend trying to use that (you might be able to actually use the 3V3 pin on the Pi for the power but I don't know if that would draw too much current.

Re: Pi Clock

Posted: Sat Jun 16, 2012 8:19 pm
by kghunt
I don't have a pi yet. This LCD is connected by USB. According the the manual it just needs the initialisation command then the corresponding commands above.

Re: Pi Clock

Posted: Sat Jun 16, 2012 10:15 pm
by kghunt
I did a quick sanity check and installed lcdproc. Working in seconds, I was starting to wonder if it was broken!!

Re: Pi Clock

Posted: Sat Jun 16, 2012 11:09 pm
by kghunt
Aha. Further forward. I discover (rather noobly) that the python environment declaration in my script was wrong as I'd copied it from the driver file instead of one of my own!!!. Doh!

I have now managed to turn the back light off using the driver woohoo! But not much else. I will have another look tomorrow.

Re: Pi Clock

Posted: Sun Jun 17, 2012 9:24 pm
by kghunt
Ok this is doing my head in.

As far as I can tell I have done everything right the commands are all correct the correspond with the commands from the manual and the driver that guy wrote.

But all I can do is turn off the backlight! nothing else works..

Code: Select all

#!/usr/bin/env python

import serial
import time
lcd = serial.Serial("/dev/ttyUSB0", 9600,
                                   serial.EIGHTBITS,
                                   serial.PARITY_NONE,
                                   serial.STOPBITS_ONE,
                                   timeout = 5,
                                   rtscts = False)
def Backlight_off():
	lcd.write("\xFE")
	time.sleep(1) 
	lcd.write("\x46")

def Backlight_on():
	lcd.write("\xFE")
	time.sleep(1) 
	lcd.write("\x42")
	time.sleep(1) 
	lcd.write("\x00")

def Writeline1():
	lcd.write("\xFE")
	lcd.write("\x47")
	lcd.write("\x01")
	lcd.write("\x01")
	lcd.write("1234567890123456")
	
def Writeline2():
	lcd.write("\xFE")
	lcd.write("\x47")
	lcd.write("\x01")
	lcd.write("\x02")
	lcd.write("1234567890123456")	
	
#Backlight_on()
#Backlight_off()
The sleeps are not necessary I was just clutching at straws.

Someone help please. I know the lcd is not faulty as it works fine with lcdproc and the supplied windows applications.

Re: Pi Clock (LCD Working!!!!!!!!!!)

Posted: Sun Jun 17, 2012 10:40 pm
by kghunt
Ok don't you just love the feeling when you figure something out by yourself that has been bugging you for ages.

I was so stuck I was messing around and made a loop and low an behold the commands worked!!

So I experimented and discovered that a 0.1 second sleep after writing the serial made everything work...

So no my lcd function is fully functional!!!

Whoop Whoop!

Code: Select all

#!/usr/bin/env python

import serial
import time
lcd = serial.Serial("/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0", 9600)
                                   #serial.EIGHTBITS,
                                   #serial.PARITY_NONE,
                                   #serial.STOPBITS_ONE,
                                   #timeout = 5,
                                   #rtscts = False)

def Backlight_off():
	lcd.write("\xFE")
	lcd.write("\x46")
	lcd.write("\xFE")
	lcd.write("\x46")
	time.sleep(0.1)
def Backlight_on():
	lcd.write("\xFE")
	lcd.write("\x42")
	lcd.write("\x00")
	lcd.write("\xFE")
	lcd.write("\x42")
	lcd.write("\x00")
	time.sleep(0.1)

def Writeline1():
	lcd.write("\xFE")
	lcd.write("\x47")
	lcd.write("\x01")
	lcd.write("\x01")
	lcd.write("I am the Kiddy  ")
	time.sleep(0.1)
	
def Writeline2():
	lcd.write("\xFE")
	lcd.write("\x47")
	lcd.write("\x01")
	lcd.write("\x02")
	lcd.write("Booyah!!!!!!!!!!")	
	time.sleep(0.1)


Backlight_on()
#Backlight_off()

Writeline1()
Writeline2()
Here is my first text sent to the LCD.
Image

Pi clock here we come.

To do

Daemon to start and monitor my services
Edit code to now output to lcd
get mifare reader working and reading UID.

Re: Pi Clock

Posted: Mon Jun 18, 2012 2:58 pm
by kghunt
Here is a video of pi clock version 1.

https://www.dropbox.com/s/nd3ag43o1gz40 ... 152016.mp4

Re: Pi Clock (help needed code optimisation)

Posted: Tue Jun 19, 2012 8:51 am
by kghunt
I am trying to optimise/speed up the lcd writing code as it is too slow I changed this

Code: Select all

def Writeline2(data):
	#global line2
	if len(data) < 16:
		data = data + " "
		Writeline2(data)
	
	elif len(data) > 16:
		data = data [:-1]
		Writeline2(data)
		
	else:
		lcd.write("\xFE")
		lcd.write("\x47")
		lcd.write("\x01")
		lcd.write("\x02")
		lcd.write(data)	
		time.sleep(wait)
		lcd.flush
to this

Code: Select all

#This function writes the date to the lines passed in as a parameter eg Writeline("This is a test","1")
#will write "This is a test" to line 1. If the data is "$blank" it will print a blank line on the display.
#The string has to be 16 characters if it is not the funtion will pad spaces after it. If it is too long
#it will trim it.

def Writeline(data,line):
	if data == "$blank":
		data ="                "
	elif line == "1":
		line = "\x01"
	elif line == "2":
		line = "\x02"
	elif line != "1" or "2":
		line = "1"
	import string
	data.ljust(16)
	data = data [:16]
	command = ["\xFE","\x47","\x01",line,data]	
	for item in command:
		lcd.write(item)
	time.sleep(wait)
It is a lot faster but ljust is not working. It will not pad out. The trim works fine if the data is longer than 16 characters though.

As far as I can tell from the documentation the ljust is right.

Re: Pi Clock

Posted: Tue Jun 19, 2012 9:41 am
by kghunt
No matter sorted it. I was left justifying without setting it to a variable. Doh!

Here is my new and improved driver

Code: Select all

#!/usr/bin/env python

import serial
import time

lcd = serial.Serial("/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0", 9600,
					serial.EIGHTBITS,
                    serial.PARITY_NONE,
                    serial.STOPBITS_ONE,
                    timeout=5,
                    rtscts = False)
#time.sleep(0.1)
wait = 0.04

def BacklightOff():
	command = ["\xFE","\x46","\xFE","\x46"]
	for item in command:
		lcd.write(item)
	#time.sleep(wait)
	
	
def BacklightOn():
	command = ["\xFE","\x42","\x00","\xFE","\x42","\x00"]
	for item in command:
		lcd.write(item)	
	time.sleep(wait)
	
#This function writes the date to the lines passed in as a parameter eg Writeline("This is a test","1")
#will write "This is a test" to line 1. If the data is "$blank" it will print a blank line on the display.
#The string has to be 16 characters if it is not the funtion will pad spaces after it. If it is too long
#it will trim it. If an invalid line is specified it assumes line 1.

def Writeline(data,line):
	if data == "$blank":
		data ="                "
	elif line == "1":
		line = "\x01"
	elif line == "2":
		line = "\x02"
	elif line != "1" or "2":
		line = "1"
	import string
	data = data.ljust(16)
	data = data [:16]
	command = ["\xFE","\x47","\x01",line,data]	
	for item in command:
		lcd.write(item)
	time.sleep(wait)

Re: Pi Clock

Posted: Tue Jun 19, 2012 10:14 am
by buxtronix
[quote="kghunt"]No matter sorted it. I was left justifying without setting it to a variable. Doh!

Code: Select all

	elif line != "1" or "2":
Just make it:

Code: Select all

    else line = "1"

Code: Select all

	import string
Unnecessary.

Code: Select all

	command = ["\xFE","\x47","\x01",line,data]	
	for item in command:
		lcd.write(item)
try:

Code: Select all

lcd.write(''.join(command))

Re: Pi Clock

Posted: Thu Jul 05, 2012 9:42 pm
by kghunt
This is coming along nicely. I have the clock communicating well with my web services. I have Cron starting all my services every 10 minutes so if any should fail they will restart automatically.

My LCD works great over USB but I plan to interface it directly with the gpio ordered a second 1602 display for £2 to do this with when I get around to it.

I have an RS232 5v Mifare reader working using a USB to RS232 converter with a TTL version on route from china.

I have started experimenting with the GPIO as input using a breadboard and I have written a 4 x 4 matrix keypad driver in python. So as soon as my keypad arrives I'll get that hooked in too.

The matrix needs 8 inputs but there are only 8 inputs in total so if I want to connect the LCD using GPIO I'll need an expansion board/chip.

Re: Pi Clock

Posted: Thu Aug 02, 2012 10:19 am
by kghunt
I have rebuilt the pi clock in a new housing using GPIO for lcd and a uart mifare reader. I have also managed to get my head around the wiegand protocol and got a wiegand reader working.

Image

I have made an io board that converts the GPIO to an LCD port, keypad port and uart port.
The board has also got a pc speaker on it and trim pots to adjust the lcd contrast and brightness.

Image

Re: Pi Clock

Posted: Thu Aug 02, 2012 6:09 pm
by texy
So far so good. I have no idea what mifare or wiegand is:oops:
Perhaps an overview would help?

Texy