Anthonyfromtheuk
Posts: 17
Joined: Sat Mar 29, 2014 3:28 pm
Location: UK

Outputting temperature sensor data to LCD

Wed Jan 07, 2015 12:52 pm

I have a LCD screen with which I want to output a temperature reading. The script which outputs the temperature just gives a numerical reading for example 21.3. When I try and run this code below to display that output on my LCD screen I always get a letter C at the front of the output on the LCD. Can anybody see what I have done wrong or why its showing a letter C at the start of the second line on the LCD from the code I am using below?
Thank you.

Image

Code: Select all

#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "sudo python /home/pi/numbertemp.py"

lcd.begin(16, 1)


def run_cmd(cmd):
    p = Popen(cmd, shell=True, stdout=PIPE)
    output = p.communicate()[0]
    return output

while 1:
    lcd.clear()
    temp = run_cmd(cmd)
    lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
    lcd.message('   Temp: %s' % (temp))
    sleep(2)

User avatar
rpdom
Posts: 15168
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Outputting temperature sensor data to LCD

Wed Jan 07, 2015 1:20 pm

Try removing the "\n" from the end of the strftime string. I don't think the lcd.message function supports it and is possibly sending the code for a new line instead (0x0a). Character 0a on that sort of display will give user-defined character 3, which might be set to "C" on initialisation, possibly. I'm just guessing, but what I see on the datasheet seems to back me up.

User avatar
paddyg
Posts: 2359
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Outputting temperature sensor data to LCD

Wed Jan 07, 2015 1:32 pm

I'm not sure (but possibly the line feed as per rpdom) I would try just printing to screen and see what I was getting. (Though that might not help in this instance!)
Why do you restart numbertemp.py every time you want to read it? Could you not:

Code: Select all

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

def run_cmd():
    global p
    output = p.communicate()[0]
    return output
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Anthonyfromtheuk
Posts: 17
Joined: Sat Mar 29, 2014 3:28 pm
Location: UK

Re: Outputting temperature sensor data to LCD

Thu Jan 08, 2015 9:47 am

it wasn't \n it was me putting a letter C after

Code: Select all

lcd.message('   Temp: %s C' % (temp))
my lack of knowledge made me think it would show after Temp:%s but it doesn't. Not sure why the code I pasted here didn't have that letter C in it that would of made it obvious to myself. I will try your suggested way paddyg I am just using the adafruit template and didn't change hardly anything.

User avatar
paddyg
Posts: 2359
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Outputting temperature sensor data to LCD

Thu Jan 08, 2015 11:26 am

It's hard to know without seeing what numbertemp.py does but if you are just using unaltered code from adafruit then the chances are it won't work the way I suggest! communicate() is generally used to wait for data to come back from a child process and for it to terminate. For my system you would need to change numbertemp so that it keeps sending info. You would then need to change your communication to read from p.stdout

Probably easiest to keep it as it is for the moment!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”