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.
