This program opens a socket and wait connection through Wi-Fi. When the socket is connected, print out received characters to LCD.

Dependencies:   TextLCD mbed

Files at this revision

API Documentation at this revision

Comitter:
nakata
Date:
Wed Oct 24 10:16:52 2012 +0000
Parent:
1:e87727c8979d
Commit message:
Interface public version

Changed in this revision

TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
WifiTerminalMode.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show diff for this revision Revisions of this file
private.h Show annotated file Show diff for this revision Revisions of this file
wifi.h Show diff for this revision Revisions of this file
diff -r e87727c8979d -r f5754fb90f07 TextLCD.lib
--- a/TextLCD.lib	Sun Jun 03 12:24:13 2012 +0000
+++ b/TextLCD.lib	Wed Oct 24 10:16:52 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/carlos_nascimento08/libraries/TextLCD/m7o9zd
\ No newline at end of file
+http://mbed.org/users/carlos_nascimento08/code/TextLCD/#b8a17b39cd0d
diff -r e87727c8979d -r f5754fb90f07 WifiTerminalMode.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WifiTerminalMode.h	Wed Oct 24 10:16:52 2012 +0000
@@ -0,0 +1,260 @@
+#include "private.h"
+#include <string.h>
+
+/*
+ * common constants
+ */
+#define BAUD_RATE 115200
+//#define BAUD_RATE 9600
+#define CR  13
+#define LF  10
+#define READ_BUFF_LEN       4096
+#define IPADDRESS_LENGTH    16
+#define PORT_NUM    "1080"
+
+/*
+ * mbed pin settings
+ */
+Serial wifi(p13, p14);
+DigitalOut PRST(p15);
+
+class WifiTerminalMode {
+private:
+    unsigned char buff[READ_BUFF_LEN];
+    unsigned char myIPAddress[IPADDRESS_LENGTH];
+
+private:
+    void putc(int c)
+    {
+        while (!wifi.writeable())
+            ;   // empty loop body
+        wifi.putc(c);
+    }
+
+    int getc()
+    {
+        return wifi.getc();
+    }
+
+    void delayedPutc(unsigned char c)
+    {
+        wait(0.00032);
+        putc(c);
+    }
+
+    void write(const unsigned char *data)
+    {
+        const unsigned char *p;
+        for ( p = data; *p != '\0'; p++ ) {
+            delayedPutc(*p);
+        }
+    }
+
+    int readToPrompt()
+    {
+        int len;
+        unsigned char *p = buff;
+        for ( len = 0; len < READ_BUFF_LEN - 1; len++, p++ ) {
+            *p = getc();
+            if ( *p == '#' ) {
+                break;
+            }
+        }
+        *p = '\0';
+
+        return len;
+    }
+
+    void waitPrompt()
+    {
+        while (getc() != '#')
+            ;   // empty loop body
+    }
+
+public:
+    unsigned char *readBlock()
+    {
+        int len;
+        unsigned char *p = buff;
+        for ( len = 0; len < READ_BUFF_LEN - 1; len++ ) {
+            *p++ = getc();
+            if ( !wifi.readable() ) {
+                break;
+            }
+        }
+        *p = '\0';
+
+        return buff;
+    }
+
+    unsigned char *readLine()
+    {
+        int len;
+        unsigned char *p = buff;
+        
+        for ( len = 0; len < READ_BUFF_LEN - 1; len++, p++ ) {
+            *p = getc();
+            if ( *p == CR || *p == LF ) {
+                p++;
+                break;
+            }
+        }
+        *p = '\0';
+
+        return buff;
+    }
+private:
+    int command(const unsigned char *data)
+    {
+        int len = 0;
+        unsigned char *p2 = buff;
+        const unsigned char *p;
+    
+        for ( p = data; *p != '\0'; p++ ) {
+            while (wifi.readable()) {
+                *p2++ = getc();
+                len++;
+            }
+            delayedPutc(*p);
+        }
+        for (; len < READ_BUFF_LEN - 1; len++, p2++ ) {
+            *p2 = getc();
+            if ( *p2 == '#') {
+                break;
+            }
+        }
+        *p2 = '\0';
+
+        return len;
+    }
+
+public:
+    void reset()
+    {
+        wifi.baud(BAUD_RATE);
+        wifi.format(8, Serial::None, 1);
+    
+        PRST = 0;       // activate reset line
+        wait(1.0);    // perhaps needs 1 sec
+        PRST = 1;       // deactivate reset line
+    }
+
+    void serialInit()
+    {
+        int i;
+        int c = 0;
+
+        while (true) {
+            if (wifi.writeable())
+                putc('A');
+            if (wifi.readable()) {
+                if ( c == '*' ) {
+                    c = getc();
+                    if ( c == CR ) {
+                        break;
+                    }
+                } else {
+                    c = getc();
+                }
+            }
+            wait(0.00032);  // this wait is important
+        }
+        // flush buffer
+        while (wifi.readable())
+            getc();
+        // change to config mode
+        for ( i = 0; i < 8; i++ ) {
+            delayedPutc(' ');
+        }
+        c = 0;
+        while (true) {
+            if (wifi.readable()) {
+                if ( c == '*' ) {
+                    c = getc();
+                    if ( c == CR ) {
+                        break;
+                    }
+                } else {
+                    c = getc();
+                }
+            }
+        }
+    }
+
+    void portSetup()
+    {
+        command("wlan_type set infra\r");
+        command("wlan_ssid set " SSID "\r");
+        command("wlan_wps set stop\r");
+        command("wlan_crdl set off\r");
+#ifdef  WEP40_KEY
+        command("wlan_sec set wep40\r");
+        command("wlan_wep set " WEP40_KEY "\r");
+#endif
+#ifdef  WEP104_KEY
+        command("wlan_sec set wep104\r");
+        command("wlan_wep set " WEP104_KEY "\r");
+#endif
+#ifdef  TKIP_KEY
+        command("wlan_sec set wpa-tkip\r");
+        command("wlan_psk set " TKIP_KEY "\r");
+#endif
+#ifdef  AES_KEY
+        command("wlan_sec set wpa2-aes\r");
+        command("wlan_psk set " AES_KEY "\r");
+#endif
+#ifdef  MIX_KEY
+        command("wlan_sec set wpa-mix\r");
+        command("wlan_psk set " MIX_KEY "\r");
+#endif
+        command("ip_dhcp set on\r");
+        command("ip_term_prot set tcps\r");
+        command("ip_term_hp set " PORT_NUM "\r");
+        command("save permit\r");
+    }
+    
+    unsigned char *getAddr()
+    {
+        int len;
+        int i = 0;
+        const char *p;
+    
+        waitPrompt();
+        while (true) {
+            len = command("wlan_con get\r");
+            if (len > 0 && strstr((const char *)buff, (const char *)"= Connect") != 0 ) {
+                break;
+            }
+            setLeds(4 | i);
+            i ^= 1;
+            wait(1.0);
+        }
+        do {
+            setLeds(6 | i);
+            i ^= 1;
+            wait(1.0);
+            len = command("ip_current get\r");
+            if ( len <= 0 ) {
+                p = (const char *)buff;
+                buff[0] = '\0';
+                continue;
+            }
+            p = strstr((const char *)buff, (const char *)"IP");
+            if ( p == NULL )
+                continue;
+            p = strstr(p, (const char *)" = ");
+            if ( p == NULL )
+                continue;
+        } while ( strncmp(p, " = 0.0.0.0", 10) == 0 );
+        p += 3;
+        for ( i = 0; i < IPADDRESS_LENGTH - 1; i++, p++ ) {
+            if ( *p == '\r' )
+                break;
+            myIPAddress[i] = *p;
+        }
+        myIPAddress[i] = '\0';
+        command("run permit\r");
+    
+        return myIPAddress;
+    }
+};
\ No newline at end of file
diff -r e87727c8979d -r f5754fb90f07 main.cpp
--- a/main.cpp	Sun Jun 03 12:24:13 2012 +0000
+++ b/main.cpp	Wed Oct 24 10:16:52 2012 +0000
@@ -1,6 +1,9 @@
 #include "mbed.h"
 #include "LcdScreen.h"
 
