
GSM example program for the SeeedStudio GPRS Shield V2.0, based on UART serial port connectivity (D0/D1 pins). This program uses a GSM library to dial a number, send SMS, read SMS or answer a call.
main.cpp@1:b22685c4503c, 2014-07-25 (annotated)
- Committer:
- screamer
- Date:
- Fri Jul 25 12:23:16 2014 +0000
- Revision:
- 1:b22685c4503c
- Parent:
- 0:cd3db55e6652
Add Apache2 license compatible header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 1:b22685c4503c | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
screamer | 1:b22685c4503c | 2 | * |
screamer | 1:b22685c4503c | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
screamer | 1:b22685c4503c | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
screamer | 1:b22685c4503c | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
screamer | 1:b22685c4503c | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
screamer | 1:b22685c4503c | 7 | * Software is furnished to do so, subject to the following conditions: |
screamer | 1:b22685c4503c | 8 | * |
screamer | 1:b22685c4503c | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
screamer | 1:b22685c4503c | 10 | * substantial portions of the Software. |
screamer | 1:b22685c4503c | 11 | * |
screamer | 1:b22685c4503c | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
screamer | 1:b22685c4503c | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
screamer | 1:b22685c4503c | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
screamer | 1:b22685c4503c | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
screamer | 1:b22685c4503c | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
screamer | 1:b22685c4503c | 17 | */ |
screamer | 1:b22685c4503c | 18 | |
screamer | 0:cd3db55e6652 | 19 | #include "mbed.h" |
screamer | 0:cd3db55e6652 | 20 | #include "GSM.h" |
screamer | 0:cd3db55e6652 | 21 | |
screamer | 0:cd3db55e6652 | 22 | #define PHONE_NUMBER "00xxxxxxxxxxxx" |
screamer | 0:cd3db55e6652 | 23 | |
screamer | 0:cd3db55e6652 | 24 | #define SEND_SMS_TEST 0 |
screamer | 0:cd3db55e6652 | 25 | #define CALL_UP_TEST 1 |
screamer | 0:cd3db55e6652 | 26 | #define ANSWER_TEST 0 |
screamer | 0:cd3db55e6652 | 27 | #define READ_SMS_TEST 0 |
screamer | 0:cd3db55e6652 | 28 | |
screamer | 0:cd3db55e6652 | 29 | /** On many platforms USBTX/USBRX overlap with serial on D1/D0 pins and enabling the below will interrupt the communication. |
screamer | 0:cd3db55e6652 | 30 | * You can use an LCD display to print the values or store them on an SD card etc. |
screamer | 0:cd3db55e6652 | 31 | */ |
screamer | 0:cd3db55e6652 | 32 | Serial pc(USBTX, USBRX); |
screamer | 0:cd3db55e6652 | 33 | |
screamer | 0:cd3db55e6652 | 34 | /** |
screamer | 0:cd3db55e6652 | 35 | * D1 - TX pin (RX on the GSM module side) |
screamer | 0:cd3db55e6652 | 36 | * D0 - RX pin (TX on the GSM module side) |
screamer | 0:cd3db55e6652 | 37 | * 19200 - GSM baud rate |
screamer | 0:cd3db55e6652 | 38 | */ |
screamer | 0:cd3db55e6652 | 39 | GSM gsm(D1, D0, 19200, PHONE_NUMBER); |
screamer | 0:cd3db55e6652 | 40 | |
screamer | 0:cd3db55e6652 | 41 | void messageHandle(void) |
screamer | 0:cd3db55e6652 | 42 | { |
screamer | 0:cd3db55e6652 | 43 | __disable_irq(); |
screamer | 0:cd3db55e6652 | 44 | int messageType = gsm.loopHandle(); |
screamer | 0:cd3db55e6652 | 45 | if(MESSAGE_RING == messageType) { |
screamer | 0:cd3db55e6652 | 46 | gsm.answer(); |
screamer | 0:cd3db55e6652 | 47 | } else if(MESSAGE_SMS == messageType) { |
screamer | 0:cd3db55e6652 | 48 | char smsMessage[SMS_MAX_LENGTH]; |
screamer | 0:cd3db55e6652 | 49 | gsm.getSMS(smsMessage); |
screamer | 0:cd3db55e6652 | 50 | } |
screamer | 0:cd3db55e6652 | 51 | __enable_irq(); |
screamer | 0:cd3db55e6652 | 52 | } |
screamer | 0:cd3db55e6652 | 53 | |
screamer | 0:cd3db55e6652 | 54 | int main(void) |
screamer | 0:cd3db55e6652 | 55 | { |
screamer | 0:cd3db55e6652 | 56 | while(0 != gsm.init()) { |
screamer | 0:cd3db55e6652 | 57 | wait(2); |
screamer | 0:cd3db55e6652 | 58 | } |
screamer | 0:cd3db55e6652 | 59 | |
screamer | 0:cd3db55e6652 | 60 | #if SEND_SMS_TEST |
screamer | 0:cd3db55e6652 | 61 | gsm.sendSMS(PHONE_NUMBER, "Hello from my GPRS shield"); |
screamer | 0:cd3db55e6652 | 62 | #endif |
screamer | 0:cd3db55e6652 | 63 | |
screamer | 0:cd3db55e6652 | 64 | #if CALL_UP_TEST |
screamer | 0:cd3db55e6652 | 65 | gsm.callUp(PHONE_NUMBER); |
screamer | 0:cd3db55e6652 | 66 | #endif |
screamer | 0:cd3db55e6652 | 67 | |
screamer | 0:cd3db55e6652 | 68 | #if ANSWER_TEST || READ_SMS_TEST |
screamer | 0:cd3db55e6652 | 69 | gsm.gsmSerial.attach(&messageHandle); |
screamer | 0:cd3db55e6652 | 70 | #endif |
screamer | 0:cd3db55e6652 | 71 | |
screamer | 0:cd3db55e6652 | 72 | while(1) { |
screamer | 0:cd3db55e6652 | 73 | wait(1); |
screamer | 0:cd3db55e6652 | 74 | } |
screamer | 0:cd3db55e6652 | 75 | } |