Simple example program to demonstrate and test cellular connection using the MTSAS library (over PPP protocol)
main.cpp@21:b32f287609be, 2015-07-09 (annotated)
- Committer:
- mfiore
- Date:
- Thu Jul 09 15:53:01 2015 +0000
- Revision:
- 21:b32f287609be
- Parent:
- 20:edc60053c31a
use mtsas from MultiTech team page instead of Multi-Hackers
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[] = ""; |
Vanger | 4:8b02a6b67f4d | 7 | |
Vanger | 9:0ed53023033b | 8 | //Sets the log level to INFO, higher log levels produce more log output. |
Vanger | 9:0ed53023033b | 9 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
Vanger | 4:8b02a6b67f4d | 10 | MTSLog::setLogLevel(MTSLog::INFO_LEVEL); |
Vanger | 0:47bc9ce390cc | 11 | |
Vanger | 0:47bc9ce390cc | 12 | /** STMicro Nucelo F401RE |
Vanger | 0:47bc9ce390cc | 13 | * The supported jumper configurations of the MTSAS do not line up with |
Vanger | 0:47bc9ce390cc | 14 | * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX |
Vanger | 0:47bc9ce390cc | 15 | * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2) |
Vanger | 0:47bc9ce390cc | 16 | * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to |
Vanger | 0:47bc9ce390cc | 17 | * Serial1 TX (Shield pin D8). |
Vanger | 18:7c94867f9412 | 18 | * Uncomment the following line to use the STMicro Nuceleo F401RE |
Vanger | 0:47bc9ce390cc | 19 | */ |
Vanger | 17:e2102f58f3c9 | 20 | MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6); |
Vanger | 0:47bc9ce390cc | 21 | |
Vanger | 16:8d511ce1bec8 | 22 | /** Dragonfly |
Vanger | 16:8d511ce1bec8 | 23 | * To configure the serial pins for the Dragonfly board, use: |
Vanger | 16:8d511ce1bec8 | 24 | * RADIO_TX = pin PC_7, RADIO_RX = pin PC_6 |
Vanger | 16:8d511ce1bec8 | 25 | * RADIO_RTS = pin PB_10,RADIO_CTS = pin PB_12 |
Vanger | 18:7c94867f9412 | 26 | * Uncomment the following line to use the Dragonfly board |
Vanger | 16:8d511ce1bec8 | 27 | */ |
mfiore | 20:edc60053c31a | 28 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); |
Vanger | 16:8d511ce1bec8 | 29 | |
Vanger | 0:47bc9ce390cc | 30 | /** Freescale KL46Z |
Vanger | 12:23c052e020a9 | 31 | * To configure the serial pins for the Freescale KL46Z board, use MTSAS jumper |
Vanger | 18:7c94867f9412 | 32 | * configuration B. Uncomment the following line to use the Freescale KL46Z board |
Vanger | 0:47bc9ce390cc | 33 | */ |
Vanger | 0:47bc9ce390cc | 34 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6); |
Vanger | 0:47bc9ce390cc | 35 | |
Vanger | 9:0ed53023033b | 36 | /** Freescale K64F |
Vanger | 12:23c052e020a9 | 37 | * To configure the serial pins for the Freescale K64F board, use MTSAS jumper |
Vanger | 18:7c94867f9412 | 38 | * configuration A. Uncomment the following line to use the Freescale K64F board |
Vanger | 0:47bc9ce390cc | 39 | */ |
Vanger | 17:e2102f58f3c9 | 40 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6); |
Vanger | 0:47bc9ce390cc | 41 | |
Vanger | 9:0ed53023033b | 42 | //Sets the baud rate for communicating with the radio |
Vanger | 1:4c54ec0a3a20 | 43 | io->baud(115200); |
Vanger | 0:47bc9ce390cc | 44 | |
Vanger | 8:95c226a1dca7 | 45 | //Create radio object |
Vanger | 0:47bc9ce390cc | 46 | Cellular* radio = CellularFactory::create(io); |
Vanger | 8:95c226a1dca7 | 47 | |
Vanger | 9:0ed53023033b | 48 | if (! radio) { |
Vanger | 9:0ed53023033b | 49 | logFatal("Failed to initialize radio"); |
Vanger | 9:0ed53023033b | 50 | return 1; |
Vanger | 9:0ed53023033b | 51 | } |
Vanger | 13:699045e9cce9 | 52 | Transport::setTransport(radio); |
Vanger | 9:0ed53023033b | 53 | |
Vanger | 8:95c226a1dca7 | 54 | //Set radio APN |
Vanger | 1:4c54ec0a3a20 | 55 | for (int i = 0; i < 10; i++) { |
Vanger | 1:4c54ec0a3a20 | 56 | if (i >= 10) { |
mfiore | 5:46e66c649006 | 57 | logError("Failed to set APN to %s", APN); |
Vanger | 1:4c54ec0a3a20 | 58 | } |
Vanger | 1:4c54ec0a3a20 | 59 | if (radio->setApn(APN) == MTS_SUCCESS) { |
mfiore | 5:46e66c649006 | 60 | logInfo("Successfully set APN to %s", APN); |
Vanger | 1:4c54ec0a3a20 | 61 | break; |
Vanger | 1:4c54ec0a3a20 | 62 | } else { |
Vanger | 1:4c54ec0a3a20 | 63 | wait(1); |
Vanger | 1:4c54ec0a3a20 | 64 | } |
Vanger | 1:4c54ec0a3a20 | 65 | } |
Vanger | 0:47bc9ce390cc | 66 | |
Vanger | 0:47bc9ce390cc | 67 | //Establish PPP link |
Vanger | 1:4c54ec0a3a20 | 68 | for (int i = 0; i < 10; i++) { |
Vanger | 1:4c54ec0a3a20 | 69 | if (i >= 10) { |
mfiore | 5:46e66c649006 | 70 | logError("Failed to establish PPP link"); |
Vanger | 1:4c54ec0a3a20 | 71 | } |
Vanger | 1:4c54ec0a3a20 | 72 | if (radio->connect() == true) { |
Vanger | 3:f22ad66e049e | 73 | logInfo("Successfully established PPP link"); |
Vanger | 1:4c54ec0a3a20 | 74 | break; |
Vanger | 1:4c54ec0a3a20 | 75 | } else { |
Vanger | 1:4c54ec0a3a20 | 76 | wait(1); |
Vanger | 1:4c54ec0a3a20 | 77 | } |
Vanger | 1:4c54ec0a3a20 | 78 | } |
Vanger | 0:47bc9ce390cc | 79 | |
Vanger | 1:4c54ec0a3a20 | 80 | //Ping google.com |
Vanger | 1:4c54ec0a3a20 | 81 | for (int i = 0; i < 10; i++) { |
Vanger | 1:4c54ec0a3a20 | 82 | if (i >= 10) { |
mfiore | 5:46e66c649006 | 83 | logError("Failed to ping www.google.com"); |
Vanger | 1:4c54ec0a3a20 | 84 | } |
Vanger | 3:f22ad66e049e | 85 | if (radio->ping("www.google.com") == true) { |
Vanger | 4:8b02a6b67f4d | 86 | logInfo("Successfully pinged www.google.com"); |
Vanger | 1:4c54ec0a3a20 | 87 | break; |
Vanger | 1:4c54ec0a3a20 | 88 | } else { |
Vanger | 1:4c54ec0a3a20 | 89 | wait(1); |
Vanger | 1:4c54ec0a3a20 | 90 | } |
Vanger | 1:4c54ec0a3a20 | 91 | } |
Vanger | 0:47bc9ce390cc | 92 | |
Vanger | 0:47bc9ce390cc | 93 | //Disconnect ppp link |
Vanger | 0:47bc9ce390cc | 94 | radio->disconnect(); |
Vanger | 0:47bc9ce390cc | 95 | |
Vanger | 3:f22ad66e049e | 96 | logInfo("End of example code"); |
Vanger | 0:47bc9ce390cc | 97 | return 0; |
Vanger | 8:95c226a1dca7 | 98 | } |