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
Revision 10:a0dc152a0060, committed 2015-07-16
- Comitter:
- gordonlu
- Date:
- Thu Jul 16 09:49:58 2015 +0000
- Parent:
- 9:999997e8e4cc
- Child:
- 11:362f6022ffef
- Commit message:
- Version 1.0.0
Changed in this revision
--- a/EthernetInterface.lib Thu Feb 12 01:57:25 2015 +0000 +++ b/EthernetInterface.lib Thu Jul 16 09:49:58 2015 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#d1ccbed7687a +http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
--- a/main.cpp Thu Feb 12 01:57:25 2015 +0000
+++ b/main.cpp Thu Jul 16 09:49:58 2015 +0000
@@ -1,193 +1,284 @@
#include "mbed.h"
-#include "rtos.h"
+#include "string"
#include "C12832.h"
-#include "EthernetInterface.h"
-#include "math.h"
-#include "Websocket.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)
-#define POT_SENSITIVITY (0.01f)
-#define WS_CONNECT_RETRY (20)
-#define WS_SEND_RETRY (5)
-
+//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);
-PwmOut r (p23);
-PwmOut g (p24);
-PwmOut b (p25);
-AnalogIn pot1(p19);
-AnalogIn pot2(p20);
-BusIn joystick(p12, p13, p14, p15, p16);
+
+
+Serial pc(USBTX, USBRX);//debug port , 115200
+Serial cli(p9, p10); //CLI port, 115200
+
+//CLI response buffer
+static char RESULT[CLI_RESPONSE_MAX_SIZE];
+
-Mutex lcd_mutex;
-Mutex second_mutex;
-
-static unsigned int second = 0;
-static float pt1 = 0;
-static float pt2 = 0;
-static int js = 0xFF;
+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
+}
-void potJoystickThread(void const *args)
-{
-
+bool CLI_wait4Response(unsigned int timeout_sec)
+{
- printf("PotJoysticThread Start\r\n");
-
- while(1)
- {
+ bool rc = false;
+ int i = 0;
+ int c = 0;
+ unsigned int to = CLI_RESPONSE_TIMEOUT * timeout_sec;
- float p1 = pot1.read();
- float p2 = pot2.read();
- int j = joystick.read();
-
- if(fabs(p1 - pt1) > POT_SENSITIVITY || fabs(p2 - pt2) > POT_SENSITIVITY || j != js)
- {
- pt1 = p1;
- pt2 = p2;
- js = j;
-
- r = 1 - pt1;
- g = 1 - pt2;
-
- if(0x10 == js)b = 0; //right, turn on blue led
- else if (0x02 == js) b = 1; //left, turn of blue led
- else if(0x01 == js) b.write( b + 0.05); //down
- else if(0x08 == js) b.write( b - 0.05); //up
-
- //printf("Pot1 : %.2f Pot2 : %.2f Joystick:%02X\r\n", pt1, pt2, js);
+ #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
- //lcd display
- lcd_mutex.lock();
- lcd.locate(0,11);
- lcd.printf("Pot1 : %.2f Pot2 : %.2f JS:%02X", pt1, pt2, js);
- lcd_mutex.unlock();
- }
-
- Thread::wait(100);
+ break;
+ }
+ }
}
+ if( 0 == to)
+ {
+ memcpy(RESULT,"ERROR;CLI Timeout",17);
+ }
+ else if (RESULT[0] == 'O' && RESULT[1] == 'K')
+ {
+ rc = true;
+ }
+
+ return rc;
}
-void netThread(void const *args)
+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 is_eth_connected = false;
- printf("EthernetInterface Thread Start\r\n");
- EthernetInterface eth;
- eth.init(); //Use DHCP
- printf("MAC:%s\r\n",eth.getMACAddress());
- lcd_mutex.lock();
- lcd.locate(0,21);
- lcd.printf("MAC:%s\r\n",eth.getMACAddress());
- lcd_mutex.unlock();
- printf("Connecting...\r\n");
-
- while(1)
- {
- if(0 != eth.connect())
- {
- printf("Connecting fail! Try again. \r\nConnecting....\r\n");
- if(is_eth_connected)
- {
- if(0 == eth.disconnect())
- {
- printf("Disconnect OK\r\n");
- }
- else
- {
- printf("Disconnect error\r\n");
- }
- }
- is_eth_connected = false;
- Thread::wait(100);
- }
- else
+ bool rc = false;
+
+ if(autoRetry)
+ {
+ for(int i=0; i<=CLI_RETRY_MAX; ++i)
{
- printf("Connect OK! IP Address is %s\r\n", eth.getIPAddress());
- lcd_mutex.lock();
- lcd.locate(0,21);
- lcd.printf("IP ADDRESS : %s\r\n",eth.getIPAddress());
- lcd_mutex.unlock();
- is_eth_connected = true;
-
- }
- if(is_eth_connected)
- {
- Websocket ws("ws://sockets.mbed.org:443/ws/gordonlu/wo");
- int i = 0;
- while(++i < WS_CONNECT_RETRY)
+ if(CLI_command(cmd, timeout_sec))
{
- printf("[%d] WebSwerivce connect to ws://sockets.mbed.org:443/ws/gordonlu/wo \r\n",i);
- if(ws.connect())break;
- Thread::wait(1000);
- }
-
- int errCnt = 0;
- while (ws.is_connected())
- {
-
- char buf[100];
- int scnt = sprintf(buf, "[%u] Hello! Pot1 : %.2f, Pot2 : %.2f, Joystick:%02X\r\n", second, pt1, pt2, js);
- if((ws.send(buf)) >= scnt)
- {
- printf(buf);
- errCnt = 0;
- }
- else
- {
- printf("[%d] ws.send failed!!\r\n",++errCnt);
- }
-
- if(errCnt > WS_SEND_RETRY)
- {
- printf("close ws and disconnect eth, retry to connect again!\r\n");
- ws.close();
- break;
- }
-
- Thread::wait(1000);
- }
- }
- }
+ rc = true;
+ break;
+ }
+
+ wait(2); //wait for a while
+ }
+ }
+ else
+ {
+ rc = CLI_command(cmd, timeout_sec);
+ }
+
+ return rc;
}
-void secondCounterThread(void const *args)
+bool initWifi(const char * apName = DEFAULT_AP_NAME, const char * apPassword = DEFAULT_AP_PASSWORD)
{
- while(1)
- {
- second_mutex.lock();
- ++second;
- second_mutex.unlock();
- myled1 != myled1;
- Thread::wait(1000);
- }
+ //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()
-{
-
- lcd.cls();
-
- r.period(0.001);
- g.period(0.001);
- b.period(0.001);
-
- r = 1;
- g = 1;
- b = 1;
+{
+ //set baud rate
+ pc.baud(BAUD_RATE_UART);
+ cli.baud(BAUD_RATE_CLI);
+
+ //show version string
+ message(VERSION_STRING);
- Thread t1(netThread);
- Thread t2(potJoystickThread);
- Thread t3(secondCounterThread);
-
- while(true)
- {
- lcd_mutex.lock();
- lcd.locate(0,1);
- lcd.printf("Second : %06d ",second);
- lcd_mutex.unlock();
- Thread::wait(1000);
+ #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);
+
+
}
\ No newline at end of file
--- a/mbed-rtos.lib Thu Feb 12 01:57:25 2015 +0000 +++ b/mbed-rtos.lib Thu Jul 16 09:49:58 2015 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/mbed_official/code/mbed-rtos/#5448826aa700 +https://mbed.org/users/mbed_official/code/mbed-rtos/#a21475017ae2
--- a/mbed.bld Thu Feb 12 01:57:25 2015 +0000 +++ b/mbed.bld Thu Jul 16 09:49:58 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/da0ca467f8b5 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/websocket_demo.lib Thu Jul 16 09:49:58 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/gordonlu/code/websocket_demo/#999997e8e4cc
