Go Down

Topic: serialpy and Arduino give unreliable outputs (Read 1 time) previous topic - next topic



I'm a total Arduino noob and I'm trying to learn some interesting things. I've made some LEDs blink and am fairly confident with the basics of my hardware stuff (read data from pins, send data to pins).

Now I'm trying to hook my Arduino up to Python in order to make a system that can send a Tweet when it's getting dark in my room. I'm using a light sensor for this and am getting the values the way I want them to.

Now in order to send the Tweet it seems I need a Serial connection (so I can tell my computer stuff) and a program that receives the stuff I send to my computer. I'm using pyserial for receiving Arduino's serial data.

In order to try out my serial connection I decided to take out the light sensor and just try sending data from Arduino to Python. I've read that the first time you send Serial data it's common to miss some data due to Arduino resetting or something (correct me if I misunderstood this).

The Tweet I'm trying to send should be something like: "Hey! Its pretty dark in here man!"
When I use Serial.println("Hey! Its pretty dark in here man!"); to write this, my Python script catches a random amount of characters instead of the whole string. Something like:
Hy Its prtty dak in here ma
is not uncommon to show in my terminal.

My Arduino code looks like this:
Code: [Select]

void setup() {
  Serial.println("hello python!");

void loop() {
  Serial.println("Hey! Its pretty dark in here man!");

And my Python code is:
Code: [Select]

import serial
import time

arduino = serial.Serial("/dev/tty.usbmodemfa131", 9600);

while 1:
    status = arduino.readline()
    del status

Nothing to fancy here and probably all wrong. Can anybody explain in a beginners language what I'm doing wrong? And also, how can I correct it to actually work?


It is the python side that is wrong. You need to read bytes out of the serial port not lines, because there is nothing to say when a line ends. Also do not sleep the python code.
Is there a python call that tells you there is a byte waiting to be read?


According to this tutorial I followed:

I should actually be using serial.readline(); because it just waits for an EOF character to come up.
But again, I barely have an idea of how this Serial stuff works so all I have to go from is this forum and the tutorial I just linked to :(


I should actually be using serial.readline(); because it just waits for an EOF character to come up.

So, where are you sending an EOF? You can't expect to read an EOF if you don't send an EOF.

I'd suggest a couple of changes. First, 9600 baud is a stone-age speed. Get it up to something more like last century - 115200.

Second, why do you want to diddle around, potentially missing characters, in the python script? Wake up, man. (Quit sleeping, in other words).
The art of getting good answers lies in asking good questions.


You might also try a short delay after opening the serial port, to give the Arduino bootloader time to do its thing:

Code: [Select]

    import time
    serial.Serial(…)     # open serial port
    time.sleep(1)        # delay one second


Go Up