
به نام خدا
پروژه ی ارسال پیامک و امکان تماس با رزبری پای و ماژول GSM

قطعات مورد نیاز ارسال پیامک و امکان تماس با رزبری پای و ماژول GSM:
- رزبری پای ۳ (هر مدلی)
- ماژول GSM
- ال سی دی ۲ ۱۶X
- کی پد ۴X4
- پتانسومتر ۱۰ کیلو اهمی
- برد بورد
- سیم برد بورد
- منبع تغذیه
- اسپیکر
- میکروفون
- سیم کارت
- مدار تقویت کننده صدا( الزامی نیست )

طرز کار:
در این پروژه از رزبری پای و ماژول GPS برای کنترل تمام امکانات و دسترسی به تمام قطعات ؛ از کی پد ۴×۴ برای وارد کردن شماره ، نوشتن پیام ، تماس گرفتن ، جواب دادن به تماس ، فرستادن پیام و خواندن پیام ؛ از ماژول GPS SIM900A برای برقراری ارتباط با شبکه های تلفن همراه (ایرانسل،همراه اول یا هر شبکه دیگری) ؛ از میکرفون و اسپیکر برای تماس های صوتی و از ال سی دی ۱۶X2 برای نمایش پیام ها و هشدارها استفاده می کنیم.
برای نوشتن حروف و عدد ، از کی پد الفبایی-عددی (Alphanumeric) استفاده می کنیم. کی پد الفانامریک قابلیت نوشتن حروف و عدد را از روی یک کی پد به ما می دهد. طریقه برنامه نویسی کی پد الفانامریک را در آخر پست مشاهده می کنید.

امکانات گوشی رزبری پای:
۱ – تماس گرفتن:
برای برقراری تماس باید دکمه “C” رااز روی کی پد فشار دهیم و سپس به وسیله کی پد شماره ای که می خواهیم با آن تماس بگیریم را وارد کنیم. پس از وارد کردن شماره باید دوباره دکمه “C” را فشار دهیم. با انجام این کار رزبری پای به وسیله دستور زیر تماس را برقرار می کند.(در کد زیر xxxxxxxxxx شماره گیرنده است)
ATDxxxxxxxxxx; <Enter> where xxxxxxxxx is entered Mobile Number.
۲ – پاسخگویی به تماس:
هنگامی که شخصی تماس می گیرد ، روی ال سی دی پیغام ‘Incoming…’ بهمراه شماره فرد تماس گیرنده ظاهر می شود. تنها کاری که ما برای برقراری تماس باید انجام دهیم فشردن دکمه ‘A’ است. با فشردن این دکمه دستور زیر به ماژول GSM فرستاده می شود.
ATA <enter>
۳ – ارسال پیام:
برای ارسال پیام ابتدا دکمه ‘D’ را فشار می دهیم، بعد باید شماره گیرنده را وارد کنیم و برای تایید دکمه ی ‘D’ را فشار دهیم. سپس پیامی که می خواهیم را وارد کرده (مانند همه گوشی های دیگر) و دوباره ‘D’ را فشار می دهیم. با انجام کار های گفته شده دستورات زیر به ماژول GSM فرستاده می شود.(در کد زیر xxxxxxxxxx شماره گیرنده پیام است)
AT+CMGF=1 <enter> AT+CMGS=”xxxxxxxxxx” <enter> where: xxxxxxxxxx is entered mobile number
بعد از دستورات فوق با فرستادن دستور “۲۶” توسط رزبری پای به ماژول GSM ، پیام فرستاده می شود.
۴ – دریافت و خواندن پیام:
پیام هایی که دریافت می کنید در سیم کارت ذخیره می شود. رزبری پای به وسیله رابط UART هر لحظه پیام های دریافت شده را چک می کند. هر وقت پیام تازه ای دریافت شود روی ال سی دی پیغام “New message” ظاهر می شود. برای دیدن پیام دریافت شده کافی است دکمه ‘B’ را فشار دهیم. وقتی پیامی توسط ماژول GSM گرفته می شود دستور زیر را از طریق UART برای رزبری پای می فرستد.(در کد زیر ‘۶’ آدرس حافظه ای است که پیام در آن ذخیره شده. )
+CMTI: “SM”,۶ Where 6 is message location where it stored in SIM card.
وقتی دستور بالا توسط رزبری پای گرفته می شود، با فرستادن دستور زیر پیام را می خواند. در کد زیر “SMS stored location” مکانی است که پیام در آن ذخیره شده.
AT+CMGR=<SMS stored location><enter>
پیام خوانده شده از ماژول GSM توسط رزبری پای روی ال سی دی نمایش داده می شود.

