Example program demonstrating use of HTTP or HTTPS protocol over a cellular connection with httpbin.org using the MTSAS library. (Demonstrates GET,POST,basic-auth)
main.cpp@5:931abf45fb92, 2015-03-16 (annotated)
- Committer:
- Vanger
- Date:
- Mon Mar 16 18:41:09 2015 +0000
- Revision:
- 5:931abf45fb92
- Parent:
- 4:09d5f99b94b1
- Child:
- 6:e5ea200c66c3
Updated MTSAS library.; Added pin declarations for the Dragonfly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vanger | 0:0be90fca1622 | 1 | #include "mbed.h" |
Vanger | 0:0be90fca1622 | 2 | #include "mtsas.h" |
Vanger | 0:0be90fca1622 | 3 | |
Vanger | 0:0be90fca1622 | 4 | int main(){ |
Vanger | 0:0be90fca1622 | 5 | //Modify to match your apn if you are using an HSPA radio with a SIM card |
Vanger | 2:e20a75d47720 | 6 | const char APN[] = ""; |
Vanger | 2:e20a75d47720 | 7 | |
Vanger | 3:85181c1324f2 | 8 | //Sets the log level to INFO, higher log levels produce more log output. |
Vanger | 3:85181c1324f2 | 9 | //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE |
Vanger | 2:e20a75d47720 | 10 | MTSLog::setLogLevel(MTSLog::INFO_LEVEL); |
Vanger | 0:0be90fca1622 | 11 | |
Vanger | 2:e20a75d47720 | 12 | //Receive buffer |
Vanger | 2:e20a75d47720 | 13 | char rbuf[4000]; |
Vanger | 0:0be90fca1622 | 14 | |
Vanger | 2:e20a75d47720 | 15 | //url strings for HTTP requests, httpbin.org is a public website for fielding test HTTP requests |
Vanger | 2:e20a75d47720 | 16 | string url, base_url = "http://httpbin.org:80", url_get = "/get"; |
Vanger | 0:0be90fca1622 | 17 | |
Vanger | 0:0be90fca1622 | 18 | /** STMicro Nucelo F401RE |
Vanger | 0:0be90fca1622 | 19 | * The supported jumper configurations of the MTSAS do not line up with |
Vanger | 0:0be90fca1622 | 20 | * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX |
Vanger | 0:0be90fca1622 | 21 | * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2) |
Vanger | 0:0be90fca1622 | 22 | * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to |
Vanger | 0:0be90fca1622 | 23 | * Serial1 TX (Shield pin D8). |
Vanger | 0:0be90fca1622 | 24 | * Uncomment the following line to use the STMicro Nuceleo F401RE |
Vanger | 0:0be90fca1622 | 25 | */ |
Vanger | 0:0be90fca1622 | 26 | MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6); |
Vanger | 0:0be90fca1622 | 27 | |
Vanger | 5:931abf45fb92 | 28 | /** Dragonfly |
Vanger | 5:931abf45fb92 | 29 | * To configure the serial pins for the Dragonfly board, use: |
Vanger | 5:931abf45fb92 | 30 | * RADIO_TX = pin PC_7, RADIO_RX = pin PC_6 |
Vanger | 5:931abf45fb92 | 31 | * RADIO_RTS = pin PB_10,RADIO_CTS = pin PB_12 |
Vanger | 5:931abf45fb92 | 32 | * Uncomment the following lines to use the Dragonfly board |
Vanger | 5:931abf45fb92 | 33 | */ |
Vanger | 5:931abf45fb92 | 34 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(PC_7, PC_6, PB_10, PB_12); |
Vanger | 5:931abf45fb92 | 35 | //reset_pin = NC; |
Vanger | 5:931abf45fb92 | 36 | |
Vanger | 0:0be90fca1622 | 37 | /** Freescale KL46Z |
Vanger | 4:09d5f99b94b1 | 38 | * To configure the serial pins for the Freescale KL46Z board, use MTSAS jumper |
Vanger | 4:09d5f99b94b1 | 39 | * configuration B. Uncomment the following line to use the Freescale KL46Z board |
Vanger | 0:0be90fca1622 | 40 | */ |
Vanger | 0:0be90fca1622 | 41 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6); |
Vanger | 0:0be90fca1622 | 42 | |
Vanger | 3:85181c1324f2 | 43 | /** Freescale K64F |
Vanger | 4:09d5f99b94b1 | 44 | * To configure the serial pins for the Freescale K64F board, use MTSAS jumper |
Vanger | 4:09d5f99b94b1 | 45 | * configuration A. Uncomment the following line to use the Freescale K64F board |
Vanger | 0:0be90fca1622 | 46 | */ |
Vanger | 0:0be90fca1622 | 47 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6); |
Vanger | 0:0be90fca1622 | 48 | |
Vanger | 4:09d5f99b94b1 | 49 | //Sets the baud rate for communicating with the radio |
Vanger | 0:0be90fca1622 | 50 | io->baud(115200); |
Vanger | 0:0be90fca1622 | 51 | |
Vanger | 2:e20a75d47720 | 52 | //Initialize radio configurations |
Vanger | 0:0be90fca1622 | 53 | Cellular* radio = CellularFactory::create(io); |
Vanger | 2:e20a75d47720 | 54 | if( ! radio) { |
Vanger | 2:e20a75d47720 | 55 | logFatal("Radio initialization failed"); |
Vanger | 3:85181c1324f2 | 56 | return 1; |
Vanger | 2:e20a75d47720 | 57 | } |
Vanger | 2:e20a75d47720 | 58 | radio->configureSignals(D4,D7,RESET); |
Vanger | 2:e20a75d47720 | 59 | Transport::setTransport(radio); |
Vanger | 2:e20a75d47720 | 60 | |
Vanger | 2:e20a75d47720 | 61 | //Set up HTTP interface |
Vanger | 2:e20a75d47720 | 62 | HTTPClient* http = new HTTPClient(); |
Vanger | 3:85181c1324f2 | 63 | HTTPText* receive = new HTTPText(rbuf, 4000); |
Vanger | 2:e20a75d47720 | 64 | |
Vanger | 3:85181c1324f2 | 65 | //Set radio APN |
Vanger | 3:85181c1324f2 | 66 | for (int i = 0; i < 10; i++) { |
Vanger | 3:85181c1324f2 | 67 | if (i >= 10) { |
Vanger | 3:85181c1324f2 | 68 | logError("Failed to set APN to %s", APN); |
Vanger | 3:85181c1324f2 | 69 | } |
Vanger | 3:85181c1324f2 | 70 | if (radio->setApn(APN) == MTS_SUCCESS) { |
Vanger | 3:85181c1324f2 | 71 | logInfo("Successfully set APN to %s", APN); |
Vanger | 3:85181c1324f2 | 72 | break; |
Vanger | 3:85181c1324f2 | 73 | } else { |
Vanger | 3:85181c1324f2 | 74 | wait(1); |
Vanger | 3:85181c1324f2 | 75 | } |
Vanger | 3:85181c1324f2 | 76 | } |
Vanger | 0:0be90fca1622 | 77 | |
Vanger | 0:0be90fca1622 | 78 | //Establish PPP link |
Vanger | 3:85181c1324f2 | 79 | for (int i = 0; i < 10; i++) { |
Vanger | 3:85181c1324f2 | 80 | if (i >= 10) { |
Vanger | 3:85181c1324f2 | 81 | logError("Failed to establish PPP link"); |
Vanger | 3:85181c1324f2 | 82 | } |
Vanger | 3:85181c1324f2 | 83 | if (radio->connect() == true) { |
Vanger | 3:85181c1324f2 | 84 | logInfo("Successfully established PPP link"); |
Vanger | 3:85181c1324f2 | 85 | break; |
Vanger | 3:85181c1324f2 | 86 | } else { |
Vanger | 3:85181c1324f2 | 87 | wait(1); |
Vanger | 3:85181c1324f2 | 88 | } |
Vanger | 3:85181c1324f2 | 89 | } |
Vanger | 0:0be90fca1622 | 90 | |
Vanger | 2:e20a75d47720 | 91 | //Format HTTP Get request |
Vanger | 2:e20a75d47720 | 92 | url = base_url + url_get; |
Vanger | 0:0be90fca1622 | 93 | |
Vanger | 2:e20a75d47720 | 94 | //Send HTTP GET request |
Vanger | 2:e20a75d47720 | 95 | for (int i = 0; i < 10; i++) { |
Vanger | 2:e20a75d47720 | 96 | if(http->get(url.c_str(), receive, 5000) == HTTP_OK) { |
Vanger | 2:e20a75d47720 | 97 | http->get(url.c_str(), receive, 5000); |
Vanger | 3:85181c1324f2 | 98 | logInfo("HTTP get succeeded\n%s", rbuf); |
Vanger | 2:e20a75d47720 | 99 | break; |
Vanger | 2:e20a75d47720 | 100 | } |
Vanger | 2:e20a75d47720 | 101 | if (i >= 10) { |
Vanger | 2:e20a75d47720 | 102 | logError("HTTP GET failed"); |
Vanger | 2:e20a75d47720 | 103 | } |
Vanger | 2:e20a75d47720 | 104 | wait(1); |
Vanger | 2:e20a75d47720 | 105 | } |
Vanger | 0:0be90fca1622 | 106 | |
Vanger | 0:0be90fca1622 | 107 | //Disconnect PPP link |
Vanger | 0:0be90fca1622 | 108 | radio->disconnect(); |
Vanger | 0:0be90fca1622 | 109 | |
Vanger | 2:e20a75d47720 | 110 | logInfo("End of example code\n"); |
Vanger | 0:0be90fca1622 | 111 | return 0; |
Vanger | 0:0be90fca1622 | 112 | } |