RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

GSM module and PIR sensors with Raspberry Pi

Thu Apr 06, 2017 10:17 am

I am having a project, wherein there are 4 PIR sensors in 4 directions. Whenever one sensor gets activated, the Servo motor has to move in that direction and the GSM module should send a message to the user.

The problem I am facing that, I try to embed the GSM module python script into the main code and whenever one sensor is activated, the GSM python script executes and untill the code has stopped running, the other sensors does not get activated.

I am posting the code, someone please help me modify it so that both the GSM code and the PIR sensor code runs simultaneously without any difficulties.

Code: Select all



    #Main code for PIR sensors

    import RPi.GPIO as GPIO
    import time
    import os
    import subprocess

    GPIO.setmode(GPIO.BCM)
    PIR_PIN1 = 12
    PIR_PIN2 = 16
    PIR_PIN3 = 20
    PIR_PIN4 = 21
    GPIO.setup(PIR_PIN1, GPIO.IN)
    GPIO.setup(PIR_PIN2, GPIO.IN)
    GPIO.setup(PIR_PIN3, GPIO.IN)
    GPIO.setup(PIR_PIN4, GPIO.IN)

    GPIO.add_event_detect(PIR_PIN1, GPIO.RISING)
    GPIO.add_event_detect(PIR_PIN2, GPIO.RISING)
    GPIO.add_event_detect(PIR_PIN3, GPIO.RISING)
    GPIO.add_event_detect(PIR_PIN4, GPIO.RISING)

    GPIO.setup(18,GPIO.OUT)

    p=GPIO.PWM(18,50)

    p.start(2.5)

    def MOTION1():
       p.ChangeDutyCycle(7.5)
       print "Motion 1 Detected!"
       

    def MOTION2():
       p.ChangeDutyCycle(12.5)
       print "Motion 2 Detected!"
       

    def MOTION3():
       p.ChangeDutyCycle(2.5)
       print "Motion 3 Detected!"
       gsm_module()
       

    def MOTION4():
       p.ChangeDutyCycle(7.5)
       print "Motion 4 Detected!"
       gsm_module()


    print "PIR Module Test (CTRL+C to exit)"
    time.sleep(2)
    print "Ready"

    def servo_movement():

       try:
          while True:
             if GPIO.event_detected(PIR_PIN1):
                MOTION1()
                time.sleep(0.2)
             if GPIO.event_detected(PIR_PIN2):
                MOTION2()
                time.sleep(0.2)
             if GPIO.event_detected(PIR_PIN3):
                MOTION3()
                time.sleep(0.2)
             if GPIO.event_detected(PIR_PIN4):
                MOTION4()
                time.sleep(0.2)
             time.sleep(0.1)   
       

       except KeyboardInterrupt:
          print "Quit"
          GPIO.cleanup()
          exit()

    def gsm_module():
       if GPIO.input(PIR_PIN1)==1 or GPIO.input(PIR_PIN2)==1 or GPIO.input(PIR_PIN3)==1 or GPIO.input(PIR_PIN4)==1 :
          subprocess.Popen(["python", 'gsm.py'])
       time.sleep(0.1)


    servo_movement()




The code for GSM module is given : gsm.py

Code: Select all



    #code for GSM

    import serial
    import RPi.GPIO as GPIO     
    import os, time
     
    GPIO.setmode(GPIO.BOARD)   
     
    # Enable Serial Communication
    port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=1)
     
    # Transmitting AT Commands to the Modem
    # '\r\n' indicates the Enter key
     
    port.write('AT'+'\r\n')
    rcv = port.read(10)
    print rcv
    time.sleep(1)
     
    port.write('ATE0'+'\r\n')      # Disable the Echo
    rcv = port.read(10)
    print rcv
    time.sleep(1)
     
    port.write('AT+CMGF=1'+'\r\n')  # Select Message format as Text mode
    rcv = port.read(10)
    print rcv
    time.sleep(1)
     
    port.write('AT+CNMI=2,1,0,0,0'+'\r\n')   # New SMS Message Indications
    rcv = port.read(10)
    print rcv
    time.sleep(1)
     
    # Sending a message to a particular Number
     
    port.write('AT+CMGS="98XXXXXXXX"'+'\r\n')
    rcv = port.read(10)
    print rcv
    time.sleep(1)
     
    port.write('Movement detected !'+'\r\n')  # Message
    rcv = port.read(10)
    print rcv
     
    port.write("\x1A") # Enable to send SMS
    for i in range(10):
        rcv = port.read(10)
        print rcv




Someone please help me with the correct code.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: GSM module and PIR sensors with Raspberry Pi

Thu Apr 06, 2017 11:39 am

You've already asked this before: viewtopic.php?f=32&t=179600&p=1142551#p1142551

Please don't double post.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

Re: GSM module and PIR sensors with Raspberry Pi

Thu Apr 06, 2017 11:41 am

I did not get any proper reply. The issue was not solved. Thats why I posted again.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: GSM module and PIR sensors with Raspberry Pi

Thu Apr 06, 2017 11:43 am

You shouldn't start a new thread though. You could have posted in your existing one.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

RGhosh99
Posts: 25
Joined: Tue Mar 21, 2017 6:57 am

Re: GSM module and PIR sensors with Raspberry Pi

Thu Apr 06, 2017 11:45 am

I am extremely sorry for that. But I am facing this issue and it needs to be solved urgently. Any solution for the problem will be very helpful to me.

Thanks

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: GSM module and PIR sensors with Raspberry Pi

Thu Apr 06, 2017 1:31 pm

That's not the way things are done here, sorry. Everyone who chooses to reply, or not reply is a volunteer here on the forums - you just need to be patient, or try elsewhere.
Locking this thread -please refer to the original as duplicates are wasteful.
viewtopic.php?f=32&t=179600&p=1142551#p1142551

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Python”