Hi
I am very new to PIC's so please excuse some of the comments that I have added to the following code.
I have put this together using data that was available earlier in this post. I have got the SMS sending working great whenever I push a button but I am also wanting an LED to be switched on when the GSM modem receives and SMS containing "SWITCH"
I think I have got everything that I require within the program but I am wondering what commands I should use in order to read the GSMBuffer then if it is true then switch on the LED.
I have added the line just to give an idea of what I'm trying to do:
IF GSMBUFFER = "SWITCH" THEN LED = 1
Here is the code:
' Setup PIC tx and rx serial port
DEFINE OSC 8
DEFINE HSER_TXSTA 20h
DEFINE HSER_RCSTA 90h
DEFINE HSER_BAUD 9600
DEFINE HSER_CLROERR 1
'Setup the names that will be used throughout the program for the led and switch
PushButton var PORTB.0 ' Setup the name PushButton to mean PortB input 0
LED VAR PORTB.1 ' Setup the name LED to mean PortB output 1
' SMS receive buffer setup:
GSMBUFFER VAR BYTE[16] '16 is used to tell buffer to store max of 16 characters
Caller VAR BYTE[13] '13 is used to tell buffer to store max of 16 characters
GSMTime VAR BYTE[17] '17 is used to tell buffer to store max of 16 characters
' Set all PortB i/o 1 to be an output and all other i/o as inputs
TRISB = %11111101
Main:
IF PushButton = 0 THEN SMS 'If button is pressed then run the SMS routine below
goto main ' however if the button is not pushed then just go back to the Main
' routine to wait for the button to be pressed
SMS: ' this is the SMS commands routine in order to send an SMS
HSEROUT ["AT" ,13,10] ' send AT to modem followed by a CR and line feed
HSERIN 5000, SMS, [WAIT("OK")] ' now wait for 5secs until OK is received however
' if it is not then goto the SMS routine again
HSEROUT ["AT+CMGF=1" ,13,10]
HSERIN 5000, SMS, [WAIT("OK")]
HSEROUT ["AT+CMGS=+447977578999"]
HSEROUT [13,10]
HSERIN 5000, SMS, [WAIT(">")]
HSEROUT ["BUTTON HAS BEEN PRESSED!"]
HSEROUT [26] ' this is ASCII for Ctrl+Z of which completes SMS sending
HSERIN 15000, SMS, [WAIT("+CMG")] ' then PIC should receive +CMG from modem
HSEROUT [10] 'ascii code for a line feed
goto main
READSMS: 'Use this routine to read first 16 characters of new messages and then
' store them in the GSMBUFFER variable above then you are able to add a line at
' the end in order for an led to be switched on whenever the modem receives the
' correct SMS command such as LED ON or SWITCH etc
HSEROUT ["AT+CMGF=1",13,10] 'Instruct modem to use text mode
HSerout ["AT+CMGL",13,10] 'List new Messages
' Below I Read Caller ID, time and 16 CHARs of Message
' The timeout value in the HSERIN statement is important, it takes some time
' before the phone replies to the AT+CMGL command
' skip works like this: skip 4 (skips 4 characters) - this is a sample of the
' message that would be received after the AT+CMGL command is sent:
' +CMGL: 2,"REC UNREAD","+447977578999" Test
' Please note that I can't seem to receive the time in the reply as in this
' example: (of which the line of code is written for below - so maybe I should
' edit the line of code below in order to miss it - also remember to update
' GSMTime variable above i.e. remove it if your going to adit the line below)
' +CMGR: "REC READ","+xx1234567890",,"05/05/26,17:19:23+00" GOOD
' Loop could be setup as a routine such as:
' Loop:
' List new Messages again - as above (think about it)
' HSerout ["AT+CMGL",13,10]
HSerin 5000,loop,[wait("UNREAD"),skip 3,STR Caller\13,skip 4,STR GSMTime\17,skip 6, STR GSMBUFFER\16\13]
' Basically in the above line, \13 - stores 13 characters and \17 - stores 17 characters etc into
' each of the buffers i.e. Caller & GSMTime etc etc
HSerout ["AT+CMGD=1;+CMGD=2",13,10] 'Intruct modem to delete SMS's from location
' 1 & 2 as this is where any new messages will be stored, so its a good idea to
' delete them after we have processed the SMS command above - please note that
' it would be a good idea to wait for an OK after this command but we can add
' this at a later time
' --- Put your code to output the result here i.e. IF gsmbuffer = (your sms command) then goto
' subroutine etc
IF GSMBUFFER = "SWITCH" THEN LED = 1
Thank you for your time.
Kind Regards,
Royce




Bookmarks