Giung Kim
/
WIZnet-IoTShield-WM01-TCP
Init Project for TCP
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED 00002 * Copyright (c) 2019 WIZnet Co., Ltd. 00003 * SPDX-License-Identifier: Apache-2.0 00004 */ 00005 00006 /* 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * 00017 * See the License for the specific language governing permissions and 00018 * limitations under the License. 00019 * 00020 */ 00021 00022 #include "mbed.h" 00023 00024 #include <string> 00025 00026 #define RET_OK 1 00027 #define RET_NOK -1 00028 #define DEBUG_ENABLE 1 00029 #define DEBUG_DISABLE 0 00030 #define ON 1 00031 #define OFF 0 00032 00033 #define MAX_BUF_SIZE 1024 00034 00035 #define WM01_APN_PROTOCOL_IPv4 1 00036 #define WM01_APN_PROTOCOL_IPv6 2 00037 #define WM01_DEFAULT_TIMEOUT 1000 00038 #define WM01_CONNECT_TIMEOUT 15000 00039 #define WM01_SEND_TIMEOUT 500 00040 #define WM01_RECV_TIMEOUT 500 00041 #define WM01_BOOTING_TIME 15000 00042 00043 #define WM01_APN_PROTOCOL WM01_APN_PROTOCOL_IPv6 00044 #define WM01_DEFAULT_BAUD_RATE 115200 00045 #define WM01_PARSER_DELIMITER "\r\n" 00046 00047 #define CATM1_APN_SKT "lte-internet.sktelecom.com" 00048 00049 #define CATM1_DEVICE_NAME_WM01 "WM01" 00050 #define DEVNAME CATM1_DEVICE_NAME_WM01 00051 00052 #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } 00053 #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} 00054 00055 /* Pin configuraiton */ 00056 // Cat.M1 00057 #define MBED_CONF_IOTSHIELD_CATM1_TX D8 00058 #define MBED_CONF_IOTSHIELD_CATM1_RX D2 00059 #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 00060 #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 00061 00062 // Sensors 00063 #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 00064 #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 00065 00066 /* Debug message settings */ 00067 #define WM01_PARSER_DEBUG DEBUG_DISABLE 00068 #define CATM1_DEVICE_DEBUG DEBUG_ENABLE 00069 00070 // Destination (Remote Host) 00071 // IP address and Port number 00072 // char dest_ip[] = "222.xxx.xxx.xxx"; 00073 // int dest_port = xxxx; 00074 char dest_ip[] = "222.98.173.214"; // Test IP 00075 int dest_port = 8080; 00076 int protocol = 1; // 1 : TCP, 2 : UPD 00077 int packet_type = 0; // 0 : ASCII, 1 : HEX, 2 : Binary 00078 00079 // Functions: Print information 00080 void printInfo(void); 00081 00082 // Functions: Module Status 00083 void waitCatM1Ready(void); 00084 int8_t setEchoStatus_WM01(bool onoff); 00085 int8_t getUsimStatus_WM01(void); 00086 int8_t getNetworkStatus_WM01(void); 00087 00088 // Functions: PDP context 00089 int8_t setContextActivate_WM01(void); // Activate a PDP Context 00090 int8_t setContextDeactivate_WM01(void); // Deactivate a PDP Context 00091 00092 // Functions: TCP/UDP Socket service 00093 int8_t sockOpenConnect_WM01(int protocol, const char *addr, int port, int pckttype); 00094 int8_t sockClose_WM01(void); 00095 int8_t sendData_WM01(char *data, int len); 00096 int8_t recvData_WM01(char *data, int *len); 00097 00098 Serial pc(USBTX, USBRX); // tx, rx 00099 00100 UARTSerial *_serial; 00101 ATCmdParser *_parser; 00102 00103 DigitalOut _RESET_WM01(MBED_CONF_IOTSHIELD_CATM1_RESET); 00104 DigitalOut _PWRKEY_WM01(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); 00105 00106 void serialPcInit(void) 00107 { 00108 pc.baud(115200); 00109 pc.format(8, Serial::None, 1); 00110 } 00111 00112 void serialDeviceInit(PinName tx, PinName rx, int baudrate) 00113 { 00114 _serial = new UARTSerial(tx, rx, baudrate); 00115 } 00116 00117 void serialAtParserInit(const char *delimiter, bool debug_en) 00118 { 00119 _parser = new ATCmdParser(_serial); 00120 _parser->debug_on(debug_en); 00121 _parser->set_delimiter(delimiter); 00122 _parser->set_timeout(WM01_DEFAULT_TIMEOUT); 00123 } 00124 00125 void catm1DeviceInit(void) 00126 { 00127 serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX, 00128 MBED_CONF_IOTSHIELD_CATM1_RX, 00129 WM01_DEFAULT_BAUD_RATE); 00130 00131 serialAtParserInit( WM01_PARSER_DELIMITER, 00132 WM01_PARSER_DEBUG); 00133 } 00134 00135 void catm1DeviceReset_WM01(void) 00136 { 00137 _RESET_WM01 = 1; 00138 _PWRKEY_WM01 = 1; 00139 wait_ms(300); 00140 00141 _RESET_WM01 = 0; 00142 _PWRKEY_WM01 = 0; 00143 wait_ms(400); 00144 00145 _RESET_WM01 = 1; 00146 wait_ms(1000); 00147 } 00148 00149 // ---------------------------------------------------------------- 00150 // Main routine 00151 // ---------------------------------------------------------------- 00152 00153 int main() 00154 { 00155 char sendbuf[] = "Hello Cat.M1\r\n"; 00156 00157 serialPcInit(); 00158 catm1DeviceInit(); 00159 00160 myprintf("Waiting for Cat.M1 Module Ready...\r\n"); 00161 00162 catm1DeviceReset_WM01(); 00163 00164 wait_ms(WM01_BOOTING_TIME); // WM01 booting time 00165 00166 waitCatM1Ready(); 00167 00168 wait_ms(5000); 00169 00170 myprintf("System Init Complete\r\n"); 00171 00172 printInfo(); 00173 00174 setEchoStatus_WM01(OFF); 00175 00176 getUsimStatus_WM01(); 00177 00178 getNetworkStatus_WM01(); 00179 00180 setContextActivate_WM01(); 00181 00182 // TCP Client: Send and Receive 00183 myprintf("TCP Client Start - Connect to %s:%d\r\n", dest_ip, dest_port); 00184 00185 if(sockOpenConnect_WM01(protocol, dest_ip, dest_port, packet_type) == RET_OK) 00186 { 00187 myprintf("sockOpenConnect: success\r\n"); 00188 00189 if(sendData_WM01(sendbuf, strlen(sendbuf))) 00190 { 00191 myprintf("sendData [%d] %s\r\n", strlen(sendbuf), sendbuf); 00192 } 00193 else 00194 { 00195 ; /* nothing to do */ 00196 } 00197 00198 } 00199 else 00200 { 00201 myprintf("sockOpen: failed\r\n"); 00202 00203 if(sockClose_WM01() == RET_OK) 00204 { 00205 myprintf("sockClose: success\r\n"); 00206 } 00207 else 00208 { 00209 myprintf("sockClose: failed\r\n"); 00210 } 00211 } 00212 00213 while(1) 00214 { 00215 // Data received 00216 char recvbuf[100] = {0, }; 00217 int recvlen = 0; 00218 00219 // have to modify 00220 if(recvData_WM01(recvbuf, &recvlen) == RET_OK) 00221 { 00222 myprintf("dataRecv [%d] %s\r\n", recvlen, recvbuf); 00223 00224 char *ptr = strstr(recvbuf, "exit"); 00225 00226 if(ptr != 0) 00227 { 00228 break; 00229 } 00230 else 00231 { 00232 ; /* nothing to do */ 00233 } 00234 } 00235 else 00236 { 00237 ; /* nothing to do */ 00238 } 00239 } 00240 00241 if(sockClose_WM01() == RET_OK) 00242 { 00243 myprintf("sockClose: success\r\n"); 00244 } 00245 else 00246 { 00247 myprintf("sockClose: failed\r\n"); 00248 } 00249 00250 setContextDeactivate_WM01(); 00251 } 00252 00253 // ---------------------------------------------------------------- 00254 // Functions: Print information 00255 // ---------------------------------------------------------------- 00256 00257 void printInfo(void) 00258 { 00259 myprintf("WIZnet IoT Shield for Arm MBED"); 00260 myprintf("LTE Cat.M1 Version"); 00261 myprintf("================================================="); 00262 myprintf(">> Target Board: WIoT-WM01 (Woorinet WM-N400MSE)"); 00263 myprintf(">> Sample Code: Ping Test"); 00264 myprintf("=================================================\r\n"); 00265 } 00266 00267 // ---------------------------------------------------------------- 00268 // Functions: Cat.M1 Status 00269 // ---------------------------------------------------------------- 00270 00271 void waitCatM1Ready(void) 00272 { 00273 while(1) 00274 { 00275 if(_parser->send("AT") && _parser->recv("OK")) 00276 { 00277 myprintf("WM01 is Available\r\n"); 00278 00279 return; 00280 } 00281 else 00282 { 00283 myprintf("WM01 is not Available\r\n"); 00284 00285 return; 00286 } 00287 } 00288 } 00289 00290 int8_t setEchoStatus_WM01(bool onoff) 00291 { 00292 int8_t ret = RET_NOK; 00293 char _buf[10]; 00294 00295 sprintf((char *)_buf, "ATE%d", onoff); 00296 00297 if(_parser->send(_buf) && _parser->recv("OK")) 00298 { 00299 devlog("Turn Echo %s success\r\n", onoff?"ON":"OFF"); 00300 00301 ret = RET_OK; 00302 } 00303 else 00304 { 00305 devlog("Turn Echo %s failed\r\n", onoff?"ON":"OFF"); 00306 } 00307 00308 return ret; 00309 } 00310 00311 int8_t getUsimStatus_WM01(void) 00312 { 00313 int8_t ret = RET_NOK; 00314 00315 if(_parser->send("AT$$STAT?") && _parser->recv("$$STAT:READY") && _parser->recv("OK")) 00316 { 00317 devlog("USIM Status: READY\r\n"); 00318 00319 ret = RET_OK; 00320 } 00321 else 00322 { 00323 devlog("Retrieving USIM Status failed\r\n"); 00324 } 00325 00326 return ret; 00327 } 00328 00329 int8_t getNetworkStatus_WM01(void) 00330 { 00331 int8_t ret = RET_NOK; 00332 int val, stat; 00333 00334 if(_parser->send("AT+CEREG?") && _parser->recv("+CEREG: %d,%d", &val, &stat) && _parser->recv("OK")) 00335 { 00336 if((val == 0) && (stat == 1)) 00337 { 00338 devlog("Network Status: attached\r\n"); 00339 00340 ret = RET_OK; 00341 } 00342 else 00343 { 00344 devlog("Network Status: %d, %d\r\n", val, stat); 00345 } 00346 } 00347 else 00348 { 00349 devlog("Network Status: Error\r\n"); 00350 } 00351 00352 return ret; 00353 } 00354 00355 // ---------------------------------------------------------------- 00356 // Functions: Cat.M1 PDP context activate / deactivate 00357 // ---------------------------------------------------------------- 00358 00359 int8_t setContextActivate_WM01(void) // Activate a PDP Context 00360 { 00361 int8_t ret = RET_NOK; 00362 00363 _parser->send("AT*RNDISDATA=1"); 00364 00365 if(_parser->recv("OK")) 00366 { 00367 devlog("Activate a PDP Context\r\n"); 00368 00369 ret = RET_OK; 00370 } 00371 else 00372 { 00373 devlog("PDP Context Activation failed\r\n"); 00374 } 00375 00376 return ret; 00377 } 00378 00379 int8_t setContextDeactivate_WM01(void) // Deactivate a PDP Context 00380 { 00381 int8_t ret = RET_NOK; 00382 00383 _parser->send("AT*RNDISDATA=0"); 00384 00385 if(_parser->recv("OK")) 00386 { 00387 devlog("Deactivate a PDP Context\r\n"); 00388 00389 ret = RET_OK; 00390 } 00391 else 00392 { 00393 devlog("PDP Context Deactivation failed\r\n"); 00394 } 00395 return ret; 00396 } 00397 00398 // ---------------------------------------------------------------- 00399 // Functions: TCP/UDP socket service 00400 // ---------------------------------------------------------------- 00401 00402 int8_t sockOpenConnect_WM01(int protocol, const char *addr, int port, int pckttype) 00403 { 00404 int8_t ret = RET_NOK; 00405 bool done = false; 00406 int ok = 0; // 0 : failure , 1 : success 00407 int id = 0; 00408 00409 Timer t; 00410 00411 _parser->set_timeout(WM01_CONNECT_TIMEOUT); 00412 00413 if((protocol != 0) && (protocol != 1)) // 0 : TCP, 1 : UDP 00414 { 00415 return RET_NOK; 00416 } 00417 00418 t.start(); 00419 00420 _parser->send("AT+WSOCR=%d,%s,%d,%d,%d", id, addr, port, protocol, pckttype); 00421 00422 do 00423 { 00424 done = (_parser->recv("+WSOCR:%d,%d", &ok,&id) && (ok == 1)); 00425 } while(!done && t.read_ms() < WM01_CONNECT_TIMEOUT); 00426 00427 if(done) 00428 { 00429 if(_parser->send("AT+WSOCO=%d", id) && _parser->recv("+WSOCO:%d,%d,OPEN_WAIT", &ok, &id) && _parser->recv("OK")) 00430 { 00431 if(ok == 1) 00432 { 00433 if(_parser->recv("+WSOCO:%d,OPEN_CMPL", &id)) 00434 { 00435 ret = RET_OK; 00436 } 00437 else 00438 { 00439 ; /* nothing to do */ 00440 } 00441 00442 } 00443 else 00444 { 00445 ; /* nothing to do */ 00446 } 00447 } 00448 else 00449 { 00450 ; /* nothing to do */ 00451 } 00452 } 00453 00454 _parser->set_timeout(WM01_DEFAULT_TIMEOUT); 00455 _parser->flush(); 00456 00457 return ret; 00458 } 00459 00460 int8_t sockClose_WM01(void) 00461 { 00462 int8_t ret = RET_NOK; 00463 int ok = 0; // 0 : failure , 1 : success 00464 int id = 0; 00465 00466 _parser->set_timeout(WM01_CONNECT_TIMEOUT); 00467 00468 if(_parser->send("AT+WSOCL=%d", id) && _parser->recv("+WSOCL:%d,%d,CLOSE_WAIT", &ok, &id) &&_parser->recv("OK")) 00469 { 00470 if(ok == 1) 00471 { 00472 ret = RET_OK; 00473 } 00474 else 00475 { 00476 ; /* nothing to do */ 00477 } 00478 00479 } 00480 _parser->set_timeout(WM01_DEFAULT_TIMEOUT); 00481 00482 return ret; 00483 } 00484 00485 int8_t sendData_WM01(char *data, int len) 00486 { 00487 int8_t ret = RET_NOK; 00488 bool done = false; 00489 int ok = 0; // 0 : failure , 1 : success 00490 int id = 0; 00491 00492 _parser->set_timeout(WM01_SEND_TIMEOUT); 00493 _parser->send("AT+WSOWR=%d,%d,%s", id, len, data); 00494 00495 if(_parser->recv("+WSOWR:%d,%d", &ok, &id) && _parser->recv("OK")) 00496 { 00497 if(ok == 1) // success 00498 { 00499 devlog("sendData: success\r\n"); 00500 00501 ret = RET_OK; 00502 } 00503 else // failure 00504 { 00505 devlog("sendData: failed\r\n"); 00506 } 00507 } 00508 else 00509 { 00510 ; /* nothing to do */ 00511 } 00512 00513 _parser->set_timeout(WM01_DEFAULT_TIMEOUT); 00514 00515 return ret; 00516 } 00517 00518 // have to modify 00519 int8_t recvData_WM01(char *data, int *len) 00520 { 00521 int8_t ret = RET_NOK; 00522 int id = 0; 00523 int recvCount = 0; 00524 00525 _parser->set_timeout(WM01_RECV_TIMEOUT); 00526 00527 if(_parser->recv("+WSORD:%d,%d,%s", &id, len, data)) 00528 { 00529 ret = RET_OK; 00530 } 00531 00532 _parser->set_timeout(WM01_DEFAULT_TIMEOUT); 00533 _parser->flush(); 00534 00535 return ret; 00536 }
Generated on Wed Jul 13 2022 10:14:00 by 1.7.2