Library for HTTPS Communication with Azure using SIMCOM Modules, such as SIM800, SIM900. SSL is required, update your module to the latest firmware.
HTTPSConnection.cpp
- Committer:
- BorjaTarazona
- Date:
- 2017-08-10
- Revision:
- 0:3cf9be45a676
File content as of revision 0:3cf9be45a676:
#define _DEBUG 1 #if _DEBUG //Enable debug #define __DEBUG__ #include <cstdio> #define DBG(x, ...) printf("[HTTPS : DBG] "x"\r\n", ##__VA_ARGS__); #define WARN(x, ...) printf("[HTTPS : WARN] "x"\r\n", ##__VA_ARGS__); #define ERR(x, ...) printf("[HTTPS : ERR] "x"\r\n", ##__VA_ARGS__); #else //Disable debug #define DBG(x, ...) #define WARN(x, ...) #define ERR(x, ...) #endif #include "HTTPSConnection.h" char* response; char *bearer; HTTPSConnection::HTTPSConnection(PinName tx, PinName rx, int baudrate) // ATSerial : sim800_HTTPS(tx, rx, baudrate) { } // Function that initializes the SIM900 module with the proper network provider parameters void HTTPSConnection::HTTPS_initialization(const char* gprs_apn){ // Check Communication with SIM900 response = sim800_HTTPS.sendCmdAndWaitForResp("ATE0\r\n", 100); //response = sim800_HTTPS_HTTPS.sendCmdAndWaitForResp("AT+CGMR=?\r\n", 2000); //response = sim800_HTTPS_HTTPS.sendCmdAndWaitForResp("AT+CGMR\r\n", 2000); DBG("%s", response); // Status of the network connection DBG("AT+CREG?"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+CREG?\r\n", 100); DBG("%s", response); DBG("AT+SAPBR=3,1, CONTYPE"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n", 2000); DBG("%s", response); // Set Network Provider APN // Freedompop // response = sim800_HTTPS_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=3,1,\"APN\",\"freedompop.foggmobile.com\"\r\n", "OK", 200); // Vodafone ES DBG("AT+SAPBR=3,1, APN"); char comm_apn[50]; sprintf(comm_apn,"AT+SAPBR=3,1,\"APN\",\"%s\"\r\n", gprs_apn); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=3,1,\"APN\",\"airtelwap.es\"\r\n", 500); DBG("%s", response); // Open bearer DBG("AT+SAPBR=1,1"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=1,1\r\n", 2000); DBG("%s", response); // Query bearer DBG("AT+SAPBR=2,1"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+SAPBR=2,1\r\n",2000); DBG("%s", response); // Enable HTTP functions DBG("AT+HTTPINIT"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPINIT\r\n", 2000); DBG("%s", response); // Does SSL functionality exist on our current firmware? //DBG("AT+HTTPSSL=?"); //response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPSSL=?\r\n", 2000); //DBG("%s", response); // Is SSL function enabled? //DBG("AT+HTTPSSL?"); //response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPSSL?\r\n", 2000); //DBG("%s", response); // Enable SSL functionality for HTTP DBG("AT+HTTPSSL=1"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPSSL=1\r\n", 2000); DBG("%s", response); } // Function to set our Azure account parameters void HTTPSConnection::HTTPS_setAzureParameters(const char* gprs_url, const char* gprs_sas, char* gprs_data){ // Set bearer profile identifier DBG("AT+HTTPPARA=CID,1"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPPARA=\"CID\",1\r\n", 2000); DBG("%s", response); // Set the URL and the Shared Access Signature DBG("AT+HTTPPARA=URL"); char comm_url[200]; sprintf(comm_url,"AT+HTTPPARA=\"URL\",\"%s\"\r\n", gprs_url); response = sim800_HTTPS.sendCmdAndWaitForResp(comm_url, 2000); DBG("%s", response); DBG("AT+HTTPPARA=USERDATA"); char comm_sas[200]; sprintf(comm_sas,"AT+HTTPPARA=\"USERDATA\",\"%s\"\r\n", gprs_sas); response = sim800_HTTPS.sendCmdAndWaitForResp(comm_sas, 2000); DBG("%s", response); // Set Content-Type field in the HTTP header DBG("AT+HTTPPARA=CONTENT"); response = sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n", 2000); DBG("%s", response); // Set Data length DBG("AT+HTTPDATA"); char https_data[30]; sprintf(https_data,"AT+HTTPDATA=%d,20000\r\n", strlen(gprs_data)); sim800_HTTPS.sendCmdAndWaitForResp(https_data, 200); // Set the JSON String char comm_data[1000]; sprintf(comm_data,"%s", gprs_data); response = sim800_HTTPS.sendCmdAndWaitForResp(comm_data, 1000); DBG("%s", response); } void HTTPSConnection::HTTPS_updateStringToSend(char* gprs_data){ // Set Data length DBG("AT+HTTPDATA"); char https_data[30]; sprintf(https_data,"AT+HTTPDATA=%d,20000\r\n", strlen(gprs_data)); sim800_HTTPS.sendCmdAndWaitForResp(https_data, 200); // Set the JSON String char comm_data[1000]; sprintf(comm_data,"%s", gprs_data); response = sim800_HTTPS.sendCmdAndWaitForResp(comm_data, 1000); DBG("%s", response); } char* HTTPSConnection::HTTPS_post(int timeout){ return sim800_HTTPS.sendCmdAndWaitForResp("AT+HTTPACTION=1\r\n", timeout); }