Usage example, Xively with wolfSSL

Dependencies:   EthernetInterface HTTPClient SDFileSystem mbed-rtos mbed wolfSSL

Fork of SimpleXively by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xively.cpp Source File

xively.cpp

00001 /* main.cpp */
00002 /* Copyright (C) 2014 wolfSSL, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 
00021 #include "mbed.h"
00022 
00023 #include "EthernetInterface.h"
00024 #include "HTTPClient.h"
00025 
00026 #define XI_API     "https://api.xively.com/v2/feeds"
00027 
00028 HTTPClient http;
00029 
00030 void xively_main(char *feed_id, char *api_key, char *ch_name)
00031 {
00032     int ret ;
00033     int i ;
00034 #define BUFFSIZE 1024
00035     static char buff[BUFFSIZE];
00036     const char put_template[] = "{\
00037 \"version\":\"1.0.0\",\"datastreams\":[\
00038 {\"id\":\"%s\",\"current_value\":\"%f\"}\
00039 ]}\r\n" ;
00040 
00041 #define API_KEY_SIZE 50
00042 #define CH_NAME_SIZE 50
00043 #define ALLOWANCE 30
00044 
00045     char uri[sizeof(XI_API)+10+ALLOWANCE] ;
00046     char put_data[sizeof(put_template)+CH_NAME_SIZE+ALLOWANCE] ;
00047     char header[API_KEY_SIZE+ALLOWANCE] ;
00048 
00049     sprintf(header, "X-ApiKey: %s\r\n", api_key) ;
00050     http.setHeader(header) ;
00051     http.dumpReqHeader(true) ;
00052     http.dumpResHeader(true) ;
00053     for(i=0; ; i++) {
00054         printf("<<<< %d >>>>\n", i) ;
00055         sprintf(uri, "%s/%s.json", XI_API, feed_id) ;
00056         sprintf(put_data, put_template, ch_name, (double)(i%100)/*tmp.read()*/) ;
00057         printf("Put Data:\n%s\n", put_data) ;
00058 
00059         HTTPText outText(put_data, strlen(put_data));
00060         HTTPText inText(buff, BUFFSIZE);
00061 
00062         ret = http.put(uri, outText, &inText) ;
00063         if (!ret) {
00064             printf("\n== HTTP PUT: OK\n\n");
00065             if(strlen(buff))
00066                 printf("Result: %s\n", buff);
00067         } else {
00068             printf("++ Err = %d - HTTP = %d ++\n", ret, http.getHTTPResponseCode());
00069         }
00070         wait(10.0) ;
00071     }
00072 }