Sample program that can send the recognition data from HVC-P2 to Fujitsu IoT Platform using REST (HTTP)
Dependencies: AsciiFont GR-PEACH_video GraphicsFramework LCD_shield_config R_BSP USBHost_custom easy-connect-gr-peach mbed-http picojson
iot_platform.cpp
00001 #include "mbed.h" 00002 #include "picojson.h" 00003 #include "iot_platform.h" 00004 #include <string> 00005 #include <iostream> 00006 #include <vector> 00007 #include "easy-connect.h" 00008 #include "http_request.h" 00009 #include "NTPClient.h" 00010 #include <time.h> 00011 00012 /* Detect result */ 00013 result_hvcp2_fd_t result_hvcp2_fd[DETECT_MAX]; 00014 result_hvcp2_bd_t result_hvcp2_bd[DETECT_MAX]; 00015 uint32_t result_hvcp2_bd_cnt; 00016 uint32_t result_hvcp2_fd_cnt; 00017 00018 Timer http_resp_time; // response time 00019 uint16_t data[4]; //for color data 00020 00021 #define JST_OFFSET 9 00022 00023 #define ACCESS_CODE "Bearer <ACCESS CODE>" 00024 #error "You have to replace the above <ACCESS CODE> with yours" 00025 00026 std::string put_uri_base("<Base URI>/v1/<Tenant ID>/<Path-to-Resource>.json"); 00027 #error "You have to replace <Base URI>, <Tenant ID> and <Path-to-Resource> with yours" 00028 00029 std::string put_uri; 00030 00031 // json-object for camera 00032 picojson::object o_bd[DETECT_MAX], o_fd[DETECT_MAX], o_fr[DETECT_MAX], o_scr[DETECT_MAX]; 00033 // json-object for sensor 00034 picojson::object o_acc, o_atmo, o_col, o_temp; 00035 00036 picojson::array data_array_acc(6); 00037 picojson::array data_array_atmo(6); 00038 picojson::array data_array_col(6); 00039 picojson::array data_array_temp(6); 00040 00041 // URI for GET request 00042 std::string get_uri("<Base URI>/v1/<Tenant ID>/<Path-to-Resource>/_past.json"); 00043 #error "You have to replace <Base URI>, <Tenant ID> and <Path-to-Resource> with yours" 00044 00045 void dump_response(HttpResponse* res) 00046 { 00047 DEBUG_PRINT("Status: %d - %s\n", res->get_status_code(), 00048 res->get_status_message().c_str()); 00049 00050 DEBUG_PRINT("Headers:\n"); 00051 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00052 DEBUG_PRINT("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), 00053 res->get_headers_values()[ix]->c_str()); 00054 } 00055 DEBUG_PRINT("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), 00056 res->get_body_as_string().c_str()); 00057 } 00058 00059 std::string create_put_uri(std::string uri_base) 00060 { 00061 time_t ctTime; 00062 struct tm *pnow; 00063 char date_and_hour[50]; 00064 std::string uri; 00065 00066 ctTime = time(NULL); 00067 pnow = localtime(&ctTime); 00068 sprintf(date_and_hour, "?$date=%04d%02d%02dT%02d%02d%02d.000%%2B%02d00", 00069 (pnow->tm_year + 1900), (pnow->tm_mon + 1), pnow->tm_mday, 00070 (pnow->tm_hour + JST_OFFSET - pnow->tm_isdst), pnow->tm_min, 00071 pnow->tm_sec, (JST_OFFSET - pnow->tm_isdst)); 00072 00073 uri = uri_base + date_and_hour; 00074 00075 return(uri); 00076 } 00077 00078 int iot_put(NetworkInterface *network, picojson::object o4) 00079 { 00080 00081 #ifdef ENABLED_NTP 00082 put_uri = create_put_uri(put_uri_base); 00083 #else 00084 put_uri = put_uri_base; 00085 #endif // ENABLED_NTP 00086 00087 // PUT request to IoT Platform 00088 HttpRequest* put_req = new HttpRequest(network, HTTP_PUT, put_uri.c_str()); 00089 put_req->set_header("Authorization", ACCESS_CODE); 00090 00091 picojson::value v_all(o4); 00092 00093 std::string body = v_all.serialize(); 00094 00095 HttpResponse* put_res = put_req->send(body.c_str(), body.length()); 00096 00097 if (!put_res) { 00098 DEBUG_PRINT("HttpRequest failed (error code %d)\n", put_req->get_error()); 00099 return 1; 00100 } 00101 00102 delete put_req; 00103 return 0; 00104 } 00105 00106 int iot_get(NetworkInterface *network) 00107 { 00108 // Do GET request to IoT Platform 00109 // By default the body is automatically parsed and stored in a buffer, this is memory heavy. 00110 // To receive chunked response, pass in a callback as last parameter to the constructor. 00111 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, get_uri.c_str()); 00112 get_req->set_header("Authorization", ACCESS_CODE); 00113 00114 HttpResponse* get_res = get_req->send(); 00115 00116 if (!get_res) { 00117 DEBUG_PRINT("HttpRequest failed (error code %d)\n", get_req->get_error()); 00118 return 1; 00119 } 00120 00121 DEBUG_PRINT("\n----- HTTP GET response -----\n"); 00122 00123 delete get_req; 00124 00125 return 0; 00126 } 00127 00128 int send_hvc_info(NetworkInterface *network) 00129 { 00130 /* No face detect */ 00131 if (result_hvcp2_fd_cnt == 0) { 00132 /* Do nothing */ 00133 } else { 00134 for (uint32_t i = 0; i < result_hvcp2_fd_cnt; i++) { 00135 /* picojson-object clear */ 00136 o_fd[i].clear(); 00137 o_fr[i].clear(); 00138 o_scr[i].clear(); 00139 /* Type */ 00140 o_fd[i]["RecordType"] = picojson::value((string)"HVC-P2(face)"); 00141 o_fd[i]["id"] = picojson::value((string) "0001-0005"); 00142 /* Age */ 00143 o_fd[i]["Age"] = picojson::value((double)result_hvcp2_fd[i].age.age); 00144 /* Gender */ 00145 o_fd[i]["Gender"] = picojson::value((double)result_hvcp2_fd[i].gender.gender); 00146 /* FaceRectangle */ 00147 o_fr[i]["Top"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.MinY); 00148 o_fr[i]["Left"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.MinX); 00149 o_fr[i]["Width"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.Width); 00150 o_fr[i]["Height"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.Height); 00151 /* Scores */ 00152 o_scr[i]["Neutral"] = picojson::value((double)result_hvcp2_fd[i].scores.score_neutral); 00153 o_scr[i]["Anger"] = picojson::value((double)result_hvcp2_fd[i].scores.score_anger); 00154 o_scr[i]["Happiness"] = picojson::value((double)result_hvcp2_fd[i].scores.score_happiness); 00155 o_scr[i]["Surprise"] = picojson::value((double)result_hvcp2_fd[i].scores.score_surprise); 00156 o_scr[i]["Sadness"] = picojson::value((double)result_hvcp2_fd[i].scores.score_sadness); 00157 /* insert 2 structures */ 00158 o_fd[i]["FaceRectangle"] = picojson::value(o_fr[i]); 00159 o_fd[i]["Scores"] = picojson::value(o_scr[i]); 00160 } 00161 } 00162 00163 /* No body detect */ 00164 if (result_hvcp2_bd_cnt == 0) { 00165 /* Do nothing */ 00166 } else { 00167 for (uint32_t i = 0; i < result_hvcp2_bd_cnt; i++) { 00168 /* picojson-object clear */ 00169 o_bd[i].clear(); 00170 /* Type */ 00171 o_bd[i]["RecordType"] = picojson::value((string)"HVC-P2(body)"); 00172 o_bd[i]["id"] = picojson::value((string)"0001-0006"); 00173 /* BodyRectangle */ 00174 o_bd[i]["Top"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.MinY); 00175 o_bd[i]["Left"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.MinX); 00176 o_bd[i]["Width"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.Width); 00177 o_bd[i]["Height"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.Height); 00178 } 00179 } 00180 00181 DEBUG_PRINT("Face detect count : %d\n", result_hvcp2_fd_cnt); 00182 DEBUG_PRINT("Body detect count : %d\n", result_hvcp2_bd_cnt); 00183 00184 http_resp_time.reset(); 00185 http_resp_time.start(); 00186 00187 /* send data */ 00188 if (result_hvcp2_fd_cnt == 0) { 00189 /* No need to send data */ 00190 } else { 00191 for (uint32_t i = 0; i < result_hvcp2_fd_cnt; i++) { 00192 iot_put(network, o_fd[i]); 00193 } 00194 } 00195 if (result_hvcp2_bd_cnt == 0) { 00196 /* No need to send data */ 00197 } else { 00198 for (uint32_t i = 0; i < result_hvcp2_bd_cnt; i++) { 00199 iot_put(network, o_bd[i]); 00200 } 00201 } 00202 DEBUG_PRINT("iot_put() Response time:%dms\n", http_resp_time.read_ms()); 00203 return 0; 00204 } 00205 00206 void iot_ready_task(void) 00207 { 00208 00209 /* Initialize http */ 00210 NetworkInterface *network = easy_connect(true); 00211 MBED_ASSERT(network); 00212 00213 #ifdef ENABLED_NTP 00214 // Generate the string indicating the date and hour specified for PUT request 00215 NTPClient ntp; 00216 time_t ctTime; 00217 struct tm *pnow; 00218 NTPResult ret; 00219 00220 ret = ntp.setTime("ntp.nict.jp"); 00221 MBED_ASSERT( ret==0 ); 00222 00223 ctTime = time(NULL); 00224 #endif // Enabled_NTP 00225 00226 while (1) { 00227 00228 semaphore_wait_ret = iot_ready_semaphore.wait(); 00229 MBED_ASSERT(semaphore_wait_ret != -1); 00230 00231 /* send hvc-p2 data */ 00232 http_resp_time.reset(); 00233 http_resp_time.start(); 00234 send_hvc_info(network); 00235 DEBUG_PRINT("send_hvc_info() Response time:%dms\n", http_resp_time.read_ms()); 00236 00237 iot_ready_semaphore.release(); 00238 00239 Thread::wait(WAIT_TIME); 00240 }; 00241 }
Generated on Thu Jul 14 2022 06:34:38 by 1.7.2