Library for HTTPS Communication with Azure using SIMCOM Modules, such as SIM800, SIM900. SSL is required, update your module to the latest firmware.

Dependents:   SIMInterface

Committer:
BorjaTarazona
Date:
Thu Aug 10 11:03:10 2017 +0000
Revision:
1:082b9b2c7935
Parent:
0:3cf9be45a676
Minor comment changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BorjaTarazona 0:3cf9be45a676 1 #define _DEBUG 1
BorjaTarazona 0:3cf9be45a676 2
BorjaTarazona 0:3cf9be45a676 3 #if _DEBUG
BorjaTarazona 0:3cf9be45a676 4 //Enable debug
BorjaTarazona 0:3cf9be45a676 5 #define __DEBUG__
BorjaTarazona 0:3cf9be45a676 6 #include <cstdio>
BorjaTarazona 0:3cf9be45a676 7 #define DBG(x, ...) printf("[HTTPS : DBG] "x"\r\n", ##__VA_ARGS__);
BorjaTarazona 0:3cf9be45a676 8 #define WARN(x, ...) printf("[HTTPS : WARN] "x"\r\n", ##__VA_ARGS__);
BorjaTarazona 0:3cf9be45a676 9 #define ERR(x, ...) printf("[HTTPS : ERR] "x"\r\n", ##__VA_ARGS__);
BorjaTarazona 0:3cf9be45a676 10
BorjaTarazona 0:3cf9be45a676 11 #else
BorjaTarazona 0:3cf9be45a676 12 //Disable debug
BorjaTarazona 0:3cf9be45a676 13 #define DBG(x, ...)
BorjaTarazona 0:3cf9be45a676 14 #define WARN(x, ...)
BorjaTarazona 0:3cf9be45a676 15 #define ERR(x, ...)
BorjaTarazona 0:3cf9be45a676 16 #endif
BorjaTarazona 0:3cf9be45a676 17
BorjaTarazona 0:3cf9be45a676 18 #include "HTTPSConnection.h"
BorjaTarazona 0:3cf9be45a676 19
BorjaTarazona 0:3cf9be45a676 20 char* response;
BorjaTarazona 0:3cf9be45a676 21 char *bearer;
BorjaTarazona 0:3cf9be45a676 22
BorjaTarazona 0:3cf9be45a676 23 HTTPSConnection::HTTPSConnection(PinName tx, PinName rx, int baudrate) // ATSerial
BorjaTarazona 0:3cf9be45a676 24 : sim800_HTTPS(tx, rx, baudrate)
BorjaTarazona 0:3cf9be45a676 25 {
BorjaTarazona 0:3cf9be45a676 26
BorjaTarazona 0:3cf9be45a676 27 }
BorjaTarazona 0:3cf9be45a676 28
BorjaTarazona 0:3cf9be45a676 29
BorjaTarazona 0:3cf9be45a676 30 // Function that initializes the SIM900 module with the proper network provider parameters
BorjaTarazona 0:3cf9be45a676 31 void HTTPSConnection::HTTPS_initialization(const char* gprs_apn){
BorjaTarazona 0:3cf9be45a676 32
BorjaTarazona 0:3cf9be45a676 33 // Check Communication with SIM900
BorjaTarazona 0:3cf9be45a676 34 response = sim800_HTTPS.sendCmdAndWaitForResp("ATE0\r\n", 100);
BorjaTarazona 0:3cf9be45a676 35 //response = sim800_HTTPS_HTTPS.sendCmdAndWaitForResp("AT+CGMR=?\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 36 //response = sim800_HTTPS_HTTPS.sendCmdAndWaitForResp("AT+CGMR\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 37 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 38
BorjaTarazona 0:3cf9be45a676 39 // Status of the network connection
BorjaTarazona 0:3cf9be45a676 40 DBG("AT+CREG?");
BorjaTarazona 0:3cf9be45a676 41 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+CREG?\r\n", 100);
BorjaTarazona 0:3cf9be45a676 42 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 43
BorjaTarazona 0:3cf9be45a676 44 DBG("AT+SAPBR=3,1, CONTYPE");
BorjaTarazona 0:3cf9be45a676 45 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 46 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 47
BorjaTarazona 0:3cf9be45a676 48 // Set Network Provider APN
BorjaTarazona 0:3cf9be45a676 49 // Freedompop
BorjaTarazona 0:3cf9be45a676 50 // response = sim800_HTTPS_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=3,1,\"APN\",\"freedompop.foggmobile.com\"\r\n", "OK", 200);
BorjaTarazona 0:3cf9be45a676 51 // Vodafone ES
BorjaTarazona 0:3cf9be45a676 52 DBG("AT+SAPBR=3,1, APN");
BorjaTarazona 0:3cf9be45a676 53 char comm_apn[50]; sprintf(comm_apn,"AT+SAPBR=3,1,\"APN\",\"%s\"\r\n", gprs_apn);
BorjaTarazona 0:3cf9be45a676 54 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=3,1,\"APN\",\"airtelwap.es\"\r\n", 500);
BorjaTarazona 0:3cf9be45a676 55 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 56
BorjaTarazona 0:3cf9be45a676 57 // Open bearer
BorjaTarazona 0:3cf9be45a676 58 DBG("AT+SAPBR=1,1");
BorjaTarazona 0:3cf9be45a676 59 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=1,1\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 60 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 61
BorjaTarazona 0:3cf9be45a676 62
BorjaTarazona 0:3cf9be45a676 63 // Query bearer
BorjaTarazona 0:3cf9be45a676 64 DBG("AT+SAPBR=2,1");
BorjaTarazona 0:3cf9be45a676 65 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=2,1\r\n",2000);
BorjaTarazona 0:3cf9be45a676 66 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 67
BorjaTarazona 0:3cf9be45a676 68 // Enable HTTP functions
BorjaTarazona 0:3cf9be45a676 69 DBG("AT+HTTPINIT");
BorjaTarazona 0:3cf9be45a676 70 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPINIT\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 71 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 72
BorjaTarazona 0:3cf9be45a676 73 // Does SSL functionality exist on our current firmware?
BorjaTarazona 0:3cf9be45a676 74 //DBG("AT+HTTPSSL=?");
BorjaTarazona 0:3cf9be45a676 75 //response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPSSL=?\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 76 //DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 77
BorjaTarazona 0:3cf9be45a676 78 // Is SSL function enabled?
BorjaTarazona 0:3cf9be45a676 79 //DBG("AT+HTTPSSL?");
BorjaTarazona 0:3cf9be45a676 80 //response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPSSL?\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 81 //DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 82
BorjaTarazona 0:3cf9be45a676 83 // Enable SSL functionality for HTTP
BorjaTarazona 0:3cf9be45a676 84 DBG("AT+HTTPSSL=1");
BorjaTarazona 0:3cf9be45a676 85 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPSSL=1\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 86 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 87
BorjaTarazona 0:3cf9be45a676 88 }
BorjaTarazona 0:3cf9be45a676 89
BorjaTarazona 0:3cf9be45a676 90 // Function to set our Azure account parameters
BorjaTarazona 0:3cf9be45a676 91 void HTTPSConnection::HTTPS_setAzureParameters(const char* gprs_url, const char* gprs_sas, char* gprs_data){
BorjaTarazona 0:3cf9be45a676 92
BorjaTarazona 0:3cf9be45a676 93 // Set bearer profile identifier
BorjaTarazona 0:3cf9be45a676 94 DBG("AT+HTTPPARA=CID,1");
BorjaTarazona 0:3cf9be45a676 95 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPPARA=\"CID\",1\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 96 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 97
BorjaTarazona 0:3cf9be45a676 98 // Set the URL and the Shared Access Signature
BorjaTarazona 0:3cf9be45a676 99 DBG("AT+HTTPPARA=URL");
BorjaTarazona 0:3cf9be45a676 100 char comm_url[200]; sprintf(comm_url,"AT+HTTPPARA=\"URL\",\"%s\"\r\n", gprs_url);
BorjaTarazona 0:3cf9be45a676 101 response = sim800_HTTPS.sendCmdAndWaitForResp(comm_url, 2000);
BorjaTarazona 0:3cf9be45a676 102 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 103 DBG("AT+HTTPPARA=USERDATA");
BorjaTarazona 0:3cf9be45a676 104 char comm_sas[200]; sprintf(comm_sas,"AT+HTTPPARA=\"USERDATA\",\"%s\"\r\n", gprs_sas);
BorjaTarazona 0:3cf9be45a676 105 response = sim800_HTTPS.sendCmdAndWaitForResp(comm_sas, 2000);
BorjaTarazona 0:3cf9be45a676 106 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 107
BorjaTarazona 0:3cf9be45a676 108 // Set Content-Type field in the HTTP header
BorjaTarazona 0:3cf9be45a676 109 DBG("AT+HTTPPARA=CONTENT");
BorjaTarazona 0:3cf9be45a676 110 response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n", 2000);
BorjaTarazona 0:3cf9be45a676 111 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 112
BorjaTarazona 0:3cf9be45a676 113 // Set Data length
BorjaTarazona 0:3cf9be45a676 114 DBG("AT+HTTPDATA");
BorjaTarazona 0:3cf9be45a676 115 char https_data[30]; sprintf(https_data,"AT+HTTPDATA=%d,20000\r\n", strlen(gprs_data));
BorjaTarazona 0:3cf9be45a676 116 sim800_HTTPS.sendCmdAndWaitForResp(https_data, 200);
BorjaTarazona 0:3cf9be45a676 117
BorjaTarazona 0:3cf9be45a676 118 // Set the JSON String
BorjaTarazona 0:3cf9be45a676 119 char comm_data[1000]; sprintf(comm_data,"%s", gprs_data);
BorjaTarazona 0:3cf9be45a676 120 response = sim800_HTTPS.sendCmdAndWaitForResp(comm_data, 1000);
BorjaTarazona 0:3cf9be45a676 121 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 122
BorjaTarazona 0:3cf9be45a676 123 }
BorjaTarazona 0:3cf9be45a676 124
BorjaTarazona 0:3cf9be45a676 125 void HTTPSConnection::HTTPS_updateStringToSend(char* gprs_data){
BorjaTarazona 0:3cf9be45a676 126
BorjaTarazona 0:3cf9be45a676 127 // Set Data length
BorjaTarazona 0:3cf9be45a676 128 DBG("AT+HTTPDATA");
BorjaTarazona 0:3cf9be45a676 129 char https_data[30]; sprintf(https_data,"AT+HTTPDATA=%d,20000\r\n", strlen(gprs_data));
BorjaTarazona 0:3cf9be45a676 130 sim800_HTTPS.sendCmdAndWaitForResp(https_data, 200);
BorjaTarazona 0:3cf9be45a676 131
BorjaTarazona 0:3cf9be45a676 132 // Set the JSON String
BorjaTarazona 0:3cf9be45a676 133 char comm_data[1000]; sprintf(comm_data,"%s", gprs_data);
BorjaTarazona 0:3cf9be45a676 134 response = sim800_HTTPS.sendCmdAndWaitForResp(comm_data, 1000);
BorjaTarazona 0:3cf9be45a676 135 DBG("%s", response);
BorjaTarazona 0:3cf9be45a676 136
BorjaTarazona 0:3cf9be45a676 137 }
BorjaTarazona 0:3cf9be45a676 138
BorjaTarazona 0:3cf9be45a676 139 char* HTTPSConnection::HTTPS_post(int timeout){
BorjaTarazona 0:3cf9be45a676 140 return sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPACTION=1\r\n", timeout);
BorjaTarazona 0:3cf9be45a676 141 }