morris1974
Posts: 25
Joined: Wed Jun 10, 2015 8:13 am

stop/start a script

Wed Nov 16, 2016 5:11 pm

hello all,
i made a script which send me mail when a contact became off it work perfectly now i'm doing a server web with flask and i would like to stop or start the script i wrote.
All i did it since now is to check if the GPIO is high or low but it doesn't stop or start.
I saw when the GPIO is high or low it doesn't stop or start the script so i search on the web but i didn't found what i would do (maybe is for my english)
Could someone help me on that? As well a right link could be useful for getting on this thing.
Thanks to all and as i said before i'm not english so if you need more information on what i would do just ask and i will try to be more clear on what i would like to do.
By the way i'm using a raspberry b+ with OS Raspbian

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: stop/start a script

Wed Nov 16, 2016 7:44 pm

Please post your script. It's not clear from what you've described exactly what is happening.
There are 10 types of people: those who understand binary and those who don't.

morris1974
Posts: 25
Joined: Wed Jun 10, 2015 8:13 am

Re: stop/start a script

Thu Nov 17, 2016 8:07 am

Hello thanks for helping me,
this is the script i would like to stop or start

Code: Select all

#!/usr/bin/env python
 
import RPi.GPIO as GPIO
import time, smtplib, sys
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(18, False)
GPIO.output(24, True)
StatoPulsante = False
smtpserver = 'smtp.gmail.com:587'
fromaddr = 'xxxx'
toaddr = 'xxxx'
header = 'To: ' + toaddr + '\n' + 'From: ' + fromaddr
header = header + '\n' + 'Subject:' + "APIARIO1" + '\n'
msg = header + '\n' + 'ALLARME ATTIVATO' + ' \n\n'
#Aggiungo i dati per l'invio
username = 'xxxx' #inserire proprio indirizzo
password = 'xxxx' #inserire propria password
       
while True:
    if not GPIO.input(23):
        StatoPulsante = True
    else:
        StatoPulsante = False
    if StatoPulsante:
        GPIO.output(18, True)
        GPIO.output(24, False)
        StatoPulsante = False
        # codice mail
        server = smtplib.SMTP(smtpserver)
        server.starttls()
        server.login(username, password)
        server.sendmail(fromaddr, toaddr, msg)
        server.quit()

        sys.exit()
and this is the code of flask which give me the state of the GPIO

Code: Select all

from flask import Flask, render_template
import datetime
import RPi.GPIO as GPIO
app = Flask(__name__)

GPIO.setmode(GPIO.BCM)

@app.route("/")
def hello():
   now = datetime.datetime.now()
   timeString = now.strftime("%Y-%m-%d %H:%M")
   templateData = {
      'title' : 'HELLO!',
      'time': timeString
      }
   return render_template('main.html', **templateData)

@app.route("/readPin/<pin>")
def readPin(pin):
   try:
      GPIO.setup(int(pin), GPIO.IN)
      if GPIO.input(int(pin)) == True:
         response = "Pin number " + pin + " is high!"
      else:
         response = "Pin number " + pin + " is low!"
   except:
      response = "There was an error reading pin " + pin + "."

   templateData = {
      'title' : 'Status of Pin' + pin,
      'response' : response
      }

   return render_template('pin.html', **templateData)


if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80, debug=True)
i followed the tutorials on that link http://mattrichardson.com/Raspberry-Pi-Flask/index.html
So at that point i see if the GPIO is high or low but it doesn't stop the script, what happen is just see high or low not stopping the script, as i wrote before i would like to stop the script for a while just to non have mail in the meantime i'm working on that box
Another little thing i'm a beekeeper and here (in Italy) people use to stole the hives i just put a interruptor in each hive so if some try to stole it it send me a mail, but when i'm working on that hive i would stop the script do my job and then restart the script without getting inside terminal via ssh
Hope again to be clear but if you have other question i will be happy to answering the question you asking
in the mean time have a nice day

Return to “General discussion”