Python 脚本在一段时间后崩溃

Python 脚本在一段时间后崩溃,python,Python,我是新来的。:)我的问题是,我为一个门铃编写了一个python脚本。该脚本在树莓圆周率上运行。我把带有磁铁触点的覆盆子放在门上。当你通过电报发送信息“门铃打开”时,脚本就起作用了。如果有人开门,我会收到电报。 但是几分钟后,我再也没有收到任何消息了?有人能看看我做错了什么吗? 多谢各位 #!/usr/bin/env python # -*- encoding: utf-8 -* import time import socket import sys import telepot import

我是新来的。:)我的问题是,我为一个门铃编写了一个python脚本。该脚本在树莓圆周率上运行。我把带有磁铁触点的覆盆子放在门上。当你通过电报发送信息“门铃打开”时,脚本就起作用了。如果有人开门,我会收到电报。 但是几分钟后,我再也没有收到任何消息了?有人能看看我做错了什么吗? 多谢各位

#!/usr/bin/env python
# -*- encoding: utf-8 -*
import time
import socket
import sys
import telepot
import os
from telepot.loop import MessageLoop
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
MAGNET_GPIO = 17
GPIO.setup(MAGNET_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)


UDP_IP = "1.1.1.1.1" 
UDP_PORT = 50000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)


#Voreinstellung Telegramm
bot = telepot.Bot('123456789xxxxxxxxxx')
chat_id = xxxxxxxx1111111xxxxx1111111

telemes=0
door_dummy=0


def action(msg):
        chat_id = msg['chat']['id']
        command = msg['text']
        global telemes
        if command == 'Test':
            bot.sendMessage (chat_id, str("Test is good" ))
        if command == 'Reboot':
            bot.sendMessage (chat_id, str("See you later!" ))
            time.sleep(1.0)
            os.system("sudo reboot -h now")
        if command == 'Shutdown':
            bot.sendMessage (chat_id, str("Byeee!" ))
            time.sleep(1)
            os.system("sudo shutdown -h now")
        if command == 'Doorbell on':
            bot.sendMessage (chat_id, str("Doorbell is switched on"))
            telemes=1
        if command == 'Doorbell off':
            bot.sendMessage (chat_id, str("Doorbell is switched off"))
            telemes=0



MessageLoop(bot, action).run_as_thread()

while True:
    while True:
        if GPIO.input(MAGNET_GPIO) == 1 and telemes==1 and door_dummy==0:
            bot.sendMessage (chat_id, str("Türalarm!" ))
            sock.sendto('Dooralam', (UDP_IP, UDP_PORT))
            door_dummy=1
        if GPIO.input(MAGNET_GPIO) == 0 and telemes==1 and door_dummy==1 :
            door_dummy=0

为什么有两个嵌套的
,而True
循环呢?噢!没错。不知道,我会修好的。你认为这是导致崩溃的原因吗?不,我认为你给设备发送的电话太多了。Try是放置一个
时间。在循环末尾的sleep(0.1)
解决了这个问题。为什么在循环中添加了2次?