Hi,
I'm using an Arduino to collect sensor data which is compiled into a string. The data is recorded perhaps once every five minutes. From the Arduino it is going to be sent over USB to a computer running a Python program that will eventually store the data in an SQLLite database.
I've installed the Python serial library and tested that it is correctly installed and can talk to the Arduino.
My problem is that when I want to send data from the Arduino to the PC. I've written a test program to show my (lack of) understanding which sends the value of a counter once every second. The minimal Arduino program looks like this:
int counter = 0;
void setup() {
Serial.begin(9600); // set the baud rate
Serial.println("Ready"); // print "Ready" once
}
void loop() {
Serial.println(counter); // send the data back in a new line
counter++;
delay(1000); // delay for 1 second
}
And the Python program looks like this:
from time import sleep
import serial
ser = serial.Serial('/dev/tty.usbmodem1d21', 9600)
newInput = ""
while True:
newInput = ser.readline()
print newInput # Read the newest output from the Arduino
sleep(1) # Delay for one second
On the Arduino side, the serial monitor shows numbers counting up normally. On the Python side I get output such as:
1
2
4
or
231
23
These are always followed by an error message:
Traceback (most recent call last):
File "/Users/mikerichards/Desktop/sqlTest.py", line 7, in
newInput = ser.readline()
File "/Library/Python/2.7/site-packages/serial/serialposix.py", line 460, in read
raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
SerialException: device reports readiness to read but returned no data (device disconnected?)
I'm guessing all of these problems are caused by lack of synchronisation between the Arduino and the computer, but I can't find a way of getting the Python program to regularly pick up the correct data without missing any values and without trying to take data from an empty buffer.
If anyone has a solution how I can do this, please let me know. I'm very much a beginner with both Arduino and Python so I'm probably making all sorts of newbie errors here. I have tried Google searches and some tutorials, but I keep ending up with the same problem.
Many thanks in advance,
Mike.