Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie with Pi3
#1
I am working on opening a door with my Pi3, I have borrowed a lot of this code so far,as to I am very new to programming. I have this much working, now i want a text message when door opens and closes. I have installed Twilio and with the simple code
from twilio.rest import Client
client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")

client.messages.create(to="+19732644152", 
                       from_="+12023351278", 
                       body="Hello from Python!")


I can receive text message "Hello from Python" my problem is when i try to put this in the coding I have I get errors. Not really sure how and where to insert the code. I hope I am making sense. I have tried copy and pasting the import
from twilio.rest import Client
at the top with the rest of the imports. Then the rest under
if GPIO.input(11) == False:
                        print ("Day")
                        print ("Open door")
                        GPIO.output(15,True)
                        print ("drive motor")
						print ("Current time %s"  % now )

all get is syntax errors or unexpected indent errors.


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(7,GPIO.IN)   #LDR (Light Dependent Resistor)
#GPIO.setup(11,GPIO.IN)  #Top Switch
#GPIO.setup(12,GPIO.IN)  # Bottom Switch

GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #top switch
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # bottom switch

GPIO.setup(15,GPIO.OUT) # Door Open
GPIO.setup(13,GPIO.OUT) # Door Close

delay1 = 1  # time between checks when door moving for switch operation may need to be shorter 
delay2 = 5  # time between checks of LDR

while True:
        while GPIO.input(7) == False:
                if GPIO.input(11) == False:
                        print ("Day")
                        print ("Open door")
                        GPIO.output(15,True)
                        print ("drive motor")
						print ("Current time %s"  % now )

                elif GPIO.input(11) == True:
                        GPIO.output(15,False)
                        print ("STOP door open")
                time.sleep(delay1)        
        time.sleep(delay2)
                        
                        
        while GPIO.input(7) == True:
                if GPIO.input(12) == False:
                        print ("Night")
                        print ("Close door")
                        GPIO.output(13,True) 
                        print ("drive motor")

                        
                elif GPIO.input(12) == True:
                        GPIO.output(13,False)
                        print ("STOP door closed")
                time.sleep(delay1)       
        time.sleep(delay2)
        
Thanks Dualxeon
Reply
#2
Of course, not able to test, but something like this

import RPi.GPIO as GPIO
import time
from twilio.rest import Client


def send_sms(client, sms_text="Hello from Python!"):
    client.messages.create(to="+19732644152", 
                           from_="+12023351278", 
                           body=sms_text)


twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
                           
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(7,GPIO.IN)   #LDR (Light Dependent Resistor)
#GPIO.setup(11,GPIO.IN)  #Top Switch
#GPIO.setup(12,GPIO.IN)  # Bottom Switch

GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #top switch
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # bottom switch

GPIO.setup(15,GPIO.OUT) # Door Open
GPIO.setup(13,GPIO.OUT) # Door Close

delay1 = 1  # time between checks when door moving for switch operation may need to be shorter 
delay2 = 5  # time between checks of LDR

while True:
    while GPIO.input(7) == False:
        if GPIO.input(11) == False:
            print ("Day")
            print ("Open door")
            GPIO.output(15,True)
            print ("drive motor")
            print ("Current time %s"  % now )
        elif GPIO.input(11) == True:
            GPIO.output(15,False)
            print ("STOP door open")
            send_sms(client=twilio_client, sms_text='Door open')
        time.sleep(delay1)
    time.sleep(delay2)

    while GPIO.input(7) == True:
        if GPIO.input(12) == False:
            print ("Night")
            print ("Close door")
            GPIO.output(13,True) 
            print ("drive motor")
        elif GPIO.input(12) == True:
            GPIO.output(13,False)
            print ("STOP door closed")
            send_sms(client=twilio_client, sms_text='Door closed')
        time.sleep(delay1)       
    time.sleep(delay2)
I changed the indentation to 4 spaces, as per PEP8. 8 spaces per level is way too much.
Didn't change the rest of the code, but probably it could be written better. At least, based on https://sourceforge.net/p/raspberry-gpio...ki/Inputs/ it's better to use edge detection than pooling
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Ok looks simple enough I'm at work now can't try it till later this afternoon. I have worked on it Google it nothing I could understand. I tried what you have, but did not do the define part. I am learning love fooling with coding, I always get started then get stuck and lost. Thanks very much for the help I will post back the results.
Thanks Dualxeon
Reply
#4
Ok still not 100 % it does send text but keeps sending them over and over. also I get a error
Error:
Traceback (most recent call last): File "/home/pi/Desktop/Duals Coop Code DateTime Text.py", line 36, in <module> print ("Current time %s" % now ) NameError: name 'now' is not defined
I am working on it will keep trying it's getting closer.
Thanks Dualxeon
Reply
#5
now is not defined anywhere in code.
I guess it should be datetime.now().
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2018, 3, 31, 0, 35, 25, 601719)

>>> print("Current time {}".format(datetime.now()))
Current time 2018-03-31 00:35:40.756065

>>> # Or just current time
>>> print("Current time {:%H:%M:%S}".format(datetime.now()))
Current time 00:36:10
Reply
#6
(Mar-30-2018, 10:19 PM)Dualxeon Wrote: Ok still not 100 % it does send text but keeps sending them over and over.
Maybe I misunderstand the work of GPIO pins (input/output)
Ignoring the sms, does the rest of the messages print continuously?
My understanding was that line print ("STOP door open") and print ("STOP door closed") are printed only once per open/close operation. Is that not the case?
Maybe you can elaborate on work of GPIO...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Mar-31-2018, 05:53 AM)buran Wrote:
(Mar-30-2018, 10:19 PM)Dualxeon Wrote: Ok still not 100 % it does send text but keeps sending them over and over.
Maybe I misunderstand the work of GPIO pins (input/output)
Ignoring the sms, does the rest of the messages print continuously?
My understanding was that line print ("STOP door open") and print ("STOP door closed") are printed only once per open/close operation. Is that not the case?
Maybe you can elaborate on work of GPIO...

