调试作为服务运行的python脚本

时间:2019-10-18 13:12:50

标签: python raspberry-pi systemd

我正在Raspberry Pi上运行一个简单的python脚本,订阅了MQTT主题以打开和关闭灯。

import RPi.GPIO as GPIO

import context
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time

time.sleep(30)

host="192.168.1.25"
clientID="RPiZero"
topic="bedroom/lighting"

# Set GPIO pins
# Lights
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)

# Set GPIO states
GPIO.output(17, GPIO.LOW)
GPIO.output(22, GPIO.LOW)
GPIO.output(27, GPIO.LOW)
GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)

client=mqtt.Client(clientID)
client.connect(host)
client.subscribe(topic)

message=""

def on_message(client, userdata, message):
    message=str(message.payload.decode("utf-8"))
    #every light
    if message=="aan":
        GPIO.output(17, GPIO.HIGH)
        GPIO.output(22, GPIO.HIGH)
        GPIO.output(27, GPIO.HIGH)
        GPIO.output(23, GPIO.HIGH)
        GPIO.output(24, GPIO.HIGH)
        GPIO.output(25, GPIO.HIGH)
    elif message=="uit":
        GPIO.output(17, GPIO.LOW)
        GPIO.output(22, GPIO.LOW)
        GPIO.output(27, GPIO.LOW)
        GPIO.output(23, GPIO.LOW)
        GPIO.output(24, GPIO.LOW)
        GPIO.output(25, GPIO.LOW)
    #individual lights
    if message=="bedaan":
        GPIO.output(24, GPIO.HIGH)
        GPIO.output(25, GPIO.HIGH)
    elif message=="beduit":
        GPIO.output(24, GPIO.LOW)
        GPIO.output(25, GPIO.LOW)

    if message=="hoekaan":
        GPIO.output(17, GPIO.HIGH)
        GPIO.output(27, GPIO.HIGH)
    elif message=="hoekuit":
        GPIO.output(17, GPIO.LOW)
        GPIO.output(27, GPIO.LOW)

client.on_message=on_message

client.loop_forever()

loop_flag=1
counter=0
while loop_flag==1:
    time.sleep(0.1)
    counter+=1
time.sleep(4)

# Clean up GPIO
GPIO.cleanup()

该脚本作为systemd服务运行,并且可以在大约12个小时内正常运行,之后该服务仍声明已加载:

● receiver.service - Openhab Receiver
   Loaded: loaded (/lib/systemd/system/receiver.service; enabled; vendor preset:
   Active: active (running) since Fri 2019-10-18 01:35:24 CEST; 13h ago
 Main PID: 421 (python)
   Memory: 10.0M
   CGroup: /system.slice/receiver.service
           └─421 /usr/bin/python /home/pi/paho.mqtt.python/examples/receiver.py

Oct 18 01:35:24 raspberrypi systemd[1]: Started Openhab Receiver.

但是,灯不再响应。我被困在如何解决此问题的方法上,不知道该从哪里开始记录日志或执行其他任何操作。据我所知,这可能是内存问题,循环错误或完全不同。在解决此问题的方法方面的所有建议将不胜感激。

0 个答案:

没有答案