Tracks GPS coordinates of an asset. Uses: GPS GSM Accelerometer mbed
Dependencies: mbed MODSERIAL ADXL345 MODGPS
sms.cpp@0:4415987ca08f, 2012-05-04 (annotated)
- Committer:
- gsulc
- Date:
- Fri May 04 14:11:45 2012 +0000
- Revision:
- 0:4415987ca08f
v0.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsulc | 0:4415987ca08f | 1 | #include "sms.h" |
gsulc | 0:4415987ca08f | 2 | #include "SecurityMechanism.h" |
gsulc | 0:4415987ca08f | 3 | |
gsulc | 0:4415987ca08f | 4 | #define MESSAGE_BUFFER_SIZE 2*1024 |
gsulc | 0:4415987ca08f | 5 | |
gsulc | 0:4415987ca08f | 6 | // must define these in main.cpp |
gsulc | 0:4415987ca08f | 7 | extern MODSERIAL gsm; |
gsulc | 0:4415987ca08f | 8 | extern Serial pc; |
gsulc | 0:4415987ca08f | 9 | extern bool command_sent; |
gsulc | 0:4415987ca08f | 10 | char messageBufferIncoming[MESSAGE_BUFFER_SIZE]; |
gsulc | 0:4415987ca08f | 11 | //char messageBufferOutgoing[MESSAGE_BUFFER_SIZE]; |
gsulc | 0:4415987ca08f | 12 | //bool messageReceived = false; |
gsulc | 0:4415987ca08f | 13 | |
gsulc | 0:4415987ca08f | 14 | void GSM_init() { |
gsulc | 0:4415987ca08f | 15 | pc.printf("Setting up GSM Modem\r\n"); |
gsulc | 0:4415987ca08f | 16 | |
gsulc | 0:4415987ca08f | 17 | gsm.baud(115200); //Set Baud Rate |
gsulc | 0:4415987ca08f | 18 | |
gsulc | 0:4415987ca08f | 19 | gsm.puts("AT\r\n"); //Check Connection |
gsulc | 0:4415987ca08f | 20 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 21 | |
gsulc | 0:4415987ca08f | 22 | // Tune Down Baud |
gsulc | 0:4415987ca08f | 23 | pc.printf("Slowing Baud Down...."); |
gsulc | 0:4415987ca08f | 24 | gsm.puts("AT+IPR=19200\r\n"); //Select Bearer Servr Type autobaud, No name, Non-transparent |
gsulc | 0:4415987ca08f | 25 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 26 | |
gsulc | 0:4415987ca08f | 27 | gsm.baud(19200); |
gsulc | 0:4415987ca08f | 28 | gsm.format(8, Serial::None, 1); //Default format |
gsulc | 0:4415987ca08f | 29 | |
gsulc | 0:4415987ca08f | 30 | gsm.attach(&messageReceive, MODSERIAL::RxAutoDetect); //Attaches Interrupts |
gsulc | 0:4415987ca08f | 31 | gsm.autoDetectChar('\n'); //Set Detection to Line Feed |
gsulc | 0:4415987ca08f | 32 | |
gsulc | 0:4415987ca08f | 33 | gsm.puts("AT\r\n"); //Check Connection |
gsulc | 0:4415987ca08f | 34 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 35 | |
gsulc | 0:4415987ca08f | 36 | gsm.puts("AT+IFC=0,0"); //Disable handshake lines |
gsulc | 0:4415987ca08f | 37 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 38 | |
gsulc | 0:4415987ca08f | 39 | gsm.puts("AT+CSMP=17,167,0,0\r\n"); //Set Text Parameters (default values) |
gsulc | 0:4415987ca08f | 40 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 41 | |
gsulc | 0:4415987ca08f | 42 | gsm.puts("AT+CSCA?\r\n"); //Check Service Center |
gsulc | 0:4415987ca08f | 43 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 44 | |
gsulc | 0:4415987ca08f | 45 | gsm.puts("AT+CMGF=1\r\n"); //Set format to Text Mode |
gsulc | 0:4415987ca08f | 46 | |
gsulc | 0:4415987ca08f | 47 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 48 | /* |
gsulc | 0:4415987ca08f | 49 | gsm.printf("AT+CNMI=1,1,0,0,0\r\n"); //Set the new messages indicator |
gsulc | 0:4415987ca08f | 50 | wait(0.5); |
gsulc | 0:4415987ca08f | 51 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 52 | |
gsulc | 0:4415987ca08f | 53 | gsm.printf("AT+CSAS\r\n"); //Save the Current Setup, REGLED will light SOLID |
gsulc | 0:4415987ca08f | 54 | wait(3.0); |
gsulc | 0:4415987ca08f | 55 | messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 56 | //*/ |
gsulc | 0:4415987ca08f | 57 | pc.printf("GSM Setup Done\r\n"); |
gsulc | 0:4415987ca08f | 58 | } |
gsulc | 0:4415987ca08f | 59 | |
gsulc | 0:4415987ca08f | 60 | void send_SMS(string phoneNumber, string text) { |
gsulc | 0:4415987ca08f | 61 | pc.printf("Sending: %s\r\n", text); |
gsulc | 0:4415987ca08f | 62 | |
gsulc | 0:4415987ca08f | 63 | gsm.printf("AT+CMGS=\"%s\"\r\n", phoneNumber); |
gsulc | 0:4415987ca08f | 64 | wait(0.4); |
gsulc | 0:4415987ca08f | 65 | gsm.printf("%s", text); |
gsulc | 0:4415987ca08f | 66 | wait(0.4); |
gsulc | 0:4415987ca08f | 67 | gsm.printf("%c", SUB); |
gsulc | 0:4415987ca08f | 68 | wait(0.4); |
gsulc | 0:4415987ca08f | 69 | gsm.printf("\r\n"); |
gsulc | 0:4415987ca08f | 70 | |
gsulc | 0:4415987ca08f | 71 | if (messageProcess() == 1) { |
gsulc | 0:4415987ca08f | 72 | pc.printf("SMS sent\r\n"); |
gsulc | 0:4415987ca08f | 73 | } |
gsulc | 0:4415987ca08f | 74 | else { |
gsulc | 0:4415987ca08f | 75 | pc.printf("SMS send failed\r\n"); |
gsulc | 0:4415987ca08f | 76 | } |
gsulc | 0:4415987ca08f | 77 | } |
gsulc | 0:4415987ca08f | 78 | |
gsulc | 0:4415987ca08f | 79 | void check_SMS(){ |
gsulc | 0:4415987ca08f | 80 | //string text_id; |
gsulc | 0:4415987ca08f | 81 | //pc.printf("Checking Messages\r\n"); |
gsulc | 0:4415987ca08f | 82 | while (!gsm.writeable()) {} |
gsulc | 0:4415987ca08f | 83 | //gsm.puts("AT+CMGL=?\r\n"); |
gsulc | 0:4415987ca08f | 84 | //messageProcess(); |
gsulc | 0:4415987ca08f | 85 | //while (1) { |
gsulc | 0:4415987ca08f | 86 | gsm.puts("AT+CMGL=\"ALL\"\r\n"); //Check ALL messages |
gsulc | 0:4415987ca08f | 87 | wait(2); |
gsulc | 0:4415987ca08f | 88 | messageProcess(); |
gsulc | 0:4415987ca08f | 89 | //gsm.puts("AT+CMGR=1\r\n"); |
gsulc | 0:4415987ca08f | 90 | //messageProcess(); |
gsulc | 0:4415987ca08f | 91 | //gsm.puts("AT+CMGD=1\r\n"); |
gsulc | 0:4415987ca08f | 92 | //messageProcess(); |
gsulc | 0:4415987ca08f | 93 | //} |
gsulc | 0:4415987ca08f | 94 | //wait(0.4); |
gsulc | 0:4415987ca08f | 95 | /*gsm.scanf("%s", text_id); |
gsulc | 0:4415987ca08f | 96 | pc.printf("textID = %s\r\n", text_id); |
gsulc | 0:4415987ca08f | 97 | text_id = strtok(text_id, ": "); |
gsulc | 0:4415987ca08f | 98 | //messageProcess(); //Process incoming message |
gsulc | 0:4415987ca08f | 99 | gsm.puts("AT+CMGR=%s\r\n", text_id); |
gsulc | 0:4415987ca08f | 100 | messageProcess();*/ |
gsulc | 0:4415987ca08f | 101 | } |
gsulc | 0:4415987ca08f | 102 | |
gsulc | 0:4415987ca08f | 103 | void messageReceive(MODSERIAL_IRQ_INFO *q) { |
gsulc | 0:4415987ca08f | 104 | MODSERIAL *sys = q->serial; |
gsulc | 0:4415987ca08f | 105 | sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE); |
gsulc | 0:4415987ca08f | 106 | if (!strncmp(messageBufferIncoming, "+CMTI", sizeof("+CMTI")-1)) |
gsulc | 0:4415987ca08f | 107 | command_sent = true; |
gsulc | 0:4415987ca08f | 108 | //messageReceived = true; |
gsulc | 0:4415987ca08f | 109 | return; |
gsulc | 0:4415987ca08f | 110 | } |
gsulc | 0:4415987ca08f | 111 | |
gsulc | 0:4415987ca08f | 112 | int messageProcess() { |
gsulc | 0:4415987ca08f | 113 | int mpResult = 0; |
gsulc | 0:4415987ca08f | 114 | wait(0.4); |
gsulc | 0:4415987ca08f | 115 | if (!strncmp(messageBufferIncoming, "OK", sizeof("OK")-1)) mpResult = 1; |
gsulc | 0:4415987ca08f | 116 | else if (!strncmp(messageBufferIncoming, "ERROR", sizeof("ERROR")-1)) mpResult = 2; |
gsulc | 0:4415987ca08f | 117 | else mpResult = 1; |
gsulc | 0:4415987ca08f | 118 | pc.printf("%s\r\n", messageBufferIncoming); |
gsulc | 0:4415987ca08f | 119 | gsm.rxBufferFlush(); //Flush the Buffer |
gsulc | 0:4415987ca08f | 120 | //messageReceived = false; |
gsulc | 0:4415987ca08f | 121 | return mpResult; |
gsulc | 0:4415987ca08f | 122 | } |