Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


LCD script
#1
I have the following python script to post data from lcd.txt to a 4x20 LCD.

Code:
import time
from RPLCD.i2c import CharLCD

lcd = CharLCD('PCF8574', 0x27)
lcd.backlight_enabled = (1)
lcd.clear()
lcd.cursor_pos = (0, 0)

mylines = []                                       # Declare an empty list.
with open ('/home/pi/lcd.txt', 'rt') as myfile:    # Open file for reading text.
   myfile = myfile.readlines()[1:4]               # Only read lines 1 to 4.
   for myline in myfile:                          # For each line in the file.
       mylines.append(myline.rstrip('\n'))        # strip newline and add to list.
for element in mylines:                            # For each element in the list.
   element=element[element.find('=')+1:]          # Only print text after = sign.
#    print(element)
   lcd.write_string(element)
   lcd.write_string('\n\r')

time.sleep(1)
lcd.close()

I am completely new to python and want to monitor the file lcd.txt to see if it has changed and then run again the above script to update the LCD.  Can anyone help I have tried lots of ideas I found online but cannot make it work.  Below is one that will monitor the file but what do i need to add to the above to do the monitoring and trigger the update?

Code:
def read_file():
   with open("/home/pi/lcd.txt", "r") as f:
       SMRF1 = f.readlines()
   return SMRF1

initial = read_file()
while True:
   current = read_file()
   if initial != current:
       for line in current:
           if line not in initial:
               print(line)
       initial = current

Thanks
Colin
Reply


Forum Jump: