Page 1 of 1

Dbus doesn't initialize properly at boot in python script

Posted: Sun Jul 14, 2019 12:27 pm
by Alias2364
I have a raspberry pi 3 B+, a raspberry pi Camera, and a robot named Thymio-II.
I also have a python script that launch at boot, this script lauch a tornado website server and initialise the connexion with the Thymio with a usb cable. On the website we have a live video with the camera, and some button to control the robot.
The script work properly if i run it myself, but when i put the command to run the python script in /etc/rc.local, the python script get stuck before the dbus definition. If i run the python script at boot with a log file, the communication is launch, i have the "continue le programme" and the print settings, but after that the progam stop, and i have no error.

Code: Select all

import dbus
import dbus.mainloop.glib
import gobject
import sys, traceback
import os
import tornado.websocket
import threading
import time
from subprocess import call

call(["sudo asebamedulla ser:device=/dev/ttyACM0 &"], shell=True)
time.sleep (10)
print ("continue le programme")

#Pour tuer le processus en arriere plan faire :
# sudo killall asebamedulla

# Use the following to start the serial service:
# sudo asebamedulla 'ser:name=Thymio-II'
# or sudo asebamedulla "ser:device=/dev/ttyACM0"

#Tornado Folder Paths
#settings = dict(
#        template_path=os.path.join(os.path.dirname(__file__), "templates"),
#        static_path=os.path.join(os.path.dirname(__file__), "static")
#        )

settings = {'template_path': '/home/pi/tornado/templates', 'static_path': '/home/pi/tornado/static'}

print settings

### Where the program stop working at boot ###

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
network = dbus.Interface(bus.get_object('ch.epfl.mobots.Aseba', '/'), dbus_interface='ch.epfl.mobots.AsebaNetwork')

PORT = 80 #Tonado server port
At the beginning i thought that dbus doesn't have enough time to start so i added 60 seconds of delay in background like "sleep 60 && sudo python /home/pi/tornado/raspthymio.py &", but it doesn't solve the problem so i do not have any ideas of where it could come from. So please help me

Re: Dbus doesn't initialize properly at boot in python script

Posted: Sun Jul 14, 2019 2:45 pm
by topguy
Dbus is often started together with the Desktop/X-server. Is the desktop loaded when you run the script manually ?

Re: Dbus doesn't initialize properly at boot in python script

Posted: Sun Jul 14, 2019 4:19 pm
by Alias2364
topguy wrote:
Sun Jul 14, 2019 2:45 pm
Dbus is often started together with the Desktop/X-server. Is the desktop loaded when you run the script manually ?
When i run it manually the GUI desktop is launch, and when i put the script in rc.local i make sure that the desktop is start, i put a delay of 60 seconds

Re: Dbus doesn't initialize properly at boot in python script

Posted: Sun Jul 14, 2019 10:28 pm
by topguy
Alias2364 wrote:
Sun Jul 14, 2019 4:19 pm
topguy wrote:
Sun Jul 14, 2019 2:45 pm
Dbus is often started together with the Desktop/X-server. Is the desktop loaded when you run the script manually ?
When i run it manually the GUI desktop is launch, and when i put the script in rc.local i make sure that the desktop is start, i put a delay of 60 seconds
Two things..
- In rc.local the script is run as "root" and not as the user "pi", this can be changed by using the "su" command. If its meant to run as "root" you dont need sudo.
- You might need to set the "DISPLAY" environment variable.

Code: Select all

sleep 60 && DISPLAY=:0 python /home/pi/tornado/raspthymio.py &

Re: Dbus doesn't initialize properly at boot in python script

Posted: Mon Jul 15, 2019 10:12 am
by Alias2364
topguy wrote:
Sun Jul 14, 2019 10:28 pm
Alias2364 wrote:
Sun Jul 14, 2019 4:19 pm
topguy wrote:
Sun Jul 14, 2019 2:45 pm
Dbus is often started together with the Desktop/X-server. Is the desktop loaded when you run the script manually ?
When i run it manually the GUI desktop is launch, and when i put the script in rc.local i make sure that the desktop is start, i put a delay of 60 seconds
Two things..
- In rc.local the script is run as "root" and not as the user "pi", this can be changed by using the "su" command. If its meant to run as "root" you dont need sudo.
- You might need to set the "DISPLAY" environment variable.

Code: Select all

sleep 60 && DISPLAY=:0 python /home/pi/tornado/raspthymio.py &
I tried to run with the DISPLAY définition but it doesn’t seem to work, i also tried to run it as pi user with su but irt says « / usr / bin / python: / usr / bin / python can not execute the binary file »

Re: Dbus doesn't initialize properly at boot in python script

Posted: Mon Jul 15, 2019 10:23 am
by RaTTuS
use the ldxe autostart stuff and not rc.local

Re: Dbus doesn't initialize properly at boot in python script

Posted: Tue Jul 16, 2019 12:55 pm
by Alias2364
RaTTuS wrote:
Mon Jul 15, 2019 10:23 am
use the ldxe autostart stuff and not rc.local
So i have tried the ldxe autorun but it seem to have some problem :

Code: Select all

(EE) Server terminated with error (1). Closing log file.
xinit: giving up
xinit: unable to connect to X server: Connection refused
xinit: server error
in the /etc/xdg/lxsession/LXDE-pi/autostart i put "@/usr/bin/python /home/pi/tornado/raspthymio.py", it also doesn't work, and i also want to run it in background with root permission

Re: Dbus doesn't initialize properly at boot in python script

Posted: Tue Jul 16, 2019 9:33 pm
by Alias2364
I finally managed to make LXDE work properly, the issue was that in crontab i leaved the python script launcher and for a certain raison it finally work so thank you for everybody