Example program that uses HTTPS to send JSON data to Axeda's cloud using a cellular device.

Dependencies:   mbed

Fork of Axeda_IoT_Connection_Example by Multi-Hackers

Revision:
1:2d299b96dc79
Parent:
0:562ad81d9dd4
Child:
2:cdfc629d20fa
--- a/main.cpp	Mon Jan 19 22:44:46 2015 +0000
+++ b/main.cpp	Mon Mar 23 19:13:52 2015 +0000
@@ -1,29 +1,41 @@
 #include "mbed.h"
 #include "mtsas.h"
+#include "certs.h"
 
 //Simple function that converts the HTTP result to a string
 //Ex: Result is 0, the string result will be "HTTP_OK"
 char * httpResToStr(HTTPResult res);
 
+/**Write the model for your device here. 
+ * Can be located on the axeda developer toolbox device info page.
+ */
+const string Modelstr = "";
+
+/**Write the serial for your device here
+ * Can be located on the axeda developer toolbox device info page.
+ */
+const string Serialstr = "";
+
+const string base_url = "https://nucleus-connect.axeda.com/ammp/data/1/";
+
 int main(){
     
     //Sets the log level to INFO, higher log levels produce more log output.
     //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
     MTSLog::setLogLevel(MTSLog::INFO_LEVEL);
     
-    /**Root Certificate(s) of the remote server you want to connect to.
-     * Make sure the certificates are in PEM format, contain \r\n to end each line,
-     * and if using multiple root CA certificates, just make multiple calls to the addRootCACertificate
-     * function for each root certificate you wish to add.
-     */
-    const char CERTIFICATES[] = "";
-    /**Example certificate formatted correctly (length not to scale):
-     -----BEGIN CERTIFICATE-----\r\n
-     aosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfija\r\n
-     afjklewijafliefhiszelifhlsfhilasihflihsalifhalhifliahlfihaslihfl\r\n
-     fawefaewf==\r\n
-     -----END CERTIFICATE-----\r\n
-     */
+    /**Root Certificate(s) of the remote server you want to connect to are listed in "certs.h"
+    * Make sure the certificates are in PEM format, contain \r\n to end each line,
+    * and if using multiple root CA certificates, just make multiple calls to the addRootCACertificate
+    * function for each root certificate you wish to add, or add them all as one concatenated string.
+    *
+    * Example certificate formatted correctly (length not to scale):
+    * -----BEGIN CERTIFICATE-----\r\n
+    * aosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfijaaosdfija\r\n
+    * afjklewijafliefhiszelifhlsfhilasihflihsalifhalhifliahlfihaslihfl\r\n
+    * fawefaewf==\r\n
+    * -----END CERTIFICATE-----\r\n
+    */
     
     //Modify to match your apn if you are using an HSPA radio with a SIM card
     const char APN[] = "";
@@ -59,7 +71,7 @@
         logFatal("Radio initialization failed");
         return 1;
     }
-    radio->configureSignals(D4,D7,RESET);
+    
     Transport::setTransport(radio);
     
     //Set radio APN
@@ -88,35 +100,53 @@
         }
     }
     
-    //Receive buffer
+    //Create receive interface and buffer
     char rbuf[2000];
     HTTPText* receive = new HTTPText(rbuf, sizeof(rbuf));
     
-    //Json output object, data must be in Json format
+    //Json output object, data must be in Json format, example data is input already:
+    //HTTPJson type merely sets the HTTP header to JSON type, nothing else is different from the HTTPText type
     char sbuf[2000] = "{\"data\":[{\"dataItems\":{\"mental_trauma\":1,\"physical_trauma\":2,\"emotional_trauma\":3}}]}\0";
     HTTPJson* send = new HTTPJson(sbuf);
     
     //Create HTTP Client Instance
     HTTPClient* http = new HTTPClient();
     if( !http || !receive || !send) {
-        logFatal("Failed to instantiate client or send buffer or receive buffer");
+        logFatal("Failed to instantiate client, send, or receive");
         return 1;
     }
     
+    /**Certificates can all be loaded concurrently as one string with the certificates
+     * concatenated after one another if so desired. Otherwise, the example here shows
+     * loading the certificates one by one.
+     */
     logTrace("Loading certificate(s)");
-    HTTPResult res = http->addRootCACertificate(CERTIFICATES);
+    HTTPResult res = http->addRootCACertificate(CERTIFICATE1);
+    if(res != HTTP_OK) {
+        logError("Failed to load CERTIFICATE1");
+    }
+    
+    res = http->addRootCACertificate(CERTIFICATE2);
     if(res != HTTP_OK) {
-        logError("Failed to load CERTIFICATES");
+        logError("Failed to load CERTIFICATE2");
+    }
+    
+    res = http->addRootCACertificate(CERTIFICATE3);
+    if(res != HTTP_OK) {
+        logError("Failed to load CERTIFICATE3");
     }
     
     /**Set whether or not to verify the remote server's certificate
-     * VERIFY_NONE,VERIFY_PEER
+     * VERIFY_NONE Sets the connection to be made using SSL protocol, 
+     * but without remot peer verification using the loaded certificates.
+     * VERIFY_PEER Sets the connection to be made using SSL protocol,
+     * and to verify the peer using the loaded root certificates.
      */
     http->setPeerVerification(VERIFY_PEER);
     
-    //URL for remote host (includes path)
-    //Format: https://nucleus-connect.axeda.com/ammp/data/1/MODEL_STRING_HERE!<SERIAL_NUMBER_HERE>
-    string url = "https://nucleus-connect.axeda.com/ammp/data/1/nlr_selfcrt_arm32!nlr__EPratt_multitech_com___1159423";
+    //URL for axeda.com device connection (includes path)
+    //Format: https://nucleus-connect.axeda.com/ammp/data/1/<MODEL_STRING_HERE>!<SERIAL_NUMBER_HERE>
+    string url = base_url + Modelstr + '!' + Serialstr;
         
     logTrace("HTTPS POST Request with Certificate");
     res = http->post(url.c_str(), *send, receive);