MultiTech Dragonfly with ROHM Sensor board sending data to IBM BlueMix Quickstart
Dependencies: mbed mtsas FXAS21002 FXOS8700 mbed-rtos
Fork of AvnetWorking_IBM_QuickStart by
Revision 0:6ce1803b7a03, committed 2015-09-30
- Comitter:
- mfiore
- Date:
- Wed Sep 30 19:08:10 2015 +0000
- Child:
- 1:26b8af61d0ac
- Commit message:
- initial commit - just set APN
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Sep 30 19:08:10 2015 +0000 @@ -0,0 +1,154 @@ +/** Dragonfly Cellular HTTPS Example + * Configures the cellular radio, brings up the cellular link, and does HTTPS GET and POST requests. + * To do HTTPS requests with a certain server, the root certificate used to validate that server's certificate must be installed. See ssl_certificates.h for information on how to get the proper root certificate. + * + * NOTE: This example changes the baud rate of the debug port to 115200 baud! + */ + +#include "mbed.h" +#include "mtsas.h" +#include "ssl_certificates.h" + +bool init_mtsas(); +char* httpResToStr(HTTPResult res); + +// The MTSSerialFlowControl object represents the physical serial link between the processor and the cellular radio. +mts::MTSSerialFlowControl* io; +// The Cellular object represents the cellular radio. +mts::Cellular* radio; + +// An APN is required for GSM radios. +static const char apn[] = ""; + +bool radio_ok = false; + +int main() { + // Change the baud rate of the debug port from the default 9600 to 115200. + Serial debug(USBTX, USBRX); + debug.baud(115200); + + //Sets the log level to INFO, higher log levels produce more log output. + //Possible levels: NONE, FATAL, ERROR, WARNING, INFO, DEBUG, TRACE + mts::MTSLog::setLogLevel(mts::MTSLog::INFO_LEVEL); + + logInfo("initializing cellular radio"); + radio_ok = init_mtsas(); + if (! radio_ok) { + while (true) { + logError("failed to initialize cellular radio"); + wait(1); + } + } + + logInfo("setting APN"); + if (radio->setApn(apn) != MTS_SUCCESS) + logError("failed to set APN to \"%s\"", apn); + + logInfo("bringing up the link"); + if (! radio->connect()) { + logError("failed to bring up the link"); + } else { + + // HTTPClient object used for HTTP requests. + HTTPClient http; + + // Enable strict certificate validation. + http.setPeerVerification(VERIFY_PEER); + + // Load certificates defined in ssl_certificates.h. + // See comments in ssl_certificates.h for information on how to get and format root certificates. + if (http.addRootCACertificate(ssl_certificates) != HTTP_OK) + logError("loading SSL certificates failed"); + + // HTTP GET example - httpbin.org + { + char http_rx_buf[1024]; + HTTPResult res; + + // IHTTPDataIn object - will contain data received from server. + HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); + + // Make a HTTP GET request to http://httpbin.org/ + res = http.get("https://httpbin.org/get", &http_rx); + if (res != HTTP_OK) + logError("HTTPS GET failed [%d][%s]", res, httpResToStr(res)); + else + logInfo("HTTPS GET succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); + } + + // HTTP POST example - httpbin.org + { + char http_rx_buf[1024]; + HTTPResult res; + + char http_tx_buf[] = "{ \"name\": \"temp_1\", \"temperature\": 75 }"; + + // IHTTPDataIn object - will contain data received from server. + HTTPText http_rx(http_rx_buf, sizeof(http_rx_buf)); + + // IHTTPDataOut object - contains data to be posted to server. + // HTTPJson automatically adds the JSON content-type header to the request. + HTTPJson http_tx(http_tx_buf, sizeof(http_tx_buf)); + + // Make a HTTP POST request to http://httpbin.org/ + res = http.post("https://httpbin.org/post", http_tx, &http_rx); + if (res != HTTP_OK) + logError("HTTPS POST failed [%d][%s]", res, httpResToStr(res)); + else + logInfo("HTTPS POST succeeded [%d]\r\n%s", http.getHTTPResponseCode(), http_rx_buf); + } + } + + logInfo("finished - bringing down link"); + radio->disconnect(); + + return 0; +} + +bool init_mtsas() { + io = new mts::MTSSerialFlowControl(RADIO_TX, RADIO_RX, RADIO_RTS, RADIO_CTS); + if (! io) + return false; + + // radio default baud rate is 115200 + io->baud(115200); + radio = mts::CellularFactory::create(io); + if (! radio) + return false; + + // Transport must be set properly before any TCPSocketConnection or UDPSocket objects are created + Transport::setTransport(radio); + + return true; +} + +char* httpResToStr(HTTPResult res) { + switch(res) { + case HTTP_PROCESSING: + return "HTTP_PROCESSING"; + case HTTP_PARSE: + return "HTTP_PARSE"; + case HTTP_DNS: + return "HTTP_DNS"; + case HTTP_PRTCL: + return "HTTP_PRTCL"; + case HTTP_NOTFOUND: + return "HTTP_NOTFOUND"; + case HTTP_REFUSED: + return "HTTP_REFUSED"; + case HTTP_ERROR: + return "HTTP_ERROR"; + case HTTP_TIMEOUT: + return "HTTP_TIMEOUT"; + case HTTP_CONN: + return "HTTP_CONN"; + case HTTP_CLOSED: + return "HTTP_CLOSED"; + case HTTP_REDIRECT: + return "HTTP_REDIRECT"; + case HTTP_OK: + return "HTTP_OK"; + default: + return "HTTP Result unknown"; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Sep 30 19:08:10 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mtsas.lib Wed Sep 30 19:08:10 2015 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/MultiTech/code/mtsas/#1667a524c7a3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ssl_certificates.h Wed Sep 30 19:08:10 2015 +0000 @@ -0,0 +1,76 @@ +/** Getting the root certificate for a server. + * + * This file currently contains root certificates for httpbin.org and google.com + * To get the root certificate for any given server, follow these steps: + * - navigate your browser to https://www.fairssl.se/en/ssltest + * - enter the address of the server you want your Dragonfly to make requests against (e.g. httpbin.org) + * - click the "Check SSL" button + * - scroll down to the end of the certificate list - the bottom certificate will probably have "root" in its name + * - click the [Click here to download the public part of this certificate] button and save the certificate + * - replace the certificate in this header with your new certificate or just add it - make sure each line is in quotes and ends with a \r\n just like the example + * + * You can have more than one root certificate installed at the same time: + * static const char ssl_certificates[] = + * "-----BEGIN CERTIFICATE-----\r\n" + * "MIIENjCCAx6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBvMQswCQYDVQQGEwJTRTEU\r\n" + * ...... + * "mnkPIAou1Z5jJh5VkpTYghdae9C8x49OhgQ=\r\n" + * "-----END CERTIFICATE-----\r\n" + * "-----BEGIN CERTIFICATE-----\r\n" + * "MIIENjCCAx6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBvMQswCQYDVQQGEwJTRTEU\r\n" + * ...... + * "mnkPIAou1Z5jJh5VkpTYghdae9C8x49OhgQ=\r\n" + * "-----END CERTIFICATE-----\r\n" + * ; + */ + +static const char ssl_certificates[] = + // Root CA for httpbin.org + "-----BEGIN CERTIFICATE-----\r\n" + "MIIENjCCAx6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBvMQswCQYDVQQGEwJTRTEU\r\n" + "MBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNVBAsTHUFkZFRydXN0IEV4dGVybmFs\r\n" + "IFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRUcnVzdCBFeHRlcm5hbCBDQSBSb290\r\n" + "MB4XDTAwMDUzMDEwNDgzOFoXDTIwMDUzMDEwNDgzOFowbzELMAkGA1UEBhMCU0Ux\r\n" + "FDASBgNVBAoTC0FkZFRydXN0IEFCMSYwJAYDVQQLEx1BZGRUcnVzdCBFeHRlcm5h\r\n" + "bCBUVFAgTmV0d29yazEiMCAGA1UEAxMZQWRkVHJ1c3QgRXh0ZXJuYWwgQ0EgUm9v\r\n" + "dDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALf3GjPm8gAELTngTlvt\r\n" + "H7xsD821+iO2zt6bETOXpClMfZOfvUq8k+0DGuOPz+VtUFrWlymUWoCwSXrbLpX9\r\n" + "uMq/NzgtHj6RQa1wVsfwTz/oMp50ysiQVOnGXw94nZpAPA6sYapeFI+eh6FqUNzX\r\n" + "mk6vBbOmcZSccbNQYArHE504B4YCqOmoaSYYkKtMsE8jqzpPhNjfzp/haW+710LX\r\n" + "a0Tkx63ubUFfclpxCDezeWWkWaCUN/cALw3CknLa0Dhy2xSoRcRdKn23tNbE7qzN\r\n" + "E0S3ySvdQwAl+mG5aWpYIxG3pzOPVnVZ9c0p10a3CitlttNCbxWyuHv77+ldU9U0\r\n" + "WicCAwEAAaOB3DCB2TAdBgNVHQ4EFgQUrb2YejS0Jvf6xCZU7wO94CTLVBowCwYD\r\n" + "VR0PBAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wgZkGA1UdIwSBkTCBjoAUrb2YejS0\r\n" + "Jvf6xCZU7wO94CTLVBqhc6RxMG8xCzAJBgNVBAYTAlNFMRQwEgYDVQQKEwtBZGRU\r\n" + "cnVzdCBBQjEmMCQGA1UECxMdQWRkVHJ1c3QgRXh0ZXJuYWwgVFRQIE5ldHdvcmsx\r\n" + "IjAgBgNVBAMTGUFkZFRydXN0IEV4dGVybmFsIENBIFJvb3SCAQEwDQYJKoZIhvcN\r\n" + "AQEFBQADggEBALCb4IUlwtYj4g+WBpKdQZic2YR5gdkeWxQHIzZlj7DYd7usQWxH\r\n" + "YINRsPkyPef89iYTx4AWpb9a/IfPeHmJIZriTAcKhjW88t5RxNKWt9x+Tu5w/Rw5\r\n" + "6wwCURQtjr0W4MHfRnXnJK3s9EK0hZNwEGe6nQY1ShjTK3rMUUKhemPR5ruhxSvC\r\n" + "Nr4TDea9Y355e6cJDUCrat2PisP29owaQgVR1EX1n6diIWgVIEM8med8vSTYqZEX\r\n" + "c4g/VhsxOBi0cQ+azcgOno4uG+GMmIPLHzHxREzGBHNJdmAPx/i9F4BrLunMTA5a\r\n" + "mnkPIAou1Z5jJh5VkpTYghdae9C8x49OhgQ=\r\n" + "-----END CERTIFICATE-----\r\n" + // Root CA for google.com + "-----BEGIN CERTIFICATE-----\r\n" + "MIIDfTCCAuagAwIBAgIDErvmMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT\r\n" + "MRAwDgYDVQQKEwdFcXVpZmF4MS0wKwYDVQQLEyRFcXVpZmF4IFNlY3VyZSBDZXJ0\r\n" + "aWZpY2F0ZSBBdXRob3JpdHkwHhcNMDIwNTIxMDQwMDAwWhcNMTgwODIxMDQwMDAw\r\n"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b8ravHNjkOR/ez4iyz0H7V84dJzjA1BOoa+Y7mHyhD8S\r\n" + "-----END CERTIFICATE-----\r\n" + ; \ No newline at end of file