![]() |
Newbie with Pi3 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Newbie with Pi3 (/thread-9266.html) |
Newbie with Pi3 - Dualxeon - Mar-30-2018 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 Clientat 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 RE: Newbie with Pi3 - buran - Mar-30-2018 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-python/wiki/Inputs/ it's better to use edge detection than pooling RE: Newbie with Pi3 - Dualxeon - Mar-30-2018 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 RE: Newbie with Pi3 - Dualxeon - Mar-30-2018 Ok still not 100 % it does send text but keeps sending them over and over. also I get a error I am working on it will keep trying it's getting closer.Thanks Dualxeon RE: Newbie with Pi3 - snippsat - Mar-30-2018 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 RE: Newbie with Pi3 - buran - Mar-31-2018 (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... RE: Newbie with Pi3 - Dualxeon - Mar-31-2018 (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) 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. RE: Newbie with Pi3 - buran - Mar-31-2018 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.
Also, if you set OUT pin HIGH, it stays so, until set LOW, right? RE: Newbie with Pi3 - Dualxeon - Mar-31-2018 (Mar-31-2018, 08:48 AM)buran Wrote: so actually when door is open it constantly prints 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 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 RE: Newbie with Pi3 - buran - Mar-31-2018 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 |