السلام عليكم,
للأسف لم ألحظ أنى لم أضع الكود الخاص بالمشروع إلا الآن أعتذر بشدة لأعضاء المنتدى
الكود يعمل بكفاءة عالية ومكتوب بلغة PicBasic Pro
تم تعديل المشروع بالكامل ليعمل على SIM900 GSM Modem
سأوافيكم بالمشروع الجديد قريبا إن شاء الله
كود:
* DEVICE MCLR_OFF,INTRC_OSC, WDT_OFF, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_OFF
'------------------------------- Communication settings ----------------------------------
Define HSER_TXSTA 24H
DEFINE HSER_RCSTA 90h
Define HSER_BAUD 19200
DEFINE HSER_CLROERR 1 ' Reset the buffer if overflow happend
'---------------------------------- Read SMS variables ------------------------------------
GSMbuffer var Byte [30]
'-----------------------------------------------------------------------------------------
'---------------------------------- Send SMS Variables declaration--------------------------
x VAR byte
y VAR byte
I VAR Byte
J VAR Byte
K1 VAR Byte
K2 VAR Byte
K VAR Byte
tel_length VAR byte ' the length of the telephone number where the message is to be send
mess_len VAR byte ' the length of the message in characters
command_len VAR byte ' the length of the entire command
'telno VAR byte(12)
SenderTEL var byte(12)
message VAR byte(50)
CR CON 13
CTRLZ CON 26
'-------------------------------------------------------------------------------
'------------------------------------ main variables ---------------------------
Relay1 var PORTB.3
Relay2 var PORTB.4
Relay3 var PORTB.5
Relay4 var PORTB.6
Phone_led var PORTB.7
dummy var byte
phone_connected var byte
Device1_Array var byte(6)
Device2_Array var byte(6)
Device3_Array var byte(6)
Device4_Array var byte(6)
'********************************* Main Program ********************************
' Variable Initialization
CMCON = 7
TRISB.3 =0
TRISB.4 =0
TRISB.5 =0
TRISB.6 =0
TRISB.7 =0
phone_connected = 0
dummy = 0
' Reset all output to 0
low phone_led
low relay1
low relay2
low relay3
low relay4
pause 2000
gosub TestRelays
Start:
gosub TestPhone
pause 1000
gosub ReadSMS
gosub action
goto Start
End 'Main program
'---------------------------------- Read and match SMS then Output --------------------------
Action:
' Device1 ON "D1ON"
Action_check1:
ARRAYREAD GSMbUffer, 30, Action_check2 ,[WAIT("C4D8D309")]
high relay1
' Device1 OFF "D1OFF"
Action_check2:
ARRAYREAD GSMbUffer, 30, Action_check3 ,[WAIT("C4D8D36804")]
low relay1
' Device2 ON "D2ON"
Action_check3:
ARRAYREAD GSMbUffer, 30, Action_check4 ,[WAIT("44D9D309")]
high relay2
' Device2 OFF "D2OFF"
Action_check4:
ARRAYREAD GSMbUffer, 30, Action_check5 ,[WAIT("44D9D36804")]
low relay2
' Device3 ON "D3ON"
Action_check5:
ARRAYREAD GSMbUffer, 30, Action_check6 ,[WAIT("C4D9D309")]
high relay3
' Device3 OFF "D3OFF"
Action_check6:
ARRAYREAD GSMbUffer, 30, Action_check7 ,[WAIT("C4D9D36804")]
low relay3
' Device4 ON "D4ON"
Action_check7:
ARRAYREAD GSMbUffer, 30, Action_check8 ,[WAIT("44DAD309")]
high relay4
' Device4 OFF "D4OFF"
Action_check8:
ARRAYREAD GSMbUffer, 30, Action_check9 ,[WAIT("44DAD36804")]
low relay4
' Devices Status "STATUS"
Action_check9:
ARRAYREAD GSMbUffer, 30, Action_not_found ,[WAIT("536A905A9D02")]
gosub Read_Status
pause 1000
gosub TestPhone
for i = 0 to 2
high phone_led
pause 150
low phone_led
pause 150
next i
GOSUB SendSMS
' gosub testsms
for i = 0 to 2
high phone_led
pause 150
low phone_led
pause 150
next i
Action_not_found:
return
'----------------------------------------------------------------------------------------------
'---------------------------------------- Send SMS procedure ----------------------------------
SendSMS:
pause 100
' Let us find out the length of the telephone no!
I = 0
I = tel_length Mod 2
J = tel_length / 2
I = I + J
command_len = 7 + I
I = mess_len Mod 8
J = mess_len / 8
J = J * 7
J = J + I
command_len = command_len + J
' The length of command is now saved in command_len
HSEROUT ["AT+CMGS=",dec command_len, CR]
pause 500
HSEROUT ["000100"] ' SCA=00, Message Report=00,MR=00
'The telephone length in Hex
HSEROUT ["0",Hex tel_length]
HSEROUT ["91"] ' International Number(91), Loc(81)
'arraywrite sendertel,["695699510654"]
hserout[str sendertel\12]
HSEROUT["0000"] ' PID=00, DCS Message type=00 -> Normal,
'Output Message length, Add 0 before the Hex Number to 01,02.... 0F
if mess_len <= 15 then
HSEROUT["0",Hex mess_len]
else
HSEROUT[Hex mess_len]
endif
' Message in PDU
I = 0
J = 1
loop3:
K1 = message[i]
K2 = message[j]
K = I Mod 8
K1 = K1 >> K
K = 7 - K
K2 = K2 << K
K1 = K1 + K2
if K1 <= 15 then
HSEROUT["0",Hex K1]
else
HSEROUT[Hex K1]
endif
I = I + 1
J = J + 1
If K = 1 Then
J = J + 1
I = I + 1
EndIf
if I>= mess_len then
goto out
endif
goto loop3:
out:
' At the end we send <ctrl-z> character
HSEROUT[CTRLZ]
pause 3000
return
'-------------------------------------------------------------------------------------------------
'----------------------------------------------- Read SMS ----------------------------------------
ReadSMS:
' Check for new incomming Messages and store the Sender_TEL and GSMBuffer
'list new unread messages
Hserout ["AT+CMGL=0",CR] ' set to 1 temp, change to 0
'Answer in 3 lines should look like this:
' at+cmgl=0
' +CMGL: 3,0,,24
' 07910201956905F0040B910201740897F800000110608152118002C834
HSerin 5000,No_SMS, [wait("+CMGL:"),skip 32,STR SenderTEL\12,skip 20,STR GSMbuffer\30 \13]
if SenderTEL[10] = "F" then
tel_length = 11
else
tel_length = 12
endif
pause 3000
Hserout ["AT+CMGD=1",cr] ' delete the message
pause 3000
goto quit_sms
No_SMS:
' Any character instead of deleting the array
GSMbuffer[0]="#"
quit_sms:
return
'-----------------------------------------------------------------
Read_Status:
if relay1 = 1 then
arraywrite device1_array,["D1 ON "]
else
arraywrite device1_array,["D1 OFF"]
endif
if relay2 = 1 then
arraywrite device2_array,["D2 ON "]
else
arraywrite device2_array,["D2 OFF"]
endif
if relay3 = 1 then
arraywrite device3_array,["D3 ON "]
else
arraywrite device3_array,["D3 OFF"]
endif
if relay4 = 1 then
arraywrite device4_array,["D4 ON "]
else
arraywrite device4_array,["D4 OFF"]
endif
arraywrite message,[str device1_array\6,cr, str device2_array\6,cr, str device3_array\6,cr, str device4_array\6, cr ]
mess_len = 28 ' 4 characters added because of the Carriage return
return
'**************************** Test for phone existance *************************
TestPhone:
Hserout ["AT", CR] ' Check for Phone presence
HSerin 5000,Not_Connected,[wait("OK")]
high phone_led
goto end_test_phone
not_connected:
low phone_led
goto testphone
end_test_phone:
pause 2000
return
TestRelays:
high relay1
pause 300
low relay1
pause 500
high relay2
pause 300
low relay2
pause 500
high relay3
pause 300
low relay3
pause 500
high relay4
pause 300
low relay4
pause 500
return