Test for HTTP functions

Dependencies:   C027_Support mbed

Committer:
seraphin
Date:
Fri Nov 25 11:07:16 2016 +0000
Revision:
0:5b9b46935640
feat: HTTPS POST to get oauth token from api.bibo-siemens.com

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seraphin 0:5b9b46935640 1 #include "mbed.h"
seraphin 0:5b9b46935640 2 #include "MDM.h"
seraphin 0:5b9b46935640 3
seraphin 0:5b9b46935640 4 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
seraphin 0:5b9b46935640 5 #define SIMPIN NULL
seraphin 0:5b9b46935640 6 /*! The APN of your network operator SIM, sometimes it is "internet" check your
seraphin 0:5b9b46935640 7 contract with the network operator. You can also try to look-up your settings in
seraphin 0:5b9b46935640 8 google: https://www.google.de/search?q=APN+list */
seraphin 0:5b9b46935640 9 #define APN "gprs.swisscom.ch"
seraphin 0:5b9b46935640 10 //! Set the user name for your APN, or NULL if not needed
seraphin 0:5b9b46935640 11 #define USERNAME NULL
seraphin 0:5b9b46935640 12 //! Set the password for your APN, or NULL if not needed
seraphin 0:5b9b46935640 13 #define PASSWORD NULL
seraphin 0:5b9b46935640 14 //---------------------------
seraphin 0:5b9b46935640 15
seraphin 0:5b9b46935640 16 int main(void)
seraphin 0:5b9b46935640 17 {
seraphin 0:5b9b46935640 18 int ret;
seraphin 0:5b9b46935640 19 char buf[2048] = "";
seraphin 0:5b9b46935640 20 //give time to module for powering up (ms)
seraphin 0:5b9b46935640 21 wait_ms(1000);
seraphin 0:5b9b46935640 22 // Create the modem object
seraphin 0:5b9b46935640 23 MDMSerial mdm;
seraphin 0:5b9b46935640 24 mdm.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
seraphin 0:5b9b46935640 25 wait(5);
seraphin 0:5b9b46935640 26 //mdm.setDebug(4); // enable this for debugging issues
seraphin 0:5b9b46935640 27 // initialize the modem
seraphin 0:5b9b46935640 28 MDMParser::DevStatus devStatus = {};
seraphin 0:5b9b46935640 29 MDMParser::NetStatus netStatus = {};
seraphin 0:5b9b46935640 30 bool mdmOk = mdm.init(SIMPIN, &devStatus);
seraphin 0:5b9b46935640 31 mdm.dumpDevStatus(&devStatus);
seraphin 0:5b9b46935640 32 if (mdmOk) {
seraphin 0:5b9b46935640 33 // wait until we are connected
seraphin 0:5b9b46935640 34 mdmOk = mdm.registerNet(&netStatus);
seraphin 0:5b9b46935640 35 mdm.dumpNetStatus(&netStatus);
seraphin 0:5b9b46935640 36 }
seraphin 0:5b9b46935640 37 if (mdmOk)
seraphin 0:5b9b46935640 38 {
seraphin 0:5b9b46935640 39 // join the internet connection
seraphin 0:5b9b46935640 40 MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
seraphin 0:5b9b46935640 41 if (ip != NOIP)
seraphin 0:5b9b46935640 42 {
seraphin 0:5b9b46935640 43 mdm.dumpIp(ip);
seraphin 0:5b9b46935640 44
seraphin 0:5b9b46935640 45 int httpProfile = mdm.httpFindProfile(); //get the HTTP profile identifier
seraphin 0:5b9b46935640 46 if (httpProfile >= 0)
seraphin 0:5b9b46935640 47 {
seraphin 0:5b9b46935640 48 printf("Make an HTTP and HTTPS GET Request\r\n");
seraphin 0:5b9b46935640 49 if(mdm.httpResetProfile(httpProfile))
seraphin 0:5b9b46935640 50 {
seraphin 0:5b9b46935640 51 if (mdm.httpSetPar(httpProfile,MDMParser::HTTP_SERVER_NAME,"api.bibo-siemens.com"))
seraphin 0:5b9b46935640 52 {
seraphin 0:5b9b46935640 53 if (mdm.httpSetPar(httpProfile,MDMParser::HTTP_SECURE,"1")) //HTTP Secure option enabled
seraphin 0:5b9b46935640 54 {
seraphin 0:5b9b46935640 55 mdm.httpSetPar(httpProfile,MDMParser::HTTP_USER_NAME,"bibo-gateway");
seraphin 0:5b9b46935640 56 mdm.httpSetPar(httpProfile,MDMParser::HTTP_PASSWORD,"bibo-gateway");
seraphin 0:5b9b46935640 57 mdm.httpSetPar(httpProfile,MDMParser::HTTP_AUTH_TYPE,"1");
seraphin 0:5b9b46935640 58 //mdm.httpSetPar(httpProfile,MDMParser::HTTP_SERVER_PORT,"443");
seraphin 0:5b9b46935640 59
seraphin 0:5b9b46935640 60 ret = mdm.httpCommand(httpProfile,MDMParser::HTTP_POST_DATA,"/v1/oauth/token?grant_type=client_credentials",
seraphin 0:5b9b46935640 61 "post","\n",4,NULL,buf,sizeof(buf));
seraphin 0:5b9b46935640 62 if(ret > 0)
seraphin 0:5b9b46935640 63 printf("HTTPS Post: %s", buf);
seraphin 0:5b9b46935640 64 } else {
seraphin 0:5b9b46935640 65 printf("Abnormal condition during the set of HTTP secure option\r\n");
seraphin 0:5b9b46935640 66 }
seraphin 0:5b9b46935640 67
seraphin 0:5b9b46935640 68 } else {
seraphin 0:5b9b46935640 69 printf("Abnormal condition during the set of the HTTP server name\r\n");
seraphin 0:5b9b46935640 70 }
seraphin 0:5b9b46935640 71 } else {
seraphin 0:5b9b46935640 72 printf("Abnormal condition during the reset of HTTP profile %d\r\n", httpProfile);
seraphin 0:5b9b46935640 73 }
seraphin 0:5b9b46935640 74 }
seraphin 0:5b9b46935640 75 }
seraphin 0:5b9b46935640 76 }
seraphin 0:5b9b46935640 77 }