Pages: [1]   Go Down
Author Topic: serialpy and Arduino give unreliable outputs  (Read 805 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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:
void setup() {
  Serial.begin(9600);
 
  Serial.println("hello python!");
}

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

And my Python code is:
Code:
import serial
import time

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

while 1:
    status = arduino.readline()
    print(status)
    time.sleep(2)
    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?
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 514
Posts: 31541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

According to this tutorial I followed:
http://wellsb.com/post/5135881420/arduino-to-twitter-over-usb

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 smiley-sad
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46227
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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).
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

-br
Logged

Pages: [1]   Go Up
Jump to: