@@ -29,12 +29,6 @@ static constexpr int POWER_ON_TIMEOUT = 1000 * 20; // [ms]
2929static constexpr int NETWORK_TIMEOUT = 1000 * 60 * 3 ; // [ms]
3030static constexpr int RECEIVE_TIMEOUT = 1000 * 10 ; // [ms]
3131
32- struct HttpResponse {
33- int statusCode;
34- std::map<std::string, std::string> headers;
35- std::string body;
36- };
37-
3832static void abortHandler (int sig) {
3933 Serial.printf (" ABORT: Signal %d received\n " , sig);
4034 yield ();
@@ -87,36 +81,8 @@ void loop() {
8781 digitalWrite (LED_BUILTIN, HIGH);
8882
8983 JsonDoc.clear ();
90- if (generateRequestBody (JsonDoc)) {
91- std::string jsonStr;
92- serializeJson (JsonDoc, jsonStr);
93- Serial.println (jsonStr.c_str ());
94-
95- HttpResponse response;
96- {
97- WioCellularArduinoTcpClient<WioCellularModule> client{ WioCellular, WioNetwork.config .pdpContextId };
98- response = httpRequest (client, HOST, PORT, PATH, " PUT" , " application/json" , jsonStr.c_str ());
99- }
100-
101- Serial.println (" Header(s):" );
102- for (auto header : response.headers ) {
103- Serial.print (" " );
104- Serial.print (header.first .c_str ());
105- Serial.print (" : " );
106- Serial.print (header.second .c_str ());
107- Serial.println ();
108- }
109- Serial.print (" Body: " );
110- Serial.println (response.body .c_str ());
111-
112- if (response.statusCode == 200 ) {
113- JsonDoc.clear ();
114- deserializeJson (JsonDoc, response.body .c_str ());
115- // Output the IMSI field as an example of how to use the response
116- Serial.print (" Response imsi> " );
117- Serial.print (JsonDoc[" imsi" ].as <String>());
118- Serial.println ();
119- }
84+ if (measure (JsonDoc)) {
85+ send (JsonDoc);
12086 }
12187
12288 digitalWrite (LED_BUILTIN, LOW);
@@ -128,7 +94,7 @@ void loop() {
12894 * Generate request body for update SIM tag
12995 * See: https://users.soracom.io/ja-jp/docs/air/use-metadata/#%E3%83%A1%E3%82%BF%E3%83%87%E3%83%BC%E3%82%BF%E3%81%AE%E6%9B%B8%E3%81%8D%E8%BE%BC%E3%81%BF%E4%BE%8B-iot-sim-%E3%81%AE%E3%82%BF%E3%82%B0%E3%82%92%E8%BF%BD%E5%8A%A0--%E6%9B%B4%E6%96%B0%E3%81%99%E3%82%8B
13096 */
131- static bool generateRequestBody (JsonDocument& doc) {
97+ static bool measure (JsonDocument & doc) {
13298 Serial.println (" ### Measuring" );
13399
134100 JsonArray rootArray = doc.to <JsonArray>();
@@ -142,54 +108,52 @@ static bool generateRequestBody(JsonDocument& doc) {
142108 return true ;
143109}
144110
145- static HttpResponse httpRequest (Client& client, const char * host, int port, const char * path, const char * method, const char * contentType, const char * requestBody) {
146- HttpResponse httpResponse;
111+ static bool send (const JsonDocument &doc) {
147112 Serial.print (" ### Requesting to [" );
148- Serial.print (host );
113+ Serial.print (HOST );
149114 Serial.println (" ]" );
150115
151- HttpClient httpClient (client, host, port);
152- httpClient.setTimeout (RECEIVE_TIMEOUT);
153- int err = httpClient.startRequest (path, method, contentType, strlen (requestBody), (const byte*)requestBody);
154- if (err != 0 ) {
155- httpClient.stop ();
156- httpResponse.statusCode = err;
157- return httpResponse;
158- }
116+ int statusCode = -1 ;
117+ int contentLength = -1 ;
118+ String responseBody;
119+ {
120+ WioCellularArduinoTcpClient<WioCellularModule> client{ WioCellular, WioNetwork.config .pdpContextId };
121+ HttpClient httpClient{ client, HOST, PORT };
122+ httpClient.setTimeout (RECEIVE_TIMEOUT);
123+
124+ std::string requestBody;
125+ serializeJson (doc, requestBody);
126+ Serial.println (requestBody.c_str ());
127+ if (const auto result = httpClient.put (PATH, " application/json" , requestBody.c_str ()); result != 0 ) {
128+ Serial.printf (" ERROR: Failed to HTTP PUT %d\n " , result);
129+ return false ;
130+ }
159131
160- int statusCode = httpClient.responseStatusCode ();
161- if (! statusCode) {
162- httpClient.stop ();
163- httpResponse. statusCode = statusCode ;
164- return httpResponse;
132+ statusCode = httpClient.responseStatusCode ();
133+ if (statusCode >= 0 ) {
134+ contentLength = httpClient.contentLength ();
135+ responseBody = httpClient. responseBody () ;
136+ }
165137 }
166138
139+ Serial.println (" ### End HTTP request" );
140+ Serial.println ();
141+
167142 Serial.print (" Status code returned " );
168143 Serial.println (statusCode);
169- httpResponse.statusCode = statusCode;
170-
171- while (httpClient.headerAvailable ()) {
172- String headerName = httpClient.readHeaderName ();
173- String headerValue = httpClient.readHeaderValue ();
174- httpResponse.headers [headerName.c_str ()] = headerValue.c_str ();
175- }
176-
177- int length = httpClient.contentLength ();
178- if (length >= 0 ) {
179- Serial.print (" Content length: " );
180- Serial.println (length);
181- }
182- if (httpClient.isResponseChunked ()) {
183- Serial.println (" The response is chunked" );
144+ Serial.print (" Content length: " );
145+ Serial.println (contentLength);
146+ Serial.print (" Body: " );
147+ Serial.println (responseBody);
148+
149+ if (statusCode == 200 ) {
150+ JsonDocument doc;
151+ deserializeJson (doc, responseBody);
152+ // Output the IMSI field as an example of how to use the response
153+ Serial.print (" Response imsi> " );
154+ Serial.print (doc[" imsi" ].as <String>());
155+ Serial.println ();
184156 }
185157
186- String responseBody = httpClient.responseBody ();
187- httpResponse.body = responseBody.c_str ();
188-
189- httpClient.stop ();
190-
191- Serial.println (" ### End HTTP request" );
192- Serial.println ();
193-
194- return httpResponse;
158+ return true ;
195159}
0 commit comments