1

Been trying to fix this. The barometer data on the Adruino looks like this:

1008.94 1008.95 1008.94

Which I'm happy with. These get "read" every 10 second for now.

The problem is with my python script. I can read and write all the data to my mySQLdb. But the the data is not always good. It will go like something like:

1008.94 108.95 108.94 1008.96

The "lost zero" or other value always appears on the Adruino's serial monitor.

How can I get the python script to read the whole xxxx.xx line? Just thought even at times of low pressure xxx.xx.

Here is my python code:

import MySQLdb
import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600)
db = MySQLdb.connect("localhost", "root", "pword","weather")
cursor = db.cursor()

while 1:
#       print ("Waiting for data...")
#       print("")
        x = ser.readline()
        clock = (time.strftime("%H:%M:%S"))
        print x
#       print ("Collecting data...")
        x = x
#       print ("Inserting to database...")
#       print ("")
        sql = ("""INSERT INTO WeatherP (pres, Time) VALUES (%s, %s)""", (x, clock,))
        cursor.execute(*sql)
        db.commit()
#

Thank you. Tinus

1 Answers1

0

I found the following here:

ser.read(ser.inWaiting())

According to the pySerial documentation inWaiting() does return the size of the received data in the buffer. This could solve your problem because the read function has a size of data it takes given by a parameter (in your case 1 byte since you haven't specified) if the received data is larger it might cause such problems.

r0w
  • 155
  • 1
  • 13