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@2:e20a75d47720, 2014-08-05 (annotated)
- Committer:
- Vanger
- Date:
- Tue Aug 05 14:43:29 2014 +0000
- Revision:
- 2:e20a75d47720
- Parent:
- 1:7a2c5889a55d
- Child:
- 3:85181c1324f2
Removed APN value
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 | 2:e20a75d47720 | 8 | /* Sets the log level for INFO level. INFO is a midway log level, available levels are: TRACE, DEBUG, INFO, WARNING, ERROR, FATAL, NONE */ |
Vanger | 2:e20a75d47720 | 9 | MTSLog::setLogLevel(MTSLog::INFO_LEVEL); |
Vanger | 0:0be90fca1622 | 10 | |
Vanger | 2:e20a75d47720 | 11 | //Receive buffer |
Vanger | 2:e20a75d47720 | 12 | char rbuf[4000]; |
Vanger | 0:0be90fca1622 | 13 | |
Vanger | 2:e20a75d47720 | 14 | //url strings for HTTP requests, httpbin.org is a public website for fielding test HTTP requests |
Vanger | 2:e20a75d47720 | 15 | string url, base_url = "http://httpbin.org:80", url_get = "/get"; |
Vanger | 0:0be90fca1622 | 16 | |
Vanger | 0:0be90fca1622 | 17 | /** STMicro Nucelo F401RE |
Vanger | 0:0be90fca1622 | 18 | * The supported jumper configurations of the MTSAS do not line up with |
Vanger | 0:0be90fca1622 | 19 | * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX |
Vanger | 0:0be90fca1622 | 20 | * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2) |
Vanger | 0:0be90fca1622 | 21 | * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to |
Vanger | 0:0be90fca1622 | 22 | * Serial1 TX (Shield pin D8). |
Vanger | 0:0be90fca1622 | 23 | * Uncomment the following line to use the STMicro Nuceleo F401RE |
Vanger | 0:0be90fca1622 | 24 | */ |
Vanger | 0:0be90fca1622 | 25 | MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6); |
Vanger | 0:0be90fca1622 | 26 | |
Vanger | 0:0be90fca1622 | 27 | /** Freescale KL46Z |
Vanger | 0:0be90fca1622 | 28 | * To configure the pins for the Freescale KL46Z board, use configuration B |
Vanger | 0:0be90fca1622 | 29 | * for the SocketModem. The TX pin should be jumped to pin D2 (JP8), and the |
Vanger | 0:0be90fca1622 | 30 | * RX pin should be jumped to pin D9 (JP9). |
Vanger | 0:0be90fca1622 | 31 | * Uncomment te following line to use the Freescale KL46Z board |
Vanger | 0:0be90fca1622 | 32 | */ |
Vanger | 0:0be90fca1622 | 33 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6); |
Vanger | 0:0be90fca1622 | 34 | |
Vanger | 0:0be90fca1622 | 35 | /** Freescale KL64F |
Vanger | 0:0be90fca1622 | 36 | * To configure the pins for the Freescale KL46Z board, use configuration A |
Vanger | 0:0be90fca1622 | 37 | * for the SocketModem. The TX pin should be jumped to pin D1 (JP8), and the |
Vanger | 0:0be90fca1622 | 38 | * RX pin should be jumped to pin D0 (JP9). |
Vanger | 0:0be90fca1622 | 39 | * Uncomment te following line to use the Freescale KL46F board |
Vanger | 0:0be90fca1622 | 40 | */ |
Vanger | 0:0be90fca1622 | 41 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6); |
Vanger | 0:0be90fca1622 | 42 | |
Vanger | 0:0be90fca1622 | 43 | //Sets the baudrate for communicating with the radio |
Vanger | 0:0be90fca1622 | 44 | io->baud(115200); |
Vanger | 0:0be90fca1622 | 45 | |
Vanger | 2:e20a75d47720 | 46 | //Initialize radio configurations |
Vanger | 0:0be90fca1622 | 47 | Cellular* radio = CellularFactory::create(io); |
Vanger | 2:e20a75d47720 | 48 | if( ! radio) { |
Vanger | 2:e20a75d47720 | 49 | logFatal("Radio initialization failed"); |
Vanger | 2:e20a75d47720 | 50 | } |
Vanger | 2:e20a75d47720 | 51 | radio->configureSignals(D4,D7,RESET); |
Vanger | 2:e20a75d47720 | 52 | Transport::setTransport(radio); |
Vanger | 2:e20a75d47720 | 53 | |
Vanger | 2:e20a75d47720 | 54 | //Set up HTTP interface |
Vanger | 2:e20a75d47720 | 55 | HTTPClient* http = new HTTPClient(); |
Vanger | 2:e20a75d47720 | 56 | HTTPText* receive = new HTTPText(rbuf); |
Vanger | 2:e20a75d47720 | 57 | |
Vanger | 2:e20a75d47720 | 58 | //Set APN value |
Vanger | 1:7a2c5889a55d | 59 | radio->setApn(APN); |
Vanger | 0:0be90fca1622 | 60 | |
Vanger | 0:0be90fca1622 | 61 | //Establish PPP link |
Vanger | 0:0be90fca1622 | 62 | radio->connect(); |
Vanger | 0:0be90fca1622 | 63 | |
Vanger | 2:e20a75d47720 | 64 | //Format HTTP Get request |
Vanger | 2:e20a75d47720 | 65 | url = base_url + url_get; |
Vanger | 0:0be90fca1622 | 66 | |
Vanger | 2:e20a75d47720 | 67 | //Send HTTP GET request |
Vanger | 2:e20a75d47720 | 68 | for (int i = 0; i < 10; i++) { |
Vanger | 2:e20a75d47720 | 69 | if(http->get(url.c_str(), receive, 5000) == HTTP_OK) { |
Vanger | 2:e20a75d47720 | 70 | http->get(url.c_str(), receive, 5000); |
Vanger | 2:e20a75d47720 | 71 | logInfo("HTTP get succeeded"); |
Vanger | 2:e20a75d47720 | 72 | break; |
Vanger | 2:e20a75d47720 | 73 | } |
Vanger | 2:e20a75d47720 | 74 | if (i >= 10) { |
Vanger | 2:e20a75d47720 | 75 | logError("HTTP GET failed"); |
Vanger | 2:e20a75d47720 | 76 | } |
Vanger | 2:e20a75d47720 | 77 | wait(1); |
Vanger | 2:e20a75d47720 | 78 | } |
Vanger | 0:0be90fca1622 | 79 | |
Vanger | 0:0be90fca1622 | 80 | //Disconnect PPP link |
Vanger | 0:0be90fca1622 | 81 | radio->disconnect(); |
Vanger | 0:0be90fca1622 | 82 | |
Vanger | 2:e20a75d47720 | 83 | logInfo("End of example code\n"); |
Vanger | 0:0be90fca1622 | 84 | return 0; |
Vanger | 0:0be90fca1622 | 85 | } |