Ramirous Ramirous - 4 months ago 7
Python Question

Python while loop not updating

I'm very new to Python.

I'm trying to display the current temperature from a sensor, which I get from a bash script. I use

sed
to replace the string in the python script (
temp.py
), which prints that string to an LCD Display.

Unfortunately, when I run
temp.py
, it only shows the temperature it has at the moment I run it, but it will not update, even though the
lcd.py
script is constantly changing (it only shows one temperature).

while True:
lcd_string("Temp: 25.123 *C",LCD_LINE_1,1)
time.sleep(1)


25.123 is changing constantly in
temp.py
, but not the different temperatures are not shown in the LCD

Is there any way to get it to update?

Answer

have your sed script constantly update a file called "label.txt" (you probably dont even need sed ... as an aside, why arent you just interfacing the sensor from python?)

then in your temp.py script

while True:
    lcd_string(open("label.txt").read(),LCD_LINE_1,1)
    time.sleep(1)

that is probably the easiest way to just make it work ... (See ned's answer as to why your original implementation did not work)

Comments