this is using the mbed os version 5-13-1
Embed:
(wiki syntax)
Show/hide line numbers
wifi_demo.cpp
00001 00002 #include "wifi_demo.h" 00003 #include "common_config.h" 00004 // Wifi-demo 00005 00006 RawSerial *device; // tx, rx 00007 extern EventQueue eventQueue(/* event count */ 20 * EVENTS_EVENT_SIZE); 00008 int chunkNum; 00009 void dump_response(HttpResponse* res) { 00010 device->printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); 00011 00012 device->printf("Headers:\n"); 00013 for (size_t ix = 0; ix < res->get_headers_length(); ix++) { 00014 device->printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); 00015 } 00016 char * body = (char *) res->get_body(); 00017 device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); 00018 for (size_t ix = 0; ix < res->get_body_length(); ix++) { 00019 device->printf("%02X: ", body[ix]); 00020 if((ix % 32) == 0 and ix) 00021 device->printf("\n"); 00022 } 00023 00024 } 00025 00026 void completed(){ 00027 } 00028 void dump_chunked_response(const char *at, uint32_t length) { 00029 device->printf("\n Chunked response: Chunk %d : Total Bytes = %d\n", chunkNum , length); 00030 //device->printf("\n Try Print Header as string:\n\n "); 00031 //device->printf("recv %d [%.*s]\n", length, strstr((char *)at, "\r\n")-(char *)at, (char *)at); 00032 //if(false) 00033 if(chunkNum < 2) 00034 for(int i=0; i < length; i++){ 00035 00036 while(device->writeable()) 00037 { 00038 device->putc((uint8_t)at[i]); 00039 } 00040 //int resp = write( (const uint8_t *)at, (int) length, &completed, SERIAL_EVENT_TX_COMPLETE); 00041 } 00042 if(false) 00043 for (size_t ix = 0; ix < length; ix++) { 00044 device->printf("%02X: ", at[ix]); 00045 if((ix % 32) == 0 and ix) 00046 device->printf("\n"); 00047 } 00048 device->printf("\n\n"); 00049 chunkNum++; 00050 //device->printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); 00051 } 00052 00053 00054 int wifi_demo_func(NetworkInterface* network){ 00055 device = new RawSerial(USBTX, USBRX, DEFAULT_BAUD_RATE); 00056 if (!network) { 00057 device->printf("Cannot connect to the network, see serial output\n"); 00058 return 1; 00059 } 00060 wait(1); // wait for 1 sec 00061 00062 mbed_trace_init(); 00063 #ifdef RUN_ALL 00064 wait(1); // wait for 1 sec 00065 00066 // GET request to os.mbed.com 00067 { 00068 chunkNum = 0; 00069 device->printf("\n----- HTTPS GET request -----\n"); 00070 00071 HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt", &dump_chunked_response); 00072 00073 HttpResponse* get_res = get_req->send(); 00074 if (!get_res) { 00075 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00076 return 1; 00077 } 00078 device->printf("\n----- HTTPS GET response -----\n"); 00079 dump_response(get_res); 00080 delete get_req; 00081 } 00082 00083 wait(1); // wait for 1 sec 00084 00085 // Do a GET request to httpbin.org 00086 { 00087 chunkNum = 0; 00088 device->printf("\n----- HTTP GET request to httpbin.org -----\n"); 00089 00090 // By default the body is automatically parsed and stored in a buffer, this is memory heavy. 00091 // To receive chunked response, pass in a callback as last parameter to the constructor. 00092 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418"); 00093 00094 HttpResponse* get_res = get_req->send(); 00095 if (!get_res) { 00096 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00097 return 1; 00098 } 00099 00100 device->printf("\n----- HTTP GET response from httpbin.org -----\n"); 00101 dump_response(get_res); 00102 00103 delete get_req; 00104 } 00105 00106 wait(1); // wait for 1 sec 00107 00108 00109 // Do a GET request to ovh.net 00110 if(false) 00111 { 00112 chunkNum = 0; 00113 device->printf("\n----- HTTP GET request to ovh.net (LARGE FILE) http://www.ovh.net/files/1Mio.dat -----\n"); 00114 Timer t; 00115 // By default the body is automatically parsed and stored in a buffer, this is memory heavy. 00116 // To receive chunked response, pass in a callback as last parameter to the constructor. 00117 t.start(); 00118 HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://www.ovh.net/files/1Mio.dat", &dump_chunked_response); 00119 00120 HttpResponse* get_res = get_req->send(); 00121 if (!get_res) { 00122 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00123 return 1; 00124 } 00125 00126 device->printf("\n----- HTTP GET response from ovh.net -----\n"); 00127 dump_response(get_res); 00128 t.stop(); 00129 device->printf("The time taken was %f seconds\n", t.read()); 00130 00131 00132 delete get_req; 00133 } 00134 wait(1); // wait for 1 sec 00135 00136 { 00137 device->printf("\n----- HTTPS GET request (small file!) -----\n"); 00138 00139 HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, "https://os.mbed.com/media/uploads/mbed_official/hello.txt"); 00140 00141 HttpResponse* get_res = get_req->send(); 00142 if (!get_res) { 00143 device->printf("HttpRequest failed (error code %d)\n", get_req->get_error()); 00144 return 1; 00145 } 00146 /* 00147 while (0 < (response = socket.recv(p, remaining))) { 00148 p += response; 00149 rcount += response; 00150 remaining -= response; 00151 } 00152 */ 00153 device->printf("\n----- HTTPS GET response -----\n"); 00154 dump_response(get_res); 00155 00156 00157 00158 delete get_req; 00159 } 00160 wait(1); // wait for 1 sec 00161 00162 // POST request to httpbin.org 00163 { 00164 device->printf("\n----- HTTPS POST request -----\n"); 00165 00166 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://httpbin.org/post"); 00167 post_req->set_header("Content-Type", "application/json"); 00168 00169 const char body[] = "{\"hello\":\"world\"}"; 00170 00171 HttpResponse* post_res = post_req->send(body, strlen(body)); 00172 if (!post_res) { 00173 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00174 return 1; 00175 } 00176 00177 device->printf("\n----- HTTPS POST response -----\n"); 00178 dump_response(post_res); 00179 00180 delete post_req; 00181 } 00182 00183 wait(1); // wait for 1 sec 00184 00185 // POST request to ws.dnanudge.io:80 00186 { 00187 device->printf("\n----- HTTP POST request (http://ws.dnanudge.io/nudgebox/v1) -----\n"); 00188 00189 HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://ws.dnanudge.io/nudgebox/v1"); 00190 post_req->set_header("Host", "ws.dnanudge.io"); 00191 post_req->set_header("Accept", "*/*"); 00192 post_req->set_header("Content-Type", "application/octet-stream"); 00193 post_req->set_header("Content-Length", "20"); 00194 // 00 08 6a 48 f8 2d 8e 82 01 68 00195 const uint8_t body[] = {0x00, 0x08, 0x6a, 0x48, 0xf8, 0x2d, 0x8e, 0x82, 0x01, 0x68, 00196 // 65 6c 6c 6f 00 00 67 c3 19 f8 00197 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x67, 0xc3, 0x19, 0xf8}; 00198 00199 HttpResponse* post_res = post_req->send(body, 20); 00200 if (!post_res) { 00201 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00202 return 1; 00203 } 00204 00205 device->printf("\n----- HTTPS POST response -----\n"); 00206 dump_response(post_res); 00207 00208 delete post_req; 00209 } 00210 00211 wait(1); // wait for 1 sec 00212 // POST request to httpbin.org 00213 if(false) 00214 { 00215 device->printf("\n----- HTTPS POST request to AWS -----\n"); 00216 00217 HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://q6bc0dxw7f.execute-api.eu-west-2.amazonaws.com/test/samples/v1"); 00218 post_req->set_header("Content-Type", "application/json"); 00219 00220 const char body[] = 00221 "{" 00222 "\"firstName\": \"Maria\", " 00223 "\"lastName\": \"Huntera\", " 00224 "\"dob\": \"1970-12-03\", " 00225 "\"mobile\": \"07841887580\", " 00226 "\"cartridgeId\": \"DN00000000RMPOE\", " 00227 "\"labSampleId\": \"DYYAK\"" 00228 "}"; 00229 HttpResponse* post_res = post_req->send(body, strlen(body)); 00230 if (!post_res) { 00231 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00232 return 1; 00233 } 00234 00235 device->printf("\n----- HTTPS POST response from AWS -----\n"); 00236 dump_response(post_res); 00237 00238 delete post_req; 00239 } 00240 00241 #endif 00242 00243 // POST request to dev2.dnanudge.io 00244 if(true) 00245 { 00246 device->printf("\n----- HTTPS POST request to dev2.dnanudge.io -----\n"); 00247 TLSSocket* socket; 00248 socket = new TLSSocket(); 00249 char hostName[] = "dev2.dnanudge.io"; 00250 nsapi_error_t r; 00251 // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) 00252 r = socket->open(network); 00253 if(r != NSAPI_ERROR_OK) 00254 { 00255 printf("TLS open failed!!\n"); 00256 return 0; 00257 } 00258 printf("TLS open passed!!\n"); 00259 r = socket->set_root_ca_cert(SSL_CA_PEM); 00260 if(r != NSAPI_ERROR_OK) 00261 { 00262 printf("TLS set_root_ca_cert failed!!\n"); 00263 return 0; 00264 } 00265 printf("TLS set_root_ca_cert passed!!\n"); 00266 r = socket->connect(hostName, 443); 00267 if(r != NSAPI_ERROR_OK) 00268 { 00269 printf("TLS connect failed for hostname %s!!\n", hostName); 00270 return 0; 00271 } 00272 printf("TLS connection successful for https site : %s\n", hostName); 00273 //HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, "https://dev2.dnanudge.io/nudgebox/v1"); 00274 HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://dev2.dnanudge.io/nudgebox/v1"); 00275 //post_req->set_header("Content-Type", "application/json"); 00276 post_req->set_header("Accept", "*/*"); 00277 post_req->set_header("Content-Type", "application/octet-stream"); 00278 post_req->set_header("Content-Length", "20"); 00279 00280 const char body[] = {0x00, 0x08, 0x6a, 0x48, 0xf8, 0x2d, 0x8e, 00281 0x82, 0x01, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x67, 00282 0xc3, 0x19, 0xf8}; 00283 HttpResponse* post_res = post_req->send(body, 20); 00284 if (!post_res) { 00285 device->printf("HttpRequest failed (error code %d)\n", post_req->get_error()); 00286 return 1; 00287 } 00288 00289 device->printf("\n----- HTTPS POST response from dev2.dnanudge.io -----\n"); 00290 dump_response(post_res); 00291 00292 delete post_req; 00293 } 00294 00295 wait(1); // wait for 1 sec 00296 delete device; 00297 return 0; 00298 }
Generated on Wed Jul 13 2022 05:40:26 by
1.7.2