if else语句中的Python计时器

时间:2017-04-07 12:00:06

标签: python

我正在尝试创建一个计时器,该计时器在满足if语句的条件时启动,然后停止并返回满足elif条件的持续时间,这可能吗?

该应用程序的目的是从Android应用程序向AWS IoT发送true或false值。 Python脚本订阅了AWS并接收该值并使用它来确定是否应该打开或关闭led。

我的代码:

from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import RPi.GPIO as GPIO
import sys
import logging
import time
from time import sleep
from timeit import default_timer as timer
import getopt
import grovepi

msgpay = None

# Custom MQTT message callback
def customCallback(client, userdata, message):
    print("Received a new message: ")
    print(message.payload)
    print("from topic: ")
    print(message.topic)
    print("--------------\n\n")
    global msgpay
    msgpay = message.payload
   led = 5
   grovepi.pinMode(led, "OUTPUT")
   start_time = timer()
   if msgpay == "true":
       print("turning on")
       grovepi.digitalWrite(led, 1)
       #time.sleep(3)
   elif msgpay == "false":
       print("turning off")
       grovepi.digitalWrite(led, 0)
       duration = timer() - start_time
       print duration

任何帮助如何解决这个问题将不胜感激。

由于

0 个答案:

没有答案