Gordon Sulc / Mbed 2 deprecated AntiTheftGPS

Dependencies:   mbed MODSERIAL ADXL345 MODGPS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sms.cpp Source File

sms.cpp

00001 #include "sms.h"
00002 #include "SecurityMechanism.h"
00003 
00004 #define MESSAGE_BUFFER_SIZE 2*1024
00005 
00006 // must define these in main.cpp
00007 extern MODSERIAL gsm;
00008 extern Serial pc;
00009 extern bool command_sent;
00010 char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
00011 //char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
00012 //bool messageReceived = false;
00013 
00014 void GSM_init() {
00015     pc.printf("Setting up GSM Modem\r\n");
00016     
00017     gsm.baud(115200);                         //Set Baud Rate
00018     
00019     gsm.puts("AT\r\n");                       //Check Connection
00020     messageProcess();                         //Process incoming message
00021     
00022     // Tune Down Baud
00023     pc.printf("Slowing Baud Down....");
00024     gsm.puts("AT+IPR=19200\r\n");            //Select Bearer Servr Type autobaud, No name, Non-transparent
00025     messageProcess();                         //Process incoming message
00026     
00027     gsm.baud(19200);
00028     gsm.format(8, Serial::None, 1);         //Default format
00029     
00030     gsm.attach(&messageReceive, MODSERIAL::RxAutoDetect);     //Attaches Interrupts
00031     gsm.autoDetectChar('\n');                                 //Set Detection to Line Feed
00032     
00033     gsm.puts("AT\r\n");                       //Check Connection
00034     messageProcess();                         //Process incoming message
00035    
00036     gsm.puts("AT+IFC=0,0");                   //Disable handshake lines
00037     messageProcess();                         //Process incoming message
00038     
00039     gsm.puts("AT+CSMP=17,167,0,0\r\n");       //Set Text Parameters (default values)
00040     messageProcess();                         //Process incoming message
00041     
00042     gsm.puts("AT+CSCA?\r\n");                 //Check Service Center
00043     messageProcess();                         //Process incoming message
00044 
00045     gsm.puts("AT+CMGF=1\r\n");                //Set format to Text Mode
00046     
00047     messageProcess();                         //Process incoming message
00048     /*
00049     gsm.printf("AT+CNMI=1,1,0,0,0\r\n");      //Set the new messages indicator
00050     wait(0.5);
00051     messageProcess();                         //Process incoming message
00052 
00053     gsm.printf("AT+CSAS\r\n");                //Save the Current Setup, REGLED will light SOLID
00054     wait(3.0);
00055     messageProcess();                         //Process incoming message
00056     //*/
00057     pc.printf("GSM Setup Done\r\n");
00058 }
00059 
00060 void send_SMS(string phoneNumber, string text) {
00061     pc.printf("Sending: %s\r\n", text);
00062 
00063     gsm.printf("AT+CMGS=\"%s\"\r\n", phoneNumber);
00064     wait(0.4);
00065     gsm.printf("%s", text);
00066     wait(0.4);
00067     gsm.printf("%c", SUB);
00068     wait(0.4);
00069     gsm.printf("\r\n");
00070     
00071     if (messageProcess() == 1) {
00072         pc.printf("SMS sent\r\n");
00073     } 
00074     else {
00075         pc.printf("SMS send failed\r\n");
00076     }
00077 }
00078 
00079 void check_SMS(){
00080     //string text_id;
00081     //pc.printf("Checking Messages\r\n");
00082     while (!gsm.writeable()) {}
00083     //gsm.puts("AT+CMGL=?\r\n");
00084     //messageProcess();
00085     //while (1) {
00086     gsm.puts("AT+CMGL=\"ALL\"\r\n");          //Check ALL messages
00087     wait(2);
00088     messageProcess();
00089     //gsm.puts("AT+CMGR=1\r\n");
00090     //messageProcess();
00091     //gsm.puts("AT+CMGD=1\r\n");
00092     //messageProcess();
00093     //}
00094     //wait(0.4);
00095     /*gsm.scanf("%s", text_id);
00096     pc.printf("textID = %s\r\n", text_id);
00097     text_id = strtok(text_id, ": ");
00098     //messageProcess();                         //Process incoming message
00099     gsm.puts("AT+CMGR=%s\r\n", text_id);
00100     messageProcess();*/
00101 }
00102 
00103 void messageReceive(MODSERIAL_IRQ_INFO *q) {
00104     MODSERIAL *sys = q->serial;
00105     sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
00106     if (!strncmp(messageBufferIncoming, "+CMTI", sizeof("+CMTI")-1))
00107         command_sent = true;
00108     //messageReceived = true;
00109     return;
00110 }
00111 
00112 int messageProcess() {
00113     int mpResult = 0;
00114     wait(0.4);
00115     if (!strncmp(messageBufferIncoming, "OK", sizeof("OK")-1)) mpResult = 1;
00116     else if (!strncmp(messageBufferIncoming, "ERROR", sizeof("ERROR")-1)) mpResult = 2;
00117     else mpResult = 1;
00118     pc.printf("%s\r\n", messageBufferIncoming);
00119     gsm.rxBufferFlush();                            //Flush the Buffer
00120     //messageReceived = false;
00121     return mpResult;
00122 }