wolf SSL / Mbed 2 deprecated CyaSSL-Xively

Dependencies:   EthernetInterface HTTPClient mbed-rtos LM75B mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xively-main.cpp Source File

xively-main.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 //#include "LM75B.h"
00026 
00027 #define XI_FEED_ID 123456789// set Xively Feed ID (numerical, no quoutes)
00028 #define XI_API_KEY "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // set Xively API key (double-quoted string)
00029 
00030 #define XI_CHANNEL "Temp" // set Channel name
00031 
00032 #define XI_API     "https://api.xively.com/v2/feeds"
00033 
00034 #define TIMEOUT 500
00035 
00036 HTTPClient http;
00037 //LM75B tmp(p28, p27);
00038 
00039 void xively(void const *av)
00040 {
00041     int ret ;
00042     int i ;
00043 #define BUFFSIZE 1024
00044     static char buff[BUFFSIZE];
00045     const char put_template[] = "{\
00046 \"version\":\"1.0.0\",\"datastreams\":[\
00047 {\"id\":\"%s\",\"current_value\":\"%f\"}\
00048 ]}\r\n" ;
00049 
00050 #define ALLOWANCE 30
00051     char uri[sizeof(XI_API)+10+ALLOWANCE] ;
00052     char put_data[sizeof(put_template)+sizeof(XI_CHANNEL)+ALLOWANCE] ;
00053     char header[sizeof(XI_API_KEY)+ALLOWANCE] ;
00054 
00055     sprintf(header, "X-ApiKey: %s\r\n", XI_API_KEY) ;
00056     http.setHeader(header) ;
00057 
00058     sprintf(uri, "%s/%d", XI_API, XI_FEED_ID) ;
00059     while(1) {
00060         ret = http.get(uri, buff, BUFFSIZE, TIMEOUT) ;
00061         if (!ret) {
00062             printf("== GET - read %d ==\n", strlen(buff));
00063             printf("Result: %s\n", buff);
00064             break ;
00065         } else {
00066             printf("++ Err = %d - HTTP ret = %d ++\n",
00067                    ret, http.getHTTPResponseCode());
00068         }
00069     }
00070  
00071     for(i=0; ; i++) {
00072         printf("<<<< %d >>>>\n", i) ;
00073         sprintf(uri, "%s/%d.json", XI_API, XI_FEED_ID) ;
00074         sprintf(put_data, put_template, XI_CHANNEL, (double)(i%100)/*tmp.read()*/) ;
00075         printf("Put Data:\n%s\n", put_data) ;
00076 
00077         HTTPText outText(put_data, strlen(put_data));
00078         HTTPText inText(buff, BUFFSIZE);
00079 
00080         ret = http.put(uri, outText, &inText, TIMEOUT) ;
00081         if (!ret) {
00082             printf("== PUT - read %d ==\n", strlen(buff));
00083             if(strlen(buff))
00084                 printf("Result: %s\n", buff);
00085         } else {
00086             printf("++ Err = %d - HTTP = %d ++\n", ret, http.getHTTPResponseCode());
00087         }
00088         wait(10.0) ;
00089     }
00090 }
00091 
00092 int main() {      
00093     int ret ;
00094     void *av ;
00095  
00096     EthernetInterface eth;
00097 
00098     printf("Xively Starting,...\n") ;
00099     ret = eth.init(); //Use DHCP
00100     while(1) {
00101         ret = eth.connect();
00102         if(ret == 0)break ;
00103     }
00104     printf("IP = %s\n", eth.getIPAddress());
00105 
00106     //#define BOARD_FRDM_K64F
00107     #ifdef BOARD_FRDM_K64F
00108     #define STACK_SIZE 10000
00109     Thread t( xively, NULL, osPriorityNormal, STACK_SIZE);
00110     #else
00111      xively(av) ;
00112     #endif
00113     while (true) {
00114         Thread::wait(1000);
00115     } 
00116 }