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)

Dependencies:   mbed mtsas

Committer:
Vanger
Date:
Thu Jul 24 19:10:18 2014 +0000
Revision:
0:0be90fca1622
Child:
1:7a2c5889a55d
Program for sending a get request to httpbin.org; Shows request and response for users to see the output and formatting.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:0be90fca1622 44 radio->setApn(APN);
Vanger 0:0be90fca1622 45 wait(10);
Vanger 0:0be90fca1622 46
Vanger 0:0be90fca1622 47 //Establish PPP link
Vanger 0:0be90fca1622 48 radio->connect();
Vanger 0:0be90fca1622 49
Vanger 0:0be90fca1622 50 //Open TCP socket to httpbin.org
Vanger 0:0be90fca1622 51 radio->open("httpbin.org", 80, IPStack::TCP);
Vanger 0:0be90fca1622 52
Vanger 0:0be90fca1622 53 //Send HTTP request
Vanger 0:0be90fca1622 54 radio->write(RequestLine, sizeof(RequestLine), 2000);
Vanger 0:0be90fca1622 55 printf("HTTP Request sent:[%s]\n",RequestLine);
Vanger 0:0be90fca1622 56
Vanger 0:0be90fca1622 57 //Read HTTP response from server
Vanger 0:0be90fca1622 58 radio->read(read_response, 4097, 2000);
Vanger 0:0be90fca1622 59 printf("HTTP Response read:[%s]\n", read_response);
Vanger 0:0be90fca1622 60
Vanger 0:0be90fca1622 61 //Disconnect PPP link
Vanger 0:0be90fca1622 62 radio->disconnect();
Vanger 0:0be90fca1622 63
Vanger 0:0be90fca1622 64 printf("End of example code\n");
Vanger 0:0be90fca1622 65 return 0;
Vanger 0:0be90fca1622 66 }