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

Committer:
wolfSSL
Date:
Thu Apr 24 11:09:53 2014 +0000
Revision:
0:70ada960373c
Child:
1:1bb146136465
CyaSSL usage example: Xively access with HTTPS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:70ada960373c 1 /* main.cpp */
wolfSSL 0:70ada960373c 2 /* Copyright (C) 2014 wolfSSL, MIT License
wolfSSL 0:70ada960373c 3 *
wolfSSL 0:70ada960373c 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wolfSSL 0:70ada960373c 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
wolfSSL 0:70ada960373c 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
wolfSSL 0:70ada960373c 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
wolfSSL 0:70ada960373c 8 * furnished to do so, subject to the following conditions:
wolfSSL 0:70ada960373c 9 *
wolfSSL 0:70ada960373c 10 * The above copyright notice and this permission notice shall be included in all copies or
wolfSSL 0:70ada960373c 11 * substantial portions of the Software.
wolfSSL 0:70ada960373c 12 *
wolfSSL 0:70ada960373c 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wolfSSL 0:70ada960373c 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wolfSSL 0:70ada960373c 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wolfSSL 0:70ada960373c 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wolfSSL 0:70ada960373c 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wolfSSL 0:70ada960373c 18 */
wolfSSL 0:70ada960373c 19
wolfSSL 0:70ada960373c 20
wolfSSL 0:70ada960373c 21 #include "mbed.h"
wolfSSL 0:70ada960373c 22 #include "EthernetInterface.h"
wolfSSL 0:70ada960373c 23 #include "HTTPClient.h"
wolfSSL 0:70ada960373c 24 #include "LM75B.h"
wolfSSL 0:70ada960373c 25
wolfSSL 0:70ada960373c 26 #define XI_FEED_ID 123456789// set Xively Feed ID (numerical, no quoutes)
wolfSSL 0:70ada960373c 27 #define XI_API_KEY "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // set Xively API key (double-quoted string)
wolfSSL 0:70ada960373c 28
wolfSSL 0:70ada960373c 29 #define XI_CHANNEL "Temp" // set Channel name
wolfSSL 0:70ada960373c 30
wolfSSL 0:70ada960373c 31 #define XI_API "https://api.xively.com/v2/feeds"
wolfSSL 0:70ada960373c 32
wolfSSL 0:70ada960373c 33 #define TIMEOUT 500
wolfSSL 0:70ada960373c 34
wolfSSL 0:70ada960373c 35 HTTPClient http;
wolfSSL 0:70ada960373c 36 LM75B tmp(p28, p27);
wolfSSL 0:70ada960373c 37
wolfSSL 0:70ada960373c 38 int xively()
wolfSSL 0:70ada960373c 39 {
wolfSSL 0:70ada960373c 40 int ret ;
wolfSSL 0:70ada960373c 41 int i ;
wolfSSL 0:70ada960373c 42 #define BUFFSIZE 1024
wolfSSL 0:70ada960373c 43 static char buff[BUFFSIZE];
wolfSSL 0:70ada960373c 44 const char put_template[] = "{\
wolfSSL 0:70ada960373c 45 \"version\":\"1.0.0\",\"datastreams\":[\
wolfSSL 0:70ada960373c 46 {\"id\":\"%s\",\"current_value\":\"%f\"}\
wolfSSL 0:70ada960373c 47 ]}\r\n" ;
wolfSSL 0:70ada960373c 48
wolfSSL 0:70ada960373c 49 #define ALLOWANCE 30
wolfSSL 0:70ada960373c 50 char uri[sizeof(XI_API)+10+ALLOWANCE] ;
wolfSSL 0:70ada960373c 51 char put_data[sizeof(put_template)+sizeof(XI_CHANNEL)+ALLOWANCE] ;
wolfSSL 0:70ada960373c 52 char header[sizeof(XI_API_KEY)+ALLOWANCE] ;
wolfSSL 0:70ada960373c 53
wolfSSL 0:70ada960373c 54 sprintf(header, "X-ApiKey: %s\r\n", XI_API_KEY) ;
wolfSSL 0:70ada960373c 55 http.setHeader(header) ;
wolfSSL 0:70ada960373c 56
wolfSSL 0:70ada960373c 57 sprintf(uri, "%s/%d", XI_API, XI_FEED_ID) ;
wolfSSL 0:70ada960373c 58 while(1) {
wolfSSL 0:70ada960373c 59 ret = http.get(uri, buff, BUFFSIZE, TIMEOUT) ;
wolfSSL 0:70ada960373c 60 if (!ret) {
wolfSSL 0:70ada960373c 61 printf("== GET - read %d ==\n", strlen(buff));
wolfSSL 0:70ada960373c 62 printf("Result: %s\n", buff);
wolfSSL 0:70ada960373c 63 break ;
wolfSSL 0:70ada960373c 64 } else {
wolfSSL 0:70ada960373c 65 printf("++ Err = %d - HTTP ret = %d ++\n",
wolfSSL 0:70ada960373c 66 ret, http.getHTTPResponseCode());
wolfSSL 0:70ada960373c 67 }
wolfSSL 0:70ada960373c 68 }
wolfSSL 0:70ada960373c 69
wolfSSL 0:70ada960373c 70 for(i=0; ; i++) {
wolfSSL 0:70ada960373c 71 printf("<<<< %d >>>>\n", i) ;
wolfSSL 0:70ada960373c 72 sprintf(uri, "%s/%d.json", XI_API, XI_FEED_ID) ;
wolfSSL 0:70ada960373c 73 sprintf(put_data, put_template, XI_CHANNEL, tmp.read()) ;
wolfSSL 0:70ada960373c 74 printf("Put Data:\n%s\n", put_data) ;
wolfSSL 0:70ada960373c 75
wolfSSL 0:70ada960373c 76 HTTPText outText(put_data, strlen(put_data));
wolfSSL 0:70ada960373c 77 HTTPText inText(buff, BUFFSIZE);
wolfSSL 0:70ada960373c 78
wolfSSL 0:70ada960373c 79 ret = http.put(uri, outText, &inText, TIMEOUT) ;
wolfSSL 0:70ada960373c 80 if (!ret) {
wolfSSL 0:70ada960373c 81 printf("== PUT - read %d ==\n", strlen(buff));
wolfSSL 0:70ada960373c 82 if(strlen(buff))
wolfSSL 0:70ada960373c 83 printf("Result: %s\n", buff);
wolfSSL 0:70ada960373c 84 } else {
wolfSSL 0:70ada960373c 85 printf("++ Err = %d - HTTP = %d ++\n", ret, http.getHTTPResponseCode());
wolfSSL 0:70ada960373c 86 }
wolfSSL 0:70ada960373c 87 wait(10.0) ;
wolfSSL 0:70ada960373c 88 }
wolfSSL 0:70ada960373c 89 }
wolfSSL 0:70ada960373c 90
wolfSSL 0:70ada960373c 91 main()
wolfSSL 0:70ada960373c 92 {
wolfSSL 0:70ada960373c 93 int ret ;
wolfSSL 0:70ada960373c 94
wolfSSL 0:70ada960373c 95 EthernetInterface eth;
wolfSSL 0:70ada960373c 96 printf("Xively Starting,...\n") ;
wolfSSL 0:70ada960373c 97 ret = eth.init(); //Use DHCP
wolfSSL 0:70ada960373c 98 while(1) {
wolfSSL 0:70ada960373c 99 ret = eth.connect();
wolfSSL 0:70ada960373c 100 if(ret == 0)break ;
wolfSSL 0:70ada960373c 101 }
wolfSSL 0:70ada960373c 102 printf("IP = %s\n", eth.getIPAddress());
wolfSSL 0:70ada960373c 103
wolfSSL 0:70ada960373c 104 xively() ;
wolfSSL 0:70ada960373c 105
wolfSSL 0:70ada960373c 106 eth.disconnect();
wolfSSL 0:70ada960373c 107 }