php access with authentication
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
Fork of SimpleXively by
Revision 1:87481e00f41f, committed 2015-07-30
- Comitter:
- thursday1024
- Date:
- Thu Jul 30 07:31:26 2015 +0000
- Parent:
- 0:a6ee4ada0d57
- Commit message:
- php access with authentication
Changed in this revision
--- a/main.cpp Mon Dec 08 12:15:57 2014 +0000 +++ b/main.cpp Thu Jul 30 07:31:26 2015 +0000 @@ -1,90 +1,70 @@ -/* main.cpp - * - * Copyright (C) 2006-2014 wolfSSL Inc. - * - * This file is part of CyaSSL. - * - * CyaSSL is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * CyaSSL is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#include "mbed.h" +// php_acces main.cpp + +#include "mbed.h" #include "EthernetInterface.h" #include "HTTPClient.h" - -#define KEYS "https://192.168.49.57/574d76fcb/keys.txt" +#define BUFF_SIZE 128 -#define BUFFSIZE (50+1) +extern void php_access_main( char *uri, char *ch_name, int* sp ); +char php_uri[]="https://alpha.c.dendai.ac.jp/plot_auth/save_data.php"; -extern void xively_main(char *feed_id, char *api_key, char *ch_name) ; +char ch_str[] ="ch13894"; extern HTTPClient http; EthernetInterface eth; -void thread_main(void const *av) -{ - int ret ; - int i ; - char recvBuff[BUFFSIZE*6] ; - char dummy1[BUFFSIZE], dummy2[BUFFSIZE], dummy3[BUFFSIZE], dummy4[BUFFSIZE] ; - char feed_id[BUFFSIZE] ; - char api_key[BUFFSIZE] ; - char ch_name[BUFFSIZE] ; - - memset(recvBuff, '\0', sizeof(recvBuff)) ; - ret = http.get(KEYS, recvBuff, sizeof(recvBuff)); - if (ret) { - printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - return ; - } +static int speed=0; +void thread_main( void const *av ){ + char ch_name[BUFF_SIZE]; + strcpy( ch_name, ch_str ); + printf( "Channel Name:%s\n", ch_name ); + + php_access_main( php_uri, ch_name, &speed ); // + + while(true) Thread::wait(1000); +} + +// memo +// R-LED -> LED1; G-LED -> LED2; B-LED -> LED3; +// sw3 -> PTA4; sw2 -> PTC6; + +DigitalOut my_LED0(LED1); +DigitalOut my_LED1(LED3); +DigitalOut my_LED2(LED2); +DigitalIn my_sw0(PTA4); +DigitalIn my_sw1(PTC6); - sscanf(recvBuff, "%50s\n%50s\n%50s\n%50s\n%50s\n%50s", feed_id, api_key, - dummy1, dummy2, dummy3, dummy4) ; - printf("ID=%s\nKey=%s\n",feed_id, api_key) ; +int main() { + my_LED0=1; + my_LED1=1; + Timer t; + t.start(); - printf("Channel Name:") ; - for(i=0; i<BUFFSIZE; i++) { - if((ch_name[i] = getchar()) == '\r') { - ch_name[i] = '\0' ; - putchar('\n') ; - break ; - } else putchar(ch_name[i]) ; + int ret = eth.init(); //Use DHCP + printf( "php_test Starting,...\n" ) ; + while(1) { + ret = eth.connect(); + if( ret==0 )break ; // DHCP OK + Thread::wait( 100 ); } - - xively_main(feed_id, api_key, ch_name) ; + t.stop(); + srand(t.read_ms()); + + printf( "IP = %s\n", eth.getIPAddress() ); - while (true) { + #define STACK_SIZE 20000 + Thread thr( thread_main, NULL, osPriorityNormal, STACK_SIZE ); + speed=0; + int i=0; + while(true){ + if(!my_sw0 && speed<=0)speed++; + if(!my_sw1 && speed>=0)speed--; + if(speed>0 ){my_LED0=0;my_LED1=1;my_LED2=1;} + if(speed<0 ){my_LED0=1;my_LED1=0;my_LED2=1;} + if(speed==0){my_LED0=1;my_LED1=1;my_LED2=0;} + speed = (rand()%3)-1; Thread::wait(1000); + i++; } } -int main() { - int ret ; - - ret = eth.init(); //Use DHCP - printf("Xively Starting,...\n") ; - - while(1) { - ret = eth.connect(); - if(ret == 0)break ; - Thread::wait(100); - } - printf("IP = %s\n", eth.getIPAddress()); - - #define STACK_SIZE 20000 - Thread t( thread_main, NULL, osPriorityNormal, STACK_SIZE); - - while(1) - Thread::wait(1000) ; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/php_access_auth.cpp Thu Jul 30 07:31:26 2015 +0000 @@ -0,0 +1,49 @@ +// php_access php_access.cpp + +#include "mbed.h" +#include "EthernetInterface.h" +#include "HTTPClient.h" + +HTTPClient http; + +void php_access_main( char *uri, char *ch_name, int *sp ){ + int ret ; + int i ; +#define BUFFSIZE 1024 +static char AccountSid[]="ox2593is"; +static char AuthToken[] ="uo2yidix"; + static char buff[BUFFSIZE]; + char str[256]; + + //sprintf( header, "ApiKey: %s\r\n", ch_name ) ; + //http.setHeader( header ); + http.basicAuth( AccountSid, AuthToken ); + int j=0; + for( i=0; ; i++ ){ + printf( "<<<< %d speed=%d data=%d >>>>\n", i, *sp, j ) ; + //sprintf( uri, "%s", uri) ; + + //HTTPText outText( put_data, strlen(put_data) ); + HTTPMap params; + HTTPText inText( buff, BUFFSIZE ); + + sprintf( str, "%d", j+=*sp ); + params.put("data", str ); + params.put("ch", ch_name ); + char str_sp[128]; sprintf( str_sp, "%d", *sp ); + params.put("sp", str_sp ); + char str_i[128]; sprintf( str_i, "%d", i ); + params.put("x", str_i ); + + ret = http.post( uri, params, &inText ); // POST + if( !ret ){ + printf( "--- POST - read return data %d ---\n", strlen(buff) ); + if( strlen(buff) )printf("Result: %s", buff); + printf( "--- POST - end %d ---\n\n", strlen(buff) ); + }else{ + printf( "++ Err = %d - HTTP = %d ++\n", + ret, http.getHTTPResponseCode() ); + } + wait(2.0); // sleep 5[s] + } +}
--- a/xively.cpp Mon Dec 08 12:15:57 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ -/* main.cpp */ -/* Copyright (C) 2014 wolfSSL, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - - -#include "mbed.h" - -#include "EthernetInterface.h" -#include "HTTPClient.h" - -#define XI_API "https://api.xively.com/v2/feeds" - -HTTPClient http; - -void xively_main(char *feed_id, char *api_key, char *ch_name) -{ - int ret ; - int i ; -#define BUFFSIZE 1024 - static char buff[BUFFSIZE]; - const char put_template[] = "{\ -\"version\":\"1.0.0\",\"datastreams\":[\ -{\"id\":\"%s\",\"current_value\":\"%f\"}\ -]}\r\n" ; - -#define API_KEY_SIZE 50 -#define CH_NAME_SIZE 50 -#define ALLOWANCE 30 - - char uri[sizeof(XI_API)+10+ALLOWANCE] ; - char put_data[sizeof(put_template)+CH_NAME_SIZE+ALLOWANCE] ; - char header[API_KEY_SIZE+ALLOWANCE] ; - - sprintf(header, "X-ApiKey: %s\r\n", api_key) ; - http.setHeader(header) ; - - for(i=0; ; i++) { - printf("<<<< %d >>>>\n", i) ; - sprintf(uri, "%s/%s.json", XI_API, feed_id) ; - sprintf(put_data, put_template, ch_name, (double)(i%100)/*tmp.read()*/) ; - printf("Put Data:\n%s\n", put_data) ; - - HTTPText outText(put_data, strlen(put_data)); - HTTPText inText(buff, BUFFSIZE); - - ret = http.put(uri, outText, &inText) ; - if (!ret) { - printf("== PUT - read %d ==\n", strlen(buff)); - if(strlen(buff)) - printf("Result: %s\n", buff); - } else { - printf("++ Err = %d - HTTP = %d ++\n", ret, http.getHTTPResponseCode()); - } - wait(10.0) ; - } -}