CyaSSL Xively example for FRDM-k64f board

Dependencies:   HTTPClient EthernetInterface mbed-rtos mbed-src

CyaSSL example of Xively, for FRDM-k64f.

http://mbed.org/users/wolfSSL/code/CyaSSL-Xively/

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

Committer:
wolfSSL
Date:
Sat Jul 12 12:16:05 2014 +0000
Revision:
4:f501fa1f42b2
Parent:
3:9669b4ffe909
CyaSSL under HTTPClient class

Who changed what in which revision?

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