Search code examples
pythonarduinoraspberry-piserial-portpopen

Sending signal from Arduino serial to a Python program


I'm new to Python and Raspberry Pi and was hoping to use them for a project with an Arduino. Basically, I want to push a button connected to an Arduino, then have that input play a video file on the raspberry pi.

I'm having some difficulty doing so via serial data. I can connect serial messages between the Arduino and Pi, ie: "Hello World", but can't quite figure out how to open a video file. Right now, when I run the Python code, nothing appears and the button presses are unresponsive. I have the right USB port since it does work with other programs that solely print the serial data. How exactly do I resolve this issue?

In short, the end goal is "Push a button-->play a video."

Arduino Code:

int pushButton=2;
int buttonState=0;

void setup()
{
    serial.Begin(9600);
    pinMode(pushButton, INPUT);
}

void loop()
{
    int buttonState=digitalRead(pushButton);
    if (buttonState==HIGH)
    {
        Serial.println("a");
        delay(100);
    }
    if (buttonState==LOW)
    {
        //do nothing
    }
}

Python Code:

import sys
import os
from subprocess import Popen
import serial

movie1=("/home/pi/Videos/test.mp4")

ser = serial.Serial('/dev/ttyUSB0',9600)

while True:
    data = ser.read()

    if data=="a":
        os.system('killall omxplayer.bin')
        print("a")
        omxc = Popen(['omxplayer','-b', movie1])

Solution

  • First I think that it is not necessary to send strings for communication in serial, you can just send bytes. The problem is when you use Serial.println("Something") you are sending Something\r\n so on the other device you should check it with Something\r\n, and as in the comments said, you can put a debug print to make sure your data is alive and the connection is OK. You can also add acknowledge on your python code to respond to the command to make sure the data has been sent and don't send another one. Arduino Code:

    int pushButton=2;
    int buttonState=0;
    
    void setup()
    {
        serial.Begin(9600);
        pinMode(pushButton, INPUT);
    }
    
    void loop()
    {
        int buttonState=digitalRead(pushButton);
        if (buttonState==HIGH)
        {
            Serial.print('1');
            delay(100);
        }
        if (buttonState==LOW)
        {
            //do nothing
        }
    }
    

    Python code:

    from subprocess import Popen
    import serial
    
    movie1 = "/home/pi/Videos/test.mp4"
    
    ser = serial.Serial('/dev/ttyUSB0',9600)
    
    while True:
        command = ser.read()
        if command:
            # flush serial for unprocessed data
            ser.flushInput()
            print("new command:", command)
            if str(command) == '1':
                print("Playing movie")
                Popen('killall omxplayer.bin')
                Popen(['omxplayer','-b', movie1])
            else:
                print("Not a valid command")