توجه داشته باشید که برای اسپیکر و میکروفون به برنامه نویسی نیاز نداریم.
شماتیک مدار:

پایه های RS, EN, D4, D5, D6, D7 از ال سی دی ۱۶X2 به ترتیب به پین های ۱۸, ۲۳, ۲۴, ۲۵, ۸ , ۷ رزبری پای متصل شده اند.پایه Rx ماژول GSM به Tx رزبری پای و پایه Tx ماژول GSM به Rx رزبری پای متصل شده (زمین ماژول و رزبری پای باید به یکدیگر متصل شوند). پایه های ستون های کی پد R1, R2, R3, R4 به ترتیب به پین های۱۲,۱۶, ۲۰, ۲۱ رزبری پای و پایه های سطر های کی پد C1, C2, C3, C4 به ۲۶, ۱۹, ۱۳ , ۶ رزبری پای متصل شده اند. پایه های میکروفون مستقیما به پین های mic+ و mic- متصل شده و تقویت کننده صدا به پین های sp+ و sp- و سپس به اسپیکر متصل شده است. امپلی فایر(تقویت کننده صدا) الزامی نیست و می توانید بجای استفاده از آن، مستقیما اسپیکر را به پین های sp+ و sp- وصل کنید.
دانلود کد :
دانلود فایلimport RPi.GPIO as gpio
import serial
import time
msg=""
# ۰ ۷ ۱۱ ۱۵ ۱۹ ۲۳ ۲۷ ۳۲ ۳۶ ۴۱۴۲۴۴ ROLL45
alpha="1!@.,:?ABC2DEF3GHI4JKL5MNO6PQRS7TUV8WXYZ90 *#"
x=0
y=0
MATRIX = [
['۱','۲','۳','A'],
['۴','۵','۶','B'],
['۷','۸','۹','C'],
['*','۰','#','D']
]
ROW = [21,20,16,12]
COL = [26,19,13,6]
moNum=['0','0','0','0','0','0','0','0','0','0']
m11=17
m12=27
led=5
buz=26
button=19
RS =18
EN =23
D4 =24
D5 =25
D6 =8
D7 =7
HIGH=1
LOW=0
gpio.setwarnings(False)
gpio.setmode(gpio.BCM)
gpio.setup(RS, gpio.OUT)
gpio.setup(EN, gpio.OUT)
gpio.setup(D4, gpio.OUT)
gpio.setup(D5, gpio.OUT)
gpio.setup(D6, gpio.OUT)
gpio.setup(D7, gpio.OUT)
gpio.setup(led, gpio.OUT)
gpio.setup(buz, gpio.OUT)
gpio.setup(m11, gpio.OUT)
gpio.setup(m12, gpio.OUT)
gpio.setup(button, gpio.IN)
gpio.output(led , 0)
gpio.output(buz , 0)
gpio.output(m11 , 0)
gpio.output(m12 , 0)
for j in range(4):
gpio.setup(COL[j], gpio.OUT)
gpio.setup(COL[j],1)
for i in range (4):
gpio.setup(ROW[i],gpio.IN,pull_up_down=gpio.PUD_UP)
Serial = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=2)
data=""
def begin():
lcdcmd(0x33)
lcdcmd(0x32)
lcdcmd(0x06)
lcdcmd(0x0C)
lcdcmd(0x28)
lcdcmd(0x01)
time.sleep(0.0005)
def lcdcmd(ch):
gpio.output(RS, 0)
gpio.output(D4, 0)
gpio.output(D5, 0)
gpio.output(D6, 0)
gpio.output(D7, 0)
if ch&0x10==0x10:
gpio.output(D4, 1)
if ch&0x20==0x20:
gpio.output(D5, 1)
if ch&0x40==0x40:
gpio.output(D6, 1)
if ch&0x80==0x80:
gpio.output(D7, 1)
gpio.output(EN, 1)
time.sleep(0.005)
gpio.output(EN, 0)
# Low bits
gpio.output(D4, 0)
gpio.output(D5, 0)
gpio.output(D6, 0)
gpio.output(D7, 0)
if ch&0x01==0x01:
gpio.output(D4, 1)
if ch&0x02==0x02:
gpio.output(D5, 1)
if ch&0x04==0x04:
gpio.output(D6, 1)
if ch&0x08==0x08:
gpio.output(D7, 1)
gpio.output(EN, 1)
time.sleep(0.005)
gpio.output(EN, 0)
def lcdwrite(ch):
gpio.output(RS, 1)
gpio.output(D4, 0)
gpio.output(D5, 0)
gpio.output(D6, 0)
gpio.output(D7, 0)
if ch&0x10==0x10:
gpio.output(D4, 1)
if ch&0x20==0x20:
gpio.output(D5, 1)
if ch&0x40==0x40:
gpio.output(D6, 1)
if ch&0x80==0x80:
gpio.output(D7, 1)
gpio.output(EN, 1)
time.sleep(0.005)
gpio.output(EN, 0)
# Low bits
gpio.output(D4, 0)
gpio.output(D5, 0)
gpio.output(D6, 0)
gpio.output(D7, 0)
if ch&0x01==0x01:
gpio.output(D4, 1)
if ch&0x02==0x02:
gpio.output(D5, 1)
if ch&0x04==0x04:
gpio.output(D6, 1)
if ch&0x08==0x08:
gpio.output(D7, 1)
gpio.output(EN, 1)
time.sleep(0.005)
gpio.output(EN, 0)
def lcdclear():
lcdcmd(0x01)
def lcdprint(Str):
l=0;
l=len(Str)
for i in range(l):
lcdwrite(ord(Str[i]))
def setCursor(x,y):
if y == 0:
n=128+x
elif y == 1:
n=192+x
lcdcmd(n)
def keypad():
for j in range(4):
gpio.setup(COL[j], gpio.OUT)
gpio.output(COL[j], 0)
ch=0
for i in range(4):
if gpio.input(ROW[i])==0:
ch=MATRIX[i][j]
#lcdwrite(ord(ch))
# print "Key Pressed:",ch
# time.sleep(2)
return ch
while (gpio.input(ROW[i]) == 0):
pass
gpio.output(COL[j],1)
# callNum[n]=ch
def serialEvent():
data = Serial.read(20)
#if data != '\0':
print data
data=""
def gsmInit():
lcdclear()
lcdprint("Finding Module");
time.sleep(1)
while 1:
data=""
Serial.write("AT\r");
data=Serial.read(10)
print data
r=data.find("OK")
if r>=0:
break
time.sleep(0.5)
while 1:
data=""
Serial.write("AT+CLIP=1\r");
data=Serial.read(10)
print data
r=data.find("OK")
if r>=0:
break
time.sleep(0.5)
lcdclear()
lcdprint("Finding Network")
time.sleep(1)
while 1:
data=""
Serial.flush()
Serial.write("AT+CPIN?\r");
data=Serial.read(30)
print data
r=data.find("READY")
if r>=0:
break
time.sleep(0.5)
lcdclear()
lcdprint("Finding Operator")
time.sleep(1)
while 1:
data=""
Serial.flush()
Serial.read(20)
Serial.write("AT+COPS?\r");
data=Serial.read(40)
#print data
r=data.find("+COPS:")
if r>=0:
l1=data.find(",\"")+2
l2=data.find("\"\r")
operator=data[l1:l2]
lcdclear()
lcdprint(operator)
time.sleep(3)
print operator
break;
time.sleep(0.5)
Serial.write("AT+CMGF=1\r");
time.sleep(0.5)
# Serial.write("AT+CNMI=2,2,0,0,0\r");
# time.sleep(0.5)
Serial.write("AT+CSMP=17,167,0,0\r");
time.sleep(0.5)
def receiveCall(data):
inNumber=""
r=data.find("+CLIP:")
if r>0:
inNumber=""
inNumber=data[r+8:r+21]
lcdclear()
lcdprint("incoming")
setCursor(0,1)
lcdprint(inNumber)
time.sleep(1)
return 1
def receiveSMS(data):
print data
r=data.find("\",")
print r
if r>0:
if data[r+4] == "\r":
smsNum=data[r+2:r+4]
elif data[r+3] == "\r":
smsNum=data[r+2]
elif data[r+5] == "\r":
smsNum=data[r+2:r+5]
else:
print "else"
print smsNum
if r>0:
lcdclear()
lcdprint("SMS Received")
setCursor(0,1)
lcdprint("Press Button B")
print "AT+CMGR="+smsNum+"\r"
time.sleep(2)
return str(smsNum)
else:
return 0
def attendCall():
print "Attend call"
Serial.write("ATA\r")
data=""
data=Serial.read(10)
l=data.find("OK")
if l>=0:
lcdclear()
lcdprint("Call attended")
time.sleep(2)
flag=-1;
while flag<0:
data=Serial.read(12);
print data
flag=data.find("NO CARRIER")
#flag=data.find("BUSY")
print flag
lcdclear()
lcdprint("Call Ended")
time.sleep(1)
lcdclear()
def readSMS(index):
print index
Serial.write("AT+CMGR="+index+"\r")
data=""
data=Serial.read(200)
print data
r=data.find("OK")
if r>=0:
r1=data.find("\"\r\n")
msg=""
msg=data[r1+3:r-4]
lcdclear()
lcdprint(msg)
print msg
time.sleep(5)
lcdclear();
smsFlag=0
print "Receive SMS"
def getChar(Key, ind, maxInd):
ch=0
ch=ind
lcdcmd(0x0e)
Char=''
count=0
global msg
global x
global y
while count<20:
key=keypad()
print key
if key== Key:
setCursor(x,y)
Char=alpha[ch]
lcdwrite(ord(Char))
ch=ch+1
if ch>maxInd:
ch=ind
count=0
count=count+1
time.sleep(0.1)
msg+=Char
x=x+1
if x>15:
x=0
y=1
lcdcmd(0x0f)
def alphaKeypad():
lcdclear()
setCursor(x,y)
lcdcmd(0x0f)
msg=""
while 1:
key=0
count=0
key=keypad()
if key == '1':
ind=0
maxInd=6
Key='1'
getChar(Key, ind, maxInd)
elif key == '2':
ind=7
maxInd=10
Key='2'
getChar(Key, ind, maxInd)
elif key == '3':
ind=11
maxInd=14
Key='3'
getChar(Key, ind, maxInd)
elif key == '4':
ind=15
maxInd=18
Key='4'
getChar(Key, ind, maxInd)
elif key == '5':
ind=19
maxInd=22
Key='5'
getChar(Key, ind, maxInd)
elif key == '6':
ind=23
maxInd=26
Key='6'
getChar(Key, ind, maxInd)
elif key == '7':
ind=27
maxInd=31
Key='7'
getChar(Key, ind, maxInd)
elif key == '8':
ind=32
maxInd=35
Key='8'
getChar(Key, ind, maxInd)
elif key == '9':
ind=36
maxInd=40
Key='9'
getChar(Key, ind, maxInd)
elif key == '0':
ind=41
maxInd=42
Key='0'
getChar(Key, ind, maxInd)
elif key == '*':
ind=43
maxInd=43
Key='*'
getChar(Key, ind, maxInd)
elif key == '#':
ind=44
maxInd=44
Key='#'
getChar(Key, ind, maxInd)
elif key== 'D':
return
def sendSMS():
print"Sending sms"
lcdclear()
lcdprint("Enter Number:")
setCursor(0,1)
time.sleep(2)
moNum=""
while 1:
key=0;
key=keypad()
#print key
if key>0:
if key == 'A' or key== 'B' or key== 'C':
print key
return
elif key == 'D':
print key
print moNum
Serial.write("AT+CMGF=1\r")
time.sleep(1)
Serial.write("AT+CMGS=\"+91"+moNum+"\"\r")
time.sleep(2)
data=""
data=Serial.read(60)
print data
alphaKeypad()
print msg
lcdclear()
lcdprint("Sending.....")
Serial.write(msg)
time.sleep(1)
Serial.write("\x1A")
while 1:
data=""
data=Serial.read(40)
print data
l=data.find("+CMGS:")
if l>=0:
lcdclear()
lcdprint("SMS Sent.")
time.sleep(2)
return;
l=data.find("Error")
if l>=0:
lcdclear()
lcdprint("Error")
time.sleep(1)
return
else:
print key
moNum+=key
lcdwrite(ord(key))
time.sleep(0.5)
def call():
print "Call"
n=0
moNum=""
lcdclear()
lcdprint("Enter Number:")
setCursor(0,1)
time.sleep(2)
while 1:
key=0;
key=keypad()
#print key
if key>0:
if key == 'A' or key== 'B' or key== 'D':
print key
return
elif key == 'C':
print key
print moNum
Serial.write("ATD+91"+moNum+";\r")
data=""
time.sleep(2)
data=Serial.read(30)
l=data.find("OK")
if l>=0:
lcdclear()
lcdprint("Calling.....")
setCursor(0,1)
lcdprint("+91"+moNum)
time.sleep(30)
lcdclear()
return
#l=data.find("Error")
#if l>=0:
else:
lcdclear()
lcdprint("Error")
time.sleep(1)
return
else:
print key
moNum+=key
lcdwrite(ord(key))
n=n+1
time.sleep(0.5)
begin()
lcdcmd(0x01)
lcdprint(" Mobile Phone ")
lcdcmd(0xc0)
lcdprint(" Using RPI ")
time.sleep(3)
lcdcmd(0x01)
lcdprint("Circuit Digest")
lcdcmd(0xc0)
lcdprint("Welcomes you")
time.sleep(3)
gsmInit()
smsFlag=0
index=""
while 1:
key=0
key=keypad()
print key
if key == 'A':
attendCall()
elif key == 'B':
readSMS(index)
smsFlag=0
elif key == 'C':
call()
elif key == 'D':
sendSMS()
data=""
Serial.flush()
data=Serial.read(150)
print data
l=data.find("RING")
if l>=0:
callstr=data
receiveCall(data)
l=data.find("\"SM\"")
if l>=0:
smsstr=data
smsIndex=""
(smsIndex)=receiveSMS(smsstr)
print smsIndex
if smsIndex>0:
smsFlag=1
index=smsIndex
if smsFlag == 1:
lcdclear()
lcdprint("New Message")
time.sleep(1)
setCursor(0,0)
lcdprint("C--> Call <--A");
setCursor(0,1);
lcdprint("D--> SMS <--B")
ببخشید میتوانید این پروژه را با اردینو بزارید
سلام
به واحد تولید محتوا ارجاع داده میشه