This program opens a socket and wait connection through Wi-Fi. When the socket is connected, print out received characters to LCD.
Diff: wifi.h
- Revision:
- 2:f5754fb90f07
- Parent:
- 1:e87727c8979d
--- 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; -}