need to clean up serial output to python

Doing test runs for a larger program that will end up being a bot written in python. The bit I'm working on at the moment is to send sensor data to a Python program that uses the data for actions. Running Arduino 1.0 and a Mega ADK board, Python 2.73, Ubuntu Precise on a 64 bit HP G62. For the "figuring it out stage" I'm just running 1 sensor, a photoresistor, set up in series with a 10k resistor, 5v to the hanging end of the 10k, hanging end of the photoresistor to GND, the joint between the 10k and the PR to pin A0 on the board.

Everything works fine except the data output to the python program is too verbose if you will. I just want the number (example: 246), what I'm getting is '246\r\n'. I could use some help.

Here's the Arduino code:

int sensorPin = A0;    // select the input pin for the photo resistor
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600); //Begin serial communication  
}

void loop() {
  // read and send to serial the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  Serial.println(sensorValue);          

  delay(1000);                  
}

and here's the Python code:

import serial
arduino = serial.Serial('/dev/ttyACM0', 9600)
while 1:
    arduino.readline()

regards, Richard

  Serial.println(sensorValue);

Perhaps a little time in the reference section, looking at the differences between the print() and the println() methods would be a useful expenditure of your time.

the \r\n is the newline, you need some char anyway as separator between values.
but it might be easier to separate the measurements if there is only a comma between them like this.
Rewrote your prog so you do not use delay() which blocks the sketch so you can add other actions if needed

give it a try

#define INTERVAL 1000   // millisecs between readings
unsigned long lastTime = 0;

int sensorPin = A0;    // select the input pin for the photo resistor
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() 
{
  pinmode(13, OUTPUT); // on board led
  Serial.begin(9600); //Begin serial communication  
}

void loop()
{
  unsigned long now = millis();
  if (now - lastTime > INTERVAL)
  {
    lastTime = now;
    // read and send to serial the value from the sensor:
    sensorValue = analogRead(sensorPin);    
    Serial.print(sensorValue);          
    Serial.print(',');
    digitalWrite(13, !digitalRead(13)); // flip the led when a sample is sent
  } 
  // here you can do other things, 
}

Thanks for the idea, but not quite working. With your prog I get nothing. I know the Arduino is transmitting, the LED is blinking and I ran into this problem earlier when working on setting this up. The problem is pyserial reads anything that ends with a newline ("\n"). I think I’m going to have to massage the Arduino output in Python to make it work. Your help did give me the thought of sending the output either to a file (clunky) or as a python dictionary though (might do the trick). Thanks for the help!

regards, Richard

The problem is pyserial reads anything that ends with a newline ("\n").

It does. It should not be including the \n in the string, though. Even if it does, it should be trivial to replace it with a NULL, effectively shortening the string.

Of course, this means that you need to send the \n and \r, using the println() method.

$ python
Python 2.6.8 (unknown, Apr 19 2012, 01:24:00) 
…
>>> foo = "some text\r\n"
>>> foo
'some text\r\n'
>>> foo.strip()
'some text'
>>>

-br

Thanks billroy, that did the trick.

regards, Richard