Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8374

Python • Re: Arduino-Pi Weather Station issue

$
0
0
Can you post an example of the readline from the Arduino.
My crude method splits the received string into 9 variables:

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]
Then writes into the mariadb (I use mysql.connector)

Code:

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)
The fields in the table are all defined as DECIMAL(6,2) NOT NULL
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***

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 = None
I believe you're asking for this specifically...

Code:

if (ser.in_waiting > 0):            line = ser.readline().decode('utf-8').rstrip()            parts = line.split(",")
Does this fall in line with what your code is trying to help me with? This new update is working on the arduino/pi setup in my garage and at last check (my wifi chip is fried so no way to putty in at this time - Pi2b with dongle...refuses to accept new wifi and bluetooth dongles...need to hit the lotto to buy another 3b+ :lol: ), the pi was recording the 4 parameters into MySQL as they were passed from the arduino to the pi.

Statistics: Posted by Osprey72 — Tue Apr 15, 2025 12:23 pm



Viewing all articles
Browse latest Browse all 8374

Trending Articles