Good morning Neil...thank you for your reply. I am uncertain of what you're asking for so here is my best stab at your request...***UPDATED CODE from my newly found aquaponics code from 2021***Can you post an example of the readline from the Arduino.
My crude method splits the received string into 9 variables:Then writes into the mariadb (I use mysql.connector)Code:
# dummy read line of valuesmyline="1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9"myvals=myline.split(",")temp = myvals[0]humid = myvals[1]bar = myvals[2]rain = myvals[3]uva = myvals[4]uvb = myvals[5]uvi = myvals[6]power = myvals[7]intensity = myvals[8]The fields in the table are all defined as DECIMAL(6,2) NOT NULLCode:
rec_add = ("INSERT INTO test ""(temp, humid, bar, rain, uva, uvb, uvi, power, intensity) ""VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")rec_data =(temp, humid, bar, rain, uva, uvb, uvi, power, intensity)
Code:
#!/usr/bin/env python3# -*- coding: utf-8 -*-#comms.pyimport serial#import mariadbimport sysimport timeFIRST_LABEL = 'UV sensor connected'LAST_LABEL = 'li'results = {}if __name__ == '__main__': ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1) ser.reset_input_buffer() while True: if (ser.in_waiting > 0): line = ser.readline().decode('utf-8').rstrip() parts = line.split(",") if line == "": continue if parts[0] == FIRST_LABEL: results = {} if results == None: continue if parts[0] == LAST LABEL: ProcessData(results) def ProcessData(results): try: conn = mariadb.connect(user="bill", password="Sarah1113!", host="127.0.0.1", port=3306, database="Temps") except mariadb.Error as e: print(f"Error connecting with Mariadb: {e}") sys.exit(1) cur = conn.cursor() try: cur.execute("INSERT INTO weather (temp, humid, bar, rain, uva, uvb, uvi, power, intensity) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", (results[temp_f], results[h], results[press], results[rainfall], results[Uva], results[Uvb], results[Uvi], results[Uvi2], results[li])) except mariadb.Error as e: print(f"Error: {e}") conn.rollback() conn.commit() conn.close() results = NoneCode:
if (ser.in_waiting > 0): line = ser.readline().decode('utf-8').rstrip() parts = line.split(",")Statistics: Posted by Osprey72 — Tue Apr 15, 2025 12:23 pm