Usage example, Xively with wolfSSL

Dependencies:   EthernetInterface HTTPClient SDFileSystem mbed-rtos mbed wolfSSL

Fork of SimpleXively by wolf SSL

Committer:
wolfSSL
Date:
Tue Jul 21 23:16:48 2015 +0000
Revision:
2:74161080023f
Parent:
0:a6ee4ada0d57
Initial public version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:a6ee4ada0d57 1 /* main.cpp */
wolfSSL 0:a6ee4ada0d57 2 /* Copyright (C) 2014 wolfSSL, MIT License
wolfSSL 0:a6ee4ada0d57 3 *
wolfSSL 0:a6ee4ada0d57 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wolfSSL 0:a6ee4ada0d57 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
wolfSSL 0:a6ee4ada0d57 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
wolfSSL 0:a6ee4ada0d57 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
wolfSSL 0:a6ee4ada0d57 8 * furnished to do so, subject to the following conditions:
wolfSSL 0:a6ee4ada0d57 9 *
wolfSSL 0:a6ee4ada0d57 10 * The above copyright notice and this permission notice shall be included in all copies or
wolfSSL 0:a6ee4ada0d57 11 * substantial portions of the Software.
wolfSSL 0:a6ee4ada0d57 12 *
wolfSSL 0:a6ee4ada0d57 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wolfSSL 0:a6ee4ada0d57 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wolfSSL 0:a6ee4ada0d57 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wolfSSL 0:a6ee4ada0d57 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wolfSSL 0:a6ee4ada0d57 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wolfSSL 0:a6ee4ada0d57 18 */
wolfSSL 0:a6ee4ada0d57 19
wolfSSL 0:a6ee4ada0d57 20
wolfSSL 0:a6ee4ada0d57 21 #include "mbed.h"
wolfSSL 0:a6ee4ada0d57 22
wolfSSL 0:a6ee4ada0d57 23 #include "EthernetInterface.h"
wolfSSL 0:a6ee4ada0d57 24 #include "HTTPClient.h"
wolfSSL 0:a6ee4ada0d57 25
wolfSSL 0:a6ee4ada0d57 26 #define XI_API "https://api.xively.com/v2/feeds"
wolfSSL 0:a6ee4ada0d57 27
wolfSSL 0:a6ee4ada0d57 28 HTTPClient http;
wolfSSL 0:a6ee4ada0d57 29
wolfSSL 0:a6ee4ada0d57 30 void xively_main(char *feed_id, char *api_key, char *ch_name)
wolfSSL 0:a6ee4ada0d57 31 {
wolfSSL 0:a6ee4ada0d57 32 int ret ;
wolfSSL 0:a6ee4ada0d57 33 int i ;
wolfSSL 0:a6ee4ada0d57 34 #define BUFFSIZE 1024
wolfSSL 0:a6ee4ada0d57 35 static char buff[BUFFSIZE];
wolfSSL 0:a6ee4ada0d57 36 const char put_template[] = "{\
wolfSSL 0:a6ee4ada0d57 37 \"version\":\"1.0.0\",\"datastreams\":[\
wolfSSL 0:a6ee4ada0d57 38 {\"id\":\"%s\",\"current_value\":\"%f\"}\
wolfSSL 0:a6ee4ada0d57 39 ]}\r\n" ;
wolfSSL 0:a6ee4ada0d57 40
wolfSSL 0:a6ee4ada0d57 41 #define API_KEY_SIZE 50
wolfSSL 0:a6ee4ada0d57 42 #define CH_NAME_SIZE 50
wolfSSL 0:a6ee4ada0d57 43 #define ALLOWANCE 30
wolfSSL 0:a6ee4ada0d57 44
wolfSSL 0:a6ee4ada0d57 45 char uri[sizeof(XI_API)+10+ALLOWANCE] ;
wolfSSL 0:a6ee4ada0d57 46 char put_data[sizeof(put_template)+CH_NAME_SIZE+ALLOWANCE] ;
wolfSSL 0:a6ee4ada0d57 47 char header[API_KEY_SIZE+ALLOWANCE] ;
wolfSSL 0:a6ee4ada0d57 48
wolfSSL 0:a6ee4ada0d57 49 sprintf(header, "X-ApiKey: %s\r\n", api_key) ;
wolfSSL 0:a6ee4ada0d57 50 http.setHeader(header) ;
wolfSSL 2:74161080023f 51 http.dumpReqHeader(true) ;
wolfSSL 2:74161080023f 52 http.dumpResHeader(true) ;
wolfSSL 0:a6ee4ada0d57 53 for(i=0; ; i++) {
wolfSSL 0:a6ee4ada0d57 54 printf("<<<< %d >>>>\n", i) ;
wolfSSL 0:a6ee4ada0d57 55 sprintf(uri, "%s/%s.json", XI_API, feed_id) ;
wolfSSL 0:a6ee4ada0d57 56 sprintf(put_data, put_template, ch_name, (double)(i%100)/*tmp.read()*/) ;
wolfSSL 0:a6ee4ada0d57 57 printf("Put Data:\n%s\n", put_data) ;
wolfSSL 0:a6ee4ada0d57 58
wolfSSL 0:a6ee4ada0d57 59 HTTPText outText(put_data, strlen(put_data));
wolfSSL 0:a6ee4ada0d57 60 HTTPText inText(buff, BUFFSIZE);
wolfSSL 0:a6ee4ada0d57 61
wolfSSL 0:a6ee4ada0d57 62 ret = http.put(uri, outText, &inText) ;
wolfSSL 0:a6ee4ada0d57 63 if (!ret) {
wolfSSL 2:74161080023f 64 printf("\n== HTTP PUT: OK\n\n");
wolfSSL 0:a6ee4ada0d57 65 if(strlen(buff))
wolfSSL 0:a6ee4ada0d57 66 printf("Result: %s\n", buff);
wolfSSL 0:a6ee4ada0d57 67 } else {
wolfSSL 0:a6ee4ada0d57 68 printf("++ Err = %d - HTTP = %d ++\n", ret, http.getHTTPResponseCode());
wolfSSL 0:a6ee4ada0d57 69 }
wolfSSL 0:a6ee4ada0d57 70 wait(10.0) ;
wolfSSL 0:a6ee4ada0d57 71 }
wolfSSL 0:a6ee4ada0d57 72 }