Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 sending arduino data to python and use a parity check
i will read sensordata in my arduino(automatic greenhouse), this data will be sent to my rpi. With a python code i will send this arduino data to my database (mysql)
but i have a problem with the reading of the arduino data because this isn't send data around the 10 secondes. for this problem i will use parity check but i don't know how to use this. Can someone help me?
thank you
import serial
import csv
import mysql.connector
from mysql.connector import Error
import time
import datetime as dt
from time import sleep
localtime = time.localtime(time.time())
ser = serial.Serial('/dev/ttyACM0', 9600)
dat = ser.readline().decode()

    connection = mysql.connector.connect(host='localhost',
    if connection.is_connected():
       db_Info = connection.get_server_info()
       print("Connected to MySQL database... MySQL Server version on ",db_Info)      
    while 1:
        dat = ser.readline().decode()
        localtime = time.asctime(time.localtime(time.time()))
        a,b,c,d,e,f,g,h = dat.split(" ; ")
        sql_insert_query = """ INSERT INTO `alle sensoren`(`tijd`, `temperatuurbinnen`,`vochtigheidbinnen`,`temperatuurbuiten`,`vochtigheidbuiten`,`pHwaarde`,`vochtigheidbodem`,
`hoeveelheidwater`,`co2`) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
        cursor = connection.cursor()
        result  = cursor.execute(sql_insert_query, (localtime,a,b,c,d,e,f,g,h,i))
        print ("Record inserted successfully into python_users table")
except mysql.connector.Error as error :
    connection.rollback() #rollback if any exception occured
    print("Failed inserting record into python_users table {}".format(error))

Pls post the errors you are getting.
In Python, time.sleep(n) sleeps for n seconds. So, your sleep command will sleep for 1000 seconds, or about 17 minutes. Was this your intent?
Yoriz likes this post
the error is: finding 4 of the 9 variables. and the time.sleep is right, i will send around the 17 min the sensor data to the database
but the 4 can be a 8 or a 5. If the code is asking for the sensor data and this is on the same time that the arduino send it, it will be send to mysql
Just print(dat) and you'll see why split returns lesser elements as expected.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
the lesser element comes becuase there is a delay in my arduino programma, and i will first save this data in python and then send to mysql. But i don't know how to save this data in python
Use exception handling and skip the lines, which do not have enough fields.

import serial

def do_something(a, b):
    print(a, b)

def read_loop(ser):    
    while True:
        dat = ser.readline().decode()
        localtime = time.asctime(time.localtime(time.time()))
            a,b,c,d,e,f,g,h = dat.split(" ; ")
        except ValueError:
            print('Incomplete data, skipping')
        do_something(a, b)

with serial.Serial('/dev/ttyACM0', 9600) as ser:

My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  send a byte of data by bluetooth to arduino using python. x1cygnus 1 923 Mar-13-2020, 04:54 PM
Last Post: VerlaHiltz

Forum Jump:

Users browsing this thread: 1 Guest(s)