I am attempting to pull data from a websites API that changes frequently and display the data on an LCD (a I2C, 20 Column, 4 line LCD to be precise). I have managed to access the data and break it up to how i would like to see it on the LCD. I am also able to get the standby screen to work, showing a clock and the words, "No Active Rescues" when there's no data to show.
The problem i am having is when the display switches between the standby screen to the data screen and back again. The LCD starts to show gibberish and the only way to get to work again is to turn the Raspberry Pi off and restart it. But as soon as data comes in, reverting from the standby screen, gibberish shows again.
After scouring the internet it appears that the LCD might be receiving two or more message packets at the same time (because of the multiprocessing) resulting with the gibberish display, but the code seems fine to me (using print to show the results seems to suggest this). I have tried threading and multiprocessing, and Pipe() following some suggestions but can't get it to work. The problem with Pipe is that i need to send two different messages to one end (not at the same time) using the other end to receive the message. I might not have fully comprehended how to use this bit of code (i'm kinda new to Python), so any help would be appreciated.
PLEASE, PLEASE any help would be appreciated. I've been stuck on this for days
ADDN: I had tried a bit of code from another source where they too had problems like mine, so they created a class, thus:
Code: Select all
Class mem:
lcd_connection = Pipe()
Code: Select all
import requests
from time import sleep
import time
import datetime
import multiprocessing as mp
# import I2C_LCD_Driver
# mylcd = I2C_LCD_Driver.lcd()
# api URL
rescue_url = "https://api.somesite.com/rescues"
headers = {"Authorization": "Bearer Key"}
json_data = []
dta_bool = False
def lcd_enter(comm):
while True:
ln1, ln2, ln3, ln4 = comm.recv()
# mylcd.lcd_clear()
# mylcd.lcd_display_string(ln1, 1)
# mylcd.lcd_display_string(ln2, 2)
# mylcd.lcd_display_string(ln3, 3)
# mylcd.lcd_display_string(ln4, 4)
print(ln1)
print(ln2)
print(ln3)
print(ln4)
def get_data(url):
try:
data = requests.get(url, headers=headers).json()
data = data['data']
return [dta['attributes'] for dta in data if dta['attributes']['status'] == 'closed']
except requests.exceptions.RequestException:
sleep(5)
data = requests.get(url, headers=headers).json()
data = data['data']
return [dta['attributes'] for dta in data if dta['attributes']['status'] == 'open']
# function to convert string datetime value when rescue commenced (usually entered by Mecha)
def dte_convert(dte):
d1 = datetime.datetime.strptime(dte, "%Y-%m-%dT%H:%M:%S.%fZ") + datetime.timedelta(hours=10)
d2 = datetime.datetime.now() # set variable to current dateTime
dur_s = (d2 - d1).total_seconds() # similar to datediff and calculates to seconds
dy = divmod(dur_s, 86400)
hrs = divmod(dy[1], 3600)
mins = divmod(hrs[1], 60)
ret = '%d d %d h %d m' % (dy[0], hrs[0], mins[0])
return ret
def clock(comm):
while True:
if not json_data:
try:
comm.send(("Date: %s" % time.strftime("%d/%m/%Y"),
"Time: %s" % time.strftime("%H:%M:%S"),
'No Active Rescues',
''))
# sleep(1)
def main(comm):
while True:
global json_data
json_data = get_data(rescue_url)
if json_data:
for each in json_data:
comm.send(("C: %s" % each['client'],
"P: %s" % each['platform'] + " CR: %s" % each['codeRed'],
"S: %s" % each['system'],
"A: %s" % dte_convert(each['createdAt'])))
else:
comm.close()
sleep(5)
if __name__ == '__main__':
j_data, c_data = mp.Pipe(False)
t_main = mp.Process(target=main, args=(c_data,))
t_clock = mp.Process(target=clock, args=(c_data,))
lcd_proc = mp.Process(target=lcd_enter, args=(j_data,))
t_main.start()
t_clock.start()
lcd_proc.start()
# t_main.join()
# t_clock.join()
# lcd_proc.join()