Okay if i go back to original script and run it. I turn on and boot up Pi3 comes up and shows door open. When I cover the photo cell it shows Night then closes door and prints time example below.
Reply
#8
so actually when door is open it constantly prints STOP door open and when it is closed - it non-stop prints STOP door closed

what pins 11 and 12 (the top and botom switch) are doing? what pins 15 and 13 are doing? how it detects to stop motor (opening/closing)? sorry, not an expert in GPIO :-)
OK, I entertain the idea of buying RPi for a log time so this is also interesting to me.
I did some raeding on GPIO, so please confirm if my understanding is correct:
when pin is IN, you get information from it, when it is OUT - you send info to whatever is connected. When HIGH it is powered.
  • pin7 is light sensor. When it is LOW (False) - there is light, when it is HIGH (True) - there is no light detected.
  • pin 11 and 12 are switches that detect the door is open/closed. When respective switch is HIGH (true) the door is already
    open/closed. i.e. while the door is open switch 11 is non-stop powered and in state HIGH
  • 15 and 13 are motors. 15 drives the motor to open the door. 13 powers the motor to close it. I don't know if the same motor with different direction of movement or different motors driving in one direction only. I guess 2 motors, because when pin 11 is HIGH, you actually sending non-stop info to pin 15 to stay LOW (False).
    Is that correct. Please correct me if not.



Also, if you set OUT pin HIGH, it stays so, until set LOW, right?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(Mar-31-2018, 08:48 AM)buran Wrote: so actually when door is open it constantly prints STOP door open and when it is closed - it non-stop prints STOP door closed

what pins 11 and 12 (the top and botom switch) are doing? what pins 15 and 13 are doing? how it detects to stop motor (opening/closing)? sorry, not an expert in GPIO :-)

I am a master Tinker ( I always end up over my head ) LOL
Yes on printing open close
Okay pin 11 & 12 are magnetic door switches (which I may change to Micros)
when photo module shows change ( night day ) the motor runs to open close door the magnetic switch is stop point.

Pins 13 and 15 goes to a small motor controller they are what control's direction of motor ( up & down for door)

(Mar-31-2018, 08:48 AM)buran Wrote: so actually when door is open it constantly prints STOP door open and when it is closed - it non-stop prints STOP door closed

what pins 11 and 12 (the top and botom switch) are doing? what pins 15 and 13 are doing? how it detects to stop motor (opening/closing)? sorry, not an expert in GPIO :-)
OK, I entertain the idea of buying RPi for a log time so this is also interesting to me.
I did some raeding on GPIO, so please confirm if my understanding is correct:
when pin is IN, you get information from it, when it is OUT - you send info to whatever is connected. When HIGH it is powered.
  • pin7 is light sensor. When it is LOW (False) - there is light, when it is HIGH (True) - there is no light detected.
  • pin 11 and 12 are switches that detect the door is open/closed. When respective switch is HIGH (true) the door is already
    open/closed. i.e. while the door is open switch 11 is non-stop powered and in state HIGH
  • 15 and 13 are motors. 15 drives the motor to open the door. 13 powers the motor to close it. I don't know if the same motor with different direction of movement or different motors driving in one direction only. I guess 2 motors, because when pin 11 is HIGH, you actually sending non-stop info to pin 15 to stay LOW (False).
    Is that correct. Please correct me if not.


Also, if you set OUT pin HIGH, it stays so, until set LOW, right?

I'm no expert but you are correct to my understanding pins 11 and 12 are to stop motor at set point Door open door close.
pins 13 and 15 control motor (small DC car window winder) I am running them into a IDUINO L298N Motor Driver Controller Board which takes care of the up and down from pin 13 and 15 it reverses the polarity out to motor
Reply
#10
Again, not able to test, but I think this should work

import RPi.GPIO as GPIO
import time
from twilio.rest import Client
 
 
def send_sms(client, sms_text="Hello from Python!"):
    client.messages.create(to="+19732644152", 
                           from_="+12023351278", 
                           body=sms_text)
 
 
twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
                            
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(7,GPIO.IN)   #LDR (Light Dependent Resistor)
 
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #top switch
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # bottom switch
 
GPIO.setup(15,GPIO.OUT) # Door Open
GPIO.setup(13,GPIO.OUT) # Door Close

# add events detection
GPIO.add_event_detect(7, GPIO.BOTH)
GPIO.add_event_detect(11, GPIO.RISING)
GPIO.add_event_detect(12, GPIO.RISING)

while True:
    if GPIO.event_detected(7) # change in light sensor
        if GPIO.input(7) == False and GPIO.input(11) == False: # it's day and door is not open
            print ("Day")
            print ("Open door")
            GPIO.output(15,True)
            print ("Start motor")
            print ("Current time %s"  % now )
        elif GPIO.input(7) == True and GPIO.input(12) == False: # it's night and door is not closed
            print ("Night")
            print ("Close door")
            GPIO.output(13,True) 
            print ("Drive motor")

    if GPIO.event_detected(11): # top switch change to HIGH
        GPIO.output(15,False)
        print ("STOP door open")
        send_sms(client=twilio_client, sms_text='Door open')
    
    if GPIO.event_detected(12): # bottom switch change to HIGH
        GPIO.output(13,False)
        print ("STOP door closed")
        send_sms(client=twilio_client, sms_text='Door closed')
Maybe it can be written better, but let's make it work as expected
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020