1

I made a project recently on my raspberry to read a 4 byte signal coming from TCP/IP protocol and play the adequate video depending on the signal (0001, 0002, 0101 or 0102).

I have 2 problems with that :

1- The code works fine when executed manually but if I launch it on reboot using cron, the code is running, but not working properly.

2- The code works (manually) on the Pi I coded it on, but if I transfer the files to the other Pis I need it on, I get the EXACT same error (launching it manually) as if I launch it through cron on the 1st Pi.

The execution error I'm mentioning here is the following : The dbus command doesn't stop the videos, it's not responding.

The code is in python 3, all the raspberries are have the exact same parameters, I took the files as they were on the 1st Pi and copied them on the others.

my cron file is

@reboot python /home/v1_M.py &

my code is :

from time import sleep
import subprocess
import os
import socket
import sys

play_video1 = None
play_video2 = None
play_video3 = None
play_video4 = None

vid_block = 0
check = None
data = None

try:

    while True:

        subprocess.Popen(["nc -l 1024 -i 1 > /home/resultat.txt"], shell=True)#Listens to port 1024 and writes data in txt file
    sleep(2)
    a=open('/home/resultat.txt','r')
    lines = a.readlines()
    a.close()
    if lines:
        signal = lines[-1]#signal in bytes
        data = signal[-4:]#converts signal to string
        #print (signal)
    subprocess.Popen(["> /home/resultat.txt"], shell=True)#Empties txt file

    #print (data)

    if data != check :
        vid_block = 0
        check = data

    if data == '0101' and vid_block == 0 : # video 1
        os.system('dbuscontrol.sh stop')
        play_video1 = subprocess.Popen(['omxplayer -o hdmi -r --no-osd --aspect-mode fill /home/misano_iway_v3.mp4'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        shell=True)
        vid_block = 1

    elif data == '0102' and vid_block == 0 : # video 2
        os.system('dbuscontrol.sh stop')
        play_video2 = subprocess.Popen(['omxplayer -o hdmi -r --no-osd --aspect-mode fill /home/indy_iway_v2.mp4'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        shell=True)
        vid_block = 1

    elif data == '0001' and vid_block == 0: # Video logo
        os.system('dbuscontrol stop')       
        play_video3 = subprocess.Popen(['omxplayer -r --no-osd --loop --aspect-mode fill /home/Logo.mp4'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        shell=True)
        vid_block = 1

    elif data == '0002' and vid_block == 0: # Video logo
        os.system('dbuscontrol.sh stop')
        play_video4 = subprocess.Popen(['omxplayer -r --no-osd --loop --aspect-mode fill /home/Logo.mp4'],
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                        shell=True)
        vid_block = 1


except KeyboardInterrupt:
    print("Exit")
    data = None
    os.system('dbuscontrol.sh stop')

I know my code isn't pretty but I'm quite new to python and raspberry and I just want to make it work for now, idc about optimization.

I'd really appreciate if you guys can help.

Maxor4
  • 11
  • 2

1 Answers1

0

Update : nevermind, I just used os.system('killall omxplayer.bin')

A bit brutal but works fine and at least I'm pretty sure I don't have any leftover unclosed process

Maxor4
  • 11
  • 2