Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: WIZnetInterface mbed
Fork of TCPClient_HelloWorld by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "cJSON.h" 00004 00005 AnalogIn Gas(A2); 00006 int socketPort = 8000; 00007 int httpPort = 80; 00008 00009 Serial pc(PA_13,PA_14); 00010 00011 char path[]="/v1/datastreams/test/datapoint/"; 00012 00013 int MallocSize = 800*sizeof(char); 00014 bool socket_GET(TCPSocketConnection &conn); 00015 bool http_GET(TCPSocketConnection &conn); 00016 bool http_POST(TCPSocketConnection &conn); 00017 bool socket_POST(TCPSocketConnection &conn,int vall); 00018 bool closeConnect(TCPSocketConnection &conn); 00019 00020 float val; 00021 //int mathtest; 00022 bool out=false; 00023 00024 int main() { 00025 int phy_link; 00026 pc.printf("Wait a second...\r\n"); 00027 uint8_t mac_addr[6] = {0x78, 0x08, 0xDC, 0x1c, 0xa8, 0x95}; 00028 00029 EthernetInterface eth; 00030 eth.init(mac_addr); //Use DHCP 00031 eth.connect(); 00032 pc.printf("start IP Address is %s\r\n", eth.getIPAddress()); 00033 /* 00034 do{ 00035 phy_link = eth.ethernet_link(); 00036 pc.printf("..."); 00037 wait(2); 00038 }while(!phy_link); 00039 printf("\r\n"); 00040 */ 00041 pc.printf("IP Address is %s\r\n", eth.getIPAddress()); 00042 00043 TCPSocketConnection conn; 00044 00045 while(true) 00046 { 00047 wait(1); 00048 val = Gas.read()*1023; 00049 int mathtest = ceil(val);//math.h 00050 pc.printf("Gas.read=%02f,n=%d\r\n",val,mathtest); 00051 pc.printf("start http_post\r\n"); 00052 if(http_POST(conn)) 00053 { 00054 if(!conn.close()){//短连接,断开一次连接一次 00055 pc.printf("conn.close OK\r\n"); 00056 }else{ 00057 pc.printf("conn.close ERR\r\n"); 00058 break; 00059 } 00060 }else{ 00061 if(out)break; 00062 pc.printf("connect close err!!!to do something\r\n"); 00063 00064 } 00065 00066 //val = 0.0; 00067 //mathtest = 0; 00068 00069 if(out)break; 00070 } 00071 00072 eth.disconnect(); 00073 pc.printf("out the program!\r\n"); 00074 return 0; 00075 } 00076 00077 /*socket GET*/ 00078 bool socket_GET(TCPSocketConnection &conn) 00079 { 00080 if(conn.is_connected()){ 00081 pc.printf("connect is already ok!\r\n"); 00082 }else{ 00083 if(!conn.connect("iot.espressif.cn", socketPort)) //80 for http,8000 for socket,处理超时,拔掉网线后,卡在conn.connect 00084 pc.printf("connect OK!\r\n"); 00085 else{ 00086 pc.printf("connct ERR!\r\n"); 00087 out = true; 00088 return false; 00089 } 00090 } 00091 00092 cJSON * pJsonRoot = NULL; 00093 pJsonRoot = cJSON_CreateObject(); 00094 if(NULL == pJsonRoot) 00095 { 00096 pc.printf("socket_GET OUT1\r\n"); 00097 out = true; 00098 return false; 00099 } 00100 00101 cJSON * pSubJson = NULL; 00102 pSubJson = cJSON_CreateObject(); 00103 if(NULL == pSubJson) 00104 { 00105 cJSON_Delete(pJsonRoot); 00106 pc.printf("socket_GET OUT2\r\n"); 00107 out = true; 00108 return false; 00109 } 00110 00111 cJSON_AddStringToObject(pJsonRoot, "path", "/v1/datastreams/test/datapoint/"); 00112 cJSON_AddStringToObject(pSubJson, "Authorization", "token 48661aa81484f501362ed5ef4cc85e67eb2a3e3d"); 00113 cJSON_AddItemToObject(pJsonRoot, "meta", pSubJson); 00114 cJSON_AddStringToObject(pJsonRoot, "method", "GET"); 00115 00116 char* pJSON = cJSON_PrintUnformatted(pJsonRoot); 00117 strcat(pJSON,"\n");//socket at the end should have '\n',use wireshark 00118 00119 if(NULL == pJSON) 00120 { 00121 cJSON_Delete(pJsonRoot); 00122 pc.printf("socket_GET OUT3\r\n"); 00123 out = true; 00124 return false; 00125 } 00126 00127 conn.send_all(pJSON, strlen(pJSON));//http_cmd for http,pJSON for socket,should be three changes 00128 pc.printf("%s\r\n",pJSON); 00129 00130 char *buffer = NULL; 00131 buffer = (char *)malloc(MallocSize); 00132 if(!buffer)pc.printf("malloc buffer err\r\n"); 00133 int ret; 00134 00135 while (true) { 00136 wait(1); 00137 ret = conn.receive(buffer, MallocSize-1); 00138 pc.printf("ret = %d\r\n",ret); 00139 if (ret <= 0) { 00140 break; 00141 } 00142 buffer[ret] = '\0'; 00143 pc.printf("Received %d chars from server: %s\n", ret, buffer); 00144 } 00145 pc.printf("socket_GET OUT\r\n"); 00146 00147 free(pJSON);//for socket 00148 cJSON_Delete(pJsonRoot);//for socket 00149 free(buffer); 00150 return true; 00151 } 00152 00153 /*http_GET*/ 00154 bool http_GET(TCPSocketConnection &conn) 00155 { 00156 if(conn.is_connected()){ 00157 pc.printf("connect is already ok!\r\n"); 00158 }else{ 00159 if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket 00160 pc.printf("connect OK!\r\n"); 00161 else{ 00162 pc.printf("connct ERR!\r\n"); 00163 out = true; 00164 return false; 00165 } 00166 } 00167 00168 char http_cmd[] = "GET /v1/datastreams/Gas/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\ 00169 "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token ebe1671e9d9f42a5231d017ec550552b9bae2cdf\r\n\r\n"; 00170 00171 conn.send_all(http_cmd, strlen(http_cmd));//http_cmd for http,pJSON for socket,should be three changes 00172 pc.printf("%s\r\n",http_cmd); 00173 00174 //char *buffer = NULL; 00175 //buffer = (char *)malloc(MallocSize); 00176 //if(!buffer)pc.printf("malloc buffer err\r\n"); 00177 char buffer[500]; 00178 int ret; 00179 00180 while (true) { 00181 wait(1); 00182 ret = conn.receive(buffer, 500-1); 00183 pc.printf("ret = %d\r\n",ret); 00184 if (ret <= 0) { 00185 break; 00186 } 00187 buffer[ret] = '\0'; 00188 pc.printf("Received %d chars from server: %s\n", ret, buffer); 00189 } 00190 pc.printf("http_GET OUT\r\n"); 00191 /* ///conn.close已经放到main中,55 行 00192 if(conn.close()){ 00193 pc.printf("conn.close OK\r\n"); 00194 }else{ 00195 pc.printf("conn.close ERR\r\n"); 00196 } 00197 */ 00198 //free(buffer); 00199 return true; 00200 } 00201 00202 /*http POST*/ 00203 bool http_POST(TCPSocketConnection &conn) 00204 { 00205 pc.printf("1111\r\n"); 00206 wait(0.5); 00207 if(conn.is_connected()){ 00208 pc.printf("connect is already ok!\r\n"); 00209 }else{ 00210 if(!conn.connect("iot.espressif.cn", httpPort)) //80 for http,8000 for socket 00211 pc.printf("connect OK!\r\n"); 00212 else{ 00213 pc.printf("connct ERR!\r\n"); 00214 out = true; 00215 return false; 00216 } 00217 } 00218 pc.printf("2222\r\n"); 00219 cJSON* http_post=NULL; 00220 http_post = cJSON_CreateObject(); 00221 if(NULL == http_post) 00222 { 00223 pc.printf("OUT4\r\n"); 00224 out = true; 00225 return false; 00226 } 00227 pc.printf("3333\r\n"); 00228 cJSON* http_post_json=NULL; 00229 http_post_json = cJSON_CreateObject(); 00230 if(NULL == http_post_json) 00231 { 00232 cJSON_Delete(http_post); 00233 pc.printf("OUT5\r\n"); 00234 out = true; 00235 return false; 00236 } 00237 cJSON_AddNumberToObject(http_post_json, "x", 2); 00238 //cJSON_AddNumberToObject(http_post_json, "y", 2); 00239 //cJSON_AddNumberToObject(http_post_json, "z", 2); 00240 cJSON_AddItemToObject(http_post, "datapoint", http_post_json); 00241 char* pJSON_http_post = cJSON_PrintUnformatted(http_post); 00242 pc.printf("4444\r\n"); 00243 if(NULL == pJSON_http_post) 00244 { 00245 cJSON_Delete(http_post); 00246 pc.printf("OUT6\r\n"); 00247 out = true; 00248 return false; 00249 } 00250 00251 //Pay attention to out of memory 00252 //char http_POST_cmd[500] = "POST /v1/datastreams/jhtest/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\ 00253 // "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token ebe1671e9d9f42a5231d017ec550552b9bae2cdf\r\n"\ 00254 // "Content-Length: 33\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"; 00255 char http_POST_cmd[500] = "POST /v1/datastreams/test/datapoint/ HTTP/1.1\r\nUser-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3\r\n"\ 00256 "Host:iot.espressif.cn\r\nAccept: */*\r\nAuthorization: token 48661aa81484f501362ed5ef4cc85e67eb2a3e3d\r\n"\ 00257 "Content-Length: "; 00258 char len[2]; 00259 len[0]=strlen(pJSON_http_post)/10%10+48; 00260 len[1]=strlen(pJSON_http_post)%10+48; 00261 strcat(http_POST_cmd,len); 00262 strcat(http_POST_cmd,"\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"); 00263 00264 strcat(http_POST_cmd,pJSON_http_post); 00265 conn.send_all(http_POST_cmd, strlen(http_POST_cmd));//http_cmd for http,pJSON for socket,should be three changes 00266 pc.printf("%s\r\n",http_POST_cmd); 00267 pc.printf("5555\r\n"); 00268 char *buffer = NULL; 00269 buffer = (char *)malloc(MallocSize); 00270 if(!buffer)pc.printf("malloc buffer err\r\n"); 00271 00272 int ret; 00273 pc.printf("6666\r\n"); 00274 while (true) { 00275 wait(1); 00276 ret = conn.receive(buffer, MallocSize-1); 00277 pc.printf("ret = %d\r\n",ret); 00278 if (ret <= 0) { 00279 break; 00280 } 00281 buffer[ret] = '\0'; 00282 pc.printf("Received %d chars from server: %s\n", ret, buffer); 00283 } 00284 pc.printf("http_POST OUT\r\n"); 00285 00286 free(pJSON_http_post); 00287 free(buffer); 00288 cJSON_Delete(http_post); 00289 pc.printf("7777\r\n"); 00290 return true; 00291 } 00292 00293 /*socket POST*/ 00294 bool socket_POST(TCPSocketConnection &conn,int vall) 00295 { 00296 if(conn.is_connected()){ 00297 pc.printf("connect is already ok!\r\n"); 00298 }else{ 00299 if(!conn.connect("iot.espressif.cn", socketPort)) //80 for http,8000 for socket 00300 pc.printf("connect OK!\r\n"); 00301 else{ 00302 pc.printf("connct ERR!\r\n"); 00303 out = true; 00304 return false; 00305 } 00306 } 00307 cJSON* socket_post=NULL; 00308 socket_post = cJSON_CreateObject(); 00309 if(NULL == socket_post) 00310 { 00311 pc.printf("OUT4\r\n"); 00312 out = true; 00313 return false; 00314 } 00315 00316 00317 cJSON_AddStringToObject(socket_post, "path", path); 00318 cJSON_AddStringToObject(socket_post, "method", "POST"); 00319 00320 cJSON* sock_post_token=NULL; 00321 sock_post_token = cJSON_CreateObject(); 00322 if(NULL == sock_post_token) 00323 { 00324 cJSON_Delete(socket_post); 00325 pc.printf("OUT5\r\n"); 00326 out = true; 00327 return false; 00328 } 00329 cJSON_AddStringToObject(sock_post_token, "Authorization", "token 514cc9c3aa07a4a56246cb9259c8264fd2ae56f6"); 00330 cJSON_AddItemToObject(socket_post, "meta", sock_post_token); 00331 00332 cJSON* sock_post_xyz=NULL; 00333 sock_post_xyz = cJSON_CreateObject(); 00334 if(NULL == sock_post_xyz) 00335 { 00336 cJSON_Delete(socket_post); 00337 pc.printf("OUT6\r\n"); 00338 out = true; 00339 return false; 00340 } 00341 cJSON_AddNumberToObject(sock_post_xyz, "x", vall); 00342 //cJSON_AddNumberToObject(sock_post_xyz, "y", 8); 00343 //cJSON_AddNumberToObject(sock_post_xyz, "z", 8); 00344 cJSON* sock_post_datapoint=NULL; 00345 sock_post_datapoint = cJSON_CreateObject(); 00346 if(NULL == sock_post_datapoint) 00347 { 00348 cJSON_Delete(socket_post); 00349 pc.printf("OUT6\r\n"); 00350 out = true; 00351 return false; 00352 } 00353 cJSON_AddItemToObject(sock_post_datapoint, "datapoint", sock_post_xyz); 00354 cJSON_AddItemToObject(socket_post, "body", sock_post_datapoint); 00355 00356 char* pJSON_sock_post = cJSON_PrintUnformatted(socket_post); 00357 00358 if(NULL == pJSON_sock_post) 00359 { 00360 cJSON_Delete(socket_post); 00361 pc.printf("OUT7\r\n"); 00362 out = true; 00363 return false; 00364 } 00365 strcat(pJSON_sock_post,"\n"); 00366 00367 conn.send_all(pJSON_sock_post, strlen(pJSON_sock_post));//http_cmd for http,pJSON for socket,should be three changes 00368 pc.printf("%s\r\n",pJSON_sock_post); 00369 00370 char *buffer = NULL; 00371 buffer = (char *)malloc(MallocSize); 00372 if(!buffer)pc.printf("malloc buffer err\r\n"); 00373 00374 int ret; 00375 00376 while (true) { 00377 ret = conn.receive(buffer, MallocSize-1); 00378 pc.printf("ret = %d\r\n",ret); 00379 if (ret <= 0) { 00380 break; 00381 } 00382 buffer[ret] = '\0'; 00383 pc.printf("Received %d chars from server: %s\n", ret, buffer); 00384 } 00385 pc.printf("socket_POST OUT\r\n"); 00386 00387 free(pJSON_sock_post); 00388 free(buffer); 00389 cJSON_Delete(socket_post); 00390 00391 return true; 00392 } 00393 00394 bool closeConnect(TCPSocketConnection &conn) 00395 { 00396 if(conn.is_connected()){ 00397 pc.printf("connect is already ok!\r\n"); 00398 }else{ 00399 if(!conn.connect("iot.espressif.cn", socketPort)) //80 for http,8000 for socket 00400 pc.printf("connect OK!\r\n"); 00401 else{ 00402 pc.printf("connct ERR!\r\n"); 00403 out = true; 00404 return false; 00405 } 00406 } 00407 00408 00409 cJSON* close_post=NULL; 00410 close_post = cJSON_CreateObject(); 00411 if(NULL == close_post) 00412 { 00413 pc.printf("OUT4\r\n"); 00414 out = true; 00415 return false; 00416 } 00417 00418 00419 cJSON_AddStringToObject(close_post, "path", "/v1/The_Gas/quit/"); 00420 cJSON_AddStringToObject(close_post, "method", "POST"); 00421 00422 cJSON* close_post_token=NULL; 00423 close_post_token = cJSON_CreateObject(); 00424 if(NULL == close_post_token) 00425 { 00426 cJSON_Delete(close_post); 00427 pc.printf("OUT5\r\n"); 00428 out = true; 00429 return false; 00430 } 00431 cJSON_AddStringToObject(close_post_token, "Authorization", "token 514cc9c3aa07a4a56246cb9259c8264fd2ae56f6"); 00432 cJSON_AddItemToObject(close_post, "meta", close_post_token); 00433 00434 char* close_pJSON = cJSON_PrintUnformatted(close_post); 00435 00436 if(NULL == close_pJSON) 00437 { 00438 cJSON_Delete(close_post); 00439 pc.printf("OUT7\r\n"); 00440 out = true; 00441 return false; 00442 } 00443 strcat(close_pJSON,"\n"); 00444 00445 conn.send_all(close_pJSON, strlen(close_pJSON));//http_cmd for http,pJSON for socket,should be three changes 00446 pc.printf("%s\r\n",close_pJSON); 00447 00448 char *buffer = NULL; 00449 buffer = (char *)malloc(MallocSize); 00450 if(!buffer)pc.printf("malloc buffer err\r\n"); 00451 00452 int ret; 00453 00454 while (true) { 00455 ret = conn.receive(buffer, MallocSize-1); 00456 pc.printf("ret = %d\r\n",ret); 00457 if (ret <= 0) { 00458 break; 00459 } 00460 buffer[ret] = '\0'; 00461 pc.printf("Received %d chars from server: %s\n", ret, buffer); 00462 } 00463 pc.printf("close_POST OUT\r\n"); 00464 00465 free(close_pJSON); 00466 free(buffer); 00467 cJSON_Delete(close_post); 00468 00469 return true; 00470 }
Generated on Thu Jul 14 2022 10:31:23 by
1.7.2
