一名 Python 初学者正在开发一个 Python 脚本,用于监控地下室集水池抽水泵中的浮标开关。该脚本需要满足两个条件来检查抽水泵是否不工作:
- 开关持续打开超过 3 分钟。
- 开关在 3 分钟内打开和关闭超过 10 次。
目前,该项目仍在开发中,代码如下所示:
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
floatSwitch = GPIO.input(17)
import smtplib
running = True
log = open("sumpPumpLog.txt", "r+")
startTime = time.time()
def elapsedTime():
"""This function checks how much time
has elapsed since the timer has started"""
endtime = time.time()
elapsed = endtime - starttime
return elapsed
def sendEmail(*msg):
"""This function sends an email to selected recipients with a custom
message as well as the log file attached."""
#enter the code that sends an email to the family with the log attached
fromaddr = 'from@email.com'
toaddrs = [to@email.com']
msg = """Please see the Pi and the data log file for more details."""
# Credentials (if needed)
username = 'my_username'
password = 'my_password'
msg.attached()
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
if running is True:
if floatSwitch is True:
#Write the time and what happened to the file
log.write(str(time.time() + "Float switch turned on")
#Wait until switch is turned off
while floatSwitch is True:
startTime = time.time()
if floatSwitch is False:
log.write(str(now) + "Float switch turned off")
break
#if elapsedTime > 3 min (in the form of 180 seconds)
elif elapsedTime() > 180:
log.write(str(now) + "Sump Pump has been deemed broaken")
sendEmail("The sump pump is now broken.")
else:
log.write(str(time.time() + "The sctipt has stopped.")
sendEmail("The script has been stopped.")
然而,在第 52 行,即 while floatSwitch is True:,出现了一个无效语法的错误。
2. 解决方案
该问题的解决方案有两个方面:
-
修复无效语法
第 52 行的错误是缺少一个括号。正确写法应该是:
while floatSwitch is True: -
简化代码
为了使代码更加简洁和易于理解,可以将以下代码替换为:
while floatSwitch:这相当于
while floatSwitch is True,因为floatSwitch本身就是一个布尔值。
因此,最终修复后的代码为:
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
floatSwitch = GPIO.input(17)
import smtplib
running = True
log = open("sumpPumpLog.txt", "r+")
startTime = time.time()
def elapsedTime():
"""This function checks how much time
has elapsed since the timer has started"""
endtime = time.time()
elapsed = endtime - starttime
return elapsed
def sendEmail(*msg):
"""This function sends an email to selected recipients with a custom
message as well as the log file attached."""
#enter the code that sends an email to the family with the log attached
fromaddr = 'from@email.com'
toaddrs = [to@email.com']
msg = """Please see the Pi and the data log file for more details."""
# Credentials (if needed)
username = 'my_username'
password = 'my_password'
msg.attached()
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
if running is True:
if floatSwitch:
#Write the time and what happened to the file
log.write(str(time.time() + "Float switch turned on")
while floatSwitch:
startTime = time.time()
if not floatSwitch:
log.write(str(now) + "Float switch turned off")
break
#if elapsedTime > 3 min (in the form of 180 seconds)
elif elapsedTime() > 180:
log.write(str(now) + "Sump Pump has been deemed broaken")
sendEmail("The sump pump is now broken.")
else:
log.write(str(time.time() + "The sctipt has stopped.")
sendEmail("The script has been stopped.")