From b6490ad458a522a6d1237e52a9a8bb23f210b0fa Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Thu, 24 May 2012 08:57:46 +0200 Subject: [PATCH 01/20] Fix ScanNetworks list --- WiFi/WiFi.cpp | 12 +++++++++++- WiFi/utility/spi_drv.cpp | 1 + WiFi/utility/wifi_drv.cpp | 23 ++++++++++++++++++++++- WiFi/utility/wifi_drv.h | 13 ++++++++++--- WiFi/utility/wifi_spi.h | 1 + 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 8bb66f215..49f7e3958 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -152,7 +152,17 @@ uint8_t WiFiClass::encryptionType() uint8_t WiFiClass::scanNetworks() { - return WiFiDrv::scanNetworks(); + uint8_t attempts = 10; + uint8_t numOfNetworks = 0; + + WiFiDrv::startScanNetworks(); + do + { + delay(2000); + numOfNetworks = WiFiDrv::getScanNetworks(); + } + while (( numOfNetworks == 0)&&(--attempts>0)); + return numOfNetworks; } char* WiFiClass::SSID(uint8_t networkItem) diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index aaa596fcf..8069b024d 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -399,6 +399,7 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, u } else { WARN("Error numParams == 0"); + readAndCheckChar(END_CMD, &_data); return 0; } readAndCheckChar(END_CMD, &_data); diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 180633852..d8785c9c4 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -299,7 +299,28 @@ uint8_t WiFiDrv::getCurrentEncryptionType() return encType; } -uint8_t WiFiDrv::scanNetworks() +uint8_t WiFiDrv::startScanNetworks() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return result; +} + + +uint8_t WiFiDrv::getScanNetworks() { WAIT_FOR_SLAVE_SELECT(); diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 056e6126a..0b6476f00 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -38,7 +38,7 @@ private: public: /* - * Driver initialization + * Driver initialization */ static void wifiDriverInit(); @@ -82,7 +82,7 @@ public: /* * Disconnect from the network * - * return: WL_SUCCESS or WL_FAILURE + * return: WL_SUCCESS or WL_FAILURE */ static uint8_t disconnect(); @@ -156,7 +156,14 @@ public: * * return: Number of discovered networks */ - static uint8_t scanNetworks(); + static uint8_t startScanNetworks(); + + /* + * Get the networks available + * + * return: Number of discovered networks + */ + static uint8_t getScanNetworks(); /* * Return the SSID discovered during the network scan. diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 01e1acd0b..cd3fab7ce 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -47,6 +47,7 @@ enum { GET_IDX_ENCT_CMD = 0x33, REQ_HOST_BY_NAME_CMD= 0x34, GET_HOST_BY_NAME_CMD= 0x35, + START_SCAN_NETWORKS = 0x36, // All command with DATA_FLAG 0x40 send a 16bit Len From 28a453783f389188e7e0dfa2f104d1ca14972366 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Fri, 25 May 2012 08:46:04 +0200 Subject: [PATCH 02/20] Fixed WebServer Issue --- WiFi/utility/server_drv.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index c11e94109..2dca2f3cc 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -249,14 +249,10 @@ uint8_t ServerDrv::checkDataSent(uint8_t sock) if (_data) timeout = 0; else{ ++timeout; - if (timeout > TIMEOUT_DATA_SENT) - { - timeout = 0; - INFO1("Timeout wainting for data sent"); - } + delay(10); } - }while((_data==0)&&(timeout Date: Sat, 26 May 2012 12:02:15 +0200 Subject: [PATCH 03/20] Added check if wifi shield is not present --- WiFi/WiFi.cpp | 5 +++-- WiFi/WiFi.h | 2 +- WiFi/utility/wifi_drv.cpp | 24 ++++++++++++++++-------- WiFi/utility/wifi_drv.h | 10 +++++----- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 49f7e3958..fd45e9689 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -150,12 +150,13 @@ uint8_t WiFiClass::encryptionType() } -uint8_t WiFiClass::scanNetworks() +int8_t WiFiClass::scanNetworks() { uint8_t attempts = 10; uint8_t numOfNetworks = 0; - WiFiDrv::startScanNetworks(); + if (WiFiDrv::startScanNetworks() == WL_FAILURE) + return WL_FAILURE; do { delay(2000); diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 16d6a0ee7..501780b3f 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -123,7 +123,7 @@ public: * * return: Number of discovered networks */ - uint8_t scanNetworks(); + int8_t scanNetworks(); /* * Return the SSID discovered during the network scan. diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index d8785c9c4..5dabc0c0b 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -59,7 +59,7 @@ void WiFiDrv::wifiDriverInit() SpiDrv::begin(); } -uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) +int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -75,13 +75,14 @@ uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); + return WL_FAILURE; } SpiDrv::spiSlaveDeselect(); return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : WL_FAILURE; } -uint8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) +int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -98,13 +99,14 @@ uint8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *pas if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); + return WL_FAILURE; } SpiDrv::spiSlaveDeselect(); return _data; } -uint8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) +int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -122,12 +124,13 @@ uint8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); + return WL_FAILURE; } SpiDrv::spiSlaveDeselect(); return _data; } -uint8_t WiFiDrv::disconnect() +int8_t WiFiDrv::disconnect() { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -142,7 +145,7 @@ uint8_t WiFiDrv::disconnect() // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); + int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -299,7 +302,7 @@ uint8_t WiFiDrv::getCurrentEncryptionType() return encType; } -uint8_t WiFiDrv::startScanNetworks() +int8_t WiFiDrv::startScanNetworks() { WAIT_FOR_SLAVE_SELECT(); @@ -312,11 +315,16 @@ uint8_t WiFiDrv::startScanNetworks() // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen); + + if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + return WL_FAILURE; + } SpiDrv::spiSlaveDeselect(); - return result; + return WL_SUCCESS; } diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 0b6476f00..37563cba6 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -52,7 +52,7 @@ public: * param ssid_len: Lenght of ssid string. * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); + static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); /* Start Wifi connection with passphrase * the most secure supported mode will be automatically selected @@ -64,7 +64,7 @@ public: * param len: Lenght of passphrase string. * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + static int8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); /* Start Wifi connection with WEP encryption. * Configure a key into the device. The key type (WEP-40, WEP-104) @@ -77,14 +77,14 @@ public: * param len: Lenght of key string. * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); + static int8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); /* * Disconnect from the network * * return: WL_SUCCESS or WL_FAILURE */ - static uint8_t disconnect(); + static int8_t disconnect(); /* * Disconnect from the network @@ -156,7 +156,7 @@ public: * * return: Number of discovered networks */ - static uint8_t startScanNetworks(); + static int8_t startScanNetworks(); /* * Get the networks available From 910111abc6b27b2dbf2d9ef22d1ff0bf3c00238f Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Sat, 26 May 2012 12:06:14 +0200 Subject: [PATCH 04/20] Added check in case of shield not present --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index 2c10fed33..7ff9410e3 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -61,7 +61,12 @@ void printMacAddress() { void listNetworks() { // scan for nearby networks: Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); + int numSsid = WiFi.scanNetworks(); + if (numSsid == -1) + { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // print the list of networks seen: Serial.print("number of available networks:"); From bc088b6e84a89d8753ec02637f6ba2d7378aeaaf Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Wed, 30 May 2012 09:07:38 +0200 Subject: [PATCH 05/20] Added Check shield present on wifi examples --- WiFi/examples/WifiChatServer/WifiChatServer.ino | 4 ++++ WiFi/examples/WifiCosmClient/WifiCosmClient.ino | 4 ++++ .../WifiCosmClientString/WifiCosmClientString.ino | 4 ++++ WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino | 4 ++++ WiFi/examples/WifiWebClient/WifiWebClient.ino | 4 ++++ .../WifiWebClientRepeating/WifiWebClientRepeating.ino | 4 ++++ WiFi/utility/wifi_drv.cpp | 10 +++++----- 7 files changed, 29 insertions(+), 5 deletions(-) diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 8b6698f40..7eb0ed545 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -42,6 +42,10 @@ void setup() { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino index bd43d59fa..210c5134a 100644 --- a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino +++ b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino @@ -54,6 +54,10 @@ void setup() { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino index ec3da3174..f862551aa 100644 --- a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino +++ b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino @@ -59,6 +59,10 @@ void setup() { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 8bf7bb2c2..a8b3ef002 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -57,6 +57,10 @@ void setup() { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index f6a3cdf23..661b447ab 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -47,6 +47,10 @@ void setup() { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino index 8e5b88547..f10d2191d 100644 --- a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -43,6 +43,10 @@ void setup() { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 5dabc0c0b..5bd59d38d 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -75,7 +75,7 @@ int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); - return WL_FAILURE; + _data = WL_FAILURE; } SpiDrv::spiSlaveDeselect(); @@ -99,7 +99,7 @@ int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *pass if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); - return WL_FAILURE; + _data = WL_FAILURE; } SpiDrv::spiSlaveDeselect(); return _data; @@ -124,7 +124,7 @@ int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); - return WL_FAILURE; + _data = WL_FAILURE; } SpiDrv::spiSlaveDeselect(); return _data; @@ -319,12 +319,12 @@ int8_t WiFiDrv::startScanNetworks() if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); - return WL_FAILURE; + _data = WL_FAILURE; } SpiDrv::spiSlaveDeselect(); - return WL_SUCCESS; + return (_data == WL_FAILURE)? _data : WL_SUCCESS; } From a69cd64d5b0aca6b77db8687e68596ecd82a9b77 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Wed, 30 May 2012 09:08:36 +0200 Subject: [PATCH 06/20] Fix issue on Cosm sketch tahat stops after some conenctions --- WiFi/WiFiClient.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index 8f517cc19..29b7699d4 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -128,6 +128,7 @@ uint8_t WiFiClient::connected() { return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || s == TIME_WAIT || + s == SYN_SENT || s== SYN_RCVD || (s == CLOSE_WAIT && !available())); } } From 6e60b13013f9d94a7f86d225acdc072d31929036 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 31 May 2012 12:15:49 -0400 Subject: [PATCH 07/20] Changed wifi_drv.cpp and wl_definitions.h to allow for shield detection --- WiFi/utility/wifi_drv.cpp | 2 +- WiFi/utility/wl_definitions.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 5bd59d38d..45da70bdb 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -163,7 +163,7 @@ uint8_t WiFiDrv::getConnectionStatus() SpiDrv::waitForSlaveReady(); // Wait for reply - uint8_t _data = 0; + uint8_t _data = -1; uint8_t _dataLen = 0; SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h index e3db8b608..15de781fc 100644 --- a/WiFi/utility/wl_definitions.h +++ b/WiFi/utility/wl_definitions.h @@ -26,7 +26,8 @@ #define WL_MAX_ATTEMPT_CONNECTION 10 typedef enum { - WL_IDLE_STATUS, + WL_NO_SHIELD = 255, + WL_IDLE_STATUS = 0, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED, WL_CONNECTED, From b2a8a47c586baef8dfc4d38d3e9ddcb6b33e0ac7 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 31 May 2012 12:16:14 -0400 Subject: [PATCH 08/20] Changed examples to include shield detection --- .../ConnectNoEncryption.ino | 37 +++++++++-------- .../ConnectWithWEP/ConnectWithWEP.ino | 36 +++++++++------- .../ConnectWithWPA/ConnectWithWPA.ino | 41 +++++++++++-------- WiFi/examples/ScanNetworks/ScanNetworks.ino | 14 +++++-- .../WifiChatServer/WifiChatServer.ino | 21 ++++++---- .../WifiCosmClient/WifiCosmClient.ino | 15 ++++--- .../WifiCosmClientString.ino | 17 +++++--- .../WifiTwitterClient/WifiTwitterClient.ino | 20 +++++---- WiFi/examples/WifiWebClient/WifiWebClient.ino | 21 ++++++---- .../WifiWebClientRepeating.ino | 16 +++++--- WiFi/examples/WifiWebServer/WifiWebServer.ino | 11 ++++- 11 files changed, 157 insertions(+), 92 deletions(-) diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino index cf1eb432d..2609320c1 100644 --- a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino +++ b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -9,8 +9,8 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 29 Feb 2012 - by Scott Fitzgerald + modified 31 May 2012 + by Tom Igoe */ #include @@ -20,23 +20,28 @@ int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { // initialize serial: Serial.begin(9600); - - // attempt to connect to an open network: - Serial.print("Attempting to connect to open network: "); - Serial.println(ssid); - status = WiFi.begin(ssid); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to open SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid); + + // wait 10 seconds for connection: + delay(10000); } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); } void loop() { diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino index 1159e37f4..e6f531ca8 100644 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -19,7 +19,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 4 Mar 2012 + modified 31 May 2012 by Tom Igoe */ #include @@ -33,22 +33,27 @@ void setup() { // initialize serial: Serial.begin(9600); - // attempt to connect to an open network: - Serial.print("Attempting to connect to WEP network: "); - Serial.println(ssid); - status = WiFi.begin(ssid, keyIndex, key); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WEP network, SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid, keyIndex, key); + + // wait 10 seconds for connection: + delay(10000); } + + // once you are connected : + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); } void loop() { @@ -60,7 +65,7 @@ void loop() { void printWifiData() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); Serial.println(ip); Serial.println(ip); @@ -115,3 +120,4 @@ void printCurrentNet() { } + diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino index 7b5752cc7..953841500 100644 --- a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -9,35 +9,42 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 29 Feb 2012 - by Scott Fitzgerald + modified 31 May 2012 + by Tom Igoe */ #include -char ssid[] = "networkName"; // your network SSID (name) -char pass[] = "yourPassword"; // your network password +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { // initialize serial: Serial.begin(9600); - - // attempt to connect to an open network: - Serial.print("Attempting to connect to WPA network: "); - Serial.println(ssid); - status = WiFi.begin(ssid, pass); - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: while(true); } - // if you are connected : - else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); + } void loop() { diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index 7ff9410e3..efe171adb 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -10,7 +10,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 22 April 2012 + modified 31 May 2012 by Tom Igoe */ @@ -21,9 +21,15 @@ void setup() { // initialize serial and wait for the port to open: Serial.begin(9600); - - // attempt to connect using WEP encryption: - Serial.println("Initializing Wifi..."); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // Print WiFi MAC address: printMacAddress(); // scan for existing networks: diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 7eb0ed545..f231073ba 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -14,7 +14,7 @@ created 18 Dec 2009 by David A. Mellis - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ @@ -22,8 +22,8 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) -char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) @@ -36,16 +36,21 @@ boolean alreadyConnected = false; // whether or not the client was connected pre void setup() { // start serial port: Serial.begin(9600); - + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino index 210c5134a..668c9e0ac 100644 --- a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino +++ b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino @@ -16,7 +16,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 13 Mar 2012 - modified 14 May 2012 + modified 31 May 2012 by Tom Igoe This code is in the public domain. @@ -49,15 +49,20 @@ void setup() { // start serial port: Serial.begin(9600); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino index f862551aa..3634ed762 100644 --- a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino +++ b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino @@ -19,7 +19,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 16 Mar 2012 - modified 14 May 2012 + modified 31 May 2012 by Tom Igoe This code is in the public domain. @@ -53,16 +53,21 @@ const unsigned long postingInterval = 10*1000; //delay between updates to cosm. void setup() { // start serial port: Serial.begin(9600); - + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index a8b3ef002..3bbe63e8e 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -14,6 +14,7 @@ * WiFi shield attached to pins 10, 11, 12, 13 created 23 apr 2012 + modified 31 May 2012 by Tom Igoe This code is in the public domain. @@ -22,7 +23,7 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) +char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) @@ -51,16 +52,21 @@ void setup() { tweet.reserve(150); // start serial port: Serial.begin(9600); - + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 661b447ab..54ec68b71 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -16,7 +16,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ @@ -24,8 +24,8 @@ #include #include -char ssid[] = "YourNetwork"; // your network SSID (name) -char pass[] = "password"; // your network password (use for WPA, or use as key for WEP) +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; @@ -41,16 +41,21 @@ WiFiClient client; void setup() { Serial.begin(9600); - + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino index f10d2191d..1c623d12e 100644 --- a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -7,7 +7,8 @@ Circuit: * Wifi shield attached to pins 10, 11, 12, 13 - created 23 Apr 2012 + created 23 April 2012 + modifide 31 May 2012 by Tom Igoe http://arduino.cc/en/Tutorial/WifiWebClientRepeating @@ -38,15 +39,20 @@ void setup() { // start serial port: Serial.begin(9600); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } + // wait 10 seconds for connection: delay(10000); } diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index 34a231b5c..72bd8e0b9 100644 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -13,7 +13,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 23 Apr 2012 + modified 31 May 2012 by Tom Igoe */ #include @@ -32,11 +32,20 @@ void setup() { // start serial port: Serial.begin(9600); + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + // attempt to connect to Wifi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); + // wait 10 seconds for connection: delay(10000); } From fb011d3c3b7cec45752676d6c4037ab32a37fcc2 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Mon, 4 Jun 2012 23:14:56 +0200 Subject: [PATCH 09/20] Fix issue compiler option --- WiFi/utility/debug.h | 9 +++++++++ WiFi/utility/spi_drv.cpp | 34 ++++++---------------------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 437ec6c94..9f71055b2 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -50,6 +50,7 @@ #define WARN(args) do {} while (0); #endif +#if _DEBUG_SPI_ #define DBG_PIN2 5 #define DBG_PIN 4 @@ -64,5 +65,13 @@ #define TOGGLE_TRIGGER() SET_TRIGGER() \ delayMicroseconds(2); \ RST_TRIGGER() +#else +#define START() +#define END() +#define SET_TRIGGER() +#define RST_TRIGGER() +#define INIT_TRIGGER() +#define TOGGLE_TRIGGER() +#endif #endif diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index 8069b024d..ceef832c0 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -13,7 +13,9 @@ extern "C" { #define SLAVESELECT 10//ss #define SLAVEREADY 3 -#define DELAY_100NS asm volatile("nop") +#define DELAY_100NS do { asm volatile("nop"); }while(0); +#define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); }while(++ii 0) && (*readChar != waitChar)); -// -// return (*readChar == waitChar); -//} - - int SpiDrv::readAndCheckChar(char checkChar, char* readChar) { - //*readChar = spiTransfer(DUMMY_DATA); //get data byte getParam((uint8_t*)readChar); return (*readChar == checkChar); @@ -128,11 +112,8 @@ char SpiDrv::readChar() uint8_t readChar = 0; getParam(&readChar); return readChar; - //return spiTransfer(DUMMY_DATA); //get data byte } -//#define WAIT_START_CMD(x) waitSpiChar(START_CMD, &x) -//#define WAIT_START_CMD(x) readAndCheckChar(START_CMD, &x) #define WAIT_START_CMD(x) waitSpiChar(START_CMD) #define IF_CHECK_START_CMD(x) \ @@ -171,10 +152,7 @@ void SpiDrv::getParam(uint8_t* param) { // Get Params data *param = spiTransfer(DUMMY_DATA); - DELAY_100NS; - DELAY_100NS; - DELAY_100NS; - DELAY_100NS; + DELAY_TRANSFER(); } int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len) From a363b196a6353313cfc3f1c80eac0e5170bed452 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Mon, 4 Jun 2012 23:29:28 +0200 Subject: [PATCH 10/20] Fix issue compiler option2 --- WiFi/utility/spi_drv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFi/utility/spi_drv.h b/WiFi/utility/spi_drv.h index 60497b585..5c2e7063f 100644 --- a/WiFi/utility/spi_drv.h +++ b/WiFi/utility/spi_drv.h @@ -4,7 +4,7 @@ #include #include "wifi_spi.h" -#define SPI_START_CMD_DELAY 10 +#define SPI_START_CMD_DELAY 12 #define NO_LAST_PARAM 0 #define LAST_PARAM 1 From a5c38d11a91a92ce833cd3044aca3033ee33ee36 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Tue, 5 Jun 2012 14:20:22 +0200 Subject: [PATCH 11/20] Fix issue with peek function --- WiFi/WiFiClient.cpp | 34 +++++++++++++++++++++++++++++----- WiFi/utility/server_drv.cpp | 7 ++++--- WiFi/utility/server_drv.h | 2 +- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index 29b7699d4..c6a416919 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -35,8 +35,22 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { { ServerDrv::startClient(uint32_t(ip), port, _sock); WiFiClass::_state[_sock] = _sock; - while(!connected()); + + unsigned long start = millis(); + + // wait 4 second for the connection to close + while (!connected() && millis() - start < 10000) + delay(1); + + if (!connected()) + { + Serial.println("timeout on client connection"); + return 0; + } + else + Serial.println("CONNECTED"); }else{ + Serial.println("No Socket available"); return 0; } return 1; @@ -94,8 +108,12 @@ int WiFiClient::read(uint8_t* buf, size_t size) { } int WiFiClient::peek() { - //TODO to be implemented - return 0; + uint8_t b; + if (!available()) + return -1; + + ServerDrv::getData(_sock, &b, 1); + return b; } void WiFiClient::flush() { @@ -112,10 +130,11 @@ void WiFiClient::stop() { unsigned long start = millis(); + // wait a second for the connection to close while (status() != CLOSED && millis() - start < 1000) delay(1); - + Serial.print("Stop client! Status:");Serial.println(status(),10); _sock = 255; } @@ -125,7 +144,12 @@ uint8_t WiFiClient::connected() { return 0; } else { uint8_t s = status(); - +/* + if (s== SYN_SENT) Serial.print("*"); + else{ + Serial.print("Status:"); Serial.println(s,10); + } +*/ return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || s == TIME_WAIT || s == SYN_SENT || s== SYN_RCVD || diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index 2dca2f3cc..a325d5a06 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -146,12 +146,13 @@ uint8_t ServerDrv::availData(uint8_t sock) return false; } -bool ServerDrv::getData(uint8_t sock, uint8_t *data) +bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek) { WAIT_FOR_SLAVE_SELECT(); // Send Command - SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendParam(&sock, sizeof(sock)); + SpiDrv::sendParam(peek, LAST_PARAM); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); diff --git a/WiFi/utility/server_drv.h b/WiFi/utility/server_drv.h index 6e2eebda7..69ba593e9 100644 --- a/WiFi/utility/server_drv.h +++ b/WiFi/utility/server_drv.h @@ -18,7 +18,7 @@ public: static uint8_t getClientState(uint8_t sock); - static bool getData(uint8_t sock, uint8_t *data); + static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0); static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len); From 5adf3e5c892c041210539ea308ee6f83af152bfe Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Tue, 5 Jun 2012 16:19:52 +0200 Subject: [PATCH 12/20] Delete console messages --- WiFi/WiFiClient.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index c6a416919..470a42938 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -44,11 +44,8 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { if (!connected()) { - Serial.println("timeout on client connection"); return 0; } - else - Serial.println("CONNECTED"); }else{ Serial.println("No Socket available"); return 0; @@ -134,7 +131,6 @@ void WiFiClient::stop() { // wait a second for the connection to close while (status() != CLOSED && millis() - start < 1000) delay(1); - Serial.print("Stop client! Status:");Serial.println(status(),10); _sock = 255; } @@ -144,12 +140,7 @@ uint8_t WiFiClient::connected() { return 0; } else { uint8_t s = status(); -/* - if (s== SYN_SENT) Serial.print("*"); - else{ - Serial.print("Status:"); Serial.println(s,10); - } -*/ + return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || s == TIME_WAIT || s == SYN_SENT || s== SYN_RCVD || From 771ef148a09cddf02d4af2831a514ba2034482a4 Mon Sep 17 00:00:00 2001 From: Federico Vanzati Date: Wed, 6 Jun 2012 11:26:41 +0200 Subject: [PATCH 13/20] Binary without staxk traces --- WiFi/WiFi.cpp | 194 ------- WiFi/WiFi.h | 177 ------ WiFi/WiFiClient.cpp | 174 ------ WiFi/WiFiClient.h | 40 -- WiFi/WiFiServer.cpp | 88 --- WiFi/WiFiServer.h | 27 - .../ConnectWithWEP/ConnectWithWEP.ino | 123 ----- .../ConnectWithWPA/ConnectWithWPA.ino | 113 ---- WiFi/examples/ScanNetworks/ScanNetworks.ino | 95 ---- .../WifiChatServer/WifiChatServer.ino | 108 ---- .../WifiCosmClient/WifiCosmClient.ino | 185 ------- .../WifiCosmClientString.ino | 172 ------ WiFi/examples/WifiWebClient/WifiWebClient.ino | 117 ---- .../WifiWebClientRepeating.ino | 135 ----- WiFi/examples/WifiWebServer/WifiWebServer.ino | 129 ----- WiFi/keywords.txt | 43 -- WiFi/utility/debug.h | 77 --- WiFi/utility/server_drv.cpp | 260 --------- WiFi/utility/server_drv.h | 34 -- WiFi/utility/socket.c | 20 - WiFi/utility/socket.h | 87 --- WiFi/utility/spi_drv.cpp | 503 ------------------ WiFi/utility/spi_drv.h | 83 --- WiFi/utility/wifi_drv.cpp | 470 ---------------- WiFi/utility/wifi_drv.h | 208 -------- WiFi/utility/wifi_spi.h | 143 ----- WiFi/utility/wl_definitions.h | 50 -- WiFi/utility/wl_types.h | 31 -- 28 files changed, 3886 deletions(-) delete mode 100755 WiFi/WiFi.cpp delete mode 100755 WiFi/WiFi.h delete mode 100755 WiFi/WiFiClient.cpp delete mode 100755 WiFi/WiFiClient.h delete mode 100644 WiFi/WiFiServer.cpp delete mode 100755 WiFi/WiFiServer.h delete mode 100644 WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino delete mode 100644 WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino delete mode 100644 WiFi/examples/ScanNetworks/ScanNetworks.ino delete mode 100644 WiFi/examples/WifiChatServer/WifiChatServer.ino delete mode 100644 WiFi/examples/WifiCosmClient/WifiCosmClient.ino delete mode 100644 WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino delete mode 100644 WiFi/examples/WifiWebClient/WifiWebClient.ino delete mode 100644 WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino delete mode 100644 WiFi/examples/WifiWebServer/WifiWebServer.ino delete mode 100755 WiFi/keywords.txt delete mode 100644 WiFi/utility/debug.h delete mode 100644 WiFi/utility/server_drv.cpp delete mode 100644 WiFi/utility/server_drv.h delete mode 100644 WiFi/utility/socket.c delete mode 100644 WiFi/utility/socket.h delete mode 100644 WiFi/utility/spi_drv.cpp delete mode 100644 WiFi/utility/spi_drv.h delete mode 100644 WiFi/utility/wifi_drv.cpp delete mode 100644 WiFi/utility/wifi_drv.h delete mode 100644 WiFi/utility/wifi_spi.h delete mode 100644 WiFi/utility/wl_definitions.h delete mode 100644 WiFi/utility/wl_types.h diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp deleted file mode 100755 index fd45e9689..000000000 --- a/WiFi/WiFi.cpp +++ /dev/null @@ -1,194 +0,0 @@ -#include "wifi_drv.h" -#include "WiFi.h" - -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. -int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; -uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; - -WiFiClass::WiFiClass() -{ - // Driver initialization - init(); -} - -void WiFiClass::init() -{ - WiFiDrv::wifiDriverInit(); -} - -uint8_t WiFiClass::getSocket() -{ - for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i) - { - if (WiFiClass::_server_port[i] == 0) - { - return i; - } - } - return NO_SOCKET_AVAIL; -} - -int WiFiClass::begin(char* ssid) -{ - uint8_t status = WL_IDLE_STATUS; - uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; - - if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE) - { - do - { - delay(WL_DELAY_START_CONNECTION); - status = WiFiDrv::getConnectionStatus(); - } - while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); - }else - { - status = WL_CONNECT_FAILED; - } - return status; -} - -int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) -{ - uint8_t status = WL_IDLE_STATUS; - uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; - - // set encryption key - if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) - { - do - { - delay(WL_DELAY_START_CONNECTION); - status = WiFiDrv::getConnectionStatus(); - } - while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); - }else{ - status = WL_CONNECT_FAILED; - } - return status; -} - -int WiFiClass::begin(char* ssid, const char *passphrase) -{ - uint8_t status = WL_IDLE_STATUS; - uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; - - // set passphrase - if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) - { - do - { - delay(WL_DELAY_START_CONNECTION); - status = WiFiDrv::getConnectionStatus(); - } - while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); - }else{ - status = WL_CONNECT_FAILED; - } - return status; -} - -int WiFiClass::disconnect() -{ - return WiFiDrv::disconnect(); -} - -uint8_t* WiFiClass::macAddress(uint8_t* mac) -{ - uint8_t* _mac = WiFiDrv::getMacAddress(); - memcpy(mac, _mac, WL_MAC_ADDR_LENGTH); - return mac; -} - -IPAddress WiFiClass::localIP() -{ - IPAddress ret; - WiFiDrv::getIpAddress(ret); - return ret; -} - -IPAddress WiFiClass::subnetMask() -{ - IPAddress ret; - WiFiDrv::getSubnetMask(ret); - return ret; -} - -IPAddress WiFiClass::gatewayIP() -{ - IPAddress ret; - WiFiDrv::getGatewayIP(ret); - return ret; -} - -char* WiFiClass::SSID() -{ - return WiFiDrv::getCurrentSSID(); -} - -uint8_t* WiFiClass::BSSID(uint8_t* bssid) -{ - uint8_t* _bssid = WiFiDrv::getCurrentBSSID(); - memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH); - return bssid; -} - -int32_t WiFiClass::RSSI() -{ - return WiFiDrv::getCurrentRSSI(); -} - -uint8_t WiFiClass::encryptionType() -{ - return WiFiDrv::getCurrentEncryptionType(); -} - - -int8_t WiFiClass::scanNetworks() -{ - uint8_t attempts = 10; - uint8_t numOfNetworks = 0; - - if (WiFiDrv::startScanNetworks() == WL_FAILURE) - return WL_FAILURE; - do - { - delay(2000); - numOfNetworks = WiFiDrv::getScanNetworks(); - } - while (( numOfNetworks == 0)&&(--attempts>0)); - return numOfNetworks; -} - -char* WiFiClass::SSID(uint8_t networkItem) -{ - return WiFiDrv::getSSIDNetoworks(networkItem); -} - -int32_t WiFiClass::RSSI(uint8_t networkItem) -{ - return WiFiDrv::getRSSINetoworks(networkItem); -} - -uint8_t WiFiClass::encryptionType(uint8_t networkItem) -{ - return WiFiDrv::getEncTypeNetowrks(networkItem); -} - -uint8_t WiFiClass::status() -{ - return WiFiDrv::getConnectionStatus(); -} - -int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) -{ - return WiFiDrv::getHostByName(aHostname, aResult); -} - -WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h deleted file mode 100755 index 501780b3f..000000000 --- a/WiFi/WiFi.h +++ /dev/null @@ -1,177 +0,0 @@ -#ifndef WiFi_h -#define WiFi_h - -#include - -extern "C" { - #include "utility/wl_definitions.h" - #include "utility/wl_types.h" -} - -#include "IPAddress.h" -#include "WiFiClient.h" -#include "WiFiServer.h" - -class WiFiClass -{ -private: - - static void init(); -public: - static int16_t _state[MAX_SOCK_NUM]; - static uint16_t _server_port[MAX_SOCK_NUM]; - - WiFiClass(); - - /* - * Get the first socket available - */ - static uint8_t getSocket(); - - /* Start Wifi connection for OPEN networks - * - * param ssid: Pointer to the SSID string. - */ - int begin(char* ssid); - - /* Start Wifi connection with WEP encryption. - * Configure a key into the device. The key type (WEP-40, WEP-104) - * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104). - * - * param ssid: Pointer to the SSID string. - * param key_idx: The key index to set. Valid values are 0-3. - * param key: Key input buffer. - */ - int begin(char* ssid, uint8_t key_idx, const char* key); - - /* Start Wifi connection with passphrase - * the most secure supported mode will be automatically selected - * - * param ssid: Pointer to the SSID string. - * param passphrase: Passphrase. Valid characters in a passphrase - * must be between ASCII 32-126 (decimal). - */ - int begin(char* ssid, const char *passphrase); - - /* - * Disconnect from the network - * - * return: one value of wl_status_t enum - */ - int disconnect(void); - - /* - * Get the interface MAC address. - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - */ - uint8_t* macAddress(uint8_t* mac); - - /* - * Get the interface IP address. - * - * return: Ip address value - */ - IPAddress localIP(); - - /* - * Get the interface subnet mask address. - * - * return: subnet mask address value - */ - IPAddress subnetMask(); - - /* - * Get the gateway ip address. - * - * return: gateway ip address value - */ - IPAddress gatewayIP(); - - /* - * Return the current SSID associated with the network - * - * return: ssid string - */ - char* SSID(); - - /* - * Return the current BSSID associated with the network. - * It is the MAC address of the Access Point - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - */ - uint8_t* BSSID(uint8_t* bssid); - - /* - * Return the current RSSI /Received Signal Strength in dBm) - * associated with the network - * - * return: signed value - */ - int32_t RSSI(); - - /* - * Return the Encryption Type associated with the network - * - * return: one value of wl_enc_type enum - */ - uint8_t encryptionType(); - - /* - * Start scan WiFi networks available - * - * return: Number of discovered networks - */ - int8_t scanNetworks(); - - /* - * Return the SSID discovered during the network scan. - * - * param networkItem: specify from which network item want to get the information - * - * return: ssid string of the specified item on the networks scanned list - */ - char* SSID(uint8_t networkItem); - - /* - * Return the encryption type of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list - */ - uint8_t encryptionType(uint8_t networkItem); - - /* - * Return the RSSI of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: signed value of RSSI of the specified item on the networks scanned list - */ - int32_t RSSI(uint8_t networkItem); - - /* - * Return Connection status. - * - * return: one of the value defined in wl_status_t - */ - uint8_t status(); - - /* - * Resolve the given hostname to an IP address. - * param aHostname: Name to be resolved - * param aResult: IPAddress structure to store the returned IP address - * result: 1 if aIPAddrString was successfully converted to an IP address, - * else error code - */ - int hostByName(const char* aHostname, IPAddress& aResult); - - friend class WiFiClient; - friend class WiFiServer; -}; - -extern WiFiClass WiFi; - -#endif diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp deleted file mode 100755 index 470a42938..000000000 --- a/WiFi/WiFiClient.cpp +++ /dev/null @@ -1,174 +0,0 @@ -extern "C" { - #include "utility/wl_definitions.h" - #include "utility/wl_types.h" - #include "socket.h" - #include "string.h" - #include "utility/debug.h" -} - -#include "WiFi.h" -#include "WiFiClient.h" -#include "WiFiServer.h" -#include "server_drv.h" - - -uint16_t WiFiClient::_srcport = 1024; - -WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) { -} - -WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) { -} - -int WiFiClient::connect(const char* host, uint16_t port) { - IPAddress remote_addr; - if (WiFi.hostByName(host, remote_addr)) - { - return connect(remote_addr, port); - } - return 0; -} - -int WiFiClient::connect(IPAddress ip, uint16_t port) { - _sock = getFirstSocket(); - if (_sock != NO_SOCKET_AVAIL) - { - ServerDrv::startClient(uint32_t(ip), port, _sock); - WiFiClass::_state[_sock] = _sock; - - unsigned long start = millis(); - - // wait 4 second for the connection to close - while (!connected() && millis() - start < 10000) - delay(1); - - if (!connected()) - { - return 0; - } - }else{ - Serial.println("No Socket available"); - return 0; - } - return 1; -} - -size_t WiFiClient::write(uint8_t b) { - return write(&b, 1); -} - -size_t WiFiClient::write(const uint8_t *buf, size_t size) { - if (_sock >= MAX_SOCK_NUM) - { - setWriteError(); - return 0; - } - if (size==0) - { - setWriteError(); - return 0; - } - - - if ((!ServerDrv::sendData(_sock, buf, size)) || - (!ServerDrv::checkDataSent(_sock))) - { - setWriteError(); - return 0; - } - return size; -} - -int WiFiClient::available() { - if (_sock != 255) - { - return ServerDrv::availData(_sock); - } - - return 0; -} - -int WiFiClient::read() { - uint8_t b; - if (!available()) - return -1; - - ServerDrv::getData(_sock, &b); - return b; -} - - -int WiFiClient::read(uint8_t* buf, size_t size) { - if (!ServerDrv::getDataBuf(_sock, buf, &size)) - return -1; - return 0; -} - -int WiFiClient::peek() { - uint8_t b; - if (!available()) - return -1; - - ServerDrv::getData(_sock, &b, 1); - return b; -} - -void WiFiClient::flush() { - while (available()) - read(); -} - -void WiFiClient::stop() { - - if (_sock == 255) - return; - - ServerDrv::stopClient(_sock); - - unsigned long start = millis(); - - - // wait a second for the connection to close - while (status() != CLOSED && millis() - start < 1000) - delay(1); - _sock = 255; -} - -uint8_t WiFiClient::connected() { - - if (_sock == 255) { - return 0; - } else { - uint8_t s = status(); - - return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || - s == FIN_WAIT_2 || s == TIME_WAIT || - s == SYN_SENT || s== SYN_RCVD || - (s == CLOSE_WAIT && !available())); - } -} - -uint8_t WiFiClient::status() { - if (_sock == 255) { - return CLOSED; - } else { - return ServerDrv::getClientState(_sock); - } -} - -WiFiClient::operator bool() { - return _sock != 255; -} - -// Private Methods -uint8_t WiFiClient::getFirstSocket() -{ - for (int i = 0; i < MAX_SOCK_NUM; i++) { - if (WiFiClass::_state[i] == 0) - { - return i; - } - } - return SOCK_NOT_AVAIL; -} - diff --git a/WiFi/WiFiClient.h b/WiFi/WiFiClient.h deleted file mode 100755 index 5a7f0f3b8..000000000 --- a/WiFi/WiFiClient.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef wificlient_h -#define wificlient_h -#include "Arduino.h" -#include "Print.h" -#include "Client.h" -#include "IPAddress.h" - -class WiFiClient : public Client { - -public: - WiFiClient(); - WiFiClient(uint8_t sock); - - uint8_t status(); - virtual int connect(IPAddress ip, uint16_t port); - virtual int connect(const char *host, uint16_t port); - virtual size_t write(uint8_t); - virtual size_t write(const uint8_t *buf, size_t size); - virtual int available(); - virtual int read(); - virtual int read(uint8_t *buf, size_t size); - virtual int peek(); - virtual void flush(); - virtual void stop(); - virtual uint8_t connected(); - virtual operator bool(); - - friend class WiFiServer; - - using Print::write; - -private: - static uint16_t _srcport; - uint8_t _sock; //not used - uint16_t _socket; - - uint8_t getFirstSocket(); -}; - -#endif diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp deleted file mode 100644 index 77dbac0b9..000000000 --- a/WiFi/WiFiServer.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include "server_drv.h" - -extern "C" { - #include "utility/debug.h" -} - -#include "WiFi.h" -#include "WiFiClient.h" -#include "WiFiServer.h" - -WiFiServer::WiFiServer(uint16_t port) -{ - _port = port; -} - -void WiFiServer::begin() -{ - uint8_t _sock = WiFiClass::getSocket(); - if (_sock != NO_SOCKET_AVAIL) - { - ServerDrv::startServer(_port, _sock); - WiFiClass::_server_port[_sock] = _port; - } -} - -WiFiClient WiFiServer::available(byte* status) -{ - static int cycle_server_down = 0; - const int TH_SERVER_DOWN = 50; - - for (int sock = 0; sock < MAX_SOCK_NUM; sock++) - { - if (WiFiClass::_server_port[sock] == _port) - { - WiFiClient client(sock); - uint8_t _status = client.status(); - uint8_t _ser_status = this->status(); - - if (status != NULL) - *status = _status; - - //server not in listen state, restart it - if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN)) - { - ServerDrv::startServer(_port, sock); - cycle_server_down = 0; - } - - if (_status == ESTABLISHED) - { - return client; //TODO - } - } - } - - return WiFiClient(255); -} - -uint8_t WiFiServer::status() { - return ServerDrv::getServerState(0); -} - - -size_t WiFiServer::write(uint8_t b) -{ - return write(&b, 1); -} - -size_t WiFiServer::write(const uint8_t *buffer, size_t size) -{ - size_t n = 0; - - for (int sock = 0; sock < MAX_SOCK_NUM; sock++) - { - if (WiFiClass::_server_port[sock] != 0) - { - WiFiClient client(sock); - - if (WiFiClass::_server_port[sock] == _port && - client.status() == ESTABLISHED) - { - n+=client.write(buffer, size); - } - } - } - return n; -} diff --git a/WiFi/WiFiServer.h b/WiFi/WiFiServer.h deleted file mode 100755 index 68b574c29..000000000 --- a/WiFi/WiFiServer.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef wifiserver_h -#define wifiserver_h - -extern "C" { - #include "utility/wl_definitions.h" -} - -#include "Server.h" - -class WiFiClient; - -class WiFiServer : public Server { -private: - uint16_t _port; - void* pcb; -public: - WiFiServer(uint16_t); - WiFiClient available(uint8_t* status = NULL); - void begin(); - virtual size_t write(uint8_t); - virtual size_t write(const uint8_t *buf, size_t size); - uint8_t status(); - - using Print::write; -}; - -#endif diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino deleted file mode 100644 index e6f531ca8..000000000 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ /dev/null @@ -1,123 +0,0 @@ -/* - - This example connects to a WEP-encrypted Wifi network. - Then it prints the MAC address of the Wifi shield, - the IP address obtained, and other network details. - - If you use 40-bit WEP, you need a key that is 10 characters long, - and the characters must be hexadecimal (0-9 or A-F). - e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work - (too short) and ABBAISDEAF won't work (I and S are not - hexadecimal characters). - - For 128-bit, you need a string that is 26 characters long. - D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters, - all in the 0-9, A-F range. - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - */ -#include - -char ssid[] = "yourNetwork"; // your network SSID (name) -char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key -int keyIndex = 0; // your network key Index number -int status = WL_IDLE_STATUS; // the Wifi radio's status - -void setup() { - // initialize serial: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to WEP network, SSID: "); - Serial.println(ssid); - status = WiFi.begin(ssid, keyIndex, key); - - // wait 10 seconds for connection: - delay(10000); - } - - // once you are connected : - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); -} - -void loop() { - // check the network connection once every 10 seconds: - delay(10000); - printCurrentNet(); -} - -void printWifiData() { - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - Serial.println(ip); - - // print your MAC address: - byte mac[6]; - WiFi.macAddress(mac); - Serial.print("MAC address: "); - Serial.print(mac[5],HEX); - Serial.print(":"); - Serial.print(mac[4],HEX); - Serial.print(":"); - Serial.print(mac[3],HEX); - Serial.print(":"); - Serial.print(mac[2],HEX); - Serial.print(":"); - Serial.print(mac[1],HEX); - Serial.print(":"); - Serial.println(mac[0],HEX); -} - -void printCurrentNet() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print the MAC address of the router you're attached to: - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],HEX); - Serial.print(":"); - Serial.print(bssid[4],HEX); - Serial.print(":"); - Serial.print(bssid[3],HEX); - Serial.print(":"); - Serial.print(bssid[2],HEX); - Serial.print(":"); - Serial.print(bssid[1],HEX); - Serial.print(":"); - Serial.println(bssid[0],HEX); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption,HEX); - Serial.println(); -} - - - diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino deleted file mode 100644 index 953841500..000000000 --- a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino +++ /dev/null @@ -1,113 +0,0 @@ -/* - - This example connects to an unencrypted Wifi network. - Then it prints the MAC address of the Wifi shield, - the IP address obtained, and other network details. - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - */ - #include - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password -int status = WL_IDLE_STATUS; // the Wifi radio's status - -void setup() { - // initialize serial: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network: - status = WiFi.begin(ssid, pass); - - // wait 10 seconds for connection: - delay(10000); - } - - // you're connected now, so print out the data: - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); - -} - -void loop() { - // check the network connection once every 10 seconds: - delay(10000); - printCurrentNet(); -} - -void printWifiData() { - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - Serial.println(ip); - - // print your MAC address: - byte mac[6]; - WiFi.macAddress(mac); - Serial.print("MAC address: "); - Serial.print(mac[5],HEX); - Serial.print(":"); - Serial.print(mac[4],HEX); - Serial.print(":"); - Serial.print(mac[3],HEX); - Serial.print(":"); - Serial.print(mac[2],HEX); - Serial.print(":"); - Serial.print(mac[1],HEX); - Serial.print(":"); - Serial.println(mac[0],HEX); - -} - -void printCurrentNet() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print the MAC address of the router you're attached to: - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],HEX); - Serial.print(":"); - Serial.print(bssid[4],HEX); - Serial.print(":"); - Serial.print(bssid[3],HEX); - Serial.print(":"); - Serial.print(bssid[2],HEX); - Serial.print(":"); - Serial.print(bssid[1],HEX); - Serial.print(":"); - Serial.println(bssid[0],HEX); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption,HEX); - Serial.println(); -} - diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino deleted file mode 100644 index efe171adb..000000000 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ /dev/null @@ -1,95 +0,0 @@ -/* - - This example prints the Wifi shield's MAC address, and - scans for available Wifi networks using the Wifi shield. - Every ten seconds, it scans again. It doesn't actually - connect to any network, so no encryption scheme is specified. - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - */ - - -#include -#include - -void setup() { - // initialize serial and wait for the port to open: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // Print WiFi MAC address: - printMacAddress(); - - // scan for existing networks: - Serial.println("Scanning available networks..."); - listNetworks(); -} - -void loop() { - delay(10000); - // scan for existing networks: - Serial.println("Scanning available networks..."); - listNetworks(); -} - -void printMacAddress() { - // the MAC address of your Wifi shield - byte mac[6]; - - // print your MAC address: - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],HEX); - Serial.print(":"); - Serial.print(mac[4],HEX); - Serial.print(":"); - Serial.print(mac[3],HEX); - Serial.print(":"); - Serial.print(mac[2],HEX); - Serial.print(":"); - Serial.print(mac[1],HEX); - Serial.print(":"); - Serial.println(mac[0],HEX); -} - -void listNetworks() { - // scan for nearby networks: - Serial.println("** Scan Networks **"); - int numSsid = WiFi.scanNetworks(); - if (numSsid == -1) - { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - - // print the list of networks seen: - Serial.print("number of available networks:"); - Serial.println(numSsid); - - // print the network number and name for each network found: - for (int thisNet = 0; thisNet -#include - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) - -int keyIndex = 0; // your network key Index number (needed only for WEP) - -int status = WL_IDLE_STATUS; - -WiFiServer server(23); - -boolean alreadyConnected = false; // whether or not the client was connected previously - -void setup() { - // start serial port: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid, pass); - - // wait 10 seconds for connection: - delay(10000); - } - // start the server: - server.begin(); - // you're connected now, so print out the status: - printWifiStatus(); - } - - -void loop() { - // wait for a new client: - WiFiClient client = server.available(); - - - // when the client sends the first byte, say hello: - if (client) { - if (!alreadyConnected) { - // clead out the input buffer: - client.flush(); - Serial.println("We have a new client"); - client.println("Hello, client!"); - alreadyConnected = true; - } - - if (client.available() > 0) { - // read the bytes incoming from the client: - char thisChar = client.read(); - // echo the bytes back to the client: - server.write(thisChar); - // echo the bytes to the server as well: - Serial.write(thisChar); - } - } -} - - -void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.print(rssi); - Serial.println(" dBm"); -} - - diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino deleted file mode 100644 index 668c9e0ac..000000000 --- a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino +++ /dev/null @@ -1,185 +0,0 @@ -/* - Wifi Cosm sensor client - - This sketch connects an analog sensor to Cosm (http://www.cosm.com) - using an Arduino Wifi shield. - - This example is written for a network using WPA encryption. For - WEP or WPA, change the Wifi.begin() call accordingly. - - This example has been updated to use version 2.0 of the Cosm.com API. - To make it work, create a feed with a datastream, and give it the ID - sensor1. Or change the code below to match your feed. - - Circuit: - * Analog sensor attached to analog in 0 - * Wifi shield attached to pins 10, 11, 12, 13 - - created 13 Mar 2012 - modified 31 May 2012 - by Tom Igoe - - This code is in the public domain. - - */ -#include -#include - -#define APIKEY "YOUR API KEY GOES HERE" // replace your cosm api key here -#define FEEDID 00000 // replace your feed ID -#define USERAGENT "My Arduino Project" // user agent is the project name - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password - -int status = WL_IDLE_STATUS; - -// initialize the library instance: -WiFiClient client; -// if you don't want to use DNS (and reduce your sketch size) -// use the numeric IP instead of the name for the server: -IPAddress server(216,52,233,121); // numeric IP for api.cosm.com -//char server[] = "api.cosm.com"; // name address for cosm API - -unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds -boolean lastConnected = false; // state of the connection last time through the main loop -const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com - -void setup() { - // start serial port: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid, pass); - - // wait 10 seconds for connection: - delay(10000); - } - // you're connected now, so print out the status: - printWifiStatus(); -} - - -void loop() { - // read the analog sensor: - int sensorReading = analogRead(A0); - - // if there's incoming data from the net connection. - // send it out the serial port. This is for debugging - // purposes only: - if (client.available()) { - char c = client.read(); - Serial.print(c); - } - - // if there's no net connection, but there was one last time - // through the loop, then stop the client: - if (!client.connected() && lastConnected) { - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - } - - // if you're not connected, and ten seconds have passed since - // your last connection, then connect again and send data: - if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { - sendData(sensorReading); - } - // store the state of the connection for next time through - // the loop: - lastConnected = client.connected(); -} - -// this method makes a HTTP connection to the server: -void sendData(int thisData) { - // if there's a successful connection: - if (client.connect(server, 80)) { - Serial.println("connecting..."); - // send the HTTP PUT request: - client.print("PUT /v2/feeds/"); - client.print(FEEDID); - client.println(".csv HTTP/1.1"); - client.println("Host: api.cosm.com"); - client.print("X-ApiKey: "); - client.println(APIKEY); - client.print("User-Agent: "); - client.println(USERAGENT); - client.print("Content-Length: "); - - // calculate the length of the sensor reading in bytes: - // 8 bytes for "sensor1," + number of digits of the data: - int thisLength = 8 + getLength(thisData); - client.println(thisLength); - - // last pieces of the HTTP PUT request: - client.println("Content-Type: text/csv"); - client.println("Connection: close"); - client.println(); - - // here's the actual content of the PUT request: - client.print("sensor1,"); - client.println(thisData); - - } - else { - // if you couldn't make a connection: - Serial.println("connection failed"); - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - } - // note the time that the connection was made or attempted: - lastConnectionTime = millis(); -} - - -// This method calculates the number of digits in the -// sensor reading. Since each digit of the ASCII decimal -// representation is a byte, the number of digits equals -// the number of bytes: - -int getLength(int someValue) { - // there's at least one byte: - int digits = 1; - // continually divide the value by ten, - // adding one to the digit count for each - // time you divide, until you're at 0: - int dividend = someValue /10; - while (dividend > 0) { - dividend = dividend /10; - digits++; - } - // return the number of digits: - return digits; -} - -void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.print(rssi); - Serial.println(" dBm"); -} - - - diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino deleted file mode 100644 index 3634ed762..000000000 --- a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino +++ /dev/null @@ -1,172 +0,0 @@ -/* - Wifi Cosm sensor client with Strings - - This sketch connects an analog sensor to Cosm (http://www.cosm.com) - using a Arduino Wifi shield. - - This example is written for a network using WPA encryption. For - WEP or WPA, change the Wifi.begin() call accordingly. - - This example has been updated to use version 2.0 of the cosm.com API. - To make it work, create a feed with a datastream, and give it the ID - sensor1. Or change the code below to match your feed. - - This example uses the String library, which is part of the Arduino core from - version 0019. - - Circuit: - * Analog sensor attached to analog in 0 - * Wifi shield attached to pins 10, 11, 12, 13 - - created 16 Mar 2012 - modified 31 May 2012 - by Tom Igoe - - This code is in the public domain. - - */ - -#include -#include - -#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here -#define FEEDID 00000 // replace your feed ID -#define USERAGENT "My Arduino Project" // user agent is the project name - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password - -int status = WL_IDLE_STATUS; - -// initialize the library instance: -WiFiClient client; - -// if you don't want to use DNS (and reduce your sketch size) -// use the numeric IP instead of the name for the server: -//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com -char server[] = "api.cosm.com"; // name address for pachube API - -unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds -boolean lastConnected = false; // state of the connection last time through the main loop -const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com - -void setup() { - // start serial port: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid, pass); - - // wait 10 seconds for connection: - delay(10000); - } - // you're connected now, so print out the status: - printWifiStatus(); -} - -void loop() { - // read the analog sensor: - int sensorReading = analogRead(A0); - // convert the data to a String to send it: - - String dataString = "sensor1,"; - dataString += sensorReading; - - // you can append multiple readings to this String if your - // pachube feed is set up to handle multiple values: - int otherSensorReading = analogRead(A1); - dataString += "\nsensor2,"; - dataString += otherSensorReading; - - // if there's incoming data from the net connection. - // send it out the serial port. This is for debugging - // purposes only: - if (client.available()) { - char c = client.read(); - Serial.print(c); - } - - // if there's no net connection, but there was one last time - // through the loop, then stop the client: - if (!client.connected() && lastConnected) { - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - } - - // if you're not connected, and ten seconds have passed since - // your last connection, then connect again and send data: - if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { - sendData(dataString); - } - // store the state of the connection for next time through - // the loop: - lastConnected = client.connected(); -} - -// this method makes a HTTP connection to the server: -void sendData(String thisData) { - // if there's a successful connection: - if (client.connect(server, 80)) { - Serial.println("connecting..."); - // send the HTTP PUT request: - client.print("PUT /v2/feeds/"); - client.print(FEEDID); - client.println(".csv HTTP/1.1"); - client.println("Host: api.cosm.com"); - client.print("X-ApiKey: "); - client.println(APIKEY); - client.print("User-Agent: "); - client.println(USERAGENT); - client.print("Content-Length: "); - client.println(thisData.length()); - - // last pieces of the HTTP PUT request: - client.println("Content-Type: text/csv"); - client.println("Connection: close"); - client.println(); - - // here's the actual content of the PUT request: - client.println(thisData); - } - else { - // if you couldn't make a connection: - Serial.println("connection failed"); - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - } - // note the time that the connection was made or attempted: - lastConnectionTime = millis(); -} - - -void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.print(rssi); - Serial.println(" dBm"); -} - - diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino deleted file mode 100644 index 54ec68b71..000000000 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ /dev/null @@ -1,117 +0,0 @@ - -/* - Web client - - This sketch connects to a website (http://www.google.com) - using a WiFi shield. - - This example is written for a network using WPA encryption. For - WEP or WPA, change the Wifi.begin() call accordingly. - - This example is written for a network using WPA encryption. For - WEP or WPA, change the Wifi.begin() call accordingly. - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) -int keyIndex = 0; // your network key Index number (needed only for WEP) - -int status = WL_IDLE_STATUS; -// if you don't want to use DNS (and reduce your sketch size) -// use the numeric IP instead of the name for the server: -IPAddress server(173,194,73,105); // numeric IP for Google (no DNS) -//char server[] = "www.google.com"; // name address for Google (using DNS) - -// Initialize the Ethernet client library -// with the IP address and port of the server -// that you want to connect to (port 80 is default for HTTP): -WiFiClient client; - -void setup() { - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid, pass); - - // wait 10 seconds for connection: - delay(10000); - } - Serial.println("Connected to wifi"); - printWifiStatus(); - - Serial.println("\nStarting connection to server..."); - // if you get a connection, report back via serial: - if (client.connect(server, 80)) { - Serial.println("connected to server"); - // Make a HTTP request: - client.println("GET /search?q=arduino HTTP/1.1"); - client.println("Host:www.google.com"); - client.println("Connection: close"); - client.println(); - } -} - -void loop() { - // if there are incoming bytes available - // from the server, read them and print them: - while (client.available()) { - char c = client.read(); - Serial.write(c); - } - - // if the server's disconnected, stop the client: - if (!client.connected()) { - Serial.println(); - Serial.println("disconnecting from server."); - client.stop(); - - // do nothing forevermore: - while(true); - } -} - - -void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.print(rssi); - Serial.println(" dBm"); -} - - - - - diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino deleted file mode 100644 index 1c623d12e..000000000 --- a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino +++ /dev/null @@ -1,135 +0,0 @@ -/* - Repeating Wifi Web client - - This sketch connects to a a web server and makes a request - using an Arduino Wifi shield. - - Circuit: - * Wifi shield attached to pins 10, 11, 12, 13 - - created 23 April 2012 - modifide 31 May 2012 - by Tom Igoe - - http://arduino.cc/en/Tutorial/WifiWebClientRepeating - This code is in the public domain. - */ - -#include -#include - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password -int keyIndex = 0; // your network key Index number (needed only for WEP) - -int status = WL_IDLE_STATUS; - -// Initialize the Wifi client library -WiFiClient client; - -// server address: -char server[] = "www.arduino.cc"; -//IPAddress server(64,131,82,241); - -unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds -boolean lastConnected = false; // state of the connection last time through the main loop -const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds - -void setup() { - // start serial port: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid, pass); - - // wait 10 seconds for connection: - delay(10000); - } - // you're connected now, so print out the status: - printWifiStatus(); -} - -void loop() { - // if there's incoming data from the net connection. - // send it out the serial port. This is for debugging - // purposes only: - while (client.available()) { - char c = client.read(); - Serial.write(c); - } - - // if there's no net connection, but there was one last time - // through the loop, then stop the client: - if (!client.connected() && lastConnected) { - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - } - - // if you're not connected, and ten seconds have passed since - // your last connection, then connect again and send data: - if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { - httpRequest(); - } - // store the state of the connection for next time through - // the loop: - lastConnected = client.connected(); -} - -// this method makes a HTTP connection to the server: -void httpRequest() { - // if there's a successful connection: - if (client.connect(server, 80)) { - Serial.println("connecting..."); - // send the HTTP PUT request: - client.println("GET /latest.txt HTTP/1.1"); - client.println("Host: www.arduino.cc"); - client.println("User-Agent: arduino-ethernet"); - client.println("Connection: close"); - client.println(); - - // note the time that the connection was made: - lastConnectionTime = millis(); - } - else { - // if you couldn't make a connection: - Serial.println("connection failed"); - Serial.println("disconnecting."); - client.stop(); - } -} - - -void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.print(rssi); - Serial.println(" dBm"); -} - - - - - - diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino deleted file mode 100644 index 72bd8e0b9..000000000 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ /dev/null @@ -1,129 +0,0 @@ -/* - Web Server - - A simple web server that shows the value of the analog input pins. - using a WiFi shield. - - This example is written for a network using WPA encryption. For - WEP or WPA, change the Wifi.begin() call accordingly. - - Circuit: - * WiFi shield attached - * Analog inputs attached to pins A0 through A5 (optional) - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe - */ -#include -#include - - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password -int keyIndex = 0; // your network key Index number (needed only for WEP) - -int status = WL_IDLE_STATUS; - -WiFiServer server(80); - -void setup() { - // start serial port: - Serial.begin(9600); - - // check for the presence of the shield: - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while(true); - } - - // attempt to connect to Wifi network: - while ( status != WL_CONNECTED) { - Serial.print("Attempting to connect to SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid, pass); - - // wait 10 seconds for connection: - delay(10000); - } - server.begin(); - // you're connected now, so print out the status: - printWifiStatus(); -} - - -void loop() { - // listen for incoming clients - WiFiClient client = server.available(); - if (client) { - Serial.println("new client"); - // an http request ends with a blank line - boolean currentLineIsBlank = true; - while (client.connected()) { - if (client.available()) { - char c = client.read(); - Serial.write(c); - // if you've gotten to the end of the line (received a newline - // character) and the line is blank, the http request has ended, - // so you can send a reply - if (c == '\n' && currentLineIsBlank) { - // send a standard http response header - client.println("HTTP/1.1 200 OK"); - client.println("Content-Type: text/html"); - client.println("Connnection: close"); - client.println(); - client.println(""); - client.println(""); - // add a meta refresh tag, so the browser pulls again every 5 seconds: - client.println(""); - // output the value of each analog input pin - for (int analogChannel = 0; analogChannel < 6; analogChannel++) { - int sensorReading = analogRead(analogChannel); - client.print("analog input "); - client.print(analogChannel); - client.print(" is "); - client.print(sensorReading); - client.println("
"); - } - client.println(""); - break; - } - if (c == '\n') { - // you're starting a new line - currentLineIsBlank = true; - } - else if (c != '\r') { - // you've gotten a character on the current line - currentLineIsBlank = false; - } - } - } - // give the web browser time to receive the data - delay(1); - // close the connection: - client.stop(); - Serial.println("client disonnected"); - } -} - - -void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.print(rssi); - Serial.println(" dBm"); -} - diff --git a/WiFi/keywords.txt b/WiFi/keywords.txt deleted file mode 100755 index 47704cd00..000000000 --- a/WiFi/keywords.txt +++ /dev/null @@ -1,43 +0,0 @@ -####################################### -# Syntax Coloring Map For WiFi -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### - -WiFi KEYWORD1 -Client KEYWORD1 -Server KEYWORD1 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### - -status KEYWORD2 -connect KEYWORD2 -write KEYWORD2 -available KEYWORD2 -read KEYWORD2 -flush KEYWORD2 -stop KEYWORD2 -connected KEYWORD2 -begin KEYWORD2 -disconnect KEYWORD2 -macAddress KEYWORD2 -localIP KEYWORD2 -subnetMask KEYWORD2 -gatewayIP KEYWORD2 -SSID KEYWORD2 -BSSID KEYWORD2 -RSSI KEYWORD2 -encryptionType KEYWORD2 -getResult KEYWORD2 -getSocket KEYWORD2 -WiFiClient KEYWORD2 -WiFiServer KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### - diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h deleted file mode 100644 index 9f71055b2..000000000 --- a/WiFi/utility/debug.h +++ /dev/null @@ -1,77 +0,0 @@ -//*********************************************/ -// -// File: debug.h -// -// Author: dlf (Metodo2 srl) -// -//********************************************/ - - -#ifndef Debug_H -#define Debug_H - -#include -#include - -#define PRINT_FILE_LINE() do { \ - Serial.print("[");Serial.print(__FILE__); \ - Serial.print("::");Serial.print(__LINE__);Serial.print("]");\ -}while (0); - -#ifdef _DEBUG_ - -#define INFO(format, args...) do { \ - char buf[250]; \ - sprintf(buf, format, args); \ - Serial.println(buf); \ -} while(0); - -#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,16);Serial.print(",");Serial.println(y,16); \ -}while (0); - - -#else -#define INFO1(x) do {} while(0); -#define INFO2(x,y) do {} while(0); -#define INFO(format, args...) do {} while(0); -#endif - -#if 0 -#define WARN(args) do { PRINT_FILE_LINE() \ - Serial.print("-W-"); Serial.println(args); \ -}while (0); -#else -#define WARN(args) do {} while (0); -#endif - -#if _DEBUG_SPI_ -#define DBG_PIN2 5 -#define DBG_PIN 4 - -#define START() digitalWrite(DBG_PIN2, HIGH); -#define END() digitalWrite(DBG_PIN2, LOW); -#define SET_TRIGGER() digitalWrite(DBG_PIN, HIGH); -#define RST_TRIGGER() digitalWrite(DBG_PIN, LOW); - -#define INIT_TRIGGER() pinMode(DBG_PIN, OUTPUT); \ - pinMode(DBG_PIN2, OUTPUT); \ - RST_TRIGGER() -#define TOGGLE_TRIGGER() SET_TRIGGER() \ - delayMicroseconds(2); \ - RST_TRIGGER() -#else -#define START() -#define END() -#define SET_TRIGGER() -#define RST_TRIGGER() -#define INIT_TRIGGER() -#define TOGGLE_TRIGGER() -#endif - -#endif diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp deleted file mode 100644 index a325d5a06..000000000 --- a/WiFi/utility/server_drv.cpp +++ /dev/null @@ -1,260 +0,0 @@ -//#define _DEBUG_ - -#include "server_drv.h" - -#include "Arduino.h" -#include "spi_drv.h" - -extern "C" { -#include "wl_types.h" -#include "debug.h" -} - - -// Start server TCP on port specified -void ServerDrv::startServer(uint16_t port, uint8_t sock) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2); - SpiDrv::sendParam(port); - SpiDrv::sendParam(&sock, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); -} - -// Start server TCP on port specified -void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_3); - SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress)); - SpiDrv::sendParam(port); - SpiDrv::sendParam(&sock, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); -} - -// Start server TCP on port specified -void ServerDrv::stopClient(uint8_t sock) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); -} - - -uint8_t ServerDrv::getServerState(uint8_t sock) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); - return _data; -} - -uint8_t ServerDrv::getClientState(uint8_t sock) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); - return _data; -} - -uint8_t ServerDrv::availData(uint8_t sock) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); - - if (_dataLen!=0) - { - return (_data == 1); - } - return false; -} - -bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2); - SpiDrv::sendParam(&sock, sizeof(sock)); - SpiDrv::sendParam(peek, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseData8(GET_DATA_TCP_CMD, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); - if (_dataLen!=0) - { - *data = _data; - return true; - } - return false; -} - -bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendBuffer(&sock, sizeof(sock), LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); - if (*_dataLen!=0) - { - return true; - } - return false; -} - - -bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2); - SpiDrv::sendBuffer(&sock, sizeof(sock)); - SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen)) - { - WARN("error waitResponse"); - } - SpiDrv::spiSlaveDeselect(); - if (_dataLen!=0) - { - return (_data == 1); - } - return false; -} - - -uint8_t ServerDrv::checkDataSent(uint8_t sock) -{ - const uint16_t TIMEOUT_DATA_SENT = 250; - static uint16_t timeout = 0; - uint8_t _data = 0; - uint8_t _dataLen = 0; - - do { - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse isDataSent"); - } - SpiDrv::spiSlaveDeselect(); - - if (_data) timeout = 0; - else{ - ++timeout; - delay(10); - } - - }while((_data==0)&&(timeout -#include "wifi_spi.h" - -class ServerDrv -{ -public: - // Start server TCP on port specified - static void startServer(uint16_t port, uint8_t sock); - - static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock); - - static void stopClient(uint8_t sock); - - static uint8_t getServerState(uint8_t sock); - - static uint8_t getClientState(uint8_t sock); - - static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0); - - static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len); - - static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len); - - static uint8_t availData(uint8_t sock); - - static uint8_t checkDataSent(uint8_t sock); -}; - -extern ServerDrv serverDrv; - -#endif diff --git a/WiFi/utility/socket.c b/WiFi/utility/socket.c deleted file mode 100644 index 665073b04..000000000 --- a/WiFi/utility/socket.c +++ /dev/null @@ -1,20 +0,0 @@ -/* -* -@file socket.c -@brief define function of socket API -* -*/ -#include -#include "socket.h" - -SOCKET socket(uint8 protocol) {return 0;} // Opens a socket(TCP or UDP or IP_RAW mode) -void close(SOCKET s) {} // Close socket -uint8 connect(SOCKET s, uint8 * addr, uint16 port) {return 0;} // Establish TCP connection (Active connection) -void disconnect(SOCKET s) {} // disconnect the connection -uint8 listen(SOCKET s) { return 0;} // Establish TCP connection (Passive connection) -uint16 send(SOCKET s, const uint8 * buf, uint16 len) { return 0;} // Send data (TCP) -uint16 recv(SOCKET s, uint8 * buf, uint16 len) {return 0;} // Receive data (TCP) -uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port) {return 0;} // Send data (UDP/IP RAW) -uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port) {return 0;} // Receive data (UDP/IP RAW) - -uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len) {return 0;} diff --git a/WiFi/utility/socket.h b/WiFi/utility/socket.h deleted file mode 100644 index 9b06d00d1..000000000 --- a/WiFi/utility/socket.h +++ /dev/null @@ -1,87 +0,0 @@ -/* -* -@file socket.h -@brief define function of socket API -* -*/ - -#ifndef _SOCKET_H_ -#define _SOCKET_H_ - -#define TCP_SOCKET 1 -#define UDP_SOCKET 2 -#define RAW_SOCKET 3 - -#define SOCK_NOT_AVAIL 255 - -#include "wl_definitions.h" -/** - * The 8-bit signed data type. - */ -typedef char int8; -/** - * The volatile 8-bit signed data type. - */ -typedef volatile char vint8; -/** - * The 8-bit unsigned data type. - */ -typedef unsigned char uint8; -/** - * The volatile 8-bit unsigned data type. - */ -typedef volatile unsigned char vuint8; - -/** - * The 16-bit signed data type. - */ -typedef int int16; -/** - * The volatile 16-bit signed data type. - */ -typedef volatile int vint16; -/** - * The 16-bit unsigned data type. - */ -typedef unsigned int uint16; -/** - * The volatile 16-bit unsigned data type. - */ -typedef volatile unsigned int vuint16; -/** - * The 32-bit signed data type. - */ -typedef long int32; -/** - * The volatile 32-bit signed data type. - */ -typedef volatile long vint32; -/** - * The 32-bit unsigned data type. - */ -typedef unsigned long uint32; -/** - * The volatile 32-bit unsigned data type. - */ -typedef volatile unsigned long vuint32; - -/* bsd */ -typedef uint8 u_char; /**< 8-bit value */ -typedef uint16_t SOCKET; -typedef uint16 u_short; /**< 16-bit value */ -typedef uint16 u_int; /**< 16-bit value */ -typedef uint32 u_long; /**< 32-bit value */ - -extern SOCKET socket(uint8 protocol); // Opens a socket(TCP or UDP or IP_RAW mode) -extern void close(SOCKET s); // Close socket -extern uint8 connect(SOCKET s, uint8 * addr, uint16 port); // Establish TCP connection (Active connection) -extern void disconnect(SOCKET s); // disconnect the connection -extern uint8 listen(SOCKET s); // Establish TCP connection (Passive connection) -extern uint16 send(SOCKET s, const uint8 * buf, uint16 len); // Send data (TCP) -extern uint16 recv(SOCKET s, uint8 * buf, uint16 len); // Receive data (TCP) -extern uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port); // Send data (UDP/IP RAW) -extern uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port); // Receive data (UDP/IP RAW) - -extern uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len); -#endif -/* _SOCKET_H_ */ diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp deleted file mode 100644 index ceef832c0..000000000 --- a/WiFi/utility/spi_drv.cpp +++ /dev/null @@ -1,503 +0,0 @@ - -#include "Arduino.h" -#include "spi_drv.h" -#include "pins_arduino.h" -//#define _DEBUG_ -extern "C" { -#include "debug.h" -} - -#define DATAOUT 11//MOSI -#define DATAIN 12//MISO -#define SPICLOCK 13//sck -#define SLAVESELECT 10//ss -#define SLAVEREADY 3 - -#define DELAY_100NS do { asm volatile("nop"); }while(0); -#define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); }while(++ii 0) && (_readChar != waitChar)); - return (_readChar == waitChar); -} - -int SpiDrv::readAndCheckChar(char checkChar, char* readChar) -{ - getParam((uint8_t*)readChar); - - return (*readChar == checkChar); -} - -char SpiDrv::readChar() -{ - uint8_t readChar = 0; - getParam(&readChar); - return readChar; -} - -#define WAIT_START_CMD(x) waitSpiChar(START_CMD) - -#define IF_CHECK_START_CMD(x) \ - if (!WAIT_START_CMD(_data)) \ - { \ - TOGGLE_TRIGGER() \ - WARN("Error waiting START_CMD"); \ - return 0; \ - }else \ - -#define CHECK_DATA(check, x) \ - if (!readAndCheckChar(check, &x)) \ - { \ - TOGGLE_TRIGGER() \ - WARN("Reply error"); \ - INFO2(check, (uint8_t)x); \ - return 0; \ - }else \ - -#define waitSlaveReady() (digitalRead(SLAVEREADY) == LOW) -#define waitSlaveSign() (digitalRead(SLAVEREADY) == HIGH) -#define waitSlaveSignalH() while(digitalRead(SLAVEREADY) != HIGH){} -#define waitSlaveSignalL() while(digitalRead(SLAVEREADY) != LOW){} - -void SpiDrv::waitForSlaveSign() -{ - while (!waitSlaveSign()); -} - -void SpiDrv::waitForSlaveReady() -{ - while (!waitSlaveReady()); -} - -void SpiDrv::getParam(uint8_t* param) -{ - // Get Params data - *param = spiTransfer(DUMMY_DATA); - DELAY_TRANSFER(); -} - -int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len) -{ - char _data = 0; - int ii = 0; - - IF_CHECK_START_CMD(_data) - { - CHECK_DATA(cmd | REPLY_FLAG, _data){}; - - CHECK_DATA(numParam, _data); - { - readParamLen8(param_len); - for (ii=0; ii<(*param_len); ++ii) - { - // Get Params data - //param[ii] = spiTransfer(DUMMY_DATA); - getParam(¶m[ii]); - } - } - - readAndCheckChar(END_CMD, &_data); - } - - return 1; -} -/* -int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len) -{ - char _data = 0; - int i =0, ii = 0; - - IF_CHECK_START_CMD(_data) - { - CHECK_DATA(cmd | REPLY_FLAG, _data){}; - - CHECK_DATA(numParam, _data); - { - readParamLen16(param_len); - for (ii=0; ii<(*param_len); ++ii) - { - // Get Params data - param[ii] = spiTransfer(DUMMY_DATA); - } - } - - readAndCheckChar(END_CMD, &_data); - } - - return 1; -} -*/ - -int SpiDrv::waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len) -{ - char _data = 0; - uint16_t ii = 0; - - IF_CHECK_START_CMD(_data) - { - CHECK_DATA(cmd | REPLY_FLAG, _data){}; - - uint8_t numParam = readChar(); - if (numParam != 0) - { - readParamLen16(param_len); - for (ii=0; ii<(*param_len); ++ii) - { - // Get Params data - param[ii] = spiTransfer(DUMMY_DATA); - } - } - - readAndCheckChar(END_CMD, &_data); - } - - return 1; -} - -int SpiDrv::waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len) -{ - char _data = 0; - int ii = 0; - - IF_CHECK_START_CMD(_data) - { - CHECK_DATA(cmd | REPLY_FLAG, _data){}; - - uint8_t numParam = readChar(); - if (numParam != 0) - { - readParamLen8(param_len); - for (ii=0; ii<(*param_len); ++ii) - { - // Get Params data - param[ii] = spiTransfer(DUMMY_DATA); - } - } - - readAndCheckChar(END_CMD, &_data); - } - - return 1; -} - -int SpiDrv::waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params) -{ - char _data = 0; - int i =0, ii = 0; - - - IF_CHECK_START_CMD(_data) - { - CHECK_DATA(cmd | REPLY_FLAG, _data){}; - - uint8_t _numParam = readChar(); - if (_numParam != 0) - { - for (i=0; i<_numParam; ++i) - { - params[i].paramLen = readParamLen8(); - for (ii=0; ii maxNumParams) - { - numParam = maxNumParams; - } - *numParamRead = numParam; - if (numParam != 0) - { - for (i=0; i maxNumParams) - { - numParam = maxNumParams; - } - *numParamRead = numParam; - if (numParam != 0) - { - for (i=0; i>8)); - spiTransfer((uint8_t)(param_len & 0xff)); -} - -uint8_t SpiDrv::readParamLen8(uint8_t* param_len) -{ - uint8_t _param_len = spiTransfer(DUMMY_DATA); - if (param_len != NULL) - { - *param_len = _param_len; - } - return _param_len; -} - -uint16_t SpiDrv::readParamLen16(uint16_t* param_len) -{ - uint16_t _param_len = spiTransfer(DUMMY_DATA)<<8 | (spiTransfer(DUMMY_DATA)& 0xff); - if (param_len != NULL) - { - *param_len = _param_len; - } - return _param_len; -} - - -void SpiDrv::sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam) -{ - uint16_t i = 0; - - // Send Spi paramLen - sendParamLen16(param_len); - - // Send Spi param data - for (i=0; i>8)); - spiTransfer((uint8_t)(param & 0xff)); - - // if lastParam==1 Send Spi END CMD - if (lastParam == 1) - spiTransfer(END_CMD); -} - -/* Cmd Struct Message */ -/* _________________________________________________________________________________ */ -/*| START CMD | C/R | CMD |[TOT LEN]| N.PARAM | PARAM LEN | PARAM | .. | END CMD | */ -/*|___________|______|______|_________|_________|___________|________|____|_________| */ -/*| 8 bit | 1bit | 7bit | 8bit | 8bit | 8bit | nbytes | .. | 8bit | */ -/*|___________|______|______|_________|_________|___________|________|____|_________| */ - -void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam) -{ - // Send Spi START CMD - spiTransfer(START_CMD); - - //waitForSlaveSign(); - //wait the interrupt trigger on slave - delayMicroseconds(SPI_START_CMD_DELAY); - - // Send Spi C + cmd - spiTransfer(cmd & ~(REPLY_FLAG)); - - // Send Spi totLen - //spiTransfer(totLen); - - // Send Spi numParam - spiTransfer(numParam); - - // If numParam == 0 send END CMD - if (numParam == 0) - spiTransfer(END_CMD); - -} - -SpiDrv spiDrv; diff --git a/WiFi/utility/spi_drv.h b/WiFi/utility/spi_drv.h deleted file mode 100644 index 5c2e7063f..000000000 --- a/WiFi/utility/spi_drv.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef SPI_Drv_h -#define SPI_Drv_h - -#include -#include "wifi_spi.h" - -#define SPI_START_CMD_DELAY 12 - -#define NO_LAST_PARAM 0 -#define LAST_PARAM 1 - -#define DUMMY_DATA 0xFF - -#define WAIT_FOR_SLAVE_SELECT() \ - SpiDrv::waitForSlaveReady(); \ - SpiDrv::spiSlaveSelect(); - - - -class SpiDrv -{ -private: - //static bool waitSlaveReady(); - static void waitForSlaveSign(); - static void getParam(uint8_t* param); -public: - - static void begin(); - - static void end(); - - static void spiDriverInit(); - - static void spiSlaveSelect(); - - static void spiSlaveDeselect(); - - static char spiTransfer(volatile char data); - - static void waitForSlaveReady(); - - //static int waitSpiChar(char waitChar, char* readChar); - - static int waitSpiChar(unsigned char waitChar); - - static int readAndCheckChar(char checkChar, char* readChar); - - static char readChar(); - - static int waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params); - - static int waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len); - - static int waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len); - - static int waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len); - /* - static int waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams); - - static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len); -*/ - static int waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams); - - static void sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM); - - static void sendParamLen8(uint8_t param_len); - - static void sendParamLen16(uint16_t param_len); - - static uint8_t readParamLen8(uint8_t* param_len = NULL); - - static uint16_t readParamLen16(uint16_t* param_len = NULL); - - static void sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam = NO_LAST_PARAM); - - static void sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM); - - static void sendCmd(uint8_t cmd, uint8_t numParam); -}; - -extern SpiDrv spiDrv; - -#endif diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp deleted file mode 100644 index 45da70bdb..000000000 --- a/WiFi/utility/wifi_drv.cpp +++ /dev/null @@ -1,470 +0,0 @@ -#include -#include -#include - -#include "Arduino.h" -#include "spi_drv.h" -#include "wifi_drv.h" - -#define _DEBUG_ - -extern "C" { -#include "wifi_spi.h" -#include "wl_types.h" -#include "debug.h" -} - -// Array of data to cache the information related to the networks discovered -char WiFiDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}}; -int32_t WiFiDrv::_networkRssi[WL_NETWORKS_LIST_MAXNUM] = { 0 }; -uint8_t WiFiDrv::_networkEncr[WL_NETWORKS_LIST_MAXNUM] = { 0 }; - -// Cached values of retrieved data -char WiFiDrv::_ssid[] = {0}; -uint8_t WiFiDrv::_bssid[] = {0}; -uint8_t WiFiDrv::_mac[] = {0}; -uint8_t WiFiDrv::_localIp[] = {0}; -uint8_t WiFiDrv::_subnetMask[] = {0}; -uint8_t WiFiDrv::_gatewayIp[] = {0}; - - -// Private Methods - -void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip) -{ - tParam params[PARAM_NUMS_3] = { {0, (char*)ip}, {0, (char*)mask}, {0, (char*)gwip}}; - - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_IPADDR_CMD, PARAM_NUMS_1); - - uint8_t _dummy = DUMMY_DATA; - SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params); - - SpiDrv::spiSlaveDeselect(); -} - -// Public Methods - - -void WiFiDrv::wifiDriverInit() -{ - SpiDrv::begin(); -} - -int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1); - SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - _data = WL_FAILURE; - } - SpiDrv::spiSlaveDeselect(); - - return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : WL_FAILURE; -} - -int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2); - SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); - SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - _data = WL_FAILURE; - } - SpiDrv::spiSlaveDeselect(); - return _data; -} - - -int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(SET_KEY_CMD, PARAM_NUMS_3); - SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); - SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM); - SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - _data = WL_FAILURE; - } - SpiDrv::spiSlaveDeselect(); - return _data; -} - -int8_t WiFiDrv::disconnect() -{ - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1); - - uint8_t _dummy = DUMMY_DATA; - SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); - - SpiDrv::spiSlaveDeselect(); - - return result; -} - -uint8_t WiFiDrv::getConnectionStatus() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = -1; - uint8_t _dataLen = 0; - SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); - - SpiDrv::spiSlaveDeselect(); - - return _data; -} - -uint8_t* WiFiDrv::getMacAddress() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_MACADDR_CMD, PARAM_NUMS_1); - - uint8_t _dummy = DUMMY_DATA; - SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _dataLen = 0; - SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); - - SpiDrv::spiSlaveDeselect(); - - return _mac; -} - -void WiFiDrv::getIpAddress(IPAddress& ip) -{ - getNetworkData(_localIp, _subnetMask, _gatewayIp); - ip = _localIp; -} - - void WiFiDrv::getSubnetMask(IPAddress& mask) - { - getNetworkData(_localIp, _subnetMask, _gatewayIp); - mask = _subnetMask; - } - - void WiFiDrv::getGatewayIP(IPAddress& ip) - { - getNetworkData(_localIp, _subnetMask, _gatewayIp); - ip = _gatewayIp; - } - -char* WiFiDrv::getCurrentSSID() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1); - - uint8_t _dummy = DUMMY_DATA; - SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _dataLen = 0; - SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); - - SpiDrv::spiSlaveDeselect(); - - return _ssid; -} - -uint8_t* WiFiDrv::getCurrentBSSID() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1); - - uint8_t _dummy = DUMMY_DATA; - SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _dataLen = 0; - SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); - - SpiDrv::spiSlaveDeselect(); - - return _bssid; -} - -int32_t WiFiDrv::getCurrentRSSI() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1); - - uint8_t _dummy = DUMMY_DATA; - SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _dataLen = 0; - int32_t rssi = 0; - SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); - - SpiDrv::spiSlaveDeselect(); - - return rssi; -} - -uint8_t WiFiDrv::getCurrentEncryptionType() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1); - - uint8_t _dummy = DUMMY_DATA; - SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t dataLen = 0; - uint8_t encType = 0; - SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); - - SpiDrv::spiSlaveDeselect(); - - return encType; -} - -int8_t WiFiDrv::startScanNetworks() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - - if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse"); - _data = WL_FAILURE; - } - - SpiDrv::spiSlaveDeselect(); - - return (_data == WL_FAILURE)? _data : WL_SUCCESS; -} - - -uint8_t WiFiDrv::getScanNetworks() -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t ssidListNum = 0; - SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM); - - SpiDrv::spiSlaveDeselect(); - - return ssidListNum; -} - -char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) -{ - if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; - - return _networkSsid[networkItem]; -} - -uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) -{ - if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; - - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1); - - SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t dataLen = 0; - uint8_t encType = 0; - SpiDrv::waitResponseCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); - - SpiDrv::spiSlaveDeselect(); - - return encType; -} - -int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) -{ - if (networkItem >= WL_NETWORKS_LIST_MAXNUM) - return NULL; - int32_t networkRssi = 0; - - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1); - - SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t dataLen = 0; - SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen); - - SpiDrv::spiSlaveDeselect(); - - return networkRssi; -} - -uint8_t WiFiDrv::reqHostByName(const char* aHostname) -{ - WAIT_FOR_SLAVE_SELECT(); - - // Send Command - SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1); - SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen); - - SpiDrv::spiSlaveDeselect(); - - return result; -} - -int WiFiDrv::getHostByName(IPAddress& aResult) -{ - uint8_t _ipAddr[WL_IPV4_LENGTH]; - IPAddress dummy(0xFF,0xFF,0xFF,0xFF); - int result = 0; - - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0); - - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); - - // Wait for reply - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen)) - { - WARN("error waitResponse"); - }else{ - aResult = _ipAddr; - result = (aResult != dummy); - } - SpiDrv::spiSlaveDeselect(); - return result; -} - -int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) -{ - uint8_t retry = 10; - if (reqHostByName(aHostname)) - { - while(!getHostByName(aResult) && --retry > 0) - { - delay(1000); - } - }else{ - return 0; - } - return (retry>0); -} - -WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h deleted file mode 100644 index 37563cba6..000000000 --- a/WiFi/utility/wifi_drv.h +++ /dev/null @@ -1,208 +0,0 @@ -#ifndef WiFi_Drv_h -#define WiFi_Drv_h - -#include -#include "wifi_spi.h" -#include "IPAddress.h" - -// Key index length -#define KEY_IDX_LEN 1 -// 5 secs of delay to have the connection established -#define WL_DELAY_START_CONNECTION 5000 - -class WiFiDrv -{ -private: - // settings of requested network - static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH]; - static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM]; - static uint8_t _networkEncr[WL_NETWORKS_LIST_MAXNUM]; - - // settings of current selected network - static char _ssid[WL_SSID_MAX_LENGTH]; - static uint8_t _bssid[WL_MAC_ADDR_LENGTH]; - static uint8_t _mac[WL_MAC_ADDR_LENGTH]; - static uint8_t _localIp[WL_IPV4_LENGTH]; - static uint8_t _subnetMask[WL_IPV4_LENGTH]; - static uint8_t _gatewayIp[WL_IPV4_LENGTH]; - - /* - * Get network Data information - */ - static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip); - - static uint8_t reqHostByName(const char* aHostname); - - static int getHostByName(IPAddress& aResult); - -public: - - /* - * Driver initialization - */ - static void wifiDriverInit(); - - /* - * Set the desired network which the connection manager should try to - * connect to. - * - * The ssid of the desired network should be specified. - * - * param ssid: The ssid of the desired network. - * param ssid_len: Lenght of ssid string. - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); - - /* Start Wifi connection with passphrase - * the most secure supported mode will be automatically selected - * - * param ssid: Pointer to the SSID string. - * param ssid_len: Lenght of ssid string. - * param passphrase: Passphrase. Valid characters in a passphrase - * must be between ASCII 32-126 (decimal). - * param len: Lenght of passphrase string. - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); - - /* Start Wifi connection with WEP encryption. - * Configure a key into the device. The key type (WEP-40, WEP-104) - * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104). - * - * param ssid: Pointer to the SSID string. - * param ssid_len: Lenght of ssid string. - * param key_idx: The key index to set. Valid values are 0-3. - * param key: Key input buffer. - * param len: Lenght of key string. - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); - - /* - * Disconnect from the network - * - * return: WL_SUCCESS or WL_FAILURE - */ - static int8_t disconnect(); - - /* - * Disconnect from the network - * - * return: one value of wl_status_t enum - */ - static uint8_t getConnectionStatus(); - - /* - * Get the interface MAC address. - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - */ - static uint8_t* getMacAddress(); - - /* - * Get the interface IP address. - * - * return: copy the ip address value in IPAddress object - */ - static void getIpAddress(IPAddress& ip); - - /* - * Get the interface subnet mask address. - * - * return: copy the subnet mask address value in IPAddress object - */ - static void getSubnetMask(IPAddress& mask); - - /* - * Get the gateway ip address. - * - * return: copy the gateway ip address value in IPAddress object - */ - static void getGatewayIP(IPAddress& ip); - - /* - * Return the current SSID associated with the network - * - * return: ssid string - */ - static char* getCurrentSSID(); - - /* - * Return the current BSSID associated with the network. - * It is the MAC address of the Access Point - * - * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH - */ - static uint8_t* getCurrentBSSID(); - - /* - * Return the current RSSI /Received Signal Strength in dBm) - * associated with the network - * - * return: signed value - */ - static int32_t getCurrentRSSI(); - - /* - * Return the Encryption Type associated with the network - * - * return: one value of wl_enc_type enum - */ - static uint8_t getCurrentEncryptionType(); - - /* - * Start scan WiFi networks available - * - * return: Number of discovered networks - */ - static int8_t startScanNetworks(); - - /* - * Get the networks available - * - * return: Number of discovered networks - */ - static uint8_t getScanNetworks(); - - /* - * Return the SSID discovered during the network scan. - * - * param networkItem: specify from which network item want to get the information - * - * return: ssid string of the specified item on the networks scanned list - */ - static char* getSSIDNetoworks(uint8_t networkItem); - - /* - * Return the RSSI of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: signed value of RSSI of the specified item on the networks scanned list - */ - static int32_t getRSSINetoworks(uint8_t networkItem); - - /* - * Return the encryption type of the networks discovered during the scanNetworks - * - * param networkItem: specify from which network item want to get the information - * - * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list - */ - static uint8_t getEncTypeNetowrks(uint8_t networkItem); - - /* - * Resolve the given hostname to an IP address. - * param aHostname: Name to be resolved - * param aResult: IPAddress structure to store the returned IP address - * result: 1 if aIPAddrString was successfully converted to an IP address, - * else error code - */ - static int getHostByName(const char* aHostname, IPAddress& aResult); - -}; - -extern WiFiDrv wiFiDrv; - -#endif diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h deleted file mode 100644 index cd3fab7ce..000000000 --- a/WiFi/utility/wifi_spi.h +++ /dev/null @@ -1,143 +0,0 @@ -#ifndef WiFi_Spi_h -#define WiFi_Spi_h - -#include "wl_definitions.h" - -#define CMD_FLAG 0 -#define REPLY_FLAG 1<<7 -#define DATA_FLAG 0x40 - -#define WIFI_SPI_ACK 1 -#define WIFI_SPI_ERR 0xFF - -#define TIMEOUT_CHAR 1000 - -//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */ -#define NO_SOCKET_AVAIL 255 - -#define START_CMD 0xE0 -#define END_CMD 0xEE -#define ERR_CMD 0xEF - -enum { - SET_NET_CMD = 0x10, - SET_PASSPHRASE_CMD = 0x11, - SET_KEY_CMD = 0x12, - TEST_CMD = 0x13, - - GET_CONN_STATUS_CMD = 0x20, - GET_IPADDR_CMD = 0x21, - GET_MACADDR_CMD = 0x22, - GET_CURR_SSID_CMD = 0x23, - GET_CURR_BSSID_CMD = 0x24, - GET_CURR_RSSI_CMD = 0x25, - GET_CURR_ENCT_CMD = 0x26, - SCAN_NETWORKS = 0x27, - START_SERVER_TCP_CMD= 0x28, - GET_STATE_TCP_CMD = 0x29, - DATA_SENT_TCP_CMD = 0x2A, - AVAIL_DATA_TCP_CMD = 0x2B, - GET_DATA_TCP_CMD = 0x2C, - START_CLIENT_TCP_CMD= 0x2D, - STOP_CLIENT_TCP_CMD = 0x2E, - GET_CLIENT_STATE_TCP_CMD= 0x2F, - DISCONNECT_CMD = 0x30, - GET_IDX_SSID_CMD = 0x31, - GET_IDX_RSSI_CMD = 0x32, - GET_IDX_ENCT_CMD = 0x33, - REQ_HOST_BY_NAME_CMD= 0x34, - GET_HOST_BY_NAME_CMD= 0x35, - START_SCAN_NETWORKS = 0x36, - - // All command with DATA_FLAG 0x40 send a 16bit Len - - SEND_DATA_TCP_CMD = 0x44, - GET_DATABUF_TCP_CMD = 0x45, -}; - - -enum wl_tcp_state { - CLOSED = 0, - LISTEN = 1, - SYN_SENT = 2, - SYN_RCVD = 3, - ESTABLISHED = 4, - FIN_WAIT_1 = 5, - FIN_WAIT_2 = 6, - CLOSE_WAIT = 7, - CLOSING = 8, - LAST_ACK = 9, - TIME_WAIT = 10 -}; - - -enum numParams{ - PARAM_NUMS_0, - PARAM_NUMS_1, - PARAM_NUMS_2, - PARAM_NUMS_3, - PARAM_NUMS_4, - PARAM_NUMS_5, - MAX_PARAM_NUMS -}; - -#define MAX_PARAMS MAX_PARAM_NUMS-1 -#define PARAM_LEN_SIZE 1 - -typedef struct __attribute__((__packed__)) -{ - uint8_t paramLen; - char* param; -}tParam; - -typedef struct __attribute__((__packed__)) -{ - uint16_t dataLen; - char* data; -}tDataParam; - - -typedef struct __attribute__((__packed__)) -{ - unsigned char cmd; - unsigned char tcmd; - unsigned char nParam; - tParam params[MAX_PARAMS]; -}tSpiMsg; - -typedef struct __attribute__((__packed__)) -{ - unsigned char cmd; - unsigned char tcmd; - unsigned char nParam; - tDataParam params[MAX_PARAMS]; -}tSpiMsgData; - - -typedef struct __attribute__((__packed__)) -{ - unsigned char cmd; - unsigned char tcmd; - //unsigned char totLen; - unsigned char nParam; -}tSpiHdr; - -typedef struct __attribute__((__packed__)) -{ - uint8_t paramLen; - uint32_t param; -}tLongParam; - -typedef struct __attribute__((__packed__)) -{ - uint8_t paramLen; - uint16_t param; -}tIntParam; - -typedef struct __attribute__((__packed__)) -{ - uint8_t paramLen; - uint8_t param; -}tByteParam; - -#endif diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h deleted file mode 100644 index 15de781fc..000000000 --- a/WiFi/utility/wl_definitions.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * wl_definitions.h - * - * Created on: Mar 6, 2011 - * Author: dlafauci - */ - -#ifndef WL_DEFINITIONS_H_ -#define WL_DEFINITIONS_H_ - -// Maximum size of a SSID -#define WL_SSID_MAX_LENGTH 32 -// Length of passphrase. Valid lengths are 8-63. -#define WL_WPA_KEY_MAX_LENGTH 63 -// Length of key in bytes. Valid values are 5 and 13. -#define WL_WEP_KEY_MAX_LENGTH 13 -// Size of a MAC-address or BSSID -#define WL_MAC_ADDR_LENGTH 6 -// Size of a MAC-address or BSSID -#define WL_IPV4_LENGTH 4 -// Maximum size of a SSID list -#define WL_NETWORKS_LIST_MAXNUM 10 -// Maxmium number of socket -#define MAX_SOCK_NUM 4 -//Maximum number of attempts to establish wifi connection -#define WL_MAX_ATTEMPT_CONNECTION 10 - -typedef enum { - WL_NO_SHIELD = 255, - WL_IDLE_STATUS = 0, - WL_NO_SSID_AVAIL, - WL_SCAN_COMPLETED, - WL_CONNECTED, - WL_CONNECT_FAILED, - WL_CONNECTION_LOST, - WL_DISCONNECTED -} wl_status_t; - -/* Encryption modes */ -enum wl_enc_type { /* Values map to 802.11 encryption suites... */ - ENC_TYPE_WEP = 5, - ENC_TYPE_TKIP = 2, - ENC_TYPE_CCMP = 4, - /* ... except these two, 7 and 8 are reserved in 802.11-2007 */ - ENC_TYPE_NONE = 7, - ENC_TYPE_AUTO = 8 -}; - - -#endif /* WL_DEFINITIONS_H_ */ diff --git a/WiFi/utility/wl_types.h b/WiFi/utility/wl_types.h deleted file mode 100644 index 82b309d7f..000000000 --- a/WiFi/utility/wl_types.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * wl_types.h - * - * Created on: Jul 30, 2010 - * Author: dlafauci - */ - - -#ifndef _WL_TYPES_H_ -#define _WL_TYPES_H_ - -#include - -typedef enum { - WL_FAILURE = -1, - WL_SUCCESS = 1, -} wl_error_code_t; - -/* Authentication modes */ -enum wl_auth_mode { - AUTH_MODE_INVALID, - AUTH_MODE_AUTO, - AUTH_MODE_OPEN_SYSTEM, - AUTH_MODE_SHARED_KEY, - AUTH_MODE_WPA, - AUTH_MODE_WPA2, - AUTH_MODE_WPA_PSK, - AUTH_MODE_WPA2_PSK -}; - -#endif //_WL_TYPES_H_ From 9c63ffb8b730e421d4c945a021d61cd800e103e0 Mon Sep 17 00:00:00 2001 From: Federico Vanzati Date: Wed, 6 Jun 2012 11:28:21 +0200 Subject: [PATCH 14/20] Revert "Binary without staxk traces" This reverts commit 3e469d7a612405642e616d6af165f4573b416e3f. --- WiFi/WiFi.cpp | 194 +++++++ WiFi/WiFi.h | 177 ++++++ WiFi/WiFiClient.cpp | 174 ++++++ WiFi/WiFiClient.h | 40 ++ WiFi/WiFiServer.cpp | 88 +++ WiFi/WiFiServer.h | 27 + .../ConnectWithWEP/ConnectWithWEP.ino | 123 +++++ .../ConnectWithWPA/ConnectWithWPA.ino | 113 ++++ WiFi/examples/ScanNetworks/ScanNetworks.ino | 95 ++++ .../WifiChatServer/WifiChatServer.ino | 108 ++++ .../WifiCosmClient/WifiCosmClient.ino | 185 +++++++ .../WifiCosmClientString.ino | 172 ++++++ WiFi/examples/WifiWebClient/WifiWebClient.ino | 117 ++++ .../WifiWebClientRepeating.ino | 135 +++++ WiFi/examples/WifiWebServer/WifiWebServer.ino | 129 +++++ WiFi/keywords.txt | 43 ++ WiFi/utility/debug.h | 77 +++ WiFi/utility/server_drv.cpp | 260 +++++++++ WiFi/utility/server_drv.h | 34 ++ WiFi/utility/socket.c | 20 + WiFi/utility/socket.h | 87 +++ WiFi/utility/spi_drv.cpp | 503 ++++++++++++++++++ WiFi/utility/spi_drv.h | 83 +++ WiFi/utility/wifi_drv.cpp | 470 ++++++++++++++++ WiFi/utility/wifi_drv.h | 208 ++++++++ WiFi/utility/wifi_spi.h | 143 +++++ WiFi/utility/wl_definitions.h | 50 ++ WiFi/utility/wl_types.h | 31 ++ 28 files changed, 3886 insertions(+) create mode 100755 WiFi/WiFi.cpp create mode 100755 WiFi/WiFi.h create mode 100755 WiFi/WiFiClient.cpp create mode 100755 WiFi/WiFiClient.h create mode 100644 WiFi/WiFiServer.cpp create mode 100755 WiFi/WiFiServer.h create mode 100644 WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino create mode 100644 WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino create mode 100644 WiFi/examples/ScanNetworks/ScanNetworks.ino create mode 100644 WiFi/examples/WifiChatServer/WifiChatServer.ino create mode 100644 WiFi/examples/WifiCosmClient/WifiCosmClient.ino create mode 100644 WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino create mode 100644 WiFi/examples/WifiWebClient/WifiWebClient.ino create mode 100644 WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino create mode 100644 WiFi/examples/WifiWebServer/WifiWebServer.ino create mode 100755 WiFi/keywords.txt create mode 100644 WiFi/utility/debug.h create mode 100644 WiFi/utility/server_drv.cpp create mode 100644 WiFi/utility/server_drv.h create mode 100644 WiFi/utility/socket.c create mode 100644 WiFi/utility/socket.h create mode 100644 WiFi/utility/spi_drv.cpp create mode 100644 WiFi/utility/spi_drv.h create mode 100644 WiFi/utility/wifi_drv.cpp create mode 100644 WiFi/utility/wifi_drv.h create mode 100644 WiFi/utility/wifi_spi.h create mode 100644 WiFi/utility/wl_definitions.h create mode 100644 WiFi/utility/wl_types.h diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp new file mode 100755 index 000000000..fd45e9689 --- /dev/null +++ b/WiFi/WiFi.cpp @@ -0,0 +1,194 @@ +#include "wifi_drv.h" +#include "WiFi.h" + +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. +int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; +uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; + +WiFiClass::WiFiClass() +{ + // Driver initialization + init(); +} + +void WiFiClass::init() +{ + WiFiDrv::wifiDriverInit(); +} + +uint8_t WiFiClass::getSocket() +{ + for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i) + { + if (WiFiClass::_server_port[i] == 0) + { + return i; + } + } + return NO_SOCKET_AVAIL; +} + +int WiFiClass::begin(char* ssid) +{ + uint8_t status = WL_IDLE_STATUS; + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE) + { + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); + }else + { + status = WL_CONNECT_FAILED; + } + return status; +} + +int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) +{ + uint8_t status = WL_IDLE_STATUS; + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + // set encryption key + if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) + { + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); + }else{ + status = WL_CONNECT_FAILED; + } + return status; +} + +int WiFiClass::begin(char* ssid, const char *passphrase) +{ + uint8_t status = WL_IDLE_STATUS; + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + // set passphrase + if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) + { + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); + }else{ + status = WL_CONNECT_FAILED; + } + return status; +} + +int WiFiClass::disconnect() +{ + return WiFiDrv::disconnect(); +} + +uint8_t* WiFiClass::macAddress(uint8_t* mac) +{ + uint8_t* _mac = WiFiDrv::getMacAddress(); + memcpy(mac, _mac, WL_MAC_ADDR_LENGTH); + return mac; +} + +IPAddress WiFiClass::localIP() +{ + IPAddress ret; + WiFiDrv::getIpAddress(ret); + return ret; +} + +IPAddress WiFiClass::subnetMask() +{ + IPAddress ret; + WiFiDrv::getSubnetMask(ret); + return ret; +} + +IPAddress WiFiClass::gatewayIP() +{ + IPAddress ret; + WiFiDrv::getGatewayIP(ret); + return ret; +} + +char* WiFiClass::SSID() +{ + return WiFiDrv::getCurrentSSID(); +} + +uint8_t* WiFiClass::BSSID(uint8_t* bssid) +{ + uint8_t* _bssid = WiFiDrv::getCurrentBSSID(); + memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH); + return bssid; +} + +int32_t WiFiClass::RSSI() +{ + return WiFiDrv::getCurrentRSSI(); +} + +uint8_t WiFiClass::encryptionType() +{ + return WiFiDrv::getCurrentEncryptionType(); +} + + +int8_t WiFiClass::scanNetworks() +{ + uint8_t attempts = 10; + uint8_t numOfNetworks = 0; + + if (WiFiDrv::startScanNetworks() == WL_FAILURE) + return WL_FAILURE; + do + { + delay(2000); + numOfNetworks = WiFiDrv::getScanNetworks(); + } + while (( numOfNetworks == 0)&&(--attempts>0)); + return numOfNetworks; +} + +char* WiFiClass::SSID(uint8_t networkItem) +{ + return WiFiDrv::getSSIDNetoworks(networkItem); +} + +int32_t WiFiClass::RSSI(uint8_t networkItem) +{ + return WiFiDrv::getRSSINetoworks(networkItem); +} + +uint8_t WiFiClass::encryptionType(uint8_t networkItem) +{ + return WiFiDrv::getEncTypeNetowrks(networkItem); +} + +uint8_t WiFiClass::status() +{ + return WiFiDrv::getConnectionStatus(); +} + +int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) +{ + return WiFiDrv::getHostByName(aHostname, aResult); +} + +WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h new file mode 100755 index 000000000..501780b3f --- /dev/null +++ b/WiFi/WiFi.h @@ -0,0 +1,177 @@ +#ifndef WiFi_h +#define WiFi_h + +#include + +extern "C" { + #include "utility/wl_definitions.h" + #include "utility/wl_types.h" +} + +#include "IPAddress.h" +#include "WiFiClient.h" +#include "WiFiServer.h" + +class WiFiClass +{ +private: + + static void init(); +public: + static int16_t _state[MAX_SOCK_NUM]; + static uint16_t _server_port[MAX_SOCK_NUM]; + + WiFiClass(); + + /* + * Get the first socket available + */ + static uint8_t getSocket(); + + /* Start Wifi connection for OPEN networks + * + * param ssid: Pointer to the SSID string. + */ + int begin(char* ssid); + + /* Start Wifi connection with WEP encryption. + * Configure a key into the device. The key type (WEP-40, WEP-104) + * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104). + * + * param ssid: Pointer to the SSID string. + * param key_idx: The key index to set. Valid values are 0-3. + * param key: Key input buffer. + */ + int begin(char* ssid, uint8_t key_idx, const char* key); + + /* Start Wifi connection with passphrase + * the most secure supported mode will be automatically selected + * + * param ssid: Pointer to the SSID string. + * param passphrase: Passphrase. Valid characters in a passphrase + * must be between ASCII 32-126 (decimal). + */ + int begin(char* ssid, const char *passphrase); + + /* + * Disconnect from the network + * + * return: one value of wl_status_t enum + */ + int disconnect(void); + + /* + * Get the interface MAC address. + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + uint8_t* macAddress(uint8_t* mac); + + /* + * Get the interface IP address. + * + * return: Ip address value + */ + IPAddress localIP(); + + /* + * Get the interface subnet mask address. + * + * return: subnet mask address value + */ + IPAddress subnetMask(); + + /* + * Get the gateway ip address. + * + * return: gateway ip address value + */ + IPAddress gatewayIP(); + + /* + * Return the current SSID associated with the network + * + * return: ssid string + */ + char* SSID(); + + /* + * Return the current BSSID associated with the network. + * It is the MAC address of the Access Point + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + uint8_t* BSSID(uint8_t* bssid); + + /* + * Return the current RSSI /Received Signal Strength in dBm) + * associated with the network + * + * return: signed value + */ + int32_t RSSI(); + + /* + * Return the Encryption Type associated with the network + * + * return: one value of wl_enc_type enum + */ + uint8_t encryptionType(); + + /* + * Start scan WiFi networks available + * + * return: Number of discovered networks + */ + int8_t scanNetworks(); + + /* + * Return the SSID discovered during the network scan. + * + * param networkItem: specify from which network item want to get the information + * + * return: ssid string of the specified item on the networks scanned list + */ + char* SSID(uint8_t networkItem); + + /* + * Return the encryption type of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ + uint8_t encryptionType(uint8_t networkItem); + + /* + * Return the RSSI of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: signed value of RSSI of the specified item on the networks scanned list + */ + int32_t RSSI(uint8_t networkItem); + + /* + * Return Connection status. + * + * return: one of the value defined in wl_status_t + */ + uint8_t status(); + + /* + * Resolve the given hostname to an IP address. + * param aHostname: Name to be resolved + * param aResult: IPAddress structure to store the returned IP address + * result: 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ + int hostByName(const char* aHostname, IPAddress& aResult); + + friend class WiFiClient; + friend class WiFiServer; +}; + +extern WiFiClass WiFi; + +#endif diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp new file mode 100755 index 000000000..470a42938 --- /dev/null +++ b/WiFi/WiFiClient.cpp @@ -0,0 +1,174 @@ +extern "C" { + #include "utility/wl_definitions.h" + #include "utility/wl_types.h" + #include "socket.h" + #include "string.h" + #include "utility/debug.h" +} + +#include "WiFi.h" +#include "WiFiClient.h" +#include "WiFiServer.h" +#include "server_drv.h" + + +uint16_t WiFiClient::_srcport = 1024; + +WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) { +} + +WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) { +} + +int WiFiClient::connect(const char* host, uint16_t port) { + IPAddress remote_addr; + if (WiFi.hostByName(host, remote_addr)) + { + return connect(remote_addr, port); + } + return 0; +} + +int WiFiClient::connect(IPAddress ip, uint16_t port) { + _sock = getFirstSocket(); + if (_sock != NO_SOCKET_AVAIL) + { + ServerDrv::startClient(uint32_t(ip), port, _sock); + WiFiClass::_state[_sock] = _sock; + + unsigned long start = millis(); + + // wait 4 second for the connection to close + while (!connected() && millis() - start < 10000) + delay(1); + + if (!connected()) + { + return 0; + } + }else{ + Serial.println("No Socket available"); + return 0; + } + return 1; +} + +size_t WiFiClient::write(uint8_t b) { + return write(&b, 1); +} + +size_t WiFiClient::write(const uint8_t *buf, size_t size) { + if (_sock >= MAX_SOCK_NUM) + { + setWriteError(); + return 0; + } + if (size==0) + { + setWriteError(); + return 0; + } + + + if ((!ServerDrv::sendData(_sock, buf, size)) || + (!ServerDrv::checkDataSent(_sock))) + { + setWriteError(); + return 0; + } + return size; +} + +int WiFiClient::available() { + if (_sock != 255) + { + return ServerDrv::availData(_sock); + } + + return 0; +} + +int WiFiClient::read() { + uint8_t b; + if (!available()) + return -1; + + ServerDrv::getData(_sock, &b); + return b; +} + + +int WiFiClient::read(uint8_t* buf, size_t size) { + if (!ServerDrv::getDataBuf(_sock, buf, &size)) + return -1; + return 0; +} + +int WiFiClient::peek() { + uint8_t b; + if (!available()) + return -1; + + ServerDrv::getData(_sock, &b, 1); + return b; +} + +void WiFiClient::flush() { + while (available()) + read(); +} + +void WiFiClient::stop() { + + if (_sock == 255) + return; + + ServerDrv::stopClient(_sock); + + unsigned long start = millis(); + + + // wait a second for the connection to close + while (status() != CLOSED && millis() - start < 1000) + delay(1); + _sock = 255; +} + +uint8_t WiFiClient::connected() { + + if (_sock == 255) { + return 0; + } else { + uint8_t s = status(); + + return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || + s == FIN_WAIT_2 || s == TIME_WAIT || + s == SYN_SENT || s== SYN_RCVD || + (s == CLOSE_WAIT && !available())); + } +} + +uint8_t WiFiClient::status() { + if (_sock == 255) { + return CLOSED; + } else { + return ServerDrv::getClientState(_sock); + } +} + +WiFiClient::operator bool() { + return _sock != 255; +} + +// Private Methods +uint8_t WiFiClient::getFirstSocket() +{ + for (int i = 0; i < MAX_SOCK_NUM; i++) { + if (WiFiClass::_state[i] == 0) + { + return i; + } + } + return SOCK_NOT_AVAIL; +} + diff --git a/WiFi/WiFiClient.h b/WiFi/WiFiClient.h new file mode 100755 index 000000000..5a7f0f3b8 --- /dev/null +++ b/WiFi/WiFiClient.h @@ -0,0 +1,40 @@ +#ifndef wificlient_h +#define wificlient_h +#include "Arduino.h" +#include "Print.h" +#include "Client.h" +#include "IPAddress.h" + +class WiFiClient : public Client { + +public: + WiFiClient(); + WiFiClient(uint8_t sock); + + uint8_t status(); + virtual int connect(IPAddress ip, uint16_t port); + virtual int connect(const char *host, uint16_t port); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buf, size_t size); + virtual int available(); + virtual int read(); + virtual int read(uint8_t *buf, size_t size); + virtual int peek(); + virtual void flush(); + virtual void stop(); + virtual uint8_t connected(); + virtual operator bool(); + + friend class WiFiServer; + + using Print::write; + +private: + static uint16_t _srcport; + uint8_t _sock; //not used + uint16_t _socket; + + uint8_t getFirstSocket(); +}; + +#endif diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp new file mode 100644 index 000000000..77dbac0b9 --- /dev/null +++ b/WiFi/WiFiServer.cpp @@ -0,0 +1,88 @@ +#include +#include "server_drv.h" + +extern "C" { + #include "utility/debug.h" +} + +#include "WiFi.h" +#include "WiFiClient.h" +#include "WiFiServer.h" + +WiFiServer::WiFiServer(uint16_t port) +{ + _port = port; +} + +void WiFiServer::begin() +{ + uint8_t _sock = WiFiClass::getSocket(); + if (_sock != NO_SOCKET_AVAIL) + { + ServerDrv::startServer(_port, _sock); + WiFiClass::_server_port[_sock] = _port; + } +} + +WiFiClient WiFiServer::available(byte* status) +{ + static int cycle_server_down = 0; + const int TH_SERVER_DOWN = 50; + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) + { + if (WiFiClass::_server_port[sock] == _port) + { + WiFiClient client(sock); + uint8_t _status = client.status(); + uint8_t _ser_status = this->status(); + + if (status != NULL) + *status = _status; + + //server not in listen state, restart it + if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN)) + { + ServerDrv::startServer(_port, sock); + cycle_server_down = 0; + } + + if (_status == ESTABLISHED) + { + return client; //TODO + } + } + } + + return WiFiClient(255); +} + +uint8_t WiFiServer::status() { + return ServerDrv::getServerState(0); +} + + +size_t WiFiServer::write(uint8_t b) +{ + return write(&b, 1); +} + +size_t WiFiServer::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) + { + if (WiFiClass::_server_port[sock] != 0) + { + WiFiClient client(sock); + + if (WiFiClass::_server_port[sock] == _port && + client.status() == ESTABLISHED) + { + n+=client.write(buffer, size); + } + } + } + return n; +} diff --git a/WiFi/WiFiServer.h b/WiFi/WiFiServer.h new file mode 100755 index 000000000..68b574c29 --- /dev/null +++ b/WiFi/WiFiServer.h @@ -0,0 +1,27 @@ +#ifndef wifiserver_h +#define wifiserver_h + +extern "C" { + #include "utility/wl_definitions.h" +} + +#include "Server.h" + +class WiFiClient; + +class WiFiServer : public Server { +private: + uint16_t _port; + void* pcb; +public: + WiFiServer(uint16_t); + WiFiClient available(uint8_t* status = NULL); + void begin(); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buf, size_t size); + uint8_t status(); + + using Print::write; +}; + +#endif diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino new file mode 100644 index 000000000..e6f531ca8 --- /dev/null +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -0,0 +1,123 @@ +/* + + This example connects to a WEP-encrypted Wifi network. + Then it prints the MAC address of the Wifi shield, + the IP address obtained, and other network details. + + If you use 40-bit WEP, you need a key that is 10 characters long, + and the characters must be hexadecimal (0-9 or A-F). + e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work + (too short) and ABBAISDEAF won't work (I and S are not + hexadecimal characters). + + For 128-bit, you need a string that is 26 characters long. + D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters, + all in the 0-9, A-F range. + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + */ +#include + +char ssid[] = "yourNetwork"; // your network SSID (name) +char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key +int keyIndex = 0; // your network key Index number +int status = WL_IDLE_STATUS; // the Wifi radio's status + +void setup() { + // initialize serial: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WEP network, SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid, keyIndex, key); + + // wait 10 seconds for connection: + delay(10000); + } + + // once you are connected : + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); +} + +void loop() { + // check the network connection once every 10 seconds: + delay(10000); + printCurrentNet(); +} + +void printWifiData() { + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + Serial.println(ip); + + // print your MAC address: + byte mac[6]; + WiFi.macAddress(mac); + Serial.print("MAC address: "); + Serial.print(mac[5],HEX); + Serial.print(":"); + Serial.print(mac[4],HEX); + Serial.print(":"); + Serial.print(mac[3],HEX); + Serial.print(":"); + Serial.print(mac[2],HEX); + Serial.print(":"); + Serial.print(mac[1],HEX); + Serial.print(":"); + Serial.println(mac[0],HEX); +} + +void printCurrentNet() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print the MAC address of the router you're attached to: + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],HEX); + Serial.print(":"); + Serial.print(bssid[4],HEX); + Serial.print(":"); + Serial.print(bssid[3],HEX); + Serial.print(":"); + Serial.print(bssid[2],HEX); + Serial.print(":"); + Serial.print(bssid[1],HEX); + Serial.print(":"); + Serial.println(bssid[0],HEX); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); + Serial.println(); +} + + + diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino new file mode 100644 index 000000000..953841500 --- /dev/null +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -0,0 +1,113 @@ +/* + + This example connects to an unencrypted Wifi network. + Then it prints the MAC address of the Wifi shield, + the IP address obtained, and other network details. + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + */ + #include + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password +int status = WL_IDLE_STATUS; // the Wifi radio's status + +void setup() { + // initialize serial: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to WPA SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + // you're connected now, so print out the data: + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); + +} + +void loop() { + // check the network connection once every 10 seconds: + delay(10000); + printCurrentNet(); +} + +void printWifiData() { + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + Serial.println(ip); + + // print your MAC address: + byte mac[6]; + WiFi.macAddress(mac); + Serial.print("MAC address: "); + Serial.print(mac[5],HEX); + Serial.print(":"); + Serial.print(mac[4],HEX); + Serial.print(":"); + Serial.print(mac[3],HEX); + Serial.print(":"); + Serial.print(mac[2],HEX); + Serial.print(":"); + Serial.print(mac[1],HEX); + Serial.print(":"); + Serial.println(mac[0],HEX); + +} + +void printCurrentNet() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print the MAC address of the router you're attached to: + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],HEX); + Serial.print(":"); + Serial.print(bssid[4],HEX); + Serial.print(":"); + Serial.print(bssid[3],HEX); + Serial.print(":"); + Serial.print(bssid[2],HEX); + Serial.print(":"); + Serial.print(bssid[1],HEX); + Serial.print(":"); + Serial.println(bssid[0],HEX); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); + Serial.println(); +} + diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino new file mode 100644 index 000000000..efe171adb --- /dev/null +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -0,0 +1,95 @@ +/* + + This example prints the Wifi shield's MAC address, and + scans for available Wifi networks using the Wifi shield. + Every ten seconds, it scans again. It doesn't actually + connect to any network, so no encryption scheme is specified. + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + */ + + +#include +#include + +void setup() { + // initialize serial and wait for the port to open: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // Print WiFi MAC address: + printMacAddress(); + + // scan for existing networks: + Serial.println("Scanning available networks..."); + listNetworks(); +} + +void loop() { + delay(10000); + // scan for existing networks: + Serial.println("Scanning available networks..."); + listNetworks(); +} + +void printMacAddress() { + // the MAC address of your Wifi shield + byte mac[6]; + + // print your MAC address: + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],HEX); + Serial.print(":"); + Serial.print(mac[4],HEX); + Serial.print(":"); + Serial.print(mac[3],HEX); + Serial.print(":"); + Serial.print(mac[2],HEX); + Serial.print(":"); + Serial.print(mac[1],HEX); + Serial.print(":"); + Serial.println(mac[0],HEX); +} + +void listNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + int numSsid = WiFi.scanNetworks(); + if (numSsid == -1) + { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + + // print the list of networks seen: + Serial.print("number of available networks:"); + Serial.println(numSsid); + + // print the network number and name for each network found: + for (int thisNet = 0; thisNet +#include + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) + +int keyIndex = 0; // your network key Index number (needed only for WEP) + +int status = WL_IDLE_STATUS; + +WiFiServer server(23); + +boolean alreadyConnected = false; // whether or not the client was connected previously + +void setup() { + // start serial port: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + // start the server: + server.begin(); + // you're connected now, so print out the status: + printWifiStatus(); + } + + +void loop() { + // wait for a new client: + WiFiClient client = server.available(); + + + // when the client sends the first byte, say hello: + if (client) { + if (!alreadyConnected) { + // clead out the input buffer: + client.flush(); + Serial.println("We have a new client"); + client.println("Hello, client!"); + alreadyConnected = true; + } + + if (client.available() > 0) { + // read the bytes incoming from the client: + char thisChar = client.read(); + // echo the bytes back to the client: + server.write(thisChar); + // echo the bytes to the server as well: + Serial.write(thisChar); + } + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino new file mode 100644 index 000000000..668c9e0ac --- /dev/null +++ b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino @@ -0,0 +1,185 @@ +/* + Wifi Cosm sensor client + + This sketch connects an analog sensor to Cosm (http://www.cosm.com) + using an Arduino Wifi shield. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example has been updated to use version 2.0 of the Cosm.com API. + To make it work, create a feed with a datastream, and give it the ID + sensor1. Or change the code below to match your feed. + + Circuit: + * Analog sensor attached to analog in 0 + * Wifi shield attached to pins 10, 11, 12, 13 + + created 13 Mar 2012 + modified 31 May 2012 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include + +#define APIKEY "YOUR API KEY GOES HERE" // replace your cosm api key here +#define FEEDID 00000 // replace your feed ID +#define USERAGENT "My Arduino Project" // user agent is the project name + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password + +int status = WL_IDLE_STATUS; + +// initialize the library instance: +WiFiClient client; +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +IPAddress server(216,52,233,121); // numeric IP for api.cosm.com +//char server[] = "api.cosm.com"; // name address for cosm API + +unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds +boolean lastConnected = false; // state of the connection last time through the main loop +const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com + +void setup() { + // start serial port: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + // you're connected now, so print out the status: + printWifiStatus(); +} + + +void loop() { + // read the analog sensor: + int sensorReading = analogRead(A0); + + // if there's incoming data from the net connection. + // send it out the serial port. This is for debugging + // purposes only: + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + // if there's no net connection, but there was one last time + // through the loop, then stop the client: + if (!client.connected() && lastConnected) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + + // if you're not connected, and ten seconds have passed since + // your last connection, then connect again and send data: + if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { + sendData(sensorReading); + } + // store the state of the connection for next time through + // the loop: + lastConnected = client.connected(); +} + +// this method makes a HTTP connection to the server: +void sendData(int thisData) { + // if there's a successful connection: + if (client.connect(server, 80)) { + Serial.println("connecting..."); + // send the HTTP PUT request: + client.print("PUT /v2/feeds/"); + client.print(FEEDID); + client.println(".csv HTTP/1.1"); + client.println("Host: api.cosm.com"); + client.print("X-ApiKey: "); + client.println(APIKEY); + client.print("User-Agent: "); + client.println(USERAGENT); + client.print("Content-Length: "); + + // calculate the length of the sensor reading in bytes: + // 8 bytes for "sensor1," + number of digits of the data: + int thisLength = 8 + getLength(thisData); + client.println(thisLength); + + // last pieces of the HTTP PUT request: + client.println("Content-Type: text/csv"); + client.println("Connection: close"); + client.println(); + + // here's the actual content of the PUT request: + client.print("sensor1,"); + client.println(thisData); + + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + // note the time that the connection was made or attempted: + lastConnectionTime = millis(); +} + + +// This method calculates the number of digits in the +// sensor reading. Since each digit of the ASCII decimal +// representation is a byte, the number of digits equals +// the number of bytes: + +int getLength(int someValue) { + // there's at least one byte: + int digits = 1; + // continually divide the value by ten, + // adding one to the digit count for each + // time you divide, until you're at 0: + int dividend = someValue /10; + while (dividend > 0) { + dividend = dividend /10; + digits++; + } + // return the number of digits: + return digits; +} + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + + diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino new file mode 100644 index 000000000..3634ed762 --- /dev/null +++ b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino @@ -0,0 +1,172 @@ +/* + Wifi Cosm sensor client with Strings + + This sketch connects an analog sensor to Cosm (http://www.cosm.com) + using a Arduino Wifi shield. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example has been updated to use version 2.0 of the cosm.com API. + To make it work, create a feed with a datastream, and give it the ID + sensor1. Or change the code below to match your feed. + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * Analog sensor attached to analog in 0 + * Wifi shield attached to pins 10, 11, 12, 13 + + created 16 Mar 2012 + modified 31 May 2012 + by Tom Igoe + + This code is in the public domain. + + */ + +#include +#include + +#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here +#define FEEDID 00000 // replace your feed ID +#define USERAGENT "My Arduino Project" // user agent is the project name + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password + +int status = WL_IDLE_STATUS; + +// initialize the library instance: +WiFiClient client; + +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com +char server[] = "api.cosm.com"; // name address for pachube API + +unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds +boolean lastConnected = false; // state of the connection last time through the main loop +const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com + +void setup() { + // start serial port: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + // you're connected now, so print out the status: + printWifiStatus(); +} + +void loop() { + // read the analog sensor: + int sensorReading = analogRead(A0); + // convert the data to a String to send it: + + String dataString = "sensor1,"; + dataString += sensorReading; + + // you can append multiple readings to this String if your + // pachube feed is set up to handle multiple values: + int otherSensorReading = analogRead(A1); + dataString += "\nsensor2,"; + dataString += otherSensorReading; + + // if there's incoming data from the net connection. + // send it out the serial port. This is for debugging + // purposes only: + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + // if there's no net connection, but there was one last time + // through the loop, then stop the client: + if (!client.connected() && lastConnected) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + + // if you're not connected, and ten seconds have passed since + // your last connection, then connect again and send data: + if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { + sendData(dataString); + } + // store the state of the connection for next time through + // the loop: + lastConnected = client.connected(); +} + +// this method makes a HTTP connection to the server: +void sendData(String thisData) { + // if there's a successful connection: + if (client.connect(server, 80)) { + Serial.println("connecting..."); + // send the HTTP PUT request: + client.print("PUT /v2/feeds/"); + client.print(FEEDID); + client.println(".csv HTTP/1.1"); + client.println("Host: api.cosm.com"); + client.print("X-ApiKey: "); + client.println(APIKEY); + client.print("User-Agent: "); + client.println(USERAGENT); + client.print("Content-Length: "); + client.println(thisData.length()); + + // last pieces of the HTTP PUT request: + client.println("Content-Type: text/csv"); + client.println("Connection: close"); + client.println(); + + // here's the actual content of the PUT request: + client.println(thisData); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + // note the time that the connection was made or attempted: + lastConnectionTime = millis(); +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino new file mode 100644 index 000000000..54ec68b71 --- /dev/null +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -0,0 +1,117 @@ + +/* + Web client + + This sketch connects to a website (http://www.google.com) + using a WiFi shield. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key Index number (needed only for WEP) + +int status = WL_IDLE_STATUS; +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +IPAddress server(173,194,73,105); // numeric IP for Google (no DNS) +//char server[] = "www.google.com"; // name address for Google (using DNS) + +// Initialize the Ethernet client library +// with the IP address and port of the server +// that you want to connect to (port 80 is default for HTTP): +WiFiClient client; + +void setup() { + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + Serial.println("Connected to wifi"); + printWifiStatus(); + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.1"); + client.println("Host:www.google.com"); + client.println("Connection: close"); + client.println(); + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + while (client.available()) { + char c = client.read(); + Serial.write(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + // do nothing forevermore: + while(true); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + + + + diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino new file mode 100644 index 000000000..1c623d12e --- /dev/null +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -0,0 +1,135 @@ +/* + Repeating Wifi Web client + + This sketch connects to a a web server and makes a request + using an Arduino Wifi shield. + + Circuit: + * Wifi shield attached to pins 10, 11, 12, 13 + + created 23 April 2012 + modifide 31 May 2012 + by Tom Igoe + + http://arduino.cc/en/Tutorial/WifiWebClientRepeating + This code is in the public domain. + */ + +#include +#include + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password +int keyIndex = 0; // your network key Index number (needed only for WEP) + +int status = WL_IDLE_STATUS; + +// Initialize the Wifi client library +WiFiClient client; + +// server address: +char server[] = "www.arduino.cc"; +//IPAddress server(64,131,82,241); + +unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds +boolean lastConnected = false; // state of the connection last time through the main loop +const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds + +void setup() { + // start serial port: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + // you're connected now, so print out the status: + printWifiStatus(); +} + +void loop() { + // if there's incoming data from the net connection. + // send it out the serial port. This is for debugging + // purposes only: + while (client.available()) { + char c = client.read(); + Serial.write(c); + } + + // if there's no net connection, but there was one last time + // through the loop, then stop the client: + if (!client.connected() && lastConnected) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + + // if you're not connected, and ten seconds have passed since + // your last connection, then connect again and send data: + if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { + httpRequest(); + } + // store the state of the connection for next time through + // the loop: + lastConnected = client.connected(); +} + +// this method makes a HTTP connection to the server: +void httpRequest() { + // if there's a successful connection: + if (client.connect(server, 80)) { + Serial.println("connecting..."); + // send the HTTP PUT request: + client.println("GET /latest.txt HTTP/1.1"); + client.println("Host: www.arduino.cc"); + client.println("User-Agent: arduino-ethernet"); + client.println("Connection: close"); + client.println(); + + // note the time that the connection was made: + lastConnectionTime = millis(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + Serial.println("disconnecting."); + client.stop(); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + + + + + + diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino new file mode 100644 index 000000000..72bd8e0b9 --- /dev/null +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -0,0 +1,129 @@ +/* + Web Server + + A simple web server that shows the value of the analog input pins. + using a WiFi shield. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + Circuit: + * WiFi shield attached + * Analog inputs attached to pins A0 through A5 (optional) + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + */ +#include +#include + + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password +int keyIndex = 0; // your network key Index number (needed only for WEP) + +int status = WL_IDLE_STATUS; + +WiFiServer server(80); + +void setup() { + // start serial port: + Serial.begin(9600); + + // check for the presence of the shield: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("WiFi shield not present"); + // don't continue: + while(true); + } + + // attempt to connect to Wifi network: + while ( status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + server.begin(); + // you're connected now, so print out the status: + printWifiStatus(); +} + + +void loop() { + // listen for incoming clients + WiFiClient client = server.available(); + if (client) { + Serial.println("new client"); + // an http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + Serial.write(c); + // if you've gotten to the end of the line (received a newline + // character) and the line is blank, the http request has ended, + // so you can send a reply + if (c == '\n' && currentLineIsBlank) { + // send a standard http response header + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: text/html"); + client.println("Connnection: close"); + client.println(); + client.println(""); + client.println(""); + // add a meta refresh tag, so the browser pulls again every 5 seconds: + client.println(""); + // output the value of each analog input pin + for (int analogChannel = 0; analogChannel < 6; analogChannel++) { + int sensorReading = analogRead(analogChannel); + client.print("analog input "); + client.print(analogChannel); + client.print(" is "); + client.print(sensorReading); + client.println("
"); + } + client.println(""); + break; + } + if (c == '\n') { + // you're starting a new line + currentLineIsBlank = true; + } + else if (c != '\r') { + // you've gotten a character on the current line + currentLineIsBlank = false; + } + } + } + // give the web browser time to receive the data + delay(1); + // close the connection: + client.stop(); + Serial.println("client disonnected"); + } +} + + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} + diff --git a/WiFi/keywords.txt b/WiFi/keywords.txt new file mode 100755 index 000000000..47704cd00 --- /dev/null +++ b/WiFi/keywords.txt @@ -0,0 +1,43 @@ +####################################### +# Syntax Coloring Map For WiFi +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +WiFi KEYWORD1 +Client KEYWORD1 +Server KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +status KEYWORD2 +connect KEYWORD2 +write KEYWORD2 +available KEYWORD2 +read KEYWORD2 +flush KEYWORD2 +stop KEYWORD2 +connected KEYWORD2 +begin KEYWORD2 +disconnect KEYWORD2 +macAddress KEYWORD2 +localIP KEYWORD2 +subnetMask KEYWORD2 +gatewayIP KEYWORD2 +SSID KEYWORD2 +BSSID KEYWORD2 +RSSI KEYWORD2 +encryptionType KEYWORD2 +getResult KEYWORD2 +getSocket KEYWORD2 +WiFiClient KEYWORD2 +WiFiServer KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h new file mode 100644 index 000000000..9f71055b2 --- /dev/null +++ b/WiFi/utility/debug.h @@ -0,0 +1,77 @@ +//*********************************************/ +// +// File: debug.h +// +// Author: dlf (Metodo2 srl) +// +//********************************************/ + + +#ifndef Debug_H +#define Debug_H + +#include +#include + +#define PRINT_FILE_LINE() do { \ + Serial.print("[");Serial.print(__FILE__); \ + Serial.print("::");Serial.print(__LINE__);Serial.print("]");\ +}while (0); + +#ifdef _DEBUG_ + +#define INFO(format, args...) do { \ + char buf[250]; \ + sprintf(buf, format, args); \ + Serial.println(buf); \ +} while(0); + +#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,16);Serial.print(",");Serial.println(y,16); \ +}while (0); + + +#else +#define INFO1(x) do {} while(0); +#define INFO2(x,y) do {} while(0); +#define INFO(format, args...) do {} while(0); +#endif + +#if 0 +#define WARN(args) do { PRINT_FILE_LINE() \ + Serial.print("-W-"); Serial.println(args); \ +}while (0); +#else +#define WARN(args) do {} while (0); +#endif + +#if _DEBUG_SPI_ +#define DBG_PIN2 5 +#define DBG_PIN 4 + +#define START() digitalWrite(DBG_PIN2, HIGH); +#define END() digitalWrite(DBG_PIN2, LOW); +#define SET_TRIGGER() digitalWrite(DBG_PIN, HIGH); +#define RST_TRIGGER() digitalWrite(DBG_PIN, LOW); + +#define INIT_TRIGGER() pinMode(DBG_PIN, OUTPUT); \ + pinMode(DBG_PIN2, OUTPUT); \ + RST_TRIGGER() +#define TOGGLE_TRIGGER() SET_TRIGGER() \ + delayMicroseconds(2); \ + RST_TRIGGER() +#else +#define START() +#define END() +#define SET_TRIGGER() +#define RST_TRIGGER() +#define INIT_TRIGGER() +#define TOGGLE_TRIGGER() +#endif + +#endif diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp new file mode 100644 index 000000000..a325d5a06 --- /dev/null +++ b/WiFi/utility/server_drv.cpp @@ -0,0 +1,260 @@ +//#define _DEBUG_ + +#include "server_drv.h" + +#include "Arduino.h" +#include "spi_drv.h" + +extern "C" { +#include "wl_types.h" +#include "debug.h" +} + + +// Start server TCP on port specified +void ServerDrv::startServer(uint16_t port, uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendParam(port); + SpiDrv::sendParam(&sock, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + +// Start server TCP on port specified +void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_3); + SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress)); + SpiDrv::sendParam(port); + SpiDrv::sendParam(&sock, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + +// Start server TCP on port specified +void ServerDrv::stopClient(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + + +uint8_t ServerDrv::getServerState(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +uint8_t ServerDrv::getClientState(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +uint8_t ServerDrv::availData(uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + + if (_dataLen!=0) + { + return (_data == 1); + } + return false; +} + +bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendParam(&sock, sizeof(sock)); + SpiDrv::sendParam(peek, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseData8(GET_DATA_TCP_CMD, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + if (_dataLen!=0) + { + *data = _data; + return true; + } + return false; +} + +bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendBuffer(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + if (*_dataLen!=0) + { + return true; + } + return false; +} + + +bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendBuffer(&sock, sizeof(sock)); + SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + if (_dataLen!=0) + { + return (_data == 1); + } + return false; +} + + +uint8_t ServerDrv::checkDataSent(uint8_t sock) +{ + const uint16_t TIMEOUT_DATA_SENT = 250; + static uint16_t timeout = 0; + uint8_t _data = 0; + uint8_t _dataLen = 0; + + do { + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse isDataSent"); + } + SpiDrv::spiSlaveDeselect(); + + if (_data) timeout = 0; + else{ + ++timeout; + delay(10); + } + + }while((_data==0)&&(timeout +#include "wifi_spi.h" + +class ServerDrv +{ +public: + // Start server TCP on port specified + static void startServer(uint16_t port, uint8_t sock); + + static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock); + + static void stopClient(uint8_t sock); + + static uint8_t getServerState(uint8_t sock); + + static uint8_t getClientState(uint8_t sock); + + static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0); + + static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len); + + static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len); + + static uint8_t availData(uint8_t sock); + + static uint8_t checkDataSent(uint8_t sock); +}; + +extern ServerDrv serverDrv; + +#endif diff --git a/WiFi/utility/socket.c b/WiFi/utility/socket.c new file mode 100644 index 000000000..665073b04 --- /dev/null +++ b/WiFi/utility/socket.c @@ -0,0 +1,20 @@ +/* +* +@file socket.c +@brief define function of socket API +* +*/ +#include +#include "socket.h" + +SOCKET socket(uint8 protocol) {return 0;} // Opens a socket(TCP or UDP or IP_RAW mode) +void close(SOCKET s) {} // Close socket +uint8 connect(SOCKET s, uint8 * addr, uint16 port) {return 0;} // Establish TCP connection (Active connection) +void disconnect(SOCKET s) {} // disconnect the connection +uint8 listen(SOCKET s) { return 0;} // Establish TCP connection (Passive connection) +uint16 send(SOCKET s, const uint8 * buf, uint16 len) { return 0;} // Send data (TCP) +uint16 recv(SOCKET s, uint8 * buf, uint16 len) {return 0;} // Receive data (TCP) +uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port) {return 0;} // Send data (UDP/IP RAW) +uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port) {return 0;} // Receive data (UDP/IP RAW) + +uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len) {return 0;} diff --git a/WiFi/utility/socket.h b/WiFi/utility/socket.h new file mode 100644 index 000000000..9b06d00d1 --- /dev/null +++ b/WiFi/utility/socket.h @@ -0,0 +1,87 @@ +/* +* +@file socket.h +@brief define function of socket API +* +*/ + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +#define TCP_SOCKET 1 +#define UDP_SOCKET 2 +#define RAW_SOCKET 3 + +#define SOCK_NOT_AVAIL 255 + +#include "wl_definitions.h" +/** + * The 8-bit signed data type. + */ +typedef char int8; +/** + * The volatile 8-bit signed data type. + */ +typedef volatile char vint8; +/** + * The 8-bit unsigned data type. + */ +typedef unsigned char uint8; +/** + * The volatile 8-bit unsigned data type. + */ +typedef volatile unsigned char vuint8; + +/** + * The 16-bit signed data type. + */ +typedef int int16; +/** + * The volatile 16-bit signed data type. + */ +typedef volatile int vint16; +/** + * The 16-bit unsigned data type. + */ +typedef unsigned int uint16; +/** + * The volatile 16-bit unsigned data type. + */ +typedef volatile unsigned int vuint16; +/** + * The 32-bit signed data type. + */ +typedef long int32; +/** + * The volatile 32-bit signed data type. + */ +typedef volatile long vint32; +/** + * The 32-bit unsigned data type. + */ +typedef unsigned long uint32; +/** + * The volatile 32-bit unsigned data type. + */ +typedef volatile unsigned long vuint32; + +/* bsd */ +typedef uint8 u_char; /**< 8-bit value */ +typedef uint16_t SOCKET; +typedef uint16 u_short; /**< 16-bit value */ +typedef uint16 u_int; /**< 16-bit value */ +typedef uint32 u_long; /**< 32-bit value */ + +extern SOCKET socket(uint8 protocol); // Opens a socket(TCP or UDP or IP_RAW mode) +extern void close(SOCKET s); // Close socket +extern uint8 connect(SOCKET s, uint8 * addr, uint16 port); // Establish TCP connection (Active connection) +extern void disconnect(SOCKET s); // disconnect the connection +extern uint8 listen(SOCKET s); // Establish TCP connection (Passive connection) +extern uint16 send(SOCKET s, const uint8 * buf, uint16 len); // Send data (TCP) +extern uint16 recv(SOCKET s, uint8 * buf, uint16 len); // Receive data (TCP) +extern uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port); // Send data (UDP/IP RAW) +extern uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port); // Receive data (UDP/IP RAW) + +extern uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len); +#endif +/* _SOCKET_H_ */ diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp new file mode 100644 index 000000000..ceef832c0 --- /dev/null +++ b/WiFi/utility/spi_drv.cpp @@ -0,0 +1,503 @@ + +#include "Arduino.h" +#include "spi_drv.h" +#include "pins_arduino.h" +//#define _DEBUG_ +extern "C" { +#include "debug.h" +} + +#define DATAOUT 11//MOSI +#define DATAIN 12//MISO +#define SPICLOCK 13//sck +#define SLAVESELECT 10//ss +#define SLAVEREADY 3 + +#define DELAY_100NS do { asm volatile("nop"); }while(0); +#define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); }while(++ii 0) && (_readChar != waitChar)); + return (_readChar == waitChar); +} + +int SpiDrv::readAndCheckChar(char checkChar, char* readChar) +{ + getParam((uint8_t*)readChar); + + return (*readChar == checkChar); +} + +char SpiDrv::readChar() +{ + uint8_t readChar = 0; + getParam(&readChar); + return readChar; +} + +#define WAIT_START_CMD(x) waitSpiChar(START_CMD) + +#define IF_CHECK_START_CMD(x) \ + if (!WAIT_START_CMD(_data)) \ + { \ + TOGGLE_TRIGGER() \ + WARN("Error waiting START_CMD"); \ + return 0; \ + }else \ + +#define CHECK_DATA(check, x) \ + if (!readAndCheckChar(check, &x)) \ + { \ + TOGGLE_TRIGGER() \ + WARN("Reply error"); \ + INFO2(check, (uint8_t)x); \ + return 0; \ + }else \ + +#define waitSlaveReady() (digitalRead(SLAVEREADY) == LOW) +#define waitSlaveSign() (digitalRead(SLAVEREADY) == HIGH) +#define waitSlaveSignalH() while(digitalRead(SLAVEREADY) != HIGH){} +#define waitSlaveSignalL() while(digitalRead(SLAVEREADY) != LOW){} + +void SpiDrv::waitForSlaveSign() +{ + while (!waitSlaveSign()); +} + +void SpiDrv::waitForSlaveReady() +{ + while (!waitSlaveReady()); +} + +void SpiDrv::getParam(uint8_t* param) +{ + // Get Params data + *param = spiTransfer(DUMMY_DATA); + DELAY_TRANSFER(); +} + +int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len) +{ + char _data = 0; + int ii = 0; + + IF_CHECK_START_CMD(_data) + { + CHECK_DATA(cmd | REPLY_FLAG, _data){}; + + CHECK_DATA(numParam, _data); + { + readParamLen8(param_len); + for (ii=0; ii<(*param_len); ++ii) + { + // Get Params data + //param[ii] = spiTransfer(DUMMY_DATA); + getParam(¶m[ii]); + } + } + + readAndCheckChar(END_CMD, &_data); + } + + return 1; +} +/* +int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len) +{ + char _data = 0; + int i =0, ii = 0; + + IF_CHECK_START_CMD(_data) + { + CHECK_DATA(cmd | REPLY_FLAG, _data){}; + + CHECK_DATA(numParam, _data); + { + readParamLen16(param_len); + for (ii=0; ii<(*param_len); ++ii) + { + // Get Params data + param[ii] = spiTransfer(DUMMY_DATA); + } + } + + readAndCheckChar(END_CMD, &_data); + } + + return 1; +} +*/ + +int SpiDrv::waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len) +{ + char _data = 0; + uint16_t ii = 0; + + IF_CHECK_START_CMD(_data) + { + CHECK_DATA(cmd | REPLY_FLAG, _data){}; + + uint8_t numParam = readChar(); + if (numParam != 0) + { + readParamLen16(param_len); + for (ii=0; ii<(*param_len); ++ii) + { + // Get Params data + param[ii] = spiTransfer(DUMMY_DATA); + } + } + + readAndCheckChar(END_CMD, &_data); + } + + return 1; +} + +int SpiDrv::waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len) +{ + char _data = 0; + int ii = 0; + + IF_CHECK_START_CMD(_data) + { + CHECK_DATA(cmd | REPLY_FLAG, _data){}; + + uint8_t numParam = readChar(); + if (numParam != 0) + { + readParamLen8(param_len); + for (ii=0; ii<(*param_len); ++ii) + { + // Get Params data + param[ii] = spiTransfer(DUMMY_DATA); + } + } + + readAndCheckChar(END_CMD, &_data); + } + + return 1; +} + +int SpiDrv::waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params) +{ + char _data = 0; + int i =0, ii = 0; + + + IF_CHECK_START_CMD(_data) + { + CHECK_DATA(cmd | REPLY_FLAG, _data){}; + + uint8_t _numParam = readChar(); + if (_numParam != 0) + { + for (i=0; i<_numParam; ++i) + { + params[i].paramLen = readParamLen8(); + for (ii=0; ii maxNumParams) + { + numParam = maxNumParams; + } + *numParamRead = numParam; + if (numParam != 0) + { + for (i=0; i maxNumParams) + { + numParam = maxNumParams; + } + *numParamRead = numParam; + if (numParam != 0) + { + for (i=0; i>8)); + spiTransfer((uint8_t)(param_len & 0xff)); +} + +uint8_t SpiDrv::readParamLen8(uint8_t* param_len) +{ + uint8_t _param_len = spiTransfer(DUMMY_DATA); + if (param_len != NULL) + { + *param_len = _param_len; + } + return _param_len; +} + +uint16_t SpiDrv::readParamLen16(uint16_t* param_len) +{ + uint16_t _param_len = spiTransfer(DUMMY_DATA)<<8 | (spiTransfer(DUMMY_DATA)& 0xff); + if (param_len != NULL) + { + *param_len = _param_len; + } + return _param_len; +} + + +void SpiDrv::sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam) +{ + uint16_t i = 0; + + // Send Spi paramLen + sendParamLen16(param_len); + + // Send Spi param data + for (i=0; i>8)); + spiTransfer((uint8_t)(param & 0xff)); + + // if lastParam==1 Send Spi END CMD + if (lastParam == 1) + spiTransfer(END_CMD); +} + +/* Cmd Struct Message */ +/* _________________________________________________________________________________ */ +/*| START CMD | C/R | CMD |[TOT LEN]| N.PARAM | PARAM LEN | PARAM | .. | END CMD | */ +/*|___________|______|______|_________|_________|___________|________|____|_________| */ +/*| 8 bit | 1bit | 7bit | 8bit | 8bit | 8bit | nbytes | .. | 8bit | */ +/*|___________|______|______|_________|_________|___________|________|____|_________| */ + +void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam) +{ + // Send Spi START CMD + spiTransfer(START_CMD); + + //waitForSlaveSign(); + //wait the interrupt trigger on slave + delayMicroseconds(SPI_START_CMD_DELAY); + + // Send Spi C + cmd + spiTransfer(cmd & ~(REPLY_FLAG)); + + // Send Spi totLen + //spiTransfer(totLen); + + // Send Spi numParam + spiTransfer(numParam); + + // If numParam == 0 send END CMD + if (numParam == 0) + spiTransfer(END_CMD); + +} + +SpiDrv spiDrv; diff --git a/WiFi/utility/spi_drv.h b/WiFi/utility/spi_drv.h new file mode 100644 index 000000000..5c2e7063f --- /dev/null +++ b/WiFi/utility/spi_drv.h @@ -0,0 +1,83 @@ +#ifndef SPI_Drv_h +#define SPI_Drv_h + +#include +#include "wifi_spi.h" + +#define SPI_START_CMD_DELAY 12 + +#define NO_LAST_PARAM 0 +#define LAST_PARAM 1 + +#define DUMMY_DATA 0xFF + +#define WAIT_FOR_SLAVE_SELECT() \ + SpiDrv::waitForSlaveReady(); \ + SpiDrv::spiSlaveSelect(); + + + +class SpiDrv +{ +private: + //static bool waitSlaveReady(); + static void waitForSlaveSign(); + static void getParam(uint8_t* param); +public: + + static void begin(); + + static void end(); + + static void spiDriverInit(); + + static void spiSlaveSelect(); + + static void spiSlaveDeselect(); + + static char spiTransfer(volatile char data); + + static void waitForSlaveReady(); + + //static int waitSpiChar(char waitChar, char* readChar); + + static int waitSpiChar(unsigned char waitChar); + + static int readAndCheckChar(char checkChar, char* readChar); + + static char readChar(); + + static int waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params); + + static int waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len); + + static int waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len); + + static int waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len); + /* + static int waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams); + + static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len); +*/ + static int waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams); + + static void sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM); + + static void sendParamLen8(uint8_t param_len); + + static void sendParamLen16(uint16_t param_len); + + static uint8_t readParamLen8(uint8_t* param_len = NULL); + + static uint16_t readParamLen16(uint16_t* param_len = NULL); + + static void sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam = NO_LAST_PARAM); + + static void sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM); + + static void sendCmd(uint8_t cmd, uint8_t numParam); +}; + +extern SpiDrv spiDrv; + +#endif diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp new file mode 100644 index 000000000..45da70bdb --- /dev/null +++ b/WiFi/utility/wifi_drv.cpp @@ -0,0 +1,470 @@ +#include +#include +#include + +#include "Arduino.h" +#include "spi_drv.h" +#include "wifi_drv.h" + +#define _DEBUG_ + +extern "C" { +#include "wifi_spi.h" +#include "wl_types.h" +#include "debug.h" +} + +// Array of data to cache the information related to the networks discovered +char WiFiDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}}; +int32_t WiFiDrv::_networkRssi[WL_NETWORKS_LIST_MAXNUM] = { 0 }; +uint8_t WiFiDrv::_networkEncr[WL_NETWORKS_LIST_MAXNUM] = { 0 }; + +// Cached values of retrieved data +char WiFiDrv::_ssid[] = {0}; +uint8_t WiFiDrv::_bssid[] = {0}; +uint8_t WiFiDrv::_mac[] = {0}; +uint8_t WiFiDrv::_localIp[] = {0}; +uint8_t WiFiDrv::_subnetMask[] = {0}; +uint8_t WiFiDrv::_gatewayIp[] = {0}; + + +// Private Methods + +void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip) +{ + tParam params[PARAM_NUMS_3] = { {0, (char*)ip}, {0, (char*)mask}, {0, (char*)gwip}}; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IPADDR_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params); + + SpiDrv::spiSlaveDeselect(); +} + +// Public Methods + + +void WiFiDrv::wifiDriverInit() +{ + SpiDrv::begin(); +} + +int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + + return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : WL_FAILURE; +} + +int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + + +int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(SET_KEY_CMD, PARAM_NUMS_3); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM); + SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM); + SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +int8_t WiFiDrv::disconnect() +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return result; +} + +uint8_t WiFiDrv::getConnectionStatus() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = -1; + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _data; +} + +uint8_t* WiFiDrv::getMacAddress() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_MACADDR_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _mac; +} + +void WiFiDrv::getIpAddress(IPAddress& ip) +{ + getNetworkData(_localIp, _subnetMask, _gatewayIp); + ip = _localIp; +} + + void WiFiDrv::getSubnetMask(IPAddress& mask) + { + getNetworkData(_localIp, _subnetMask, _gatewayIp); + mask = _subnetMask; + } + + void WiFiDrv::getGatewayIP(IPAddress& ip) + { + getNetworkData(_localIp, _subnetMask, _gatewayIp); + ip = _gatewayIp; + } + +char* WiFiDrv::getCurrentSSID() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _ssid; +} + +uint8_t* WiFiDrv::getCurrentBSSID() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _bssid; +} + +int32_t WiFiDrv::getCurrentRSSI() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + int32_t rssi = 0; + SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return rssi; +} + +uint8_t WiFiDrv::getCurrentEncryptionType() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t encType = 0; + SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return encType; +} + +int8_t WiFiDrv::startScanNetworks() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + + if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + + SpiDrv::spiSlaveDeselect(); + + return (_data == WL_FAILURE)? _data : WL_SUCCESS; +} + + +uint8_t WiFiDrv::getScanNetworks() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t ssidListNum = 0; + SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM); + + SpiDrv::spiSlaveDeselect(); + + return ssidListNum; +} + +char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + + return _networkSsid[networkItem]; +} + +uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1); + + SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t encType = 0; + SpiDrv::waitResponseCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return encType; +} + +int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + int32_t networkRssi = 0; + + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1); + + SpiDrv::sendParam(&networkItem, 1, LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t dataLen = 0; + SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return networkRssi; +} + +uint8_t WiFiDrv::reqHostByName(const char* aHostname) +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1); + SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return result; +} + +int WiFiDrv::getHostByName(IPAddress& aResult) +{ + uint8_t _ipAddr[WL_IPV4_LENGTH]; + IPAddress dummy(0xFF,0xFF,0xFF,0xFF); + int result = 0; + + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen)) + { + WARN("error waitResponse"); + }else{ + aResult = _ipAddr; + result = (aResult != dummy); + } + SpiDrv::spiSlaveDeselect(); + return result; +} + +int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) +{ + uint8_t retry = 10; + if (reqHostByName(aHostname)) + { + while(!getHostByName(aResult) && --retry > 0) + { + delay(1000); + } + }else{ + return 0; + } + return (retry>0); +} + +WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h new file mode 100644 index 000000000..37563cba6 --- /dev/null +++ b/WiFi/utility/wifi_drv.h @@ -0,0 +1,208 @@ +#ifndef WiFi_Drv_h +#define WiFi_Drv_h + +#include +#include "wifi_spi.h" +#include "IPAddress.h" + +// Key index length +#define KEY_IDX_LEN 1 +// 5 secs of delay to have the connection established +#define WL_DELAY_START_CONNECTION 5000 + +class WiFiDrv +{ +private: + // settings of requested network + static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH]; + static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM]; + static uint8_t _networkEncr[WL_NETWORKS_LIST_MAXNUM]; + + // settings of current selected network + static char _ssid[WL_SSID_MAX_LENGTH]; + static uint8_t _bssid[WL_MAC_ADDR_LENGTH]; + static uint8_t _mac[WL_MAC_ADDR_LENGTH]; + static uint8_t _localIp[WL_IPV4_LENGTH]; + static uint8_t _subnetMask[WL_IPV4_LENGTH]; + static uint8_t _gatewayIp[WL_IPV4_LENGTH]; + + /* + * Get network Data information + */ + static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip); + + static uint8_t reqHostByName(const char* aHostname); + + static int getHostByName(IPAddress& aResult); + +public: + + /* + * Driver initialization + */ + static void wifiDriverInit(); + + /* + * Set the desired network which the connection manager should try to + * connect to. + * + * The ssid of the desired network should be specified. + * + * param ssid: The ssid of the desired network. + * param ssid_len: Lenght of ssid string. + * return: WL_SUCCESS or WL_FAILURE + */ + static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); + + /* Start Wifi connection with passphrase + * the most secure supported mode will be automatically selected + * + * param ssid: Pointer to the SSID string. + * param ssid_len: Lenght of ssid string. + * param passphrase: Passphrase. Valid characters in a passphrase + * must be between ASCII 32-126 (decimal). + * param len: Lenght of passphrase string. + * return: WL_SUCCESS or WL_FAILURE + */ + static int8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + + /* Start Wifi connection with WEP encryption. + * Configure a key into the device. The key type (WEP-40, WEP-104) + * is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104). + * + * param ssid: Pointer to the SSID string. + * param ssid_len: Lenght of ssid string. + * param key_idx: The key index to set. Valid values are 0-3. + * param key: Key input buffer. + * param len: Lenght of key string. + * return: WL_SUCCESS or WL_FAILURE + */ + static int8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); + + /* + * Disconnect from the network + * + * return: WL_SUCCESS or WL_FAILURE + */ + static int8_t disconnect(); + + /* + * Disconnect from the network + * + * return: one value of wl_status_t enum + */ + static uint8_t getConnectionStatus(); + + /* + * Get the interface MAC address. + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + static uint8_t* getMacAddress(); + + /* + * Get the interface IP address. + * + * return: copy the ip address value in IPAddress object + */ + static void getIpAddress(IPAddress& ip); + + /* + * Get the interface subnet mask address. + * + * return: copy the subnet mask address value in IPAddress object + */ + static void getSubnetMask(IPAddress& mask); + + /* + * Get the gateway ip address. + * + * return: copy the gateway ip address value in IPAddress object + */ + static void getGatewayIP(IPAddress& ip); + + /* + * Return the current SSID associated with the network + * + * return: ssid string + */ + static char* getCurrentSSID(); + + /* + * Return the current BSSID associated with the network. + * It is the MAC address of the Access Point + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + static uint8_t* getCurrentBSSID(); + + /* + * Return the current RSSI /Received Signal Strength in dBm) + * associated with the network + * + * return: signed value + */ + static int32_t getCurrentRSSI(); + + /* + * Return the Encryption Type associated with the network + * + * return: one value of wl_enc_type enum + */ + static uint8_t getCurrentEncryptionType(); + + /* + * Start scan WiFi networks available + * + * return: Number of discovered networks + */ + static int8_t startScanNetworks(); + + /* + * Get the networks available + * + * return: Number of discovered networks + */ + static uint8_t getScanNetworks(); + + /* + * Return the SSID discovered during the network scan. + * + * param networkItem: specify from which network item want to get the information + * + * return: ssid string of the specified item on the networks scanned list + */ + static char* getSSIDNetoworks(uint8_t networkItem); + + /* + * Return the RSSI of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: signed value of RSSI of the specified item on the networks scanned list + */ + static int32_t getRSSINetoworks(uint8_t networkItem); + + /* + * Return the encryption type of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ + static uint8_t getEncTypeNetowrks(uint8_t networkItem); + + /* + * Resolve the given hostname to an IP address. + * param aHostname: Name to be resolved + * param aResult: IPAddress structure to store the returned IP address + * result: 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ + static int getHostByName(const char* aHostname, IPAddress& aResult); + +}; + +extern WiFiDrv wiFiDrv; + +#endif diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h new file mode 100644 index 000000000..cd3fab7ce --- /dev/null +++ b/WiFi/utility/wifi_spi.h @@ -0,0 +1,143 @@ +#ifndef WiFi_Spi_h +#define WiFi_Spi_h + +#include "wl_definitions.h" + +#define CMD_FLAG 0 +#define REPLY_FLAG 1<<7 +#define DATA_FLAG 0x40 + +#define WIFI_SPI_ACK 1 +#define WIFI_SPI_ERR 0xFF + +#define TIMEOUT_CHAR 1000 + +//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */ +#define NO_SOCKET_AVAIL 255 + +#define START_CMD 0xE0 +#define END_CMD 0xEE +#define ERR_CMD 0xEF + +enum { + SET_NET_CMD = 0x10, + SET_PASSPHRASE_CMD = 0x11, + SET_KEY_CMD = 0x12, + TEST_CMD = 0x13, + + GET_CONN_STATUS_CMD = 0x20, + GET_IPADDR_CMD = 0x21, + GET_MACADDR_CMD = 0x22, + GET_CURR_SSID_CMD = 0x23, + GET_CURR_BSSID_CMD = 0x24, + GET_CURR_RSSI_CMD = 0x25, + GET_CURR_ENCT_CMD = 0x26, + SCAN_NETWORKS = 0x27, + START_SERVER_TCP_CMD= 0x28, + GET_STATE_TCP_CMD = 0x29, + DATA_SENT_TCP_CMD = 0x2A, + AVAIL_DATA_TCP_CMD = 0x2B, + GET_DATA_TCP_CMD = 0x2C, + START_CLIENT_TCP_CMD= 0x2D, + STOP_CLIENT_TCP_CMD = 0x2E, + GET_CLIENT_STATE_TCP_CMD= 0x2F, + DISCONNECT_CMD = 0x30, + GET_IDX_SSID_CMD = 0x31, + GET_IDX_RSSI_CMD = 0x32, + GET_IDX_ENCT_CMD = 0x33, + REQ_HOST_BY_NAME_CMD= 0x34, + GET_HOST_BY_NAME_CMD= 0x35, + START_SCAN_NETWORKS = 0x36, + + // All command with DATA_FLAG 0x40 send a 16bit Len + + SEND_DATA_TCP_CMD = 0x44, + GET_DATABUF_TCP_CMD = 0x45, +}; + + +enum wl_tcp_state { + CLOSED = 0, + LISTEN = 1, + SYN_SENT = 2, + SYN_RCVD = 3, + ESTABLISHED = 4, + FIN_WAIT_1 = 5, + FIN_WAIT_2 = 6, + CLOSE_WAIT = 7, + CLOSING = 8, + LAST_ACK = 9, + TIME_WAIT = 10 +}; + + +enum numParams{ + PARAM_NUMS_0, + PARAM_NUMS_1, + PARAM_NUMS_2, + PARAM_NUMS_3, + PARAM_NUMS_4, + PARAM_NUMS_5, + MAX_PARAM_NUMS +}; + +#define MAX_PARAMS MAX_PARAM_NUMS-1 +#define PARAM_LEN_SIZE 1 + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + char* param; +}tParam; + +typedef struct __attribute__((__packed__)) +{ + uint16_t dataLen; + char* data; +}tDataParam; + + +typedef struct __attribute__((__packed__)) +{ + unsigned char cmd; + unsigned char tcmd; + unsigned char nParam; + tParam params[MAX_PARAMS]; +}tSpiMsg; + +typedef struct __attribute__((__packed__)) +{ + unsigned char cmd; + unsigned char tcmd; + unsigned char nParam; + tDataParam params[MAX_PARAMS]; +}tSpiMsgData; + + +typedef struct __attribute__((__packed__)) +{ + unsigned char cmd; + unsigned char tcmd; + //unsigned char totLen; + unsigned char nParam; +}tSpiHdr; + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + uint32_t param; +}tLongParam; + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + uint16_t param; +}tIntParam; + +typedef struct __attribute__((__packed__)) +{ + uint8_t paramLen; + uint8_t param; +}tByteParam; + +#endif diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h new file mode 100644 index 000000000..15de781fc --- /dev/null +++ b/WiFi/utility/wl_definitions.h @@ -0,0 +1,50 @@ +/* + * wl_definitions.h + * + * Created on: Mar 6, 2011 + * Author: dlafauci + */ + +#ifndef WL_DEFINITIONS_H_ +#define WL_DEFINITIONS_H_ + +// Maximum size of a SSID +#define WL_SSID_MAX_LENGTH 32 +// Length of passphrase. Valid lengths are 8-63. +#define WL_WPA_KEY_MAX_LENGTH 63 +// Length of key in bytes. Valid values are 5 and 13. +#define WL_WEP_KEY_MAX_LENGTH 13 +// Size of a MAC-address or BSSID +#define WL_MAC_ADDR_LENGTH 6 +// Size of a MAC-address or BSSID +#define WL_IPV4_LENGTH 4 +// Maximum size of a SSID list +#define WL_NETWORKS_LIST_MAXNUM 10 +// Maxmium number of socket +#define MAX_SOCK_NUM 4 +//Maximum number of attempts to establish wifi connection +#define WL_MAX_ATTEMPT_CONNECTION 10 + +typedef enum { + WL_NO_SHIELD = 255, + WL_IDLE_STATUS = 0, + WL_NO_SSID_AVAIL, + WL_SCAN_COMPLETED, + WL_CONNECTED, + WL_CONNECT_FAILED, + WL_CONNECTION_LOST, + WL_DISCONNECTED +} wl_status_t; + +/* Encryption modes */ +enum wl_enc_type { /* Values map to 802.11 encryption suites... */ + ENC_TYPE_WEP = 5, + ENC_TYPE_TKIP = 2, + ENC_TYPE_CCMP = 4, + /* ... except these two, 7 and 8 are reserved in 802.11-2007 */ + ENC_TYPE_NONE = 7, + ENC_TYPE_AUTO = 8 +}; + + +#endif /* WL_DEFINITIONS_H_ */ diff --git a/WiFi/utility/wl_types.h b/WiFi/utility/wl_types.h new file mode 100644 index 000000000..82b309d7f --- /dev/null +++ b/WiFi/utility/wl_types.h @@ -0,0 +1,31 @@ +/* + * wl_types.h + * + * Created on: Jul 30, 2010 + * Author: dlafauci + */ + + +#ifndef _WL_TYPES_H_ +#define _WL_TYPES_H_ + +#include + +typedef enum { + WL_FAILURE = -1, + WL_SUCCESS = 1, +} wl_error_code_t; + +/* Authentication modes */ +enum wl_auth_mode { + AUTH_MODE_INVALID, + AUTH_MODE_AUTO, + AUTH_MODE_OPEN_SYSTEM, + AUTH_MODE_SHARED_KEY, + AUTH_MODE_WPA, + AUTH_MODE_WPA2, + AUTH_MODE_WPA_PSK, + AUTH_MODE_WPA2_PSK +}; + +#endif //_WL_TYPES_H_ From 970dd043ce1838b527f12e83b1f5beb180e3f54f Mon Sep 17 00:00:00 2001 From: Federico Vanzati Date: Wed, 6 Jun 2012 17:31:14 +0200 Subject: [PATCH 15/20] added serial opening mod for Leonardo in examples --- WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino | 7 +++++-- WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino | 7 +++++-- WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino | 7 +++++-- WiFi/examples/ScanNetworks/ScanNetworks.ino | 7 +++++-- WiFi/examples/WifiChatServer/WifiChatServer.ino | 7 +++++-- WiFi/examples/WifiCosmClient/WifiCosmClient.ino | 7 +++++-- .../examples/WifiCosmClientString/WifiCosmClientString.ino | 7 +++++-- WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino | 7 +++++-- WiFi/examples/WifiWebClient/WifiWebClient.ino | 6 +++++- .../WifiWebClientRepeating/WifiWebClientRepeating.ino | 7 +++++-- WiFi/examples/WifiWebServer/WifiWebServer.ino | 7 +++++-- 11 files changed, 55 insertions(+), 21 deletions(-) diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino index 2609320c1..f42a7f377 100644 --- a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino +++ b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -18,8 +18,11 @@ char ssid[] = "yourNetwork"; // the name of your network int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { - // initialize serial: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino index e6f531ca8..19736b5b2 100644 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -30,8 +30,11 @@ int keyIndex = 0; // your network key Index numbe int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { - // initialize serial: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino index 953841500..fcc33ecaa 100644 --- a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -19,8 +19,11 @@ char pass[] = "secretPassword"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status void setup() { - // initialize serial: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index efe171adb..829e25409 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -19,8 +19,11 @@ #include void setup() { - // initialize serial and wait for the port to open: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index f231073ba..e4b1d1a3b 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -34,8 +34,11 @@ WiFiServer server(23); boolean alreadyConnected = false; // whether or not the client was connected previously void setup() { - // start serial port: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino index 668c9e0ac..cc456a14e 100644 --- a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino +++ b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino @@ -46,8 +46,11 @@ boolean lastConnected = false; // state of the connection last t const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com void setup() { - // start serial port: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino index 3634ed762..a7b0b223f 100644 --- a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino +++ b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino @@ -51,8 +51,11 @@ boolean lastConnected = false; // state of the connection last t const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com void setup() { - // start serial port: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 3bbe63e8e..3dc2c8d33 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -50,8 +50,11 @@ void setup() { // reserve space for the strings: currentLine.reserve(256); tweet.reserve(150); - // start serial port: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 54ec68b71..17f44a3aa 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -40,7 +40,11 @@ IPAddress server(173,194,73,105); // numeric IP for Google (no DNS) WiFiClient client; void setup() { - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino index 1c623d12e..96eb6283d 100644 --- a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -36,8 +36,11 @@ boolean lastConnected = false; // state of the connection last const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds void setup() { - // start serial port: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index 72bd8e0b9..ac5f056f1 100644 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -29,8 +29,11 @@ int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { - // start serial port: - Serial.begin(9600); + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { From 28df8dc4b1c934d17cf7ce637a31af6c67e91236 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Thu, 7 Jun 2012 00:55:35 +0200 Subject: [PATCH 16/20] Fix issue on write error --- WiFi/WiFiClient.cpp | 9 +++++++-- WiFi/utility/server_drv.cpp | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index 470a42938..83c0d10b9 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -70,12 +70,17 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size) { } - if ((!ServerDrv::sendData(_sock, buf, size)) || - (!ServerDrv::checkDataSent(_sock))) + if (!ServerDrv::sendData(_sock, buf, size)) { setWriteError(); return 0; } + if (!ServerDrv::checkDataSent(_sock)) + { + setWriteError(); + return 0; + } + return size; } diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index a325d5a06..ce03604b4 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -226,8 +226,8 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) uint8_t ServerDrv::checkDataSent(uint8_t sock) { - const uint16_t TIMEOUT_DATA_SENT = 250; - static uint16_t timeout = 0; + const uint16_t TIMEOUT_DATA_SENT = 25; + uint16_t timeout = 0; uint8_t _data = 0; uint8_t _dataLen = 0; @@ -250,7 +250,7 @@ uint8_t ServerDrv::checkDataSent(uint8_t sock) if (_data) timeout = 0; else{ ++timeout; - delay(10); + delay(100); } }while((_data==0)&&(timeout Date: Thu, 7 Jun 2012 13:51:48 +0200 Subject: [PATCH 17/20] Fixed issue with Cosm sketch --- WiFi/examples/WifiCosmClient/WifiCosmClient.ino | 2 +- WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino index cc456a14e..cac43c06d 100644 --- a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino +++ b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino @@ -81,7 +81,7 @@ void loop() { // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: - if (client.available()) { + while (client.available()) { char c = client.read(); Serial.print(c); } diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino index a7b0b223f..0c78a638c 100644 --- a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino +++ b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino @@ -95,7 +95,7 @@ void loop() { // if there's incoming data from the net connection. // send it out the serial port. This is for debugging // purposes only: - if (client.available()) { + while (client.available()) { char c = client.read(); Serial.print(c); } From 6f27863ae5c4cda1b587b67c30364fa5700e7d95 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Fri, 8 Jun 2012 18:56:47 +0200 Subject: [PATCH 18/20] Added function to get firmware version --- WiFi/WiFi.cpp | 5 +++++ WiFi/WiFi.h | 6 ++++++ WiFi/utility/spi_drv.cpp | 13 ++++++++----- WiFi/utility/wifi_drv.cpp | 21 +++++++++++++++++++++ WiFi/utility/wifi_drv.h | 11 +++++++++++ WiFi/utility/wifi_spi.h | 1 + 6 files changed, 52 insertions(+), 5 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index fd45e9689..c0cb0016d 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -34,6 +34,11 @@ uint8_t WiFiClass::getSocket() return NO_SOCKET_AVAIL; } +char* WiFiClass::firmwareVersion() +{ + return WiFiDrv::getFwVersion(); +} + int WiFiClass::begin(char* ssid) { uint8_t status = WL_IDLE_STATUS; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 501780b3f..9a86701a0 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -28,6 +28,12 @@ public: */ static uint8_t getSocket(); + /* + * Get firmware version + */ + static char* firmwareVersion(); + + /* Start Wifi connection for OPEN networks * * param ssid: Pointer to the SSID string. diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index ceef832c0..fb792bcd1 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -7,11 +7,12 @@ extern "C" { #include "debug.h" } -#define DATAOUT 11//MOSI -#define DATAIN 12//MISO -#define SPICLOCK 13//sck -#define SLAVESELECT 10//ss -#define SLAVEREADY 3 +#define DATAOUT 11 // MOSI +#define DATAIN 12 // MISO +#define SPICLOCK 13 // sck +#define SLAVESELECT 10 // ss +#define SLAVEREADY 3 // handshake pin +#define WIFILED 9 // led on wifi shield #define DELAY_100NS do { asm volatile("nop"); }while(0); #define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); }while(++ii0); } +char* WiFiDrv::getFwVersion() +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(GET_FW_VERSION_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(GET_FW_VERSION_CMD, PARAM_NUMS_1, (uint8_t*)fwVersion, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return fwVersion; +} + WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 37563cba6..c4f04dbe7 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -9,6 +9,8 @@ #define KEY_IDX_LEN 1 // 5 secs of delay to have the connection established #define WL_DELAY_START_CONNECTION 5000 +// firmware version string length +#define WL_FW_VER_LENGTH 6 class WiFiDrv { @@ -18,6 +20,9 @@ private: static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM]; static uint8_t _networkEncr[WL_NETWORKS_LIST_MAXNUM]; + // firmware version string in the format a.b.c + static char fwVersion[WL_FW_VER_LENGTH]; + // settings of current selected network static char _ssid[WL_SSID_MAX_LENGTH]; static uint8_t _bssid[WL_MAC_ADDR_LENGTH]; @@ -201,6 +206,12 @@ public: */ static int getHostByName(const char* aHostname, IPAddress& aResult); + /* + * Get the firmware version + * result: version as string with this format a.b.c + */ + static char* getFwVersion(); + }; extern WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index cd3fab7ce..bf479e2ec 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -48,6 +48,7 @@ enum { REQ_HOST_BY_NAME_CMD= 0x34, GET_HOST_BY_NAME_CMD= 0x35, START_SCAN_NETWORKS = 0x36, + GET_FW_VERSION_CMD = 0x37, // All command with DATA_FLAG 0x40 send a 16bit Len From 056895d1f9b35a53d29fc4d12b4304ba77391974 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Mon, 18 Jun 2012 07:44:30 +0200 Subject: [PATCH 19/20] Changed Handshake pin to 7. Add some spi stats --- WiFi/utility/spi_drv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index fb792bcd1..12a320b0d 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -11,7 +11,7 @@ extern "C" { #define DATAIN 12 // MISO #define SPICLOCK 13 // sck #define SLAVESELECT 10 // ss -#define SLAVEREADY 3 // handshake pin +#define SLAVEREADY 7 // handshake pin #define WIFILED 9 // led on wifi shield #define DELAY_100NS do { asm volatile("nop"); }while(0); From 156c102c0debaa42cdff60d115bbff76e448f4f3 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 21 Jun 2012 22:04:31 -0400 Subject: [PATCH 20/20] Updated ScanNetworks example to give network encryption types by name --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 31 +++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index 829e25409..93b30006e 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -10,8 +10,8 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 31 May 2012 - by Tom Igoe + modified 21 Junn 2012 + by Tom Igoe and Jaymes Dec */ @@ -24,14 +24,14 @@ void setup() { while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } - + // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); // don't continue: while(true); } - + // Print WiFi MAC address: printMacAddress(); @@ -90,9 +90,30 @@ void listNetworks() { Serial.print(WiFi.RSSI(thisNet)); Serial.print(" dBm"); Serial.print("\tEncryption: "); - Serial.println(WiFi.encryptionType(thisNet)); + printEncryptionType(WiFi.encryptionType(thisNet)); } } +void printEncryptionType(int thisType) { + // read the encryption type and print out the name: + switch (thisType) { + case ENC_TYPE_WEP: + Serial.println("WEP"); + break; + case ENC_TYPE_TKIP: + Serial.println("WPA"); + break; + case ENC_TYPE_CCMP: + Serial.println("WPA2"); + break; + case ENC_TYPE_NONE: + Serial.println("None"); + break; + case ENC_TYPE_AUTO: + Serial.println("Auto"); + break; + } +} +