Python脚本不会通过SSH运行,但会在本地运行

时间:2017-09-22 19:47:43

标签: python-3.x ssh raspberry-pi

我正在编写一个小应用程序来使用覆盆子pi来监控某些温度。我希望能够远程启动或重新启动监视脚本。我通过SSH,cd连接到.py文件的目录,然后我“python temp_controller.py&”。这导致我输入错误的错误。 “来自w1thermsensor导入W1ThermSensor”。直接在Pi上从Thonny运行脚本时不会发生此错误。

“主要”文件。

import Send_Email
import Temp_Sensor
import os
import glob
import math
import timefuncs
import apc_controls

program_start_time = timefuncs.get_time() #Used to determine how long the program has been running.
printable_start_time = timefuncs.get_printable_time()
filename = ("Temperature Data " + printable_start_time.strftime("%c") + ".txt")
state = 0 #Used to switch between activities and determine if there has been an error.
temps = [0,0,0,0,0,0,0,0],[0,0] #Holds temperature data
over_temp_counter = 0 #variable for tracking consecutive over temp values

newdir = os.getcwd() + "/temps files"; os.chdir(newdir) #Changes directory to storage location for temperature files
with open(filename, "w+") as tempsfile:
tempsfile.write("Long Term Temperature Monitor Project\r\n")
tempsfile.write("Date-Time,Sensor ID,Sensor Name,Temperature\r\n")
test = 0
while True:
    if (math.floor(timefuncs.get_time())) % 30 == 0 and state == 0:
        print("sample")
        state = 1 #stops this from executing multiple times per second
        length = Temp_Sensor.read_sensors(temps) #gets number of sensors and sensor data with IDs

        #Writes data line to log file
        now = timefuncs.get_printable_time()
        tempsfile.write("%s"%now)
        i = 0
        while i < length:
            print("Sensor %s has temperature %.2f" % (temps[i][0], temps[i][1]))
            tempsfile.write(",%s"%temps[i][0])
            tempsfile.write(",%s"%Temp_Sensor.get_sensor_name(temps[i][0]))
            tempsfile.write(",%f"%temps[i][1])
            i += 1  
        tempsfile.write("\r\n")

        #Checks temperatures to see if over temp
        i = 0
        over = False
        while i < length:
            if Temp_Sensor.check_temp(temps[i][1]):#if over temp
                over = True
                if over_temp_counter > 1:#ensures there is not a single fluke reading that causes error
                    print("over temp")
                    tempsfile.close()#close log file
                    Send_Email.send_fail_email(filename)#send email with log file
                    apc_controls.turn_off_apc()#shut down power to test
                    tempsfile = open("(After error" + printable_start_time.strftime("%c") + ".txt", "w+")
                else:
                    print("increment over")
                    over_temp_counter += 1
            i+=1

        if over == False:
            over_temp_counter = 0





    elif (math.floor(timefuncs.get_time())) % 30 != 0:#if not 30 second increment reset blocker used to prevent the 30 secodn operations from occuring more than once
        state = 0

带错误的文件。

import time
import glob
from w1thermsensor import W1ThermSensor

def read_sensors(data):
i = 0
j = 0
for sensor in W1ThermSensor.get_available_sensors([W1ThermSensor.THERM_SENSOR_DS18B20]):
    data[i][j] = sensor.id
    j+=1
    data[i][j] = sensor.get_temperature()
    i+=1
    j = 0
return i

def get_sensor_name(id):
    if id == "000009ac911f":
        return "Sensor 1"
    elif id == "000009aecc36":
        return "Sensor 2"

def check_temp(value):
    if value > 80:
        return 1
    else:
        return 0

1 个答案:

答案 0 :(得分:0)

我猜您在本地计算机上执行了类似pip install w1thermsensor的操作,对吗?您还需要在Raspberry pi上安装w1thermsensor依赖