Raspberry Pi Serial Communication with Arduino with Python
up vote
0
down vote
favorite
I want to communicate between my Raspberry Pi and Arduino using Python.
So far the Arduino successfully sends a serial message to the Raspberry Pi and the message is read with the ser.readline() function in Python.
But when I want to blink a led connected to my Raspberry Pi with an IF statement it won't work
The blink() function and everything else works but the code won't go into the IF statement that checks the ser.readline() value with a string variable
This is the code of my Arduino:
String data="hello";
void setup()
// put your setup code here, to run once:
Serial.begin(9600);
void loop()
// put your main code here, to run repeatedly:
Serial.println(data);//data that is being Sent
delay(5000);
And this is the Python code wich runs on my Raspberry Pi:
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
This is what I see in the terminal:
terminal_image
I've been searching for hours trying to find a solution but with no luck.
I've also just started programming in Python.
Thank you for your time.
python arduino raspberry-pi serial-port
add a comment |
up vote
0
down vote
favorite
I want to communicate between my Raspberry Pi and Arduino using Python.
So far the Arduino successfully sends a serial message to the Raspberry Pi and the message is read with the ser.readline() function in Python.
But when I want to blink a led connected to my Raspberry Pi with an IF statement it won't work
The blink() function and everything else works but the code won't go into the IF statement that checks the ser.readline() value with a string variable
This is the code of my Arduino:
String data="hello";
void setup()
// put your setup code here, to run once:
Serial.begin(9600);
void loop()
// put your main code here, to run repeatedly:
Serial.println(data);//data that is being Sent
delay(5000);
And this is the Python code wich runs on my Raspberry Pi:
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
This is what I see in the terminal:
terminal_image
I've been searching for hours trying to find a solution but with no luck.
I've also just started programming in Python.
Thank you for your time.
python arduino raspberry-pi serial-port
Might be a good idea to test with a standard serial terminal app on the Pi just to check the link is working
– Martin Beckett
Nov 6 at 14:22
1
Assuming the indentation shown here accurately reflects your source, yourifstatement and call toblink()aren't inside thewhileloop. They need to be indented.
– larsks
Nov 6 at 14:26
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to communicate between my Raspberry Pi and Arduino using Python.
So far the Arduino successfully sends a serial message to the Raspberry Pi and the message is read with the ser.readline() function in Python.
But when I want to blink a led connected to my Raspberry Pi with an IF statement it won't work
The blink() function and everything else works but the code won't go into the IF statement that checks the ser.readline() value with a string variable
This is the code of my Arduino:
String data="hello";
void setup()
// put your setup code here, to run once:
Serial.begin(9600);
void loop()
// put your main code here, to run repeatedly:
Serial.println(data);//data that is being Sent
delay(5000);
And this is the Python code wich runs on my Raspberry Pi:
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
This is what I see in the terminal:
terminal_image
I've been searching for hours trying to find a solution but with no luck.
I've also just started programming in Python.
Thank you for your time.
python arduino raspberry-pi serial-port
I want to communicate between my Raspberry Pi and Arduino using Python.
So far the Arduino successfully sends a serial message to the Raspberry Pi and the message is read with the ser.readline() function in Python.
But when I want to blink a led connected to my Raspberry Pi with an IF statement it won't work
The blink() function and everything else works but the code won't go into the IF statement that checks the ser.readline() value with a string variable
This is the code of my Arduino:
String data="hello";
void setup()
// put your setup code here, to run once:
Serial.begin(9600);
void loop()
// put your main code here, to run repeatedly:
Serial.println(data);//data that is being Sent
delay(5000);
And this is the Python code wich runs on my Raspberry Pi:
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
This is what I see in the terminal:
terminal_image
I've been searching for hours trying to find a solution but with no luck.
I've also just started programming in Python.
Thank you for your time.
python arduino raspberry-pi serial-port
python arduino raspberry-pi serial-port
edited Nov 10 at 11:57
Sharat Chandra
135
135
asked Nov 6 at 14:09
BoarBoar
11
11
Might be a good idea to test with a standard serial terminal app on the Pi just to check the link is working
– Martin Beckett
Nov 6 at 14:22
1
Assuming the indentation shown here accurately reflects your source, yourifstatement and call toblink()aren't inside thewhileloop. They need to be indented.
– larsks
Nov 6 at 14:26
add a comment |
Might be a good idea to test with a standard serial terminal app on the Pi just to check the link is working
– Martin Beckett
Nov 6 at 14:22
1
Assuming the indentation shown here accurately reflects your source, yourifstatement and call toblink()aren't inside thewhileloop. They need to be indented.
– larsks
Nov 6 at 14:26
Might be a good idea to test with a standard serial terminal app on the Pi just to check the link is working
– Martin Beckett
Nov 6 at 14:22
Might be a good idea to test with a standard serial terminal app on the Pi just to check the link is working
– Martin Beckett
Nov 6 at 14:22
1
1
Assuming the indentation shown here accurately reflects your source, your
if statement and call to blink() aren't inside the while loop. They need to be indented.– larsks
Nov 6 at 14:26
Assuming the indentation shown here accurately reflects your source, your
if statement and call to blink() aren't inside the while loop. They need to be indented.– larsks
Nov 6 at 14:26
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
Python uses spacing to detect blocks of code.
You need to put an if statement with a call to the blink method inside a while loop.
Also you can check incoming serial data by running command on Raspberry pi
sudo cat /dev/ttyUSB0
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
Python uses spacing to detect blocks of code.
You need to put an if statement with a call to the blink method inside a while loop.
Also you can check incoming serial data by running command on Raspberry pi
sudo cat /dev/ttyUSB0
add a comment |
up vote
0
down vote
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
Python uses spacing to detect blocks of code.
You need to put an if statement with a call to the blink method inside a while loop.
Also you can check incoming serial data by running command on Raspberry pi
sudo cat /dev/ttyUSB0
add a comment |
up vote
0
down vote
up vote
0
down vote
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
Python uses spacing to detect blocks of code.
You need to put an if statement with a call to the blink method inside a while loop.
Also you can check incoming serial data by running command on Raspberry pi
sudo cat /dev/ttyUSB0
import serial
import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
ser=serial.Serial("/dev/ttyUSB0",9600) #change ACM number as found from ls /dev/tty/ACM*
ser.baudrate=9600
def setup():
GPIO.setmode(GPIO.BOARD) # Set the board mode to numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
def blink():
print 'led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(1.0) # wait 1 sec
print 'led off'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(1.0) # wait 1 sec
setup()
while True:
serialmessage = ser.readline()
print("serial message is " + serialmessage)
if serialmessage == "hello":
print("message recieved")
blink()
Python uses spacing to detect blocks of code.
You need to put an if statement with a call to the blink method inside a while loop.
Also you can check incoming serial data by running command on Raspberry pi
sudo cat /dev/ttyUSB0
answered 13 hours ago
Karen Simonyan
1616
1616
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53173588%2fraspberry-pi-serial-communication-with-arduino-with-python%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Might be a good idea to test with a standard serial terminal app on the Pi just to check the link is working
– Martin Beckett
Nov 6 at 14:22
1
Assuming the indentation shown here accurately reflects your source, your
ifstatement and call toblink()aren't inside thewhileloop. They need to be indented.– larsks
Nov 6 at 14:26