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
xively-main.cpp@5:7ec4f0a1a465, 2014-07-12 (annotated)
- Committer:
- wolfSSL
- Date:
- Sat Jul 12 12:14:35 2014 +0000
- Revision:
- 5:7ec4f0a1a465
- Parent:
- 1:1bb146136465
CyaSSL under HTTPClient class
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:1bb146136465 | 22 | |
wolfSSL | 0:70ada960373c | 23 | #include "EthernetInterface.h" |
wolfSSL | 0:70ada960373c | 24 | #include "HTTPClient.h" |
wolfSSL | 1:1bb146136465 | 25 | //#include "LM75B.h" |
wolfSSL | 0:70ada960373c | 26 | |
wolfSSL | 0:70ada960373c | 27 | #define XI_FEED_ID 123456789// set Xively Feed ID (numerical, no quoutes) |
wolfSSL | 0:70ada960373c | 28 | #define XI_API_KEY "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // set Xively API key (double-quoted string) |
wolfSSL | 0:70ada960373c | 29 | |
wolfSSL | 0:70ada960373c | 30 | #define XI_CHANNEL "Temp" // set Channel name |
wolfSSL | 0:70ada960373c | 31 | |
wolfSSL | 0:70ada960373c | 32 | #define XI_API "https://api.xively.com/v2/feeds" |
wolfSSL | 0:70ada960373c | 33 | |
wolfSSL | 0:70ada960373c | 34 | #define TIMEOUT 500 |
wolfSSL | 0:70ada960373c | 35 | |
wolfSSL | 0:70ada960373c | 36 | HTTPClient http; |
wolfSSL | 1:1bb146136465 | 37 | //LM75B tmp(p28, p27); |
wolfSSL | 0:70ada960373c | 38 | |
wolfSSL | 1:1bb146136465 | 39 | void xively(void const *av) |
wolfSSL | 0:70ada960373c | 40 | { |
wolfSSL | 0:70ada960373c | 41 | int ret ; |
wolfSSL | 0:70ada960373c | 42 | int i ; |
wolfSSL | 0:70ada960373c | 43 | #define BUFFSIZE 1024 |
wolfSSL | 0:70ada960373c | 44 | static char buff[BUFFSIZE]; |
wolfSSL | 0:70ada960373c | 45 | const char put_template[] = "{\ |
wolfSSL | 0:70ada960373c | 46 | \"version\":\"1.0.0\",\"datastreams\":[\ |
wolfSSL | 0:70ada960373c | 47 | {\"id\":\"%s\",\"current_value\":\"%f\"}\ |
wolfSSL | 0:70ada960373c | 48 | ]}\r\n" ; |
wolfSSL | 0:70ada960373c | 49 | |
wolfSSL | 0:70ada960373c | 50 | #define ALLOWANCE 30 |
wolfSSL | 0:70ada960373c | 51 | char uri[sizeof(XI_API)+10+ALLOWANCE] ; |
wolfSSL | 0:70ada960373c | 52 | char put_data[sizeof(put_template)+sizeof(XI_CHANNEL)+ALLOWANCE] ; |
wolfSSL | 0:70ada960373c | 53 | char header[sizeof(XI_API_KEY)+ALLOWANCE] ; |
wolfSSL | 0:70ada960373c | 54 | |
wolfSSL | 0:70ada960373c | 55 | sprintf(header, "X-ApiKey: %s\r\n", XI_API_KEY) ; |
wolfSSL | 0:70ada960373c | 56 | http.setHeader(header) ; |
wolfSSL | 0:70ada960373c | 57 | |
wolfSSL | 0:70ada960373c | 58 | sprintf(uri, "%s/%d", XI_API, XI_FEED_ID) ; |
wolfSSL | 0:70ada960373c | 59 | while(1) { |
wolfSSL | 0:70ada960373c | 60 | ret = http.get(uri, buff, BUFFSIZE, TIMEOUT) ; |
wolfSSL | 0:70ada960373c | 61 | if (!ret) { |
wolfSSL | 0:70ada960373c | 62 | printf("== GET - read %d ==\n", strlen(buff)); |
wolfSSL | 0:70ada960373c | 63 | printf("Result: %s\n", buff); |
wolfSSL | 0:70ada960373c | 64 | break ; |
wolfSSL | 0:70ada960373c | 65 | } else { |
wolfSSL | 0:70ada960373c | 66 | printf("++ Err = %d - HTTP ret = %d ++\n", |
wolfSSL | 0:70ada960373c | 67 | ret, http.getHTTPResponseCode()); |
wolfSSL | 0:70ada960373c | 68 | } |
wolfSSL | 0:70ada960373c | 69 | } |
wolfSSL | 1:1bb146136465 | 70 | |
wolfSSL | 0:70ada960373c | 71 | for(i=0; ; i++) { |
wolfSSL | 0:70ada960373c | 72 | printf("<<<< %d >>>>\n", i) ; |
wolfSSL | 0:70ada960373c | 73 | sprintf(uri, "%s/%d.json", XI_API, XI_FEED_ID) ; |
wolfSSL | 1:1bb146136465 | 74 | sprintf(put_data, put_template, XI_CHANNEL, (double)(i%100)/*tmp.read()*/) ; |
wolfSSL | 0:70ada960373c | 75 | printf("Put Data:\n%s\n", put_data) ; |
wolfSSL | 0:70ada960373c | 76 | |
wolfSSL | 0:70ada960373c | 77 | HTTPText outText(put_data, strlen(put_data)); |
wolfSSL | 0:70ada960373c | 78 | HTTPText inText(buff, BUFFSIZE); |
wolfSSL | 0:70ada960373c | 79 | |
wolfSSL | 0:70ada960373c | 80 | ret = http.put(uri, outText, &inText, TIMEOUT) ; |
wolfSSL | 0:70ada960373c | 81 | if (!ret) { |
wolfSSL | 0:70ada960373c | 82 | printf("== PUT - read %d ==\n", strlen(buff)); |
wolfSSL | 0:70ada960373c | 83 | if(strlen(buff)) |
wolfSSL | 0:70ada960373c | 84 | printf("Result: %s\n", buff); |
wolfSSL | 0:70ada960373c | 85 | } else { |
wolfSSL | 0:70ada960373c | 86 | printf("++ Err = %d - HTTP = %d ++\n", ret, http.getHTTPResponseCode()); |
wolfSSL | 0:70ada960373c | 87 | } |
wolfSSL | 0:70ada960373c | 88 | wait(10.0) ; |
wolfSSL | 0:70ada960373c | 89 | } |
wolfSSL | 0:70ada960373c | 90 | } |
wolfSSL | 0:70ada960373c | 91 | |
wolfSSL | 1:1bb146136465 | 92 | int main() { |
wolfSSL | 0:70ada960373c | 93 | int ret ; |
wolfSSL | 1:1bb146136465 | 94 | void *av ; |
wolfSSL | 1:1bb146136465 | 95 | |
wolfSSL | 1:1bb146136465 | 96 | EthernetInterface eth; |
wolfSSL | 0:70ada960373c | 97 | |
wolfSSL | 0:70ada960373c | 98 | printf("Xively Starting,...\n") ; |
wolfSSL | 0:70ada960373c | 99 | ret = eth.init(); //Use DHCP |
wolfSSL | 0:70ada960373c | 100 | while(1) { |
wolfSSL | 0:70ada960373c | 101 | ret = eth.connect(); |
wolfSSL | 0:70ada960373c | 102 | if(ret == 0)break ; |
wolfSSL | 0:70ada960373c | 103 | } |
wolfSSL | 0:70ada960373c | 104 | printf("IP = %s\n", eth.getIPAddress()); |
wolfSSL | 0:70ada960373c | 105 | |
wolfSSL | 1:1bb146136465 | 106 | //#define BOARD_FRDM_K64F |
wolfSSL | 1:1bb146136465 | 107 | #ifdef BOARD_FRDM_K64F |
wolfSSL | 1:1bb146136465 | 108 | #define STACK_SIZE 10000 |
wolfSSL | 1:1bb146136465 | 109 | Thread t( xively, NULL, osPriorityNormal, STACK_SIZE); |
wolfSSL | 1:1bb146136465 | 110 | #else |
wolfSSL | 1:1bb146136465 | 111 | xively(av) ; |
wolfSSL | 1:1bb146136465 | 112 | #endif |
wolfSSL | 1:1bb146136465 | 113 | while (true) { |
wolfSSL | 1:1bb146136465 | 114 | Thread::wait(1000); |
wolfSSL | 1:1bb146136465 | 115 | } |
wolfSSL | 0:70ada960373c | 116 | } |