CyaSSL usage example: Xively access with HTTPS

Dependencies:   EthernetInterface HTTPClient mbed-rtos LM75B mbed

Import this for FRDM-k64f: http://mbed.org/users/wolfSSL/code/CyaSSL-Xively-FRDM/

For using this example:

- Prepare mbed with Ethernet connection.

- Go to http://xively.com. Sign up, and create your channel.

- Copy "Feed ID" and "API Key" on the page to XI_FEED_ID and XI_API_KEY in xively-main.cpp.

- The demo assumes LM75B temperature sensor on Xively Jumpstart Kit board. If your board does not have it, replace tmp.read() with your own read.

- Build, download it to mbed, and you will get the result "Temp" on your Xively channel page.

- Click "Temp" to get temperature graph.

Have fun!

日本語: https://mbed.org/users/wolfSSL/code/CyaSSL-Xively/wiki/Xively-example-jp

Files at this revision

API Documentation at this revision

Comitter:
wolfSSL
Date:
Thu Apr 24 11:09:53 2014 +0000
Child:
1:1bb146136465
Commit message:
CyaSSL usage example: Xively access with HTTPS

Changed in this revision

CyaSSL.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
LM75B.lib Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
xively-main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CyaSSL.lib	Thu Apr 24 11:09:53 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/wolfSSL/code/CyaSSL/#9d17e4342598
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Apr 24 11:09:53 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#6a67d2bddc7c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Thu Apr 24 11:09:53 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/wolfSSL/code/HTTPClient/#14ecc2b2e282
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.lib	Thu Apr 24 11:09:53 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/chris/code/LM75B/#6a70c9303bbe
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Apr 24 11:09:53 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#4e0e79c9b976
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Apr 24 11:09:53 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6473597d706e
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xively-main.cpp	Thu Apr 24 11:09:53 2014 +0000
@@ -0,0 +1,107 @@
+/* main.cpp */
+/* Copyright (C) 2014 wolfSSL, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "HTTPClient.h"
+#include "LM75B.h"
+
+#define XI_FEED_ID 123456789// set Xively Feed ID (numerical, no quoutes)
+#define XI_API_KEY "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // set Xively API key (double-quoted string)
+
+#define XI_CHANNEL "Temp" // set Channel name
+
+#define XI_API     "https://api.xively.com/v2/feeds"
+
+#define TIMEOUT 500
+
+HTTPClient http;
+LM75B tmp(p28, p27);
+
+int xively()
+{
+    int ret ;
+    int i ;
+#define BUFFSIZE 1024
+    static char buff[BUFFSIZE];
+    const char put_template[] = "{\
+\"version\":\"1.0.0\",\"datastreams\":[\
+{\"id\":\"%s\",\"current_value\":\"%f\"}\
+]}\r\n" ;
+
+#define ALLOWANCE 30
+    char uri[sizeof(XI_API)+10+ALLOWANCE] ;
+    char put_data[sizeof(put_template)+sizeof(XI_CHANNEL)+ALLOWANCE] ;
+    char header[sizeof(XI_API_KEY)+ALLOWANCE] ;
+
+    sprintf(header, "X-ApiKey: %s\r\n", XI_API_KEY) ;
+    http.setHeader(header) ;
+
+    sprintf(uri, "%s/%d", XI_API, XI_FEED_ID) ;
+    while(1) {
+        ret = http.get(uri, buff, BUFFSIZE, TIMEOUT) ;
+        if (!ret) {
+            printf("== GET - read %d ==\n", strlen(buff));
+            printf("Result: %s\n", buff);
+            break ;
+        } else {
+            printf("++ Err = %d - HTTP ret = %d ++\n",
+                   ret, http.getHTTPResponseCode());
+        }
+    }
+
+    for(i=0; ; i++) {
+        printf("<<<< %d >>>>\n", i) ;
+        sprintf(uri, "%s/%d.json", XI_API, XI_FEED_ID) ;
+        sprintf(put_data, put_template, XI_CHANNEL, tmp.read()) ;
+        printf("Put Data:\n%s\n", put_data) ;
+
+        HTTPText outText(put_data, strlen(put_data));
+        HTTPText inText(buff, BUFFSIZE);
+
+        ret = http.put(uri, outText, &inText, TIMEOUT) ;
+        if (!ret) {
+            printf("== PUT - read %d ==\n", strlen(buff));
+            if(strlen(buff))
+                printf("Result: %s\n", buff);
+        } else {
+            printf("++ Err = %d - HTTP = %d ++\n", ret, http.getHTTPResponseCode());
+        }
+        wait(10.0) ;
+    }
+}
+
+main()
+{
+    int ret ;
+
+    EthernetInterface eth;
+    printf("Xively Starting,...\n") ;
+    ret = eth.init(); //Use DHCP
+    while(1) {
+        ret = eth.connect();
+        if(ret == 0)break ;
+    }
+    printf("IP = %s\n", eth.getIPAddress());
+
+    xively() ;
+
+    eth.disconnect();
+}