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@1:7a2c5889a55d, 2014-07-24 (annotated)
- Committer:
- Vanger
- Date:
- Thu Jul 24 20:03:05 2014 +0000
- Revision:
- 1:7a2c5889a55d
- Parent:
- 0:0be90fca1622
- Child:
- 2:e20a75d47720
Tweaked output to be easier to distinguish
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 | 0:0be90fca1622 | 6 | const char APN[] = "internet"; |
Vanger | 0:0be90fca1622 | 7 | |
Vanger | 0:0be90fca1622 | 8 | //HTTP Test server is httpbin.org, the RequestLine is the HTTP request string |
Vanger | 0:0be90fca1622 | 9 | const char RequestLine[] = "GET /get HTTP/1.1\r\nHost: httpbin.org\r\nContent-type: application/MTSAS-DEV\r\nContent-length: 0\r\n\r\n"; |
Vanger | 0:0be90fca1622 | 10 | |
Vanger | 0:0be90fca1622 | 11 | //Response character array to read in the HTTP response |
Vanger | 0:0be90fca1622 | 12 | char read_response[4096] = {0}; |
Vanger | 0:0be90fca1622 | 13 | |
Vanger | 0:0be90fca1622 | 14 | /** STMicro Nucelo F401RE |
Vanger | 0:0be90fca1622 | 15 | * The supported jumper configurations of the MTSAS do not line up with |
Vanger | 0:0be90fca1622 | 16 | * the pin mapping of the Nucleo F401RE. Therefore, the MTSAS serial TX |
Vanger | 0:0be90fca1622 | 17 | * pin (JP8 Pin 2) must be manually jumped to Serial1 RX (Shield pin D2) |
Vanger | 0:0be90fca1622 | 18 | * and the MTSAS serial RX pin (JP9 Pin 2) pin must be manually jumped to |
Vanger | 0:0be90fca1622 | 19 | * Serial1 TX (Shield pin D8). |
Vanger | 0:0be90fca1622 | 20 | * Uncomment the following line to use the STMicro Nuceleo F401RE |
Vanger | 0:0be90fca1622 | 21 | */ |
Vanger | 0:0be90fca1622 | 22 | MTSSerialFlowControl* io = new MTSSerialFlowControl(D8, D2, D3, D6); |
Vanger | 0:0be90fca1622 | 23 | |
Vanger | 0:0be90fca1622 | 24 | /** Freescale KL46Z |
Vanger | 0:0be90fca1622 | 25 | * To configure the pins for the Freescale KL46Z board, use configuration B |
Vanger | 0:0be90fca1622 | 26 | * for the SocketModem. The TX pin should be jumped to pin D2 (JP8), and the |
Vanger | 0:0be90fca1622 | 27 | * RX pin should be jumped to pin D9 (JP9). |
Vanger | 0:0be90fca1622 | 28 | * Uncomment te following line to use the Freescale KL46Z board |
Vanger | 0:0be90fca1622 | 29 | */ |
Vanger | 0:0be90fca1622 | 30 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D2, D9, D3, D6); |
Vanger | 0:0be90fca1622 | 31 | |
Vanger | 0:0be90fca1622 | 32 | /** Freescale KL64F |
Vanger | 0:0be90fca1622 | 33 | * To configure the pins for the Freescale KL46Z board, use configuration A |
Vanger | 0:0be90fca1622 | 34 | * for the SocketModem. The TX pin should be jumped to pin D1 (JP8), and the |
Vanger | 0:0be90fca1622 | 35 | * RX pin should be jumped to pin D0 (JP9). |
Vanger | 0:0be90fca1622 | 36 | * Uncomment te following line to use the Freescale KL46F board |
Vanger | 0:0be90fca1622 | 37 | */ |
Vanger | 0:0be90fca1622 | 38 | //MTSSerialFlowControl* io = new MTSSerialFlowControl(D1, D0, D3, D6); |
Vanger | 0:0be90fca1622 | 39 | |
Vanger | 0:0be90fca1622 | 40 | //Sets the baudrate for communicating with the radio |
Vanger | 0:0be90fca1622 | 41 | io->baud(115200); |
Vanger | 0:0be90fca1622 | 42 | |
Vanger | 0:0be90fca1622 | 43 | Cellular* radio = CellularFactory::create(io); |
Vanger | 1:7a2c5889a55d | 44 | radio->setApn(APN); |
Vanger | 0:0be90fca1622 | 45 | |
Vanger | 0:0be90fca1622 | 46 | //Establish PPP link |
Vanger | 0:0be90fca1622 | 47 | radio->connect(); |
Vanger | 0:0be90fca1622 | 48 | |
Vanger | 0:0be90fca1622 | 49 | //Open TCP socket to httpbin.org |
Vanger | 0:0be90fca1622 | 50 | radio->open("httpbin.org", 80, IPStack::TCP); |
Vanger | 0:0be90fca1622 | 51 | |
Vanger | 0:0be90fca1622 | 52 | //Send HTTP request |
Vanger | 0:0be90fca1622 | 53 | radio->write(RequestLine, sizeof(RequestLine), 2000); |
Vanger | 1:7a2c5889a55d | 54 | printf("HTTP Request sent:\n\n%s\n\n",RequestLine); |
Vanger | 0:0be90fca1622 | 55 | |
Vanger | 0:0be90fca1622 | 56 | //Read HTTP response from server |
Vanger | 0:0be90fca1622 | 57 | radio->read(read_response, 4097, 2000); |
Vanger | 1:7a2c5889a55d | 58 | printf("HTTP Response read:\n\n%s\n\n", read_response); |
Vanger | 0:0be90fca1622 | 59 | |
Vanger | 0:0be90fca1622 | 60 | //Disconnect PPP link |
Vanger | 0:0be90fca1622 | 61 | radio->disconnect(); |
Vanger | 0:0be90fca1622 | 62 | |
Vanger | 0:0be90fca1622 | 63 | printf("End of example code\n"); |
Vanger | 0:0be90fca1622 | 64 | return 0; |
Vanger | 0:0be90fca1622 | 65 | } |