FIAPの使用の例(温度ノード)
Dependencies: EthernetNetIf NTPClient_NetServices TextLCD mbed
Revision 0:e489b051cf96, committed 2012-01-04
- Comitter:
- yueee_yt
- Date:
- Wed Jan 04 12:49:14 2012 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r e489b051cf96 EthernetNetIf.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r e489b051cf96 Fiap/fiap.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Fiap/fiap.cpp Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,94 @@ +#include "fiap.h" +#include "mbed.h" +FIAP::FIAP(string Storage,string PointSetId) { + debug_mode=false; + _fiap_storage=Storage; + _fiap_id_prefix=PointSetId; + + _soap_header ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; + _soap_header += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"; + _soap_header += "<soapenv:Body>"; + _soap_header += "<ns2:dataRQ xmlns:ns2=\"http://soap.fiap.org/\">"; + _soap_header += "<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">"; + _soap_header += "<body>"; + _soap_header += "<pointSet id=\"" + _fiap_id_prefix +"\">"; + + _soap_footer ="</pointSet>"; + _soap_footer+="</body>"; + _soap_footer+="</transport>"; + _soap_footer+="</ns2:dataRQ>"; + _soap_footer+="</soapenv:Body>"; + _soap_footer+="</soapenv:Envelope>"; + _soap_footer+="\r\n\r\n"; +} + +int FIAP::post(struct fiap_element* v, unsigned int esize) { + HTTPClient http; + int i; + char requestBuffer[50]; + string _date_time; + string _soap_text=_soap_header; + for (i=0; i<esize; i++) { + strftime(&requestBuffer[0],50,"%Y-%m-%dT%H:%M:%S.0000000",v[i].t); + _soap_text += "<point id=\""; + _soap_text += _fiap_id_prefix; + _soap_text += v[i].cid; + _soap_text += "\">"; + _soap_text += "<value time=\""; + _soap_text += requestBuffer; + _soap_text += v[i].timezone; + _soap_text += "\">"; + _soap_text += v[i].value; + _soap_text += "</value>"; + _soap_text += "</point>"; + } + _soap_text += _soap_footer; +if(debug_mode){ +printf(_soap_text.c_str()); +printf("<<< Request(end)\n"); +} + http.setRequestHeader("Content-Type","text/xml; charset=UTF-8"); + http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/data\""); + HTTPText InData; + InData.set(_soap_text.c_str()); + HTTPText OutData; + HTTPResult r = http.post(_fiap_storage.c_str(),InData,&OutData); + if (r==HTTP_PROCESSING) { + if (debug_mode)printf("Processing \n"); + return -1; + } + if (r==HTTP_PARSE) { + if (debug_mode) printf("URI Parse error \n"); + return -1; + } + if (r==HTTP_DNS) { + if (debug_mode) printf("Could not resolve name\n"); + return -1; + } + if (r==HTTP_PRTCL) { + if (debug_mode)printf("Protocol error\n"); + return -1; + } + if (r==HTTP_NOTFOUND) { + if (debug_mode)printf("HTTP 404 Error\n"); + return -1; + } + if (r==HTTP_REFUSED) { + if (debug_mode) printf("HTTP 403 Error \n"); + return -1; + } + if (r==HTTP_ERROR) { + if (debug_mode)printf("HTTP xxx error \n"); + return -1; + } + if (r==HTTP_TIMEOUT) { + if (debug_mode)printf("Connection timeout\n"); + return -1; + } + if (r==HTTP_CONN) { + if (debug_mode)printf("Connection error\n"); + return -1; + } + + return 0; +}
diff -r 000000000000 -r e489b051cf96 Fiap/fiap.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Fiap/fiap.h Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,143 @@ +// IEEE1888 / FIAP Uploader Library for Arduino +// +// 2011/09/19 ver.1 H.Inoue & H.Ochiai +// 2011/12/31 Ver.1 for mbed Y.Tauchi + +// --------- FIAP.h --------- +#ifndef MBED_FIAP_H +#define MBED_FIAP_H + +#include "mbed.h" +#include "HTTPClient.h" + +// return code of post method +#define FIAP_UPLOAD_OK 0 // Succeeded +#define FIAP_UPLOAD_CONNFAIL 1 // Connection faild (Socket I/O error) +#define FIAP_UPLOAD_DNSERR 2 // DNS error +#define FIAP_UPLOAD_HTTPERR 3 // HTTP Server error (The response was not "200 OK") +#define FIAP_UPLOAD_FIAPERR 4 // FIAP Server error + +// point element + +/** fiap_element + * + *@param cid Point ID + *@param value Data + *@param t DateTime + *@param timezone Timezone ie JST="+09:00" + */ +struct fiap_element { + const char* cid; // Set PointID + string value; // Set Value + struct tm *t; // Set DateTime + char* timezone; // Set Timezon JST="+09:00" +}; + +/** FIAP Class + * + * for mbed + * + *Example: + *@code + * #include "mbed.h" + * #include "EthernetNetIf.h" + * #include "NTPClient.h" + * #include "fiap.h" + * + * #define ntp_server "ntp server address" + * + * DigitalOut led1(LED1); + * DigitalOut led2(LED2); + * DigitalOut led3(LED3); + * DigitalOut led4(LED4); + * + * EthernetNetIf eth; + * NTPClient ntp; + * FIAP ieee1888("http://192.168.1.5/axis2/services/FIAPStorage","http://test.fiap.org/mbed_hello/"); + * + * char timezone[] = "+09:00"; // JST + * + * struct fiap_element element[]={ + * {"P1",NULL,NULL,timezone}, + * {"P2",NULL,NULL,timezone}, + * }; + * + * int main() { + * led1=led2=led3=led4=0; + * ieee1888.debug_mode=true; + * //EthernetNetIf initialize + * EthernetErr ethErr = eth.setup(); + * if (ethErr) { + * // lcd.locate(0,1); + * // lcd.printf("Error %d in setup.\n", ethErr); + * return -1; + * } + * led1=1; + * //NTPClient initia,lize + * Host server(IpAddr(), 123, ntp_server); + * NTPResult Ntpr=ntp.setTime(server); + * //UTC-->JST +9Hour(32400Sec) + * time_t ctTime; + * ctTime = time(NULL); + * ctTime+=32400; + * set_time(ctTime); + * led2=1; + * + * //post + * int i,j; + * for (j=0; j<10; j++) { + * //data initialize + * time_t seconds = time(NULL); + * for (i=0; i<2; i++) { + * char data[5]; + * int s=j*pow((double)10,i); + * sprintf(data,"%03d",s); + * element[i].value=data; + * element[i].t=localtime(&seconds); + * } + * //do post + * int ret=ieee1888.post(element,2); + * if(ret!=0){ + * while (1) { + * led3=!led3; + * wait(0.5); + * } + * return 0; + * } + * wait(1); + * } + * //finish + * while (1) { + * led4=!led4; + * wait(0.5); + * } + * } + *@endcode + */ +class FIAP { +public: +/** Create fiap object + * @param Storage FIAP Storage Addrees (ie. "http://fiap.org/axis2/services/FIAPStorage") + * @param PointSetId Point Set ID(ie. http://test.fiap.jp/bldg_1/3F/) + */ + FIAP(string Storage,string PointSetId); + +/** post + * @param v data vaule + * @param esize number of data + */ + int post(struct fiap_element* v, unsigned int esize); + +/** debug_mode + * Output XML and Error + */ +bool debug_mode; + +private: + string _fiap_storage; + string _fiap_id_prefix; + string _soap_header; + string _soap_footer; +}; + +#endif
diff -r 000000000000 -r e489b051cf96 HTTPClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
diff -r 000000000000 -r e489b051cf96 NTPClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NTPClient.lib Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/NTPClient/#7c3f1199256a
diff -r 000000000000 -r e489b051cf96 TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 000000000000 -r e489b051cf96 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,92 @@ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "NTPClient.h" +#include "fiap.h" +#include "TextLCD.h" + +#define ntp_server "ntp.cc.yamaguchi-u.ac.jp" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +EthernetNetIf eth; +NTPClient ntp; +FIAP fiap("http://192.168.1.5/axis2/services/FIAPStorage","http://test.fiap.org/mbed_temp/"); + +TextLCD lcd(p24, p26, p27, p28, p29, p30); +AnalogIn temp(p20); +Ticker timer; + +char timezone[] = "+09:00"; // JST +int ret; +time_t next_record; + +struct fiap_element element[]={ + {"TEMP1",NULL,NULL,timezone}, +// {"P2",NULL,NULL,timezone}, +}; + +void send_fiap_storage() { + //data initialize + char buffer[17]; + float temp_data=temp.read()*330.0; + time_t seconds = time(NULL); + strftime(buffer,17," %X ",localtime(&seconds)); + lcd.locate(0,0); + lcd.printf("%s",buffer); + lcd.locate(0,1); + lcd.printf("TEMP=%4.1f Deg",temp_data); + if (seconds>=next_record) { + next_record+=60; + char data[5]; + sprintf(data,"%4.1f",temp_data); + element[0].value=data; + element[0].t=localtime(&seconds); + //do post + ret=fiap.post(element,1); + return ; + } +} + +int main() { + led1=led2=led3=led4=0; + lcd.printf("FIAP TEMP"); + fiap.debug_mode=true; + //EthernetNetIf initialize + EthernetErr ethErr = eth.setup(); + if (ethErr) { + // lcd.locate(0,1); + // lcd.printf("Error %d in setup.\n", ethErr); + return -1; + } + led1=1; + //NTPClient initia,lize + Host server(IpAddr(), 123, ntp_server); + NTPResult Ntpr=ntp.setTime(server); + //UTC-->JST +9Hour(32400Sec) + time_t ctTime; + ctTime = time(NULL); + ctTime+=32400; + set_time(ctTime); + led2=1; + next_record = (time(NULL)/60+1)*60; + + timer.attach(&send_fiap_storage, 1); + + //do + if (ret!=0) { + led4=0; + while (1) { + led3=!led3; + wait(0.5); + } + } else { + led3=0; + while (1) { + led4=!led4; + wait(0.5); + } + } +}
diff -r 000000000000 -r e489b051cf96 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jan 04 12:49:14 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/078e4b97a13e