Simple example program to demonstrate and test cellular connection using the MTSAS library (over PPP protocol)
main.cpp@15:ae7bfcdf9e6a, 2014-11-11 (annotated)
- Committer:
- mfiore
- Date:
- Tue Nov 11 21:29:35 2014 +0000
- Revision:
- 15:ae7bfcdf9e6a
- Parent:
- 13:699045e9cce9
- Child:
- 16:8d511ce1bec8
kludge: handle different reset pin names
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vanger | 0:47bc9ce390cc | 1 | #include "mbed.h" |
Vanger | 0:47bc9ce390cc | 2 | #include "mtsas.h" |
Vanger | 0:47bc9ce390cc | 3 | |
Vanger | 0:47bc9ce390cc | 4 | int main(){ |
Vanger | 0:47bc9ce390cc | 5 | //Modify to match your apn if you are using an HSPA radio with a SIM card |
Vanger | 4:8b02a6b67f4d | 6 | const char APN[] = ""; |
mfiore | 15:ae7bfcdf9e6a | 7 | PinName reset_pin; |
Vanger | 4:8b02a6b67f4d | 8 | |
Vanger | 9:0ed53023033b | 9 | //Sets the log level to INFO, higher log levels produce more log output. |
Vanger | 9:0ed53023033b | 10 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
Vanger | 4:8b02a6b67f4d | 11 | MTSLog::setLogLevel(MTSLog::INFO_LEVEL); |
Vanger | 0:47bc9ce390cc | 12 | |
Vanger | 0:47bc9ce390cc | 13 | /** STMicro Nucelo F401RE |
Vanger | 0:47bc9ce390cc | 14 | * The supported jumper configurations of the MTSAS do not line up with |
Vanger | 0:47bc9ce390cc | 15 | * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX |
Vanger | 0:47bc9ce390cc | 16 | * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2) |
Vanger | 0:47bc9ce390cc | 17 | * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to |
Vanger | 0:47bc9ce390cc | 18 | * Serial1 TX (Shield pin D8). |
mfiore | 15:ae7bfcdf9e6a | 19 | * Uncomment the following lines to use the STMicro Nuceleo F401RE |
Vanger | 0:47bc9ce390cc | 20 | */ |
mfiore | 15:ae7bfcdf9e6a | 21 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6); |
mfiore | 15:ae7bfcdf9e6a | 22 | //reset_pin = RESET; |
Vanger | 0:47bc9ce390cc | 23 | |
Vanger | 0:47bc9ce390cc | 24 | /** Freescale KL46Z |
Vanger | 12:23c052e020a9 | 25 | * To configure the serial pins for the Freescale KL46Z board, use MTSAS jumper |
mfiore | 15:ae7bfcdf9e6a | 26 | * configuration B. Uncomment the following lines to use the Freescale KL46Z board |
Vanger | 0:47bc9ce390cc | 27 | */ |
Vanger | 0:47bc9ce390cc | 28 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6); |
mfiore | 15:ae7bfcdf9e6a | 29 | //reset_pin = PTA_20; |
Vanger | 0:47bc9ce390cc | 30 | |
Vanger | 9:0ed53023033b | 31 | /** Freescale K64F |
Vanger | 12:23c052e020a9 | 32 | * To configure the serial pins for the Freescale K64F board, use MTSAS jumper |
mfiore | 15:ae7bfcdf9e6a | 33 | * configuration A. Uncomment the following lines to use the Freescale K64F board |
Vanger | 0:47bc9ce390cc | 34 | */ |
mfiore | 15:ae7bfcdf9e6a | 35 | MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6); |
mfiore | 15:ae7bfcdf9e6a | 36 | reset_pin = NC; |
Vanger | 0:47bc9ce390cc | 37 | |
Vanger | 9:0ed53023033b | 38 | //Sets the baud rate for communicating with the radio |
Vanger | 1:4c54ec0a3a20 | 39 | io->baud(115200); |
Vanger | 0:47bc9ce390cc | 40 | |
Vanger | 8:95c226a1dca7 | 41 | //Create radio object |
Vanger | 0:47bc9ce390cc | 42 | Cellular* radio = CellularFactory::create(io); |
Vanger | 8:95c226a1dca7 | 43 | |
Vanger | 9:0ed53023033b | 44 | if (! radio) { |
Vanger | 9:0ed53023033b | 45 | logFatal("Failed to initialize radio"); |
Vanger | 9:0ed53023033b | 46 | return 1; |
Vanger | 9:0ed53023033b | 47 | } |
Vanger | 13:699045e9cce9 | 48 | Transport::setTransport(radio); |
mfiore | 15:ae7bfcdf9e6a | 49 | radio->configureSignals(D4,D7,reset_pin); |
Vanger | 9:0ed53023033b | 50 | |
Vanger | 8:95c226a1dca7 | 51 | //Set radio APN |
Vanger | 1:4c54ec0a3a20 | 52 | for (int i = 0; i < 10; i++) { |
Vanger | 1:4c54ec0a3a20 | 53 | if (i >= 10) { |
mfiore | 5:46e66c649006 | 54 | logError("Failed to set APN to %s", APN); |
Vanger | 1:4c54ec0a3a20 | 55 | } |
Vanger | 1:4c54ec0a3a20 | 56 | if (radio->setApn(APN) == MTS_SUCCESS) { |
mfiore | 5:46e66c649006 | 57 | logInfo("Successfully set APN to %s", APN); |
Vanger | 1:4c54ec0a3a20 | 58 | break; |
Vanger | 1:4c54ec0a3a20 | 59 | } else { |
Vanger | 1:4c54ec0a3a20 | 60 | wait(1); |
Vanger | 1:4c54ec0a3a20 | 61 | } |
Vanger | 1:4c54ec0a3a20 | 62 | } |
Vanger | 0:47bc9ce390cc | 63 | |
Vanger | 0:47bc9ce390cc | 64 | //Establish PPP link |
Vanger | 1:4c54ec0a3a20 | 65 | for (int i = 0; i < 10; i++) { |
Vanger | 1:4c54ec0a3a20 | 66 | if (i >= 10) { |
mfiore | 5:46e66c649006 | 67 | logError("Failed to establish PPP link"); |
Vanger | 1:4c54ec0a3a20 | 68 | } |
Vanger | 1:4c54ec0a3a20 | 69 | if (radio->connect() == true) { |
Vanger | 3:f22ad66e049e | 70 | logInfo("Successfully established PPP link"); |
Vanger | 1:4c54ec0a3a20 | 71 | break; |
Vanger | 1:4c54ec0a3a20 | 72 | } else { |
Vanger | 1:4c54ec0a3a20 | 73 | wait(1); |
Vanger | 1:4c54ec0a3a20 | 74 | } |
Vanger | 1:4c54ec0a3a20 | 75 | } |
Vanger | 0:47bc9ce390cc | 76 | |
Vanger | 1:4c54ec0a3a20 | 77 | //Ping google.com |
Vanger | 1:4c54ec0a3a20 | 78 | for (int i = 0; i < 10; i++) { |
Vanger | 1:4c54ec0a3a20 | 79 | if (i >= 10) { |
mfiore | 5:46e66c649006 | 80 | logError("Failed to ping www.google.com"); |
Vanger | 1:4c54ec0a3a20 | 81 | } |
Vanger | 3:f22ad66e049e | 82 | if (radio->ping("www.google.com") == true) { |
Vanger | 4:8b02a6b67f4d | 83 | logInfo("Successfully pinged www.google.com"); |
Vanger | 1:4c54ec0a3a20 | 84 | break; |
Vanger | 1:4c54ec0a3a20 | 85 | } else { |
Vanger | 1:4c54ec0a3a20 | 86 | wait(1); |
Vanger | 1:4c54ec0a3a20 | 87 | } |
Vanger | 1:4c54ec0a3a20 | 88 | } |
Vanger | 0:47bc9ce390cc | 89 | |
Vanger | 0:47bc9ce390cc | 90 | //Disconnect ppp link |
Vanger | 0:47bc9ce390cc | 91 | radio->disconnect(); |
Vanger | 0:47bc9ce390cc | 92 | |
Vanger | 3:f22ad66e049e | 93 | logInfo("End of example code"); |
Vanger | 0:47bc9ce390cc | 94 | return 0; |
Vanger | 8:95c226a1dca7 | 95 | } |