Dan,Your code is quite complicated. Maybe you just need to fill the data in dictionary and just check when is the last variable.
I also add a Validate function just in case you didn't received the corresponding variable. Presently it returns "" if the key is not there but you could put something else.
This way is quite simple you loop and record all name=value into dictionary. When the last item is received you simply process all your data and clear the dictionary to start again. Way more simple. No need to check the start , just the last item.Code:
def Validate(results, key) if results.__contains__(key): return results[key] else: return ""def ProcessData(results): try: print("Connecting...") conn = mariadb.connect(user="$$$$$$$$$$$$$$$", password="**********************", host="localhost", port=# , database="heater") cur=conn.cursor() print("Sending data to database...") cur.execute("INSERT INTO heater (hot_fluid_temp, air_temp, left_tank_temp, right_tank_temp) VALUES (%s, %s, %s, %s)", (Validate(results, HOT_FLUID_TEMP), Validate(results, AIR_TEMP), Validate(results, LEFT_TANK_TEMP), Validate(results, RIGHT_TANK_TEMP))) print(Validate(results, HOT_FLUID_TEMP), Validate(results, AIR_TEMP), Validate(results, LEFT_TANK_TEMP), Validate(results, RIGHT_TANK_TEMP)) conn.commit() conn.close() except mariadb.Error as e: print("Error connecting to MariaDB Platform: {e}", e) exit(1)def Webpage(results): print("webpage") file=open("/var/www/html/data.txt", "w") file.write(results[what_was_suppose to be the value]) file.close() file=open("/air.txt", "w") file.write(results[what_was_suppose to be the value]) file.close()# create a empty dictionaryresults={}while True: line=ser.readline().decode('utf-8').rstrip() if len(line) == 0: continue parts=line.split("=") if len(parts) == 2: # ok we got variable name and value results[parts[0] = parts[1] if parts[0] == LAST_LABEL: # all data are suppose to be in the dictionary # process your stuff here processData(results) WaterLevel(result) # don't know what it does WebPage(results) results.clear() # clear the dictionary here
I implemented your code update...had to change a line due to syntax issue...
results[parts[0] = parts[1]
-> results[parts[0]] = results[parts[1]]
now, the syntax is correct but we are back to the KeyError: ' 71.15' (current temp of the water) and it points to that results[parts[0]] line
also had an indent error with the ProcessData(results) and down...took care of that
I also see no call for Validate(results, key) nor do I see where key is assigned anything. Am I missing something?
Statistics: Posted by Osprey72 — Sun May 05, 2024 3:42 pm