I'm looking to control a script via Zigbee/XBee using X-CTU. I've created a script named zb_control.py
. Now I'm trying to start and stop another script within this script. A script adxl345test.py
is used to collect data from an attached accelerometer on my Raspberry Pi.
The idea behind the zb_control.py
script is that I run it and then if I type "run" in X-CTU the script will start running adxl345test.py
and collect data.
I'm trying to create a script within a script that can also be stopped again and then still have the zb_control.py
running ready to recieve new input from X-CTU.
As you can tell I've tried different things:
import serial, time, sys, os, subprocess
from subprocess import check_call
from subprocess import call
while True:
ser=serial.Serial('/dev/ttyUSB0',9600,timeout=2)
inc=ser.readline().strip()
if inc=='run':
print("---------------")
print("Collecting data")
print("---------------")
p = subprocess.Popen("/home/pi/adxl345test.py", stdout=subprocess.PIPE, shell=True)
elif inc=='stop':
# check_call(["pkill", "-9", "-f", adxl345test.py])
# serial.write('\x03')
# os.system("pkill –f adxl345test.py")
# call(["killall", "adxl345test.py"])
p.kill()
print("-----------------------")
print("Script has been stopped")
print("-----------------------")
I got it to run and it's now collecting data properly. However now the problem is stopping the adxl345test.py again. As you can tell from the script from above I'm using p.kill() but the script doesn't stop collecting data. When I type "stop" in XCTU my zb_control.py
does print the print-commands but the p.kill()
isn't being executed. Any suggestions?
I've tried using p.terminate()
alone and together with p.kill()
aswell as the commands by themselves however it doesn't stop the adxl345test.py
script. I can tell that the .csv-file is still increasing in size and therefore the script must still be collecting data.