+static void setLeds(int data);
+#include "WifiTerminalMode.h"
+
 DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
 DigitalOut myled3(LED3);
@@ -13,30 +16,32 @@
     myled4 = (data & 1);
 }
 
-#include "wifi.h"
-
 int main() {
     int i = 0;
     unsigned char *p;
     LcdScreen *ls = new LcdScreen();
+    WifiTerminalMode *terminal = new WifiTerminalMode();
 
-    wifiReset();
-    wifiSerialInit();
-    wifiPortSetup();
+    // 1st time set up parameters
+    ls->print("setting parameters\r\n");
+    terminal->reset();
+    terminal->serialInit();
+    terminal->portSetup();
     setLeds(1);
-    wifiReset();
+    // 2nd time do connect
+    ls->print("connecting\r\n");
+    terminal->reset();
     setLeds(2);
-    wifiSerialInit();
+    terminal->serialInit();
     setLeds(3);
-    p = wifiGetAddr();
+    p = terminal->getAddr();
     ls->print(p);
     ls->print((const unsigned char *)"\r\n");
     setLeds(8);
-    wifiReadBlock();
     while (true) {
         setLeds(8 | i);
         i ^= 1;
-        p = wifiReadLine();
+        p = terminal->readLine();
         ls->print(p);
     }
 }
