cmbrooks
cmbrooks

Reputation: 47

Python while loop with invalid syntax

Just to give some background on the project I am working on before I show the code. I am currently developing a Python script that will be running on a Raspberry Pi to monitor the float switch from a sump pump in my basement. This code will check to see if the sump pump is not working by these two criteria:

  1. If the switch is on for more than three minutes
  2. If the switch turns on and off more than 10 times in three minutes

I am not done with the rest of the code, but here is what I have:

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 = '[email protected]'
    toaddrs = [[email protected]']
    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.")

My problem is that on line 52 when it says

while floatSwitch is True:

There is an error in the code, and all it says is 'invalid syntax' I am very new to Python, and this is my first real project with it. I am unfamiliar with a lot of the syntax, so this could be a very elementary mistake. Can anyone please help me fix the syntax of this statement so that I can get my code to work. I know that there are numerous other mistakes without the rest of the code, but I am planning to work out those bugs when I find them. I have searched around, but I cannot find another example like this. Any and all help is very appreciated!

Upvotes: 2

Views: 40816

Answers (2)

user2555451
user2555451

Reputation:

Actually, your problem is with the line above the while-loop. You are missing a parenthesis:

log.write(str(time.time() + "Float switch turned on"))
                                               here--^

Also, just a tip for the future, instead of doing this:

while floatSwitch is True:

it is cleaner to just do this:

while floatSwitch:

Upvotes: 5

Rohit Jain
Rohit Jain

Reputation: 213311

You have an unbalanced parenthesis on your previous line:

log.write(str(time.time() + "Float switch turned on"))  # Last parenthesis is missing

And also in sendEmail() method, you have a missing opening quote:

toaddrs = [[email protected]']  # Opening quote missing

Upvotes: 2

Related Questions