diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 59bbad9b8..cb433d2bc 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -2,9 +2,12 @@ #include "WiFi.h" #include "wiring.h" +#define _DEBUG_ + extern "C" { #include "utility/wl_definitions.h" #include "utility/wl_types.h" + #include "debug.h" } // XXX: don't make assumptions about the value of MAX_SOCK_NUM. @@ -19,7 +22,6 @@ wl_status_t WiFiClass::_status; WiFiClass::WiFiClass() { - init(); } void WiFiClass::init() @@ -41,42 +43,66 @@ uint8_t WiFiClass::getSocket() int WiFiClass::begin() { + init(); } int WiFiClass::begin(char* ssid) { + uint8_t status = WL_IDLE_STATUS; + init(); + if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE) { - delay(WL_DELAY_START_CONNECTION); - return WiFiDrv::getConnectionStatus(); + + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); }else { - return WL_CONNECT_FAILED; + status = WL_CONNECT_FAILED; } + return status; } int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) { + uint8_t status = WL_IDLE_STATUS; + init(); // set encryption key if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) { - delay(WL_DELAY_START_CONNECTION); - return WiFiDrv::getConnectionStatus(); + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); }else{ - return WL_CONNECT_FAILED; + status = WL_CONNECT_FAILED; } + return status; } int WiFiClass::begin(char* ssid, const char *passphrase) { + uint8_t status = WL_IDLE_STATUS; + init(); // set passphrase if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) { - delay(WL_DELAY_START_CONNECTION); - return WiFiDrv::getConnectionStatus(); + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); }else{ - return WL_CONNECT_FAILED; + status = WL_CONNECT_FAILED; } + return status; } int WiFiClass::disconnect() @@ -86,7 +112,8 @@ int WiFiClass::disconnect() uint8_t* WiFiClass::macAddress(uint8_t* mac) { - mac = WiFiDrv::getMacAddress(); + uint8_t* _mac = WiFiDrv::getMacAddress(); + memcpy(mac, _mac, WL_MAC_ADDR_LENGTH); return mac; } @@ -118,7 +145,8 @@ char* WiFiClass::SSID() uint8_t* WiFiClass::BSSID(uint8_t* bssid) { - bssid = WiFiDrv::getCurrentBSSID(); + uint8_t* _bssid = WiFiDrv::getCurrentBSSID(); + memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH); return bssid; } @@ -153,4 +181,10 @@ uint8_t WiFiClass::encryptionType(uint8_t networkItem) return WiFiDrv::getEncTypeNetowrks(networkItem); } +uint8_t WiFiClass::status() +{ + return WiFiDrv::getConnectionStatus(); +} + + WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 3a97ffcec..d97209b63 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -83,6 +83,9 @@ public: // Return the current RSSI /Received Signal Strength in dBm) associated with the network identified with networkItem int32_t RSSI(uint8_t networkItem); + // Return Connection status + uint8_t status(); + friend class Client; friend class Server; }; diff --git a/WiFi/examples/wifi_Server_example/wifi_Server_example.pde b/WiFi/examples/wifi_Server_example/wifi_Server_example.pde index 2d8166e6c..2a88a5617 100644 --- a/WiFi/examples/wifi_Server_example/wifi_Server_example.pde +++ b/WiFi/examples/wifi_Server_example/wifi_Server_example.pde @@ -94,7 +94,7 @@ void setup() { Serial.begin(9600); Serial.println("Setup WiFi..."); - char ssid[] = "Cariddi"; + strcpy(ssid, "Vodafone-10289870"); Serial.println(ssid); int status = WiFi.begin(ssid); if ( status != WL_CONNECTED) @@ -131,6 +131,8 @@ void setup() void loop() { static uint8_t count = 0; + Serial.println("Retry connect..."); + status = WiFi.begin(ssid); if (status == WL_CONNECTED) { byte status = 0; diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde new file mode 100644 index 000000000..557c5039c --- /dev/null +++ b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde @@ -0,0 +1,172 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); +} + +void scanNetworks() +{ + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + //Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } + */ +} + + diff --git a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde new file mode 100644 index 000000000..498cf1191 --- /dev/null +++ b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde @@ -0,0 +1,174 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); +} + +void scanNetworks() +{ + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + //Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } + */ +} + + diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/wifi_example/wifi_example.pde index c86df452e..6606c2b1f 100755 --- a/WiFi/examples/wifi_example/wifi_example.pde +++ b/WiFi/examples/wifi_example/wifi_example.pde @@ -1,13 +1,13 @@ /* - WiFi example + WiFi Server example A simple connection with WiFi AP with Wireless Security information try to access with WPA or WEP security keys + A simple server is setup to exchange data. created 13 July 2010 by Domenico La Fauci */ - #include #include @@ -15,52 +15,32 @@ byte mac[6] = { 0 }; IPAddress ip; IPAddress gateway; IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + +Server server(23); -void setup() +void printIpData() { - Serial.begin(9600); - Serial.println("Setup WiFi..."); - char ssid[] = "Cariddi"; - Serial.println(ssid); - int status = WiFi.begin(ssid); - if ( status != WL_CONNECTED) - { - // Using WPA - Serial.println("Trying with Passphrase..."); - const char *pass = "12345678"; - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) - { - // using WEP - Serial.println("Trying with Key..."); - uint8_t key_idx = 0; - const char *key = "12345678"; - status = WiFi.begin(ssid,key_idx, key); - if ( status != WL_CONNECTED) - { - Serial.println("Wifi Connection failed!"); - return; - } - } - } - Serial.println("Wifi Connected!"); - ip = WiFi.localIp(); - + Serial.print("IP: "); Serial.print(ip[3],10);Serial.print("."); Serial.print(ip[2],10);Serial.print("."); Serial.print(ip[1],10);Serial.print("."); Serial.println(ip[0],10); - + subnet = WiFi.subnetMask(); Serial.print("NETMASK: "); Serial.print(subnet[3],10);Serial.print("."); Serial.print(subnet[2],10);Serial.print("."); Serial.print(subnet[1],10);Serial.print("."); Serial.println(subnet[0],10); - + gateway = WiFi.gatewayIP(); Serial.print("GATEWAY: "); Serial.print(gateway[3],10);Serial.print("."); @@ -78,7 +58,114 @@ void setup() Serial.println(mac[0],16); } -void loop() +void printCurrNet() { + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); } +void scanNetworks() +{ + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + //Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } + */ +} + + diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 1f1296686..7647417c6 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -13,6 +13,8 @@ #include #include +#include "HardwareSerial.h" + #define INFO_0 1 #define INFO_1 2 #define INFO_2 4 @@ -30,13 +32,13 @@ #ifdef _DEBUG_ -#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-W-");\ +#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-I-");\ Serial.println(x); \ }while (0); #define INFO2(x,y) do { PRINT_FILE_LINE() Serial.print("-I-");\ - Serial.print(x);Serial.print(",");Serial.println(y); \ + Serial.print(x,16);Serial.print(",");Serial.println(y,16); \ }while (0); @@ -49,7 +51,7 @@ Serial.print("-W-"); Serial.println(args); \ }while (0); -#define DBG_PIN2 3 +#define DBG_PIN2 5 #define DBG_PIN 4 #define START() digitalWrite(DBG_PIN2, HIGH); diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index d88703b88..6de08b207 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -19,7 +19,7 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) SpiDrv::sendParam(&sock, 1, LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -40,7 +40,7 @@ uint8_t ServerDrv::getState(uint8_t sock) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -64,7 +64,7 @@ uint8_t ServerDrv::availData(uint8_t sock) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -92,7 +92,7 @@ bool ServerDrv::getData(uint8_t sock, uint8_t *data) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -118,7 +118,7 @@ bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply if (!SpiDrv::waitResponse(GET_DATABUF_TCP_CMD, _data, _dataLen)) @@ -143,7 +143,7 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -169,7 +169,7 @@ uint8_t ServerDrv::isDataSent(uint8_t sock) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index 86af01a0e..e2a487d80 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -1,7 +1,8 @@ #include "WProgram.h" #include "spi_drv.h" - +#include "pins_arduino.h" +#define _DEBUG_ extern "C" { #include "debug.h" } @@ -9,31 +10,40 @@ extern "C" { #define DATAOUT 11//MOSI #define DATAIN 12//MISO #define SPICLOCK 13//sck -#define SLAVESELECT 10//ss +#define SLAVESELECT 2//ss +#define SLAVEREADY 3 -void SpiDrv::spiSetup() +void SpiDrv::begin() { - int clr = 0; - pinMode(DATAOUT, OUTPUT); - pinMode(DATAIN, INPUT); - pinMode(SPICLOCK,OUTPUT); - pinMode(SLAVESELECT,OUTPUT); - digitalWrite(SLAVESELECT,HIGH); //disable device - // SPCR = 01010000 - //interrupt disabled,spi enabled,msb 1st,master,clk low when idle, - //sample on leading edge of clk,system clock/4 rate (fastest) - SPCR = (1< maxNumParams) + { + numParam = maxNumParams; + } + *numParamRead = numParam; + if (numParam != 0) + { + for (i=0; i