diff -r e87727c8979d -r f5754fb90f07 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Oct 24 10:16:52 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
\ No newline at end of file
diff -r e87727c8979d -r f5754fb90f07 mbed.lib
--- a/mbed.lib	Sun Jun 03 12:24:13 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/projects/libraries/svn/mbed/trunk@43
\ No newline at end of file
diff -r e87727c8979d -r f5754fb90f07 private.h
--- a/private.h	Sun Jun 03 12:24:13 2012 +0000
+++ b/private.h	Wed Oct 24 10:16:52 2012 +0000
@@ -2,10 +2,10 @@
  * personal setting
  * please set your personal environment
  */
-#define SSID    "0007406A33DE"
+#define SSID    "testnet"
 
 //#define WEP40_KEY ""
 //#define WEP104_KEY ""
-//#define AES_KEY ""
-#define TKIP_KEY "osakanakuwaetadoraneko"
+#define AES_KEY "0000000000"
+//#define TKIP_KEY ""
 //#define MIX_KEY ""
diff -r e87727c8979d -r f5754fb90f07 wifi.h
--- a/wifi.h	Sun Jun 03 12:24:13 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,271 +0,0 @@
-#include "private.h"
-#include <string.h>
-
-/*
- * common constants
- */
-#define BAUD_RATE 115200
-//#define BAUD_RATE 9600
-#define CR  13
-#define LF  10
-#define READ_BUFF_LEN       4096
-#define IPADDRESS_LENGTH    16
-#define PORT_NUM    "1080"
-
-/*
- * mbed pin settings
- */
-Serial wifi(p13, p14);
-DigitalOut PRST(p15);
-
-/*
- * program starts here
- */
-void wifiPutc(int c)
-{
-    while (!wifi.writeable())
-        ;   // empty loop body
-    wifi.putc(c);
-}
-
-int wifiGetc()
-{
-    return wifi.getc();
-}
-
-void wifiDelayedPutc(unsigned char c)
-{
-    wifiPutc(c);
-    wait(0.00032);
-}
-
-static int sequence = 0;
-void wifiWrite(const unsigned char *data)
-{
-    const unsigned char *p;
-    for ( p = data; *p != '\0'; p++ ) {
-        wifiDelayedPutc(*p);
-    }
-}
-
-static unsigned char buff[READ_BUFF_LEN];
-int wifiReadToPrompt()
-{
-    int len;
-    unsigned char *p = buff;
-    for ( len = 0; len < READ_BUFF_LEN - 1; len++, p++ ) {
-        *p = wifiGetc();
-        if ( *p == '#' ) {
-            break;
-        }
-    }
-    *p = '\0';
-
-    return len;
-}
-
-unsigned char *wifiReadBlock()
-{
-    int len;
-    unsigned char *p = buff;
-    for ( len = 0; len < READ_BUFF_LEN - 1; len++, p++ ) {
-        *p = wifiGetc();
-        if ( !wifi.readable() ) {
-            p++;
-            break;
-        }
-    }
-    *p = '\0';
-
-    return buff;
-}
-    
-
-unsigned char *wifiReadLine()
-{
-    int len;
-    unsigned char *p = buff;
-    for ( len = 0; len < READ_BUFF_LEN - 1; len++, p++ ) {
-        *p = wifiGetc();
-        if ( *p == CR || *p == LF ) {
-            p++;
-            break;
-        }
-    }
-    *p = '\0';
-
-    return buff;
-}
-
-int wifiCommand2(const unsigned char *data)
-{
-    int len = 0;
-    unsigned char *p2 = buff;
-    const unsigned char *p;
-    
-    for ( p = data; *p != '\0'; p++ ) {
-        while (wifi.readable()) {
-            *p2++ = wifiGetc();
-            len++;
-        }
-        wifiDelayedPutc(*p);
-    }
-    for (; len < READ_BUFF_LEN - 1; len++, p2++ ) {
-        *p2 = wifiGetc();
-        if ( *p2 == CR || *p2 == LF ) {
-            break;
-        }
-    }
-    *p2 = '\0';
-
-    return len;
-}
-
-void wifiWaitPrompt();
-int wifiCommand(const unsigned char *data)
-{
-    wifiWaitPrompt();
-    return wifiCommand2(data);
-}
-
-void wifiReset()
-{
-    wifi.baud(BAUD_RATE);
-    wifi.format(8, Serial::None, 1);
-    
-    sequence = 0;
-    PRST = 0;       // activate reset line
-    wait(1.0);    // perhaps needs 1 sec
-    PRST = 1;       // deactivate reset line
-}
-
-void wifiSerialInit()
-{
-    int i;
-    int c = 0;
-    //int oldc = 0;
-
-    while (true) {
-        if (wifi.writeable())
-            wifiPutc('A');
-        if (wifi.readable()) {
-            if ( c == '*' ) {
-                c = wifiGetc();
-                if ( c == CR ) {
-                    break;
-                }
-            } else {
-                c = wifiGetc();
-            }
-        }
-        wait(0.00032);  // this wait is important
-    }
-    // flush buffer
-    while (wifi.readable())
-        wifiGetc();
-    // change to config mode
-    for ( i = 0; i < 8; i++ ) {
-        wifiDelayedPutc(' ');
-    }
-    c = 0;
-    while (true) {
-        if (wifi.readable()) {
-            if ( c == '*' ) {
-                c = wifiGetc();
-                if ( c == CR ) {
-                    break;
-                }
-            } else {
-                c = wifiGetc();
-            }
-        }
-    }
-}
-
-void wifiWaitPrompt()
-{
-    while (wifiGetc() != '#')
-        ;   // empty loop body
-}
-
-unsigned char myIPAddress[IPADDRESS_LENGTH];
-void wifiPortSetup()
-{
-    wifiCommand("wlan_type set infra\r");
-    wifiCommand("wlan_ssid set " SSID "\r");
-    wifiCommand("wlan_wps set stop\r");
-    wifiCommand("wlan_crdl set off\r");
-#ifdef  WEP40_KEY
-    wifiCommand("wlan_sec set wep40\r");
-    wifiCommand("wlan_wep set " WEP40_KEY "\r");
-#endif
-#ifdef  WEP104_KEY
-    wifiCommand("wlan_sec set wep104\r");
-    wifiCommand("wlan_wep set " WEP104_KEY "\r");
-#endif
-#ifdef  TKIP_KEY
-    wifiCommand("wlan_sec set wpa-tkip\r");
-    wifiCommand("wlan_psk set " TKIP_KEY "\r");
-#endif
-#ifdef  AES_KEY
-    wifiCommand("wlan_sec set wpa2-aes\r");
-    wifiCommand("wlan_psk set " AES_KEY "\r");
-#endif
-#ifdef  MIX_KEY
-    wifiCommand("wlan_sec set wpa-mix\r");
-    wifiCommand("wlan_psk set " MIX_KEY "\r");
-#endif
-    wifiCommand("ip_dhcp set on\r");
-    wifiCommand("ip_term_prot set tcps\r");
-    wifiCommand("ip_term_hp set " PORT_NUM "\r");
-    wifiCommand("save permit\r");
-    wifiWaitPrompt();
-}
-    
-unsigned char *wifiGetAddr()
-{
-    int len;
-    int i = 0;
-    const char *p;
-    
-    wifiWaitPrompt();
-    while (true) {
-        wifiCommand2("wlan_con get\r");
-        len = wifiReadToPrompt();
-
-        if (len > 0 && strstr((const char *)buff, (const char *)"= Connect") != 0 ) {
-            break;
-        }
-        setLeds(4 | i);
-        i ^= 1;
-        wait(1.0);
-    }
-    do {
-        setLeds(6 | i);
-        i ^= 1;
-        wait(1.0);
-        wifiCommand2("ip_current get\r");
-        len = wifiReadToPrompt();
-        if ( len <= 0 ) {
-            p = (const char *)buff;
-            buff[0] = '\0';
-            continue;
-        }
-        p = strstr((const char *)buff, (const char *)"IP");
-        if ( p == NULL )
-            continue;
-        p = strstr(p, (const char *)" = ");
-        if ( p == NULL )
-            continue;
-    } while ( strncmp(p, " = 0.0.0.0", 10) == 0 );
-    p += 3;
-    for ( i = 0; i < IPADDRESS_LENGTH; i++, p++ ) {
-        if ( *p == '\r' )
-            break;
-        myIPAddress[i] = *p;
-    }
-    myIPAddress[i] = '\0';
-    wifiCommand2("run permit\r");
-    
-    return myIPAddress;
-}