[英]I want to run two funtions simultaneously but one should run after 60 seconds on an rpi3 using python
I have written a code python to upload data to the thingspeak website. 我写了一个代码python,将数据上传到Thingspeak网站。 I connected a button to pin no 25 and have a code that calculates the number of times the button is pressed(age).
我将一个按钮连接到25号插针,并有一个代码来计算按钮被按下的次数(年龄)。
I want to upload the value of 'age' every 60 seconds but the code to calculate the button press must be continuously running so that i don't miss any button presses. 我想每60秒上传一次“年龄”值,但计算按钮按下次数的代码必须连续运行,这样我才不会错过任何按钮按下事件。 Below is the code.
下面是代码。
#!/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'
I need to upload the data every 60 seconds. 我需要每60秒上传一次数据。 The above code is uploading the value every second.
上面的代码每秒上传一次值。
The function time.time() return the time in seconds since the epoch as a floating point number, ie Time elapsed since UTC 00:00:00 1 January 1970 .
函数time.time()返回以纪元为单位的时间(以秒为单位
) ,即从1970年1月1日UTC 00:00:00开始经过的时间。
sendTime = time.time() + 60
--> This is to add 60 seconds to the clock and stored it in a variable ie sendTime .
sendTime = time.time() + 60
>这将使时钟增加60秒,并将其存储在变量sendTime中 。
In your function thermometer()
i have added a condition to check whether the system time(ie time.time()
) has a greater value than the sendTime
or not. 在您的
thermometer()
函数中,我添加了一个条件来检查系统时间(即time.time()
)的值是否大于sendTime
值。
If the condition is satisfied the data will be uploaded, if not the loop breaks and goes back to the while loop, Which you have writen in the main()
如果满足条件,则将上载数据,否则循环将中断并返回while循环,该循环已在
main()
写入
#!/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'
Hope this works. 希望这行得通。
Use the modulo function %
, this will upload if age
if the remainder of the division age / 60
is 0
. 使用模数函数
%
,如果age
age / 60
除法的余数为0
,它将在age
上载。 Else the condition evaluates to False
. 否则条件评估为
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()
But one important thing to note here is, that you are uploading not every 60 seconds, but every 60 iterations of the loop. 但要注意的一件事是,您并不是每60秒上传一次,而是每60次循环上传一次。 If you need to upload every fixed time step look at the
datetime
package, there you can check the current time and also use the current seconds with modulo. 如果您需要上载每个固定时间的步骤,请查看
datetime
包,在那里您可以检查当前时间,也可以使用当前秒数取模。
If your data needs to be in fixed intervals, then upload time is not that important. 如果您的数据需要固定间隔,那么上载时间并不那么重要。 Instead you need to record a value every 60 seconds.
相反,您需要每60秒记录一次值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.