我想同时运行两个功能,但是一个应该在使用python的rpi3上运行60秒后运行

时间:2018-02-05 07:28:35

标签: python raspberry-pi

我编写了一个代码python来将数据上传到thingspeak网站。我将一个按钮连接到25号针,并有一个代码来计算按下按钮的次数(年龄)。

我想每60秒上传一次'age'的值,但计算按钮按下的代码必须连续运行,这样我就不会错过按任何按钮。以下是代码。

 #!/usr/bin/env python
 import httplib, urllib
 import time
 import RPi.GPIO as GPIO
 import time
 age=15                 //initia;ized to 15 for debugging purposes
 GPIO.setmode(GPIO.BCM)

 GPIO.setup(25,GPIO.IN,GPIO.PUD_DOWN)



 key = ''  

def thermometer():
    global age
    while True:

    temp = age 
    params = urllib.urlencode({'field1': temp,'key':key })
    headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
    conn = httplib.HTTPConnection("api.thingspeak.com:80")
    try:
        conn.request("POST", "/update", params, headers)
        response = conn.getresponse()
        print temp
        print response.status, response.reason
        data = response.read()
        conn.close()
    except:
        print "connection failed"
    break
   if __name__ == "__main__":
      try:
            while True:
                    a=GPIO.input(25) //checks whether input is high or low
                    print (age)      //prints the count 
                    if a==1:         //condition
                            age+=1   //increments every time button is pressed
                    time.sleep(1)
                    thermometer()

    except KeyboardInterrupt:
            GPIO.cleanup()
            print 'EXITING'

我需要每60秒上传一次数据。上面的代码每秒都会上传一次。

2 个答案:

答案 0 :(得分:1)

函数 time.time()将自纪元以来的秒数作为浮点数返回,即从 UTC 00:00:00 1970年1月1日起经过的时间

sendTime = time.time() + 60 - >这是为时钟添加60秒并将其存储在变量中,即 sendTime

在你的函数thermometer()中,我添加了一个条件来检查系统时间(即time.time())是否具有比sendTime更大的值。

如果满足条件,数据将被上传,如果没有,则循环中断并返回到已在main()

中写入的while循环
#!/usr/bin/env python
import httplib, urllib
import time
import RPi.GPIO as GPIO
import time
age=15                 //initia;ized to 15 for debugging purposes
GPIO.setmode(GPIO.BCM)
sendTime = time.time() + 60 # runs for 60 sec ( 60*15) --> runs for 15 mins
GPIO.setup(25,GPIO.IN,GPIO.PUD_DOWN)
key = ''  

def thermometer():
    if time.time() > sendTime:  # Check if the is greater than sendTime
        sendTime += 60     #Resets the timer 60 seconds ahead
        global age
        while True:

        temp = age 
        params = urllib.urlencode({'field1': temp,'key':key })
        headers = {"Content-typZZe": "application/x-www-form- urlencoded","Accept": "text/plain"}
        conn = httplib.HTTPConnection("api.thingspeak.com:80")
        try:
           conn.request("POST", "/update", params, headers)
           response = conn.getresponse()
           print temp
           print response.status, response.reason
           data = response.read()
           conn.close()
        except:
            print "connection failed"
            break
   else
        break

   if __name__ == "__main__":
      try:
        while True:
                a=GPIO.input(25) //checks whether input is high or low
                print (age)      //prints the count 
                if a==1:         //condition
                        age+=1   //increments every time button is pressed
                time.sleep(1)
                thermometer()

except KeyboardInterrupt:
        GPIO.cleanup()
        print 'EXITING'

希望这有效。

答案 1 :(得分:0)

使用模数函数%,如果age的其余部分为age / 60,则会在0上传。否则,条件评估为False

while True:

    a=GPIO.input(25) //checks whether input is high or low
    print (age)      //prints the count 
    if a==1:         //condition
            age+=1   //increments every time button is pressed
    time.sleep(1)

    if not (age % 60): #use modulo
        thermometer()

但这里要注意的一件重要事情是,你不是每60秒上传一次,而是每60次循环上传。如果您需要上传每个固定时间步骤,请查看datetime包,在那里您可以检查当前时间,并使用模数下的当前秒数。

如果您的数据需要固定间隔,那么上传时间并不重要。相反,你需要每60秒记录一次值。