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.










share|improve this question























  • 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 if statement and call to blink() aren't inside the while loop. They need to be indented.
    – larsks
    Nov 6 at 14:26














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.










share|improve this question























  • 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 if statement and call to blink() aren't inside the while loop. They need to be indented.
    – larsks
    Nov 6 at 14:26












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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, your if statement and call to blink() aren't inside the while loop. 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







  • 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















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












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





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer
























      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





      share|improve this answer






















        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 hours ago









        Karen Simonyan

        1616




        1616



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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














































































            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands