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: C12832 mbed-rtos mbed websocket_demo
Fork of NNN40_CLI_HOST_WIFI by
main.cpp
- Committer:
- gordonlu
- Date:
- 2015-07-16
- Revision:
- 10:a0dc152a0060
- Parent:
- 9:999997e8e4cc
- Child:
- 11:362f6022ffef
File content as of revision 10:a0dc152a0060:
#include "mbed.h" #include "string" #include "C12832.h" //debug message #define MESSAGE_TO_LCD (1) #define MESSAGE_TO_UART (1) //measure reponse time #define MEASURE_RESPONSE_TIME (0) //UART baud rate #define BAUD_RATE_CLI (115200) #define BAUD_RATE_UART (115200) //CLI parameters #define CLI_RESPONSE_MAX_SIZE (64) #define CLI_RETRY_MAX (3) #define CLI_RESPONSE_TIMEOUT (3500000) //1 sec //version string #define VERSION_STRING ("NNN40_CLI_HOST_WIFI : 1.00") #define DEFAULT_AP_NAME "Airport123" #define DEFAULT_AP_PASSWORD "12345678" #define DEFAULT_HOST_IP "10.0.1.3" #define DEFAULT_HOST_PORT (5222) #define DEFAULT_CLI_TIMEOUT_SEC (3) //3 sec C12832 lcd(p5, p7, p6, p8, p11); DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4); Serial pc(USBTX, USBRX);//debug port , 115200 Serial cli(p9, p10); //CLI port, 115200 //CLI response buffer static char RESULT[CLI_RESPONSE_MAX_SIZE]; void message(const char * const msg, bool isClearLcd = true, int lcd_X = 0, int lcd_Y = 0) { #if(MESSAGE_TO_UART) puts(msg); #endif #if(MESSAGE_TO_LCD) if(isClearLcd)lcd.cls(); lcd.locate(lcd_X,lcd_Y); lcd.puts(msg); #endif } bool CLI_wait4Response(unsigned int timeout_sec) { bool rc = false; int i = 0; int c = 0; unsigned int to = CLI_RESPONSE_TIMEOUT * timeout_sec; #if (MEASURE_RESPONSE_TIME) int j = 0; #endif //clear buffer memset(RESULT,0,sizeof(RESULT)); #if (MEASURE_RESPONSE_TIME) while(1) #else while(--to) #endif { #if (MEASURE_RESPONSE_TIME) ++j; #endif if(cli.readable() && EOF != (c = cli.getc())) { if( c!=0x0D && c!= 0x0A) { if(i < sizeof(RESULT)) { RESULT[i++] = (char)c; to = CLI_RESPONSE_TIMEOUT; } } else if( 0 != i) //get one line response { #if (MEASURE_RESPONSE_TIME) printf("MEASURE_RESPONSE_TIME : %d\n",j); #endif break; } } } if( 0 == to) { memcpy(RESULT,"ERROR;CLI Timeout",17); } else if (RESULT[0] == 'O' && RESULT[1] == 'K') { rc = true; } return rc; } bool CLI_command(const char * cmd, unsigned int timeout_sec) { bool rc = false; message(cmd); //clear rx buffer at first while(cli.readable()) { cli.getc(); } //send CLI command cli.puts(cmd); //wait for reponse rc = CLI_wait4Response(timeout_sec); message(RESULT,false,0,11); return rc; } bool CLI_CMD(const char * cmd, bool autoRetry = true, unsigned int timeout_sec = DEFAULT_CLI_TIMEOUT_SEC) { bool rc = false; if(autoRetry) { for(int i=0; i<=CLI_RETRY_MAX; ++i) { if(CLI_command(cmd, timeout_sec)) { rc = true; break; } wait(2); //wait for a while } } else { rc = CLI_command(cmd, timeout_sec); } return rc; } bool initWifi(const char * apName = DEFAULT_AP_NAME, const char * apPassword = DEFAULT_AP_PASSWORD) { //reset module if (!CLI_CMD("cynb reset\r")) return false; //wait for reset completely wait(2); //get infomation of module if(!CLI_CMD("cynb info\r")) return false; //switch to wifi module if(!CLI_CMD("cynw device_switch 1\r")) return false; //setup AP anme and password char cmd[128]; sprintf(cmd, "cynw device_network %s %s 0\r", apName, apPassword); if(!CLI_CMD(cmd))return false; //init ethernet if(!CLI_CMD("cynw ethernet_init\r"))return false; //connecting..., no need to retry CLI_CMD("cynw ethernet_connect 40000\r",false); //wait for response if(!CLI_wait4Response(40))return false; //get mac address if(!CLI_CMD("cynw ethernet_mac\r"))return false; //get ip address if(!CLI_CMD("cynw ethernet_ip\r"))return false; return true; } bool connectToHost(const char * ip = DEFAULT_HOST_IP, unsigned int port = DEFAULT_HOST_PORT) { char cmd[64]; sprintf(cmd, "cynw tcp_connection_connect %s %d\r", ip, port); //no retry , timeout=20sec if(!CLI_CMD(cmd,false,20))return false; if(!CLI_CMD("cynw tcp_connection_is_connect\r")) return false; if(strncpy(RESULT, "OK;false", 8) == 0) return false; return true; } int main() { //set baud rate pc.baud(BAUD_RATE_UART); cli.baud(BAUD_RATE_CLI); //show version string message(VERSION_STRING); #if(MESSAGE_TO_LCD) //give some display time for lcd wait(1); #endif initWifi: //init wifi module while(!initWifi()); connectToHost: //connect to host bool isConnectToHost = false; for(int i=0; i<=CLI_RETRY_MAX; ++i) { if(connectToHost()) { isConnectToHost = true; break; } wait(1); } if(isConnectToHost) { int i = 0; char cmd[64]; while(1) { sprintf(cmd, "cynw tcp_connection_send test-%d\r",i++); if(!CLI_CMD(cmd)) { if(CLI_CMD("cynw tcp_connection_is_connect\r")) { if(strncmp(RESULT, "OK;true", 7) != 0) //disconnect { goto connectToHost; } } } wait(1); } } else { goto initWifi; } while(1); }