Python脚本在手动运行时工作,但在启动时不工作:";“字符串索引超出范围”;

Python脚本在手动运行时工作,但在启动时不工作:";“字符串索引超出范围”;,python,Python,我试图弄明白为什么在通过命令行运行该脚本时,它会运行得非常好,但当我尝试在启动时使用crontab运行它时,它会给我一个错误:indexer:string index out-range 背景: 我的脚本以两位数的形式跟踪WIFI信号强度,并根据信号强度在我的车上执行锁定和解锁功能,具体取决于我与手机的距离 我想在启动时运行脚本的原因是,我的Pi配置为连接到我的手机Wifi热点。当我通过SSH手动运行python脚本时,SSH将断开连接,如果超出范围,脚本将停止。当我在范围内时,pi将自动重新连

我试图弄明白为什么在通过命令行运行该脚本时,它会运行得非常好,但当我尝试在启动时使用crontab运行它时,它会给我一个错误:
indexer:string index out-range

背景

我的脚本以两位数的形式跟踪WIFI信号强度,并根据信号强度在我的车上执行锁定和解锁功能,具体取决于我与手机的距离

我想在启动时运行脚本的原因是,我的Pi配置为连接到我的手机Wifi热点。当我通过SSH手动运行python脚本时,SSH将断开连接,如果超出范围,脚本将停止。当我在范围内时,pi将自动重新连接到我的手机热点,但每次我都必须手动重新启动脚本

给出错误的行(仅在启动时运行时)是:

这是怎么回事?为什么只有当我尝试在启动时作为后台脚本运行时才会发生这种情况。否则,它只输出两位数的信号强度,并按预期工作

这是完整的代码。这个概念是当我用手机靠近我的车时,它会自动解锁。当信号强度随着我离得更远而减弱时,汽车会自动锁定

我在代码中添加了一些注释来解释发生了什么,我不得不做一些聪明的事情来获取Wifi信号作为一个数字。(为什么这么简单的事情这么难做?)

来自子流程导入的
*
将RPi.GPIO作为io导入
导入时间
io.setmode(io.BCM)
io.setwarnings(错误)
UNLK=16
锁=12
io.setup(解锁、io.OUT)
io.setup(锁定、io.OUT)
io.输出(UNLK,1)
io.输出(锁,1)
##我在网上找到一个脚本,用谷歌搜索不同的方式获取Wifi信号
##Python,这是我能找到的最有效的方法。我真的不知道grep,Popen和一些
##这些命令中的一个是,但是当我添加下面的索引部分时,它就可以工作了。
def getQuality():
shell_cmd='iwconfig{}| grep Link'。格式('wlan0')
proc=Popen(shell_cmd,shell=True,stdout=PIPE,stderr=PIPE)
输出,err=proc.communicate()
msg=output.decode('utf-8').strip()
##我在最终弄清楚如何使用括号[]对输出进行“索引”后添加了这个块
##上一个块,仅获取两位数字(输出的位置13和14)(输出为
##始终链接质量=??/70,其中??是线路的第13和第14个位置。)
wifi=int(msg[13]+msg[14])35;#这是一行代码,这两行代码都为我提供了使脚本正常工作所需的神奇数字,但也是在启动时尝试运行的唯一不喜欢的代码
返回wifi
###下面的一切都很好#####
###lockstat.txt是一个包含1或0的txt文件,用于跟踪我的汽车的锁定状态
def getlock():
打开(“lockstat.txt”,“r”)为锁定状态:
如果“1”处于锁定状态:
返回真值
尽管如此:
如果getLocked():
如果getQuality()>=35:
打印(“解锁”)
io.输出(16,0)
时间。睡眠(.1)
io.输出(16,1)
打开(“lockstat.txt”、“w”)时锁定:
已锁定。写入('0')
其他:
如果getQuality()<35:
打印(“锁定”)
io.输出(12,0)
时间。睡眠(.1)
io.输出(12,1)
打开(“lockstat.txt”、“w”)时锁定:
已锁定。写入('1')
打印(getQuality())
时间。睡眠(1)

检查字符串的长度
msg
,它应该大于>=15才能得到
msg[14]
。嗯,我在为
特斯拉开发应用程序时遇到了同样的问题。我可以为您确认,启动完成后,您需要在5秒钟左右进行睡眠,以便将设备连接到
WIFI
网络!这是allUse
try:wifi=int(msg[13]+msg[14])返回wifi
除了:打印(“尚未连接到wifi的设备”)返回(0)
谢谢您的建议!尝试删除错误消息。但是,即使连接了Wifi,try:expression每次都会失败,脚本似乎什么也没做。然后打印异常并查看错误所在。使用
例外情况除外,例如e:print(str(e))
。根据@aԋɱҽԃαєιcαη,您需要让它睡眠并连接到Wifi。可能尝试睡眠和循环直到
len(msg)>=14
wifi = int(msg[13] + msg[14])
IndexError: string index out of range
from subprocess import *
import RPi.GPIO as io
import time

io.setmode(io.BCM)
io.setwarnings(False)

UNLK = 16
LOCK = 12

io.setup(UNLK, io.OUT)
io.setup(LOCK, io.OUT)
io.output(UNLK, 1)
io.output(LOCK, 1)

##I took this block from a script I found online when Googling different ways to get the Wifi signal in

##Python, this is the best one I could find that worked. I have really no idea what grep, Popen and some 

##of these commands are, but it works when I add the indexing part below.

def getQuality():
    shell_cmd = 'iwconfig {} | grep Link'.format('wlan0')
    proc = Popen(shell_cmd, shell=True, stdout=PIPE, stderr=PIPE)
    output, err = proc.communicate()
    msg = output.decode('utf-8').strip()

##I added this block after eventually figuring out how to use the brackets [] to "index" the output of 

##the previous block and only get the two digits (position 13 and 14 of the output) (the output is 

##always Link Quality = ??/70 where ?? are the 13th and 14th position of the line.)

    wifi = int(msg[13] + msg[14]) ##this is the line that both gives me the magic number that I need to make this script work, but also the only line that trying to run on startup doesn't like

    return wifi

### Everything below works fine #####
### lockstat.txt is a txt file with either 1 or 0 to track the Locked status of my car ## 

def getLocked():
    with open("lockstat.txt", "r") as LOCKED:
        if '1' in LOCKED:
            return True

while True:

    if getLocked():
        if getQuality() >= 35:
            print("Unlocked")
            io.output(16, 0)
            time.sleep(.1)
            io.output(16, 1)
            with open("lockstat.txt", "w") as LOCKED:
                LOCKED.write('0')
    else:
        if getQuality() < 35:
            print("Locked")
            io.output(12, 0)
            time.sleep(.1)
            io.output(12, 1)
            with open("lockstat.txt", "w") as LOCKED:
                LOCKED.write('1')
    print(getQuality())
    time.sleep(1)