1 回答
TA贡献1877条经验 获得超1个赞
所以我的脚本现在运行了一段时间。我对其进行了修改,以便用继电器控制关税开关。这可能会让知道的人感兴趣:)
#!/usr/bin/env python3
import mysql.connector as mariadb
import datetime
import time
import RPi.GPIO as GPIO
mariadb_connection = mariadb.connect(user='', password='',
database='')
cursor = mariadb_connection.cursor()
print ('connected')
cursor.execute("SELECT datetime FROM Compteur ORDER BY nb DESC LIMIT 1")
gett = cursor.fetchone()
last = gett[0]
print (last)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.OUT)
HPHC = ''
def impulse(channel):
global last
global HPHC
now = datetime.datetime.now()
#print (now)
delta = datetime.timedelta.total_seconds(now-last)
#print (delta)
last = datetime.datetime.now()
Pinst = round(7.2/delta ,3)
#print (Pinst)
heure = int(now.strftime("%H"))
minute = int(now.strftime("%M"))
#print(heure,minute)
if heure == 20 and minute >= 56 :
HPHC = 'HC'
elif heure > 20:
HPHC = 'HC'
elif heure == 4 and minute <= 56 :
HPHC = 'HC'
elif heure < 4:
HPHC = 'HC'
else :
HPHC = 'HP'
#print (HPHC)
cursor.execute("INSERT INTO Compteur (Puiss,datetime,HPHC) VALUES (%s,%s,%s)", (Pinst,now.strftime("%Y-%m-%d %H:%M:%S"), HPHC))
mariadb_connection.commit()
return HPHC
GPIO.add_event_detect(17, GPIO.FALLING, callback=impulse, bouncetime=500)
print ('boucle')
while True:
GPIO.setmode(GPIO.BCM)
time.sleep(0.1)
if HPHC is 'HC' :
GPIO.output(18, GPIO.HIGH)
elif HPHC is 'HP' :
GPIO.output(18, GPIO.LOW)
添加回答
举报