Sample program for communicating with Fujitsuu IoT Platform using HTTP
Dependencies: AsciiFont GR-PEACH_video GraphicsFramework LCD_shield_config R_BSP USBHost_custom easy-connect-gr-peach mbed-http picojson BM1383GLV KX022 rohm-sensor-hal rohm-bh1745
Overview
This sample program shows how to send the cognitive data and sensing data gathered by Omron HVC-P2 and Rohm Sensor Shield respectively to IoT Platform managed by FUJITSU ( http://jp.fujitsu.com/solutions/cloud/k5/function/paas/iot-platform/ ).
Required Hardware
- GR-PEACH ( https://developer.mbed.org/platforms/Renesas-GR-PEACH/ )
- LCD Shield ( https://developer.mbed.org/teams/Renesas/Wiki/LCD-shield )
- HVC-P2 ( Human Vision Components B5T-007001 ) ( https://plus-sensin.omron.com/product/B5T-007001/ )
- SensorShield-EVK-001 ( Rohm Sensor Evaluation Kit ) ( http://www.rohm.com/web/global/sensor-shield-support )
Application Setup
- Configure the connection type. For details, please refer to the following link:
https://developer.mbed.org/teams/Renesas/code/GR-PEACH_IoT_Platform_HTTP_sample/wiki/Connection-Type - Configure Ethernet settings. For details, please refer to the following link:
https://developer.mbed.org/teams/Renesas/code/GR-PEACH_IoT_Platform_HTTP_sample/wiki/Ethernet-settings - Set up the Access Code of resource where the gathered data would be stored. For details on Access Code, please refer to the following links:
https://iot-docs.jp-east-1.paas.cloud.global.fujitsu.com/en/manual/userguide_en.pdf
https://iot-docs.jp-east-1.paas.cloud.global.fujitsu.com/en/manual/apireference_en.pdf
https://iot-docs.jp-east-1.paas.cloud.global.fujitsu.com/en/manual/portalmanual_en.pdf
- Set up URI for the resource where the gathered data would be stored. For details, please refer to the following link:
https://iot-docs.jp-east-1.paas.cloud.global.fujitsu.com/en/manual/userguide_en.pdf
https://iot-docs.jp-east-1.paas.cloud.global.fujitsu.com/en/manual/apireference_en.pdf
Building Example
- Import this sample program onto mbed Compiler
- Configure the program in accordance with the description of Application Setup above
- Compile the sample program
- Plug the Ethernet cable into GR-PEACH if you would like Ethernet mode
- Plug micro-USB cable into the OpenSDA port which lies on the next to the RESET button
- Copy the binary previously downloaded to your PC to GR-PEACH in order to flash this program. When the copy is successfully completed, the drive named MBED should be re-mounted automatically
- Press the RESET button on the board to run the sample application
Data Format sent to IoT Platform
In this sample program, the cognitive data and sensing data are serialized into the following JSON format using picojson (https://developer.mbed.org/users/mimil/code/picojson/):
- Face detection data
{ "RecordType": "HVC-P2(face)", "id": "<GR-PEACH ID>-<Sensor ID>", "Age": xxx, "FaceRectangle": { "Height": xxx, "Left": xxx, "Top": xxx, "Width": xxx }, "Gender": xxx, "Scores": { "Anger": xxx, "Happiness": xxx, "Neutral": xxx, "Sadness": xxx, "Surprise": xxx } }
- Body detection data
{ "RecodeType": "HVC-P2(body)", "id": "<GR-PEACH ID>-<Sensor ID>", "BodyRectangle": { "Height": xxx, "Left": xxx, "Top": xxx, "Width": xxx } }
- Accelerometer data
{ "RecodeType": "Accelerometer", "id": "<GR-PEACH ID>-<Sensor ID>", "data": [ acceleratoin(x-direction), acceleration(y-direction), acceleration(z-direction), null, null, null ] }
Note that data[0], data[1] and data[2] are filled with the acceleration data in x, y and z direction respectively, and the remaining elements are filled with null.
- Atmosphere data
{ "RecodeType": "Atmosphere", "id": "<GR-PEACH ID>-<Sensor ID>", "data": [ atmosphere data, null, null, null, null, null ] }
Note that data[0] is filled with atmosphere data, and the remaining elements are filled with null.
- Color sensor data
{ "RecodeType": "Color", "id": "<GR-PEACH ID>-<Sensor ID>", "data": [ Red, Green, Blue, Alpha, null, null] }
Note that data[0], data[1], data[2] and data[3] are filled with Red, Green, Blue and Alpha elements of color respectively, and the remaining elements are filled with null.
- Temperature data
{ "RecodeType": "Temperature", "id": "<GR-PEACH ID>-<Sensor ID>", "data": [ Temperature, null, null, null, null, null ] }
Note that data[0] is filled with temperature data, the remaining elements are filled with null.
- Geomagnetism
{ "RecodeType": "Geomagnetism", "id": "<GR-PEACH ID>-<Sensor ID>", "data": [ geomagnetism(x-direction), geomagnetism(y-direction), geomagnetism(z-direction), null, null, null] }
Note that data[0], data[1] and data[2] are filled with the geomagnetism data in x, y and z direction respectively, and the remaining elements are filled with null.
IoT_Platform/iot_platform.cpp@3:0eaa47c59bb3, 2017-09-07 (annotated)
- Committer:
- Osamu Nakamura
- Date:
- Thu Sep 07 14:48:21 2017 +0900
- Revision:
- 3:0eaa47c59bb3
- Parent:
- 2:a191fff7103b
- Child:
- 5:2a16f2a5bd02
Rename ACCESS_KEY macro with ACCESS_CODE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Osamu Nakamura |
0:8373b6833bde | 1 | #include "mbed.h" |
Osamu Nakamura |
0:8373b6833bde | 2 | #include "picojson.h" |
Osamu Nakamura |
0:8373b6833bde | 3 | #include "select-demo.h" |
Osamu Nakamura |
0:8373b6833bde | 4 | #include "iot_platform.h" |
Osamu Nakamura |
0:8373b6833bde | 5 | #include <string> |
Osamu Nakamura |
0:8373b6833bde | 6 | #include <iostream> |
Osamu Nakamura |
0:8373b6833bde | 7 | #include <vector> |
Osamu Nakamura |
0:8373b6833bde | 8 | #include "BM1383GLV.h" |
Osamu Nakamura |
0:8373b6833bde | 9 | #include "KX022.h" |
Osamu Nakamura |
0:8373b6833bde | 10 | #include "BH1745NUC.h" |
Osamu Nakamura |
0:8373b6833bde | 11 | #include "bh1745_driver.h" |
Osamu Nakamura |
0:8373b6833bde | 12 | #include "I2CCommon.h" |
Osamu Nakamura |
0:8373b6833bde | 13 | |
Osamu Nakamura |
0:8373b6833bde | 14 | #if DEMO == DEMO_HTTP |
Osamu Nakamura |
0:8373b6833bde | 15 | #include "easy-connect.h" |
Osamu Nakamura |
0:8373b6833bde | 16 | #include "http_request.h" |
Osamu Nakamura |
0:8373b6833bde | 17 | #include "NTPClient.h" |
Osamu Nakamura |
0:8373b6833bde | 18 | #include <time.h> |
Osamu Nakamura |
0:8373b6833bde | 19 | |
Osamu Nakamura |
0:8373b6833bde | 20 | /* Detect result */ |
Osamu Nakamura |
0:8373b6833bde | 21 | result_hvcp2_fd_t result_hvcp2_fd[DETECT_MAX]; |
Osamu Nakamura |
0:8373b6833bde | 22 | result_hvcp2_bd_t result_hvcp2_bd[DETECT_MAX]; |
Osamu Nakamura |
0:8373b6833bde | 23 | uint32_t result_hvcp2_bd_cnt; |
Osamu Nakamura |
0:8373b6833bde | 24 | uint32_t result_hvcp2_fd_cnt; |
Osamu Nakamura |
0:8373b6833bde | 25 | |
Osamu Nakamura |
0:8373b6833bde | 26 | Timer http_resp_time; // response time |
Osamu Nakamura |
0:8373b6833bde | 27 | uint16_t data[4]; //for color data |
Osamu Nakamura |
0:8373b6833bde | 28 | |
Osamu Nakamura |
0:8373b6833bde | 29 | BM1383GLV sensor_BM(I2C_SDA, I2C_SCL); // Atmosphere |
Osamu Nakamura |
0:8373b6833bde | 30 | KX022 sensor_acc(I2C_SDA, I2C_SCL); // Accelerometer |
Osamu Nakamura |
0:8373b6833bde | 31 | |
Osamu Nakamura |
0:8373b6833bde | 32 | #define JST_OFFSET 9 |
Osamu Nakamura |
0:8373b6833bde | 33 | |
Osamu Nakamura |
2:a191fff7103b | 34 | #define ACCESS_CODE <Access CODE> |
Osamu Nakamura |
2:a191fff7103b | 35 | #error "You need to replace <Access CODE for your resource> with yours" |
Osamu Nakamura |
0:8373b6833bde | 36 | |
Osamu Nakamura |
0:8373b6833bde | 37 | std::string put_uri_base("<Base URI>/v1/<Tenant ID>/<Path-to-Resource>.json?"); |
Osamu Nakamura |
0:8373b6833bde | 38 | #error "You need to replace <Base URI>, <Tenant ID> and <Path-to-Resource> with yours" |
Osamu Nakamura |
0:8373b6833bde | 39 | |
Osamu Nakamura |
0:8373b6833bde | 40 | std::string put_uri; |
Osamu Nakamura |
0:8373b6833bde | 41 | |
Osamu Nakamura |
0:8373b6833bde | 42 | // json-object for camera |
Osamu Nakamura |
0:8373b6833bde | 43 | picojson::object o_bd[DETECT_MAX], o_fd[DETECT_MAX], o_fr[DETECT_MAX], o_scr[DETECT_MAX]; |
Osamu Nakamura |
0:8373b6833bde | 44 | // json-object for sensor |
Osamu Nakamura |
0:8373b6833bde | 45 | picojson::object o_acc, o_atmo, o_col, o_temp; |
Osamu Nakamura |
0:8373b6833bde | 46 | |
Osamu Nakamura |
0:8373b6833bde | 47 | picojson::array data_array_acc(6); |
Osamu Nakamura |
0:8373b6833bde | 48 | picojson::array data_array_atmo(6); |
Osamu Nakamura |
0:8373b6833bde | 49 | picojson::array data_array_col(6); |
Osamu Nakamura |
0:8373b6833bde | 50 | picojson::array data_array_temp(6); |
Osamu Nakamura |
0:8373b6833bde | 51 | |
Osamu Nakamura |
0:8373b6833bde | 52 | // URI for GET request |
Osamu Nakamura |
0:8373b6833bde | 53 | std::string get_uri("<Base URI>/v1/<Tenant ID>/<Path-to-Resource>/_past.json"); |
Osamu Nakamura |
0:8373b6833bde | 54 | #error "You need to replace <Base URI>, <Tenant ID> and <Path-to-Resource> with yours" |
Osamu Nakamura |
0:8373b6833bde | 55 | |
Osamu Nakamura |
0:8373b6833bde | 56 | void dump_response(HttpResponse* res) |
Osamu Nakamura |
0:8373b6833bde | 57 | { |
Osamu Nakamura |
0:8373b6833bde | 58 | DEBUG_PRINT("Status: %d - %s\n", res->get_status_code(), |
Osamu Nakamura |
0:8373b6833bde | 59 | res->get_status_message().c_str()); |
Osamu Nakamura |
0:8373b6833bde | 60 | |
Osamu Nakamura |
0:8373b6833bde | 61 | DEBUG_PRINT("Headers:\n"); |
Osamu Nakamura |
0:8373b6833bde | 62 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
Osamu Nakamura |
0:8373b6833bde | 63 | DEBUG_PRINT("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), |
Osamu Nakamura |
0:8373b6833bde | 64 | res->get_headers_values()[ix]->c_str()); |
Osamu Nakamura |
0:8373b6833bde | 65 | } |
Osamu Nakamura |
0:8373b6833bde | 66 | DEBUG_PRINT("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), |
Osamu Nakamura |
0:8373b6833bde | 67 | res->get_body_as_string().c_str()); |
Osamu Nakamura |
0:8373b6833bde | 68 | } |
Osamu Nakamura |
0:8373b6833bde | 69 | |
Osamu Nakamura |
2:a191fff7103b | 70 | std::string create_put_uri(std::string uri_base) |
Osamu Nakamura |
2:a191fff7103b | 71 | { |
Osamu Nakamura |
2:a191fff7103b | 72 | time_t ctTime; |
Osamu Nakamura |
2:a191fff7103b | 73 | struct tm *pnow; |
Osamu Nakamura |
2:a191fff7103b | 74 | char date_and_hour[50]; |
Osamu Nakamura |
2:a191fff7103b | 75 | std::string uri; |
Osamu Nakamura |
2:a191fff7103b | 76 | |
Osamu Nakamura |
2:a191fff7103b | 77 | ctTime = time(NULL); |
Osamu Nakamura |
2:a191fff7103b | 78 | pnow = localtime(&ctTime); |
Osamu Nakamura |
2:a191fff7103b | 79 | sprintf(date_and_hour, "?$date=%04d%02d%02dT%02d%02d%02d.000%%2B%02d00", |
Osamu Nakamura |
2:a191fff7103b | 80 | (pnow->tm_year + 1900), (pnow->tm_mon + 1), pnow->tm_mday, |
Osamu Nakamura |
2:a191fff7103b | 81 | (pnow->tm_hour + JST_OFFSET - pnow->tm_isdst), pnow->tm_min, |
Osamu Nakamura |
2:a191fff7103b | 82 | pnow->tm_sec, (JST_OFFSET - pnow->tm_isdst)); |
Osamu Nakamura |
2:a191fff7103b | 83 | |
Osamu Nakamura |
2:a191fff7103b | 84 | uri = uri_base + date_and_hour; |
Osamu Nakamura |
2:a191fff7103b | 85 | |
Osamu Nakamura |
2:a191fff7103b | 86 | return(uri); |
Osamu Nakamura |
2:a191fff7103b | 87 | } |
Osamu Nakamura |
2:a191fff7103b | 88 | |
Osamu Nakamura |
0:8373b6833bde | 89 | int iot_put(NetworkInterface *network, picojson::object o4) |
Osamu Nakamura |
0:8373b6833bde | 90 | { |
Osamu Nakamura |
0:8373b6833bde | 91 | |
Osamu Nakamura |
2:a191fff7103b | 92 | #ifdef ENABLED_NTP |
Osamu Nakamura |
2:a191fff7103b | 93 | put_uri = create_put_uri(put_uri_base); |
Osamu Nakamura |
2:a191fff7103b | 94 | #else |
Osamu Nakamura |
2:a191fff7103b | 95 | put_uri = put_uri_base; |
Osamu Nakamura |
2:a191fff7103b | 96 | #endif // ENABLED_NTP |
Osamu Nakamura |
2:a191fff7103b | 97 | |
Osamu Nakamura |
0:8373b6833bde | 98 | // PUT request to IoT Platform |
Osamu Nakamura |
0:8373b6833bde | 99 | HttpRequest* put_req = new HttpRequest(network, HTTP_PUT, put_uri.c_str()); |
Osamu Nakamura |
3:0eaa47c59bb3 | 100 | put_req->set_header("Authorization", ACCESS_CODE); |
Osamu Nakamura |
0:8373b6833bde | 101 | |
Osamu Nakamura |
0:8373b6833bde | 102 | picojson::value v_all(o4); |
Osamu Nakamura |
0:8373b6833bde | 103 | |
Osamu Nakamura |
0:8373b6833bde | 104 | std::string body = v_all.serialize(); |
Osamu Nakamura |
0:8373b6833bde | 105 | |
Osamu Nakamura |
0:8373b6833bde | 106 | HttpResponse* put_res = put_req->send(body.c_str(), body.length()); |
Osamu Nakamura |
0:8373b6833bde | 107 | |
Osamu Nakamura |
0:8373b6833bde | 108 | if (!put_res) { |
Osamu Nakamura |
0:8373b6833bde | 109 | DEBUG_PRINT("HttpRequest failed (error code %d)\n", put_req->get_error()); |
Osamu Nakamura |
0:8373b6833bde | 110 | return 1; |
Osamu Nakamura |
0:8373b6833bde | 111 | } |
Osamu Nakamura |
0:8373b6833bde | 112 | |
Osamu Nakamura |
0:8373b6833bde | 113 | delete put_req; |
Osamu Nakamura |
0:8373b6833bde | 114 | return 0; |
Osamu Nakamura |
0:8373b6833bde | 115 | } |
Osamu Nakamura |
0:8373b6833bde | 116 | |
Osamu Nakamura |
0:8373b6833bde | 117 | int iot_get(NetworkInterface *network) |
Osamu Nakamura |
0:8373b6833bde | 118 | { |
Osamu Nakamura |
0:8373b6833bde | 119 | // Do GET request to IoT Platform |
Osamu Nakamura |
0:8373b6833bde | 120 | // By default the body is automatically parsed and stored in a buffer, this is memory heavy. |
Osamu Nakamura |
0:8373b6833bde | 121 | // To receive chunked response, pass in a callback as last parameter to the constructor. |
Osamu Nakamura |
0:8373b6833bde | 122 | HttpRequest* get_req = new HttpRequest(network, HTTP_GET, get_uri.c_str()); |
Osamu Nakamura |
3:0eaa47c59bb3 | 123 | get_req->set_header("Authorization", ACCESS_CODE); |
Osamu Nakamura |
0:8373b6833bde | 124 | |
Osamu Nakamura |
0:8373b6833bde | 125 | HttpResponse* get_res = get_req->send(); |
Osamu Nakamura |
0:8373b6833bde | 126 | |
Osamu Nakamura |
0:8373b6833bde | 127 | if (!get_res) { |
Osamu Nakamura |
0:8373b6833bde | 128 | DEBUG_PRINT("HttpRequest failed (error code %d)\n", get_req->get_error()); |
Osamu Nakamura |
0:8373b6833bde | 129 | return 1; |
Osamu Nakamura |
0:8373b6833bde | 130 | } |
Osamu Nakamura |
0:8373b6833bde | 131 | |
Osamu Nakamura |
0:8373b6833bde | 132 | DEBUG_PRINT("\n----- HTTP GET response -----\n"); |
Osamu Nakamura |
0:8373b6833bde | 133 | |
Osamu Nakamura |
0:8373b6833bde | 134 | delete get_req; |
Osamu Nakamura |
0:8373b6833bde | 135 | |
Osamu Nakamura |
0:8373b6833bde | 136 | return 0; |
Osamu Nakamura |
0:8373b6833bde | 137 | } |
Osamu Nakamura |
0:8373b6833bde | 138 | #endif // DEMO == DEMO_HTTP |
Osamu Nakamura |
0:8373b6833bde | 139 | |
Osamu Nakamura |
0:8373b6833bde | 140 | void read_color(void) |
Osamu Nakamura |
0:8373b6833bde | 141 | { |
Osamu Nakamura |
0:8373b6833bde | 142 | bool error; |
Osamu Nakamura |
0:8373b6833bde | 143 | I2CCommonBegin(); |
Osamu Nakamura |
0:8373b6833bde | 144 | bh1745_wait_until_found(); |
Osamu Nakamura |
0:8373b6833bde | 145 | bh1745_initial_setup(); |
Osamu Nakamura |
0:8373b6833bde | 146 | error = bh1745_read_data(&data[0]); |
Osamu Nakamura |
2:a191fff7103b | 147 | MBED_ASSERT(error==0); |
Osamu Nakamura |
0:8373b6833bde | 148 | } |
Osamu Nakamura |
0:8373b6833bde | 149 | |
Osamu Nakamura |
0:8373b6833bde | 150 | int send_sensor_info(NetworkInterface *network) |
Osamu Nakamura |
0:8373b6833bde | 151 | { |
Osamu Nakamura |
0:8373b6833bde | 152 | |
Osamu Nakamura |
0:8373b6833bde | 153 | /* Type */ |
Osamu Nakamura |
0:8373b6833bde | 154 | o_acc["RecordType"] = picojson::value((string)"Accelerometer"); |
Osamu Nakamura |
0:8373b6833bde | 155 | o_atmo["RecordType"] = picojson::value((string)"Atmosphere"); |
Osamu Nakamura |
0:8373b6833bde | 156 | o_col["RecordType"] = picojson::value((string)"Color"); |
Osamu Nakamura |
0:8373b6833bde | 157 | o_temp["RecordType"] = picojson::value((string)"Temperature"); |
Osamu Nakamura |
0:8373b6833bde | 158 | |
Osamu Nakamura |
0:8373b6833bde | 159 | /* ID */ |
Osamu Nakamura |
0:8373b6833bde | 160 | o_acc["id"] = picojson::value((string)"0001-0001"); |
Osamu Nakamura |
0:8373b6833bde | 161 | o_atmo["id"] = picojson::value((string)"0001-0002"); |
Osamu Nakamura |
0:8373b6833bde | 162 | o_col["id"] = picojson::value((string)"0001-0003"); |
Osamu Nakamura |
0:8373b6833bde | 163 | o_temp["id"] = picojson::value((string)"0001-0004"); |
Osamu Nakamura |
0:8373b6833bde | 164 | |
Osamu Nakamura |
0:8373b6833bde | 165 | /* Accelerometer data array */ |
Osamu Nakamura |
0:8373b6833bde | 166 | data_array_acc[0] = picojson::value((double)sensor_acc.getAccX()); |
Osamu Nakamura |
0:8373b6833bde | 167 | data_array_acc[1] = picojson::value((double)sensor_acc.getAccY()); |
Osamu Nakamura |
0:8373b6833bde | 168 | data_array_acc[2] = picojson::value((double)sensor_acc.getAccZ()); |
Osamu Nakamura |
0:8373b6833bde | 169 | |
Osamu Nakamura |
0:8373b6833bde | 170 | /* Atmosphere data array */ |
Osamu Nakamura |
0:8373b6833bde | 171 | data_array_atmo[0] = picojson::value((double)sensor_BM.getPressure()); |
Osamu Nakamura |
0:8373b6833bde | 172 | |
Osamu Nakamura |
0:8373b6833bde | 173 | /* Color data array */ |
Osamu Nakamura |
0:8373b6833bde | 174 | read_color(); |
Osamu Nakamura |
0:8373b6833bde | 175 | for(int i = 0; i < 4; i++) { |
Osamu Nakamura |
0:8373b6833bde | 176 | data_array_col[i] = picojson::value(picojson::value((double)data[i])); |
Osamu Nakamura |
0:8373b6833bde | 177 | } |
Osamu Nakamura |
0:8373b6833bde | 178 | |
Osamu Nakamura |
0:8373b6833bde | 179 | /* Temperature data array */ |
Osamu Nakamura |
0:8373b6833bde | 180 | data_array_temp[0] = picojson::value((double)sensor_BM.getTemperature()); |
Osamu Nakamura |
0:8373b6833bde | 181 | |
Osamu Nakamura |
0:8373b6833bde | 182 | /* Combine data array with object. */ |
Osamu Nakamura |
0:8373b6833bde | 183 | o_acc["data"] = picojson::value(data_array_acc); |
Osamu Nakamura |
0:8373b6833bde | 184 | o_atmo["data"] = picojson::value(data_array_atmo); |
Osamu Nakamura |
0:8373b6833bde | 185 | o_col["data"] = picojson::value(data_array_col); |
Osamu Nakamura |
0:8373b6833bde | 186 | o_temp["data"] = picojson::value(data_array_temp); |
Osamu Nakamura |
0:8373b6833bde | 187 | |
Osamu Nakamura |
0:8373b6833bde | 188 | http_resp_time.reset(); |
Osamu Nakamura |
0:8373b6833bde | 189 | http_resp_time.start(); |
Osamu Nakamura |
0:8373b6833bde | 190 | |
Osamu Nakamura |
0:8373b6833bde | 191 | /* send data */ |
Osamu Nakamura |
0:8373b6833bde | 192 | iot_put(network, o_acc); |
Osamu Nakamura |
0:8373b6833bde | 193 | iot_put(network, o_atmo); |
Osamu Nakamura |
0:8373b6833bde | 194 | iot_put(network, o_col); |
Osamu Nakamura |
0:8373b6833bde | 195 | iot_put(network, o_temp); |
Osamu Nakamura |
0:8373b6833bde | 196 | |
Osamu Nakamura |
0:8373b6833bde | 197 | |
Osamu Nakamura |
0:8373b6833bde | 198 | DEBUG_PRINT("iot_put() Response time:%dms\n", http_resp_time.read_ms()); |
Osamu Nakamura |
0:8373b6833bde | 199 | |
Osamu Nakamura |
0:8373b6833bde | 200 | return 0; |
Osamu Nakamura |
0:8373b6833bde | 201 | } |
Osamu Nakamura |
0:8373b6833bde | 202 | |
Osamu Nakamura |
0:8373b6833bde | 203 | int send_hvc_info(NetworkInterface *network) |
Osamu Nakamura |
0:8373b6833bde | 204 | { |
Osamu Nakamura |
0:8373b6833bde | 205 | /* No face detect */ |
Osamu Nakamura |
0:8373b6833bde | 206 | if (result_hvcp2_fd_cnt == 0) { |
Osamu Nakamura |
2:a191fff7103b | 207 | /* Do nothing */ |
Osamu Nakamura |
0:8373b6833bde | 208 | } else { |
Osamu Nakamura |
0:8373b6833bde | 209 | for (uint32_t i = 0; i < result_hvcp2_fd_cnt; i++) { |
Osamu Nakamura |
0:8373b6833bde | 210 | /* picojson-object clear */ |
Osamu Nakamura |
0:8373b6833bde | 211 | o_fd[i].clear(); |
Osamu Nakamura |
0:8373b6833bde | 212 | o_fr[i].clear(); |
Osamu Nakamura |
0:8373b6833bde | 213 | o_scr[i].clear(); |
Osamu Nakamura |
0:8373b6833bde | 214 | /* Type */ |
Osamu Nakamura |
0:8373b6833bde | 215 | o_fd[i]["RecordType"] = picojson::value((string)"HVC-P2(face)"); |
Osamu Nakamura |
0:8373b6833bde | 216 | o_fd[i]["id"] = picojson::value((string) "0001-0005"); |
Osamu Nakamura |
0:8373b6833bde | 217 | /* Age */ |
Osamu Nakamura |
0:8373b6833bde | 218 | o_fd[i]["Age"] = picojson::value((double)result_hvcp2_fd[i].age.age); |
Osamu Nakamura |
0:8373b6833bde | 219 | /* Gender */ |
Osamu Nakamura |
0:8373b6833bde | 220 | o_fd[i]["Gender"] = picojson::value((double)result_hvcp2_fd[i].gender.gender); |
Osamu Nakamura |
0:8373b6833bde | 221 | /* FaceRectangle */ |
Osamu Nakamura |
0:8373b6833bde | 222 | o_fr[i]["Top"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.MinY); |
Osamu Nakamura |
0:8373b6833bde | 223 | o_fr[i]["Left"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.MinX); |
Osamu Nakamura |
0:8373b6833bde | 224 | o_fr[i]["Width"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.Width); |
Osamu Nakamura |
0:8373b6833bde | 225 | o_fr[i]["Height"] = picojson::value((double)result_hvcp2_fd[i].face_rectangle.Height); |
Osamu Nakamura |
0:8373b6833bde | 226 | /* Scores */ |
Osamu Nakamura |
0:8373b6833bde | 227 | o_scr[i]["Neutral"] = picojson::value((double)result_hvcp2_fd[i].scores.score_neutral); |
Osamu Nakamura |
0:8373b6833bde | 228 | o_scr[i]["Anger"] = picojson::value((double)result_hvcp2_fd[i].scores.score_anger); |
Osamu Nakamura |
0:8373b6833bde | 229 | o_scr[i]["Happiness"] = picojson::value((double)result_hvcp2_fd[i].scores.score_happiness); |
Osamu Nakamura |
0:8373b6833bde | 230 | o_scr[i]["Surprise"] = picojson::value((double)result_hvcp2_fd[i].scores.score_surprise); |
Osamu Nakamura |
0:8373b6833bde | 231 | o_scr[i]["Sadness"] = picojson::value((double)result_hvcp2_fd[i].scores.score_sadness); |
Osamu Nakamura |
0:8373b6833bde | 232 | /* insert 2 structures */ |
Osamu Nakamura |
0:8373b6833bde | 233 | o_fd[i]["FaceRectangle"] = picojson::value(o_fr[i]); |
Osamu Nakamura |
0:8373b6833bde | 234 | o_fd[i]["Scores"] = picojson::value(o_scr[i]); |
Osamu Nakamura |
0:8373b6833bde | 235 | } |
Osamu Nakamura |
0:8373b6833bde | 236 | } |
Osamu Nakamura |
0:8373b6833bde | 237 | |
Osamu Nakamura |
0:8373b6833bde | 238 | /* No body detect */ |
Osamu Nakamura |
0:8373b6833bde | 239 | if (result_hvcp2_bd_cnt == 0) { |
Osamu Nakamura |
2:a191fff7103b | 240 | /* Do nothing */ |
Osamu Nakamura |
0:8373b6833bde | 241 | } else { |
Osamu Nakamura |
0:8373b6833bde | 242 | for (uint32_t i = 0; i < result_hvcp2_bd_cnt; i++) { |
Osamu Nakamura |
0:8373b6833bde | 243 | /* picojson-object clear */ |
Osamu Nakamura |
0:8373b6833bde | 244 | o_bd[i].clear(); |
Osamu Nakamura |
0:8373b6833bde | 245 | /* Type */ |
Osamu Nakamura |
0:8373b6833bde | 246 | o_bd[i]["RecordType"] = picojson::value((string)"HVC-P2(body)"); |
Osamu Nakamura |
0:8373b6833bde | 247 | o_bd[i]["id"] = picojson::value((string)"0001-0006"); |
Osamu Nakamura |
0:8373b6833bde | 248 | /* BodyRectangle */ |
Osamu Nakamura |
0:8373b6833bde | 249 | o_bd[i]["Top"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.MinY); |
Osamu Nakamura |
0:8373b6833bde | 250 | o_bd[i]["Left"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.MinX); |
Osamu Nakamura |
0:8373b6833bde | 251 | o_bd[i]["Width"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.Width); |
Osamu Nakamura |
0:8373b6833bde | 252 | o_bd[i]["Height"] = picojson::value((double)result_hvcp2_bd[i].body_rectangle.Height); |
Osamu Nakamura |
0:8373b6833bde | 253 | } |
Osamu Nakamura |
0:8373b6833bde | 254 | } |
Osamu Nakamura |
0:8373b6833bde | 255 | |
Osamu Nakamura |
0:8373b6833bde | 256 | DEBUG_PRINT("Face detect count : %d\n", result_hvcp2_fd_cnt); |
Osamu Nakamura |
0:8373b6833bde | 257 | DEBUG_PRINT("Body detect count : %d\n", result_hvcp2_bd_cnt); |
Osamu Nakamura |
0:8373b6833bde | 258 | |
Osamu Nakamura |
0:8373b6833bde | 259 | http_resp_time.reset(); |
Osamu Nakamura |
0:8373b6833bde | 260 | http_resp_time.start(); |
Osamu Nakamura |
0:8373b6833bde | 261 | |
Osamu Nakamura |
0:8373b6833bde | 262 | /* send data */ |
Osamu Nakamura |
0:8373b6833bde | 263 | if (result_hvcp2_fd_cnt == 0) { |
Osamu Nakamura |
2:a191fff7103b | 264 | /* No need to send data */ |
Osamu Nakamura |
0:8373b6833bde | 265 | } else { |
Osamu Nakamura |
0:8373b6833bde | 266 | for (uint32_t i = 0; i < result_hvcp2_fd_cnt; i++) { |
Osamu Nakamura |
0:8373b6833bde | 267 | iot_put(network, o_fd[i]); |
Osamu Nakamura |
0:8373b6833bde | 268 | } |
Osamu Nakamura |
0:8373b6833bde | 269 | } |
Osamu Nakamura |
0:8373b6833bde | 270 | if (result_hvcp2_bd_cnt == 0) { |
Osamu Nakamura |
2:a191fff7103b | 271 | /* No need to send data */ |
Osamu Nakamura |
0:8373b6833bde | 272 | } else { |
Osamu Nakamura |
0:8373b6833bde | 273 | for (uint32_t i = 0; i < result_hvcp2_bd_cnt; i++) { |
Osamu Nakamura |
0:8373b6833bde | 274 | iot_put(network, o_bd[i]); |
Osamu Nakamura |
0:8373b6833bde | 275 | } |
Osamu Nakamura |
0:8373b6833bde | 276 | } |
Osamu Nakamura |
0:8373b6833bde | 277 | DEBUG_PRINT("iot_put() Response time:%dms\n", http_resp_time.read_ms()); |
Osamu Nakamura |
0:8373b6833bde | 278 | return 0; |
Osamu Nakamura |
0:8373b6833bde | 279 | } |
Osamu Nakamura |
0:8373b6833bde | 280 | |
Osamu Nakamura |
0:8373b6833bde | 281 | void iot_ready_task(void) |
Osamu Nakamura |
0:8373b6833bde | 282 | { |
Osamu Nakamura |
0:8373b6833bde | 283 | |
Osamu Nakamura |
0:8373b6833bde | 284 | /* Initialize http */ |
Osamu Nakamura |
0:8373b6833bde | 285 | NetworkInterface *network = easy_connect(true); |
Osamu Nakamura |
0:8373b6833bde | 286 | MBED_ASSERT(network); |
Osamu Nakamura |
0:8373b6833bde | 287 | |
Osamu Nakamura |
2:a191fff7103b | 288 | #ifdef ENABLED_NTP |
Osamu Nakamura |
0:8373b6833bde | 289 | // Generate the string indicating the date and hour specified for PUT request |
Osamu Nakamura |
0:8373b6833bde | 290 | NTPClient ntp; |
Osamu Nakamura |
0:8373b6833bde | 291 | time_t ctTime; |
Osamu Nakamura |
0:8373b6833bde | 292 | struct tm *pnow; |
Osamu Nakamura |
0:8373b6833bde | 293 | NTPResult ret; |
Osamu Nakamura |
0:8373b6833bde | 294 | |
Osamu Nakamura |
0:8373b6833bde | 295 | ret = ntp.setTime("ntp.nict.jp"); |
Osamu Nakamura |
0:8373b6833bde | 296 | MBED_ASSERT( ret==0 ); |
Osamu Nakamura |
0:8373b6833bde | 297 | |
Osamu Nakamura |
0:8373b6833bde | 298 | ctTime = time(NULL); |
Osamu Nakamura |
2:a191fff7103b | 299 | #endif // Enabled_NTP |
Osamu Nakamura |
0:8373b6833bde | 300 | |
Osamu Nakamura |
0:8373b6833bde | 301 | while (1) { |
Osamu Nakamura |
0:8373b6833bde | 302 | |
Osamu Nakamura |
0:8373b6833bde | 303 | semaphore_wait_ret = iot_ready_semaphore.wait(); |
Osamu Nakamura |
0:8373b6833bde | 304 | MBED_ASSERT(semaphore_wait_ret != -1); |
Osamu Nakamura |
0:8373b6833bde | 305 | |
Osamu Nakamura |
0:8373b6833bde | 306 | #ifdef USE_HVC_P2 |
Osamu Nakamura |
0:8373b6833bde | 307 | /* send hvc-p2 data */ |
Osamu Nakamura |
0:8373b6833bde | 308 | http_resp_time.reset(); |
Osamu Nakamura |
0:8373b6833bde | 309 | http_resp_time.start(); |
Osamu Nakamura |
0:8373b6833bde | 310 | send_hvc_info(network); |
Osamu Nakamura |
0:8373b6833bde | 311 | DEBUG_PRINT("send_hvc_info() Response time:%dms\n", |
Osamu Nakamura |
0:8373b6833bde | 312 | http_resp_time.read_ms()); |
Osamu Nakamura |
0:8373b6833bde | 313 | #endif // USE_HVC_P2 |
Osamu Nakamura |
0:8373b6833bde | 314 | |
Osamu Nakamura |
0:8373b6833bde | 315 | #ifdef USE_SENSOR_SHIELD |
Osamu Nakamura |
0:8373b6833bde | 316 | /* send sensor data */ |
Osamu Nakamura |
0:8373b6833bde | 317 | http_resp_time.reset(); |
Osamu Nakamura |
0:8373b6833bde | 318 | http_resp_time.start(); |
Osamu Nakamura |
0:8373b6833bde | 319 | send_sensor_info(network); |
Osamu Nakamura |
0:8373b6833bde | 320 | DEBUG_PRINT("send_sensor_info() Response time:%dms\n", |
Osamu Nakamura |
0:8373b6833bde | 321 | http_resp_time.read_ms()); |
Osamu Nakamura |
0:8373b6833bde | 322 | #endif // USE_SENSOR_SHIELD |
Osamu Nakamura |
0:8373b6833bde | 323 | iot_ready_semaphore.release(); |
Osamu Nakamura |
0:8373b6833bde | 324 | |
Osamu Nakamura |
0:8373b6833bde | 325 | Thread::wait(WAIT_TIME); |
Osamu Nakamura |
0:8373b6833bde | 326 | }; |
Osamu Nakamura |
0:8373b6833bde | 327 | } |