Flawl3ssSWE
Posts: 5
Joined: Mon Jul 09, 2018 5:53 pm

Help with Code: If button not pressed after certain time send alert.

Mon Jul 09, 2018 6:05 pm

This is the problem: sometimes my automatic lawn mower does not dock on the night and the power goes out in the middle of my yard. Since the dock is on the edge of my house it is not visible from any window and it sometimes takes me some time to find out that it has not docked in the station.

My idea: Have a microswitch or button that sends an alert when docked sending an alert to my phone that it is docked. If it has not docked until a certain time say 17:30 then send me an alert that it has not docked. The pi does not matter have a zero w laying around so that would be the best. The alert service does not matter Pushbullet, Telegram, Email anything that is free works for me.

What I need help with is getting this project working since I don't know how to code I wonder if anyone can give me a hand on how to make this project into a reality.

Big thanks in advance,
Anton

pcmanbob
Posts: 4340
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help with Code: If button not pressed after certain time send alert.

Tue Jul 10, 2018 8:13 am

Hi.

The following should get you started with your project, people will help you solve problems but they wont write your entire project for you.

The use of a switch is covered in the physical computing pages on the raspberry pi web site

https://projects.raspberrypi.org/en/pro ... omputing/7

and here

https://github.com/raspberrypilearning/ ... up_down.md

To set up a free gmail account for your pi, start by setting up a new account on gmail use only letters and numbers in the account name and password , ( a-z, A-Z, 0-9 ) and also turn on allow access from less secure apps.

you can use ssmtp to send emails from your pi there is a how to here
http://www.raspberry-projects.com/pi/so ... end-emails

you can also send email from within python using smtplib
https://www.tutorialspoint.com/python/p ... _email.htm
http://naelshiab.com/tutorial-send-email-python/

the sites linked are just examples you will find many more on the web just google it.

So once you have your python program written to test the switch and then send an email if it is not pressed, you can then simpole call it one a day using cron to check if your mower is parked or not.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Flawl3ssSWE
Posts: 5
Joined: Mon Jul 09, 2018 5:53 pm

Re: Help with Code: If button not pressed after certain time send alert.

Tue Jul 10, 2018 4:00 pm

Thank you very much did not think that I could use Crontab for this, but you are right it is just to run and check it when needed at a certain time. Big thanks!

Flawl3ssSWE
Posts: 5
Joined: Mon Jul 09, 2018 5:53 pm

Re: Help with Code: If button not pressed after certain time send alert.

Tue Jul 10, 2018 6:48 pm

I have a problem now with my code, sometime it suddenly decides to trigger on it's own when running it a second time, and I need it to be able to detect when the button is already pushed in. At the moment it only works when pressing it, not when holding it down and then running the Python script.

Code: Select all

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    GPIO.wait_for_edge(23, GPIO.FALLING)
    print "Button was pressed."
except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

pcmanbob
Posts: 4340
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help with Code: If button not pressed after certain time send alert.

Tue Jul 10, 2018 11:20 pm

Here some example code that runs in a loop checking the button, it will tell you if it pressed or not even if you press it before you run the code.

if you don't want it to run in a constant loop just remove the while True: and change all the indenting to match the first print statment

Code: Select all

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    print  " Start "
    while True:
    
        # waiting for button press
        while GPIO.input(23) == 1:
            time.sleep(0.5)
            
        print "Button is pressed."
        
        # waiting for button release
        while GPIO.input(23) == 0:
            time.sleep(0.5)
            
        print "Button is release."
        
        
except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Flawl3ssSWE
Posts: 5
Joined: Mon Jul 09, 2018 5:53 pm

Re: Help with Code: If button not pressed after certain time send alert.

Thu Jul 12, 2018 5:45 pm

Now I have been able to remove the loop. Now I have the problem making it only test the input if it is pressed or not, now it wants me to press (or have it pressed earlier) and then released to stop. I want it to only check and output if it is pressed or not.

Code: Select all

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    print "Start"

# waiting for button press
    while GPIO.input(23) == 1:
            time.sleep(0.5)

    print "Button is pressed."

# waiting for button release
    while GPIO.input(23) == 0:
            time.sleep(0.5)
    print "Button is release."


except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit[code]

d_older
Posts: 102
Joined: Mon Jun 25, 2012 5:04 pm
Location: East Yorkshire, UK

Re: Help with Code: If button not pressed after certain time send alert.

Thu Jul 12, 2018 9:33 pm

Hi Flawl3ssSWE,

If you are going to call this from cron at the desired time and simply want the button state at that time this modification should do it:-

Code: Select all

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    print "Start"

    if GPIO.input(23) == 0:        # button pressed / ground 
       print "Button is pressed."
    else:                          # button not pressed
       print "Button is released."

except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit[code]
Hope this helps,
Dave

pcmanbob
Posts: 4340
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help with Code: If button not pressed after certain time send alert.

Thu Jul 12, 2018 9:37 pm

I guess you did not really understand the example then.

This code will do what you want.

Code: Select all

#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)


print "Start"

# tests for button press
    if GPIO.input(23) == 1:
        print "Button is released."

    else:
       print "Button is pressed."


print " finished "

GPIO.cleanup()           # clean up GPIO on normal exit
this is basic python , if you could not work this out you will struggle to send an email using python.
Last edited by pcmanbob on Fri Jul 13, 2018 9:02 am, edited 1 time in total.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Flawl3ssSWE
Posts: 5
Joined: Mon Jul 09, 2018 5:53 pm

Re: Help with Code: If button not pressed after certain time send alert.

Thu Jul 12, 2018 10:09 pm

Ohh, i just realised how stupid I was. I just need to change print to what ever I want it to do next.

pcmanbob
Posts: 4340
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help with Code: If button not pressed after certain time send alert.

Fri Jul 13, 2018 9:06 am

If you use the last lot of code I posted ( note corrected indenting error so you might want to copy it again )
you just need to replace the print "Button is released." line with your code to send the email.
you could leave the print line in place so that when you call the program using cron you can output the printed lines to a log file good for solving problems should your code not work.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 14 guests