From 7f3e8a6c5bc7fc50a51071a3f1540017a25d1ab5 Mon Sep 17 00:00:00 2001 From: Mimmo Date: Mon, 28 Feb 2011 13:49:08 +0100 Subject: [PATCH 01/98] WiFi API first draft --- WiFi/Client.cpp | 164 ++++++++++++++++ WiFi/Client.h | 35 ++++ WiFi/Server.cpp | 78 ++++++++ WiFi/Server.h | 25 +++ WiFi/WiFi.cpp | 184 ++++++++++++++++++ WiFi/WiFi.h | 100 ++++++++++ WiFi/examples/wifi_example/wifi_example.pde | 29 +++ WiFi/examples/wifi_example2/wifi_example2.pde | 107 ++++++++++ WiFi/keywords.txt | 45 +++++ 9 files changed, 767 insertions(+) create mode 100755 WiFi/Client.cpp create mode 100755 WiFi/Client.h create mode 100755 WiFi/Server.cpp create mode 100755 WiFi/Server.h create mode 100755 WiFi/WiFi.cpp create mode 100755 WiFi/WiFi.h create mode 100755 WiFi/examples/wifi_example/wifi_example.pde create mode 100755 WiFi/examples/wifi_example2/wifi_example2.pde create mode 100755 WiFi/keywords.txt diff --git a/WiFi/Client.cpp b/WiFi/Client.cpp new file mode 100755 index 000000000..36f54cde7 --- /dev/null +++ b/WiFi/Client.cpp @@ -0,0 +1,164 @@ +extern "C" { + #include "wl_types.h" + #include "socket.h" + #include "string.h" +} + +#include "WProgram.h" + +#include "WiFi.h" +#include "Client.h" +#include "Server.h" +#include "server_drv.h" + +uint16_t Client::_srcport = 0; + +Client::Client(uint8_t sock) { + _sock = sock; +} + +Client::Client(uint8_t *ip, uint16_t port) { + _ip = ip; + _port = port; + _sock = 255; +} + +uint8_t Client::getFirstSock() +{ + for (int i = 0; i < MAX_SOCK_NUM; i++) { + if (WiFiClass::_state[i] < 0) + { + return i; + } + } + return SOCK_NOT_AVAIL; +} + +uint8_t Client::connect() { + _sock = getFirstSock(); + +// _srcport++; +// if (_srcport + 1024 == 0) _srcport = 0; + _socket = socket(TCP_SOCKET); + if (_socket<0) + { + return 0; + }else{ + WiFiClass::_state[_sock] = _socket; + } + + if (!::connect(_socket, _ip, _port)) { + return 0; + } + return 1; +} + +void Client::write(uint8_t b) { + if (_sock != 255) + { + while (!ServerDrv::isDataSent(_sock)); + ServerDrv::sendData(_sock, &b, 1); + } +} + +void Client::write(const char *str) { + if (_sock != 255) + { + while (!ServerDrv::isDataSent(_sock)); + unsigned int len = strlen(str); + ServerDrv::sendData(_sock, (const uint8_t *)str, len); + } +} + +void Client::write(const uint8_t *buf, size_t size) { + if (_sock != 255) + { + while (!ServerDrv::isDataSent(_sock)); + ServerDrv::sendData(_sock, buf, size); + } + +} + +int Client::available() { + if (_sock != 255) + { + return ServerDrv::availData(_sock); + } + + return 0; +} + +int Client::read() { + uint8_t b; + if (!available()) + return -1; + ServerDrv::getData(_sock, &b); + return b; +} + + +int Client::readBuf(uint8_t* buf, uint16_t* len) { + if (!ServerDrv::getDataBuf(_sock, buf, len)) + return -1; + return 0; +} + +void Client::flush() { + while (available()) + read(); +} + +void Client::stop() { + if (_sock == 255) + return; + + // attempt to close the connection gracefully (send a FIN to other side) + disconnect(WiFiClass::_state[_sock]); + unsigned long start = millis(); + + // wait a second for the connection to close + while (status() != CLOSED && millis() - start < 1000) + delay(1); + + // if it hasn't closed, close it forcefully + if (status() != CLOSED) + close(_sock); + + WiFiClass::_server_port[_sock] = 0; + _sock = 255; +} + +uint8_t Client::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 == CLOSE_WAIT && !available())); + } +} + +uint8_t Client::status() { + if (_sock == 255) { + return CLOSED; + } else { + return ServerDrv::getState(_sock); + } +} + +// the next three functions are a hack so we can compare the client returned +// by Server::available() to null, or use it as the condition in an +// if-statement. this lets us stay compatible with the Processing network +// library. + +uint8_t Client::operator==(int p) { + return _sock == 255; +} + +uint8_t Client::operator!=(int p) { + return _sock != 255; +} + +Client::operator bool() { + return _sock != 255; +} diff --git a/WiFi/Client.h b/WiFi/Client.h new file mode 100755 index 000000000..621229155 --- /dev/null +++ b/WiFi/Client.h @@ -0,0 +1,35 @@ +#ifndef Client_h +#define Client_h + +#include "Print.h" + +class Client : public Print { +private: + static uint16_t _srcport; + uint8_t _sock; //not used + uint8_t *_ip; + uint16_t _port; + uint16_t _socket; + +public: + Client(uint8_t); + Client(uint8_t *, uint16_t); + uint8_t status(); + uint8_t connect(); + virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); + int available(); + int read(); + int readBuf(uint8_t* buf, uint16_t* len); + void flush(); + void stop(); + uint8_t connected(); + uint8_t operator==(int); + uint8_t operator!=(int); + operator bool(); + uint8_t getFirstSock(); + friend class Server; +}; + +#endif diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp new file mode 100755 index 000000000..e9b58137e --- /dev/null +++ b/WiFi/Server.cpp @@ -0,0 +1,78 @@ +#include +#include "HardwareSerial.h" +#include "wifi_spi.h" +#include "Server.h" +#include "Client.h" +#include "WiFi.h" +#include "server_drv.h" +#include "wiring.h" + +Server::Server(uint16_t port) +{ + _port = port; +} + +void Server::begin() +{ + uint8_t _sock = WiFiClass::getSocket(); + if (_sock != NO_SOCKET_AVAIL) + { + ServerDrv::StartServer(_port, _sock); + WiFiClass::_server_port[_sock] = _port; + } +} + +Client Server::available(byte* status) +{ + //accept(); + + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) + { + if (WiFiClass::_server_port[sock] != 0) + { + Client client(sock); + *status = client.status(); + + if (WiFiClass::_server_port[sock] == _port && + *status == ESTABLISHED) + { + return client; //TODO + }else{ + delayMicroseconds(100); + } + + } + } + + return Client(255); +} + +void Server::write(uint8_t b) +{ + write(&b, 1); +} + +void Server::write(const char *str) +{ + write((const uint8_t *)str, strlen(str)); +} + +void Server::write(const uint8_t *buffer, size_t size) +{ + for (int sock = 0; sock < MAX_SOCK_NUM; sock++) + { + if (WiFiClass::_server_port[sock] != 0) + { + Client client(sock); + + if (WiFiClass::_server_port[sock] == _port && + client.status() == ESTABLISHED) + { + client.write(buffer, size); + }else{ + delay(20); + } + + } + } +} diff --git a/WiFi/Server.h b/WiFi/Server.h new file mode 100755 index 000000000..fdab7945b --- /dev/null +++ b/WiFi/Server.h @@ -0,0 +1,25 @@ +#ifndef Server_h +#define Server_h + +extern "C" { + #include "utility/wl_types.h" +} + +#include "Print.h" + +class Client; + +class Server : public Print { +private: + uint16_t _port; + void* pcb; +public: + Server(uint16_t); + Client available(uint8_t*); + void begin(); + virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); +}; + +#endif diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp new file mode 100755 index 000000000..e68a98d42 --- /dev/null +++ b/WiFi/WiFi.cpp @@ -0,0 +1,184 @@ +#include "WProgram.h" +#include "WiFi.h" +#include +#include "HardwareSerial.h" +#include "wifi_drv.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 }; + +char WiFiClass::ssid[WL_SSID_MAX_LENGTH] = { 0 }; +uint8_t WiFiClass::ssid_len; +char WiFiClass::key[13] = { 0 }; +uint8_t WiFiClass::key_len; +char WiFiClass::passph[63] = { 0 }; +uint8_t WiFiClass::passph_len; +wl_status_t WiFiClass::status; + +void readEEdata(int addr, uint8_t* data, uint8_t len) +{ + for (int i = 0; i < len; ++i) + { + data[i]= EEPROM.read(addr); + } +} + +WiFiClass::WiFiClass() +{ +} + +void WiFiClass::init() +{ + Serial.begin(9600); + Serial.println("WiFi initializing..."); + WiFiDrv::wifi_drv_init(); +} + + +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; +} + +void WiFiClass::begin() +{ + init(); + Serial.println("WiFi Starting..."); + ssid_len = EEPROM.read(EE_WIFI_SSID_LEN); + if ((ssid_len == 0)||(ssid_len > WL_SSID_MAX_LENGTH)) + { + Serial.println("No SSID in EEPROM"); + + status = WL_NO_SSID_AVAIL; + return; + } + readEEdata(EE_WIFI_SSID_DATA, (uint8_t*)&ssid[0], ssid_len); + + Serial.print("SSID: "); + Serial.print(ssid_len, 10); + Serial.print(" - "); + Serial.print(ssid[0]); + Serial.println(""); + key_len = EEPROM.read(EE_WIFI_KEY_LEN); + if (key_len == 0) + { + Serial.println("No PASSPHRASE in EEPROM"); + + passph_len = EEPROM.read(EE_WIFI_PASSPH_LEN); + if (passph_len == 0) + { + begin(ssid, ssid_len); + }else{ + readEEdata(EE_WIFI_PASSPH_DATA, (uint8_t*)&passph[0], passph_len); + beginp(ssid,ssid_len,&passph[0],passph_len); + } + }else{ + Serial.println("No KEY in EEPROM"); + + readEEdata(EE_WIFI_KEY_DATA, (uint8_t*)&key[0], key_len); + begink(ssid,ssid_len, 0, &key[0],key_len); + } +} + +void WiFiClass::begin(char* ssid, uint8_t ssid_len) +{ + wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_net(ssid, ssid_len); + if (result == WL_SUCCESS) + { + status = WL_CONNECTED; + Serial.println("WiFi Connected!"); + }else{ + status = WL_CONNECT_FAILED; + Serial.println("WiFi Connection failed!"); + } +} + +void WiFiClass::begink(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t key_len) +{ + // set encryption key + wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_key(ssid, ssid_len, key_idx, key, key_len); + if (result == WL_SUCCESS) + { + //begin(ssid, ssid_len); + }else{ + // Error setting passphrase + } + +} + +void WiFiClass::beginp(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) +{ + // set passphrase + wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_passpharse(ssid, ssid_len, passphrase, len); + if (result == WL_SUCCESS) + { + //begin(ssid, ssid_len); + }else{ + // Error setting passphrase + } +} + +wl_status_t WiFiClass::get_status() +{ + return status; +} + +uint8_t WiFiClass::get_result(uint8_t dummy) +{ + uint8_t result = WiFiDrv::wifi_get_result(dummy); + return result; +} + + +void WiFiClass::getIpAddr(uint8_t *ip, uint8_t *mask, uint8_t *gwip) +{ + WiFiDrv::getIpAddr(ip,mask,gwip); +} + + +uint8_t WiFiClass::getMacAddr(uint8_t* mac) +{ + return WiFiDrv::wl_get_mac_addr(mac); +} + +void WiFiClass::getSSIDList(char** ssid_list, uint8_t* ssidListNum) +{ + WiFiDrv::getSSIDList(ssid_list, ssidListNum); +} + +void WiFiClass::getCurrSSID(char* ssid) +{ + WiFiDrv::getCurrSSID(ssid); +} + +void WiFiClass::getCurrBSSID(uint8_t* bssid) +{ + WiFiDrv::getCurrBSSID(bssid); +} + +void WiFiClass::getCurrRSSI(int32_t* rssi) +{ + WiFiDrv::getCurrRSSI(rssi); +} + +void WiFiClass::getCurrEncType(uint8_t* enc_type) +{ + WiFiDrv::getCurrEncType(enc_type); +} + +uint8_t WiFiClass::disconnect() +{ + return WiFiDrv::disconnect(); +} + + +WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h new file mode 100755 index 000000000..d4edb3ec4 --- /dev/null +++ b/WiFi/WiFi.h @@ -0,0 +1,100 @@ +#ifndef WiFi_h +#define WiFi_h + +#include + +extern "C" { + #include "utility/wl_types.h" + #include "utility/wifi_spi.h" + #include "utility/debug.h" // only for test, not released +} + +#include "Client.h" +#include "Server.h" + +// location in EEPROM to store key information +#define EE_WIFI_DATA_ADDR 0x10 +#define EE_WIFI_SSID_LEN EE_WIFI_DATA_ADDR +#define EE_WIFI_KEY_LEN EE_WIFI_DATA_ADDR + 1 +#define EE_WIFI_PASSPH_LEN EE_WIFI_DATA_ADDR + 2 +#define EE_WIFI_SSID_DATA EE_WIFI_DATA_ADDR + 3 +#define EE_WIFI_KEY_DATA EE_WIFI_DATA_ADDR + 4 +#define EE_WIFI_PASSPH_DATA EE_WIFI_DATA_ADDR + 4 +// Note: Or KEY or PASSPH can be defined. +// The selection is made by the len not equal to zero + +class WiFiClass +{ +private: + // this data are stored in EEPROM and loaded at begin + // The next connect overwrite these values + static char ssid[WL_SSID_MAX_LENGTH]; + static uint8_t ssid_len; + static char key[13]; + static uint8_t key_len; + static char passph[63]; + static uint8_t passph_len; + static wl_status_t status; + + void init(); +public: + //static wl_state_t _wl_state[MAX_SOCK_NUM]; + static int16_t _state[MAX_SOCK_NUM]; + static uint16_t _server_port[MAX_SOCK_NUM]; + WiFiClass(); + + // Start Wifi connection with data stored in EEPROM + void begin(); + + // Start Wifi connection without WEP or WPA + void begin(char* ssid, uint8_t ssid_len); + + // Start Wifi connection with WEP + void begink(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t key_len); + + // Start Wifi connection with WPA + void beginp(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + + //getSock get the first socket available + static uint8_t getSocket(); + + // verify the completion of the scan command + wl_status_t get_status(); + + // get the result of the last operation + uint8_t get_result(uint8_t dummy = 0); + + // Disconnect from the network + uint8_t disconnect(void); + + //Get the interface MAC address. + uint8_t getMacAddr(uint8_t* mac); + + //Get the DHCP infortion related to IP, netmas, gateway + void getIpAddr(uint8_t *ip, uint8_t *mask, uint8_t *gwip); + + // Get the list of currently known networks. + wl_error_code_t wl_get_network_list(struct wl_network_t** network_list, uint8_t* network_cnt); + + // Return a list of all SSID available + void getSSIDList(char** ssid_list, uint8_t* ssidListNum); + + // Return the current SSID associated with the network + void getCurrSSID(char* ssid); + + // Return the current BSSID associated with the network + void getCurrBSSID(uint8_t* bssid); + + // Return the current RSSI associated with the network + void getCurrRSSI(int32_t* rssi); + + // Return the current Encryption Type associated with the network + void getCurrEncType(uint8_t* enc_type); + + friend class Client; + friend class Server; +}; + +extern WiFiClass WiFi; + +#endif diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/wifi_example/wifi_example.pde new file mode 100755 index 000000000..a96c5fc32 --- /dev/null +++ b/WiFi/examples/wifi_example/wifi_example.pde @@ -0,0 +1,29 @@ +/* + WiFi example + + A simple connection with WiFi AP with Wireless Security + information loaded in EEPROM + + created 13 July 2010 + by Domenico La Fauci + */ + +#include +#include + +void setup() +{ + Serial.begin(9600); + WiFi.begin(); + if (WiFi.get_status() == WL_NO_SSID_AVAIL) + { + // SSID not present in EEPROM + char ssid[] = "Cariddi"; + WiFi.begin(ssid, strlen(ssid)); + } +} + +void loop() +{ +} + diff --git a/WiFi/examples/wifi_example2/wifi_example2.pde b/WiFi/examples/wifi_example2/wifi_example2.pde new file mode 100755 index 000000000..d16908300 --- /dev/null +++ b/WiFi/examples/wifi_example2/wifi_example2.pde @@ -0,0 +1,107 @@ +/* + WiFi example + + A simple connection with WiFi AP with Wireless Security + information loaded in EEPROM, if not available + try to access with WPA or WEP security keys + + created 13 July 2010 + by Domenico La Fauci + */ + +#include +#include +#include + +// network configuration. gateway and subnet are optional. +byte mac[6] = { 0 }; +byte ip[4] = { 0 }; +byte gateway[4] = { 0 }; +byte subnet[4] = { 0 }; + + +void setup() +{ + Serial.begin(9600); + Serial.println("Setup WiFi..."); + WiFi.begin(); + if (WiFi.get_status() == WL_NO_SSID_AVAIL) + { + //uint8_t result = WiFi.get_result(); + //Serial.println(result,16); + + // SSID not present in EEPROM + char ssid[] = "Cariddi"; + Serial.println(ssid); + delay(2000); + + // Using WPA + WiFi.begin(ssid, strlen(ssid)); + delay(10000); + if (WiFi.get_result() == WL_CONNECT_FAILED) + { + Serial.println("Trying with Passphrase..."); + uint8_t key_idx = 0; + const char *pass = "12345678"; + uint8_t pass_len = strlen(pass); + WiFi.beginp(ssid, strlen(ssid), pass, pass_len); + } + // wait to trying connection... + delay(5000); + + // using WEP + if (WiFi.get_result() == WL_CONNECT_FAILED) + { + Serial.println("Trying with Key..."); + uint8_t key_idx = 0; + const char *key = "12345678"; + uint8_t key_len = strlen(key); + WiFi.begink(ssid, strlen(ssid),key_idx, key, key_len); + } + // wait to trying connection... + delay(5000); + + { + WiFi.getIpAddr(ip,subnet,gateway); + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.getMacAddr(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); + + } + } +} + +void loop() +{ + static uint8_t count = 0; + while (WiFi.get_status() != WL_CONNECTED) + { + uint8_t result = WiFi.get_result(count++); + Serial.println(result); + delay(3000); + } +} + diff --git a/WiFi/keywords.txt b/WiFi/keywords.txt new file mode 100755 index 000000000..ce613bdbb --- /dev/null +++ b/WiFi/keywords.txt @@ -0,0 +1,45 @@ +####################################### +# 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 +begink KEYWORD2 +beginp KEYWORD2 +disconnect KEYWORD2 +getMacAddr KEYWORD2 +getIpAddr KEYWORD2 +getSSIDList KEYWORD2 +getCurrSSID KEYWORD2 +getCurrBSSID KEYWORD2 +getCurrRSSI KEYWORD2 +getCurrEncType KEYWORD2 +wl_get_network_list KEYWORD2 +get_result KEYWORD2 +get_status KEYWORD2 +getSocket KEYWORD2 + + +####################################### +# Constants (LITERAL1) +####################################### + From 58fb458101f589f45290142394d7b272d09220ba Mon Sep 17 00:00:00 2001 From: Mimmo Date: Tue, 1 Mar 2011 19:38:14 +0100 Subject: [PATCH 02/98] Updated header file --- WiFi/WiFi.h | 80 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index d4edb3ec4..3f9051205 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -15,81 +15,91 @@ extern "C" { // location in EEPROM to store key information #define EE_WIFI_DATA_ADDR 0x10 #define EE_WIFI_SSID_LEN EE_WIFI_DATA_ADDR -#define EE_WIFI_KEY_LEN EE_WIFI_DATA_ADDR + 1 -#define EE_WIFI_PASSPH_LEN EE_WIFI_DATA_ADDR + 2 -#define EE_WIFI_SSID_DATA EE_WIFI_DATA_ADDR + 3 -#define EE_WIFI_KEY_DATA EE_WIFI_DATA_ADDR + 4 -#define EE_WIFI_PASSPH_DATA EE_WIFI_DATA_ADDR + 4 -// Note: Or KEY or PASSPH can be defined. -// The selection is made by the len not equal to zero +#define EE_WIFI_ENC_TYPE EE_WIFI_SSID_LEN + 1 +#define EE_WIFI_KEY_LEN EE_WIFI_ENC_TYPE + 1 +#define EE_WIFI_SSID_DATA EE_WIFI_KEY_LEN + 1 +#define EE_WIFI_KEY_DATA EE_WIFI_SSID_DATA + WL_SSID_MAX_LENGTH +#define EE_WIFI_END_DATA EE_WIFI_KEY_DATA + WL_KEY_MAX_LENGTH + +// Wifi Encryption selection type +#define WIFI_ENC_NONE 0 +#define WIFI_ENC_WEP 1 +#define WIFI_ENC_WPA 2 + class WiFiClass { private: // this data are stored in EEPROM and loaded at begin // The next connect overwrite these values - static char ssid[WL_SSID_MAX_LENGTH]; - static uint8_t ssid_len; - static char key[13]; - static uint8_t key_len; - static char passph[63]; - static uint8_t passph_len; - static wl_status_t status; + static char _ssid[WL_SSID_MAX_LENGTH]; + static uint8_t _ssid_len; + static char _key[WL_WEP_KEY_MAX_LENGTH]; + static uint8_t _key_len; + static char _passph[WL_WPA_KEY_MAX_LENGTH]; + static uint8_t _passph_len; + static wl_status_t _status; void init(); public: - //static wl_state_t _wl_state[MAX_SOCK_NUM]; static int16_t _state[MAX_SOCK_NUM]; static uint16_t _server_port[MAX_SOCK_NUM]; + WiFiClass(); - // Start Wifi connection with data stored in EEPROM + // Start Wifi connection void begin(); - // Start Wifi connection without WEP or WPA - void begin(char* ssid, uint8_t ssid_len); + // Set SSID name + void setSSID(char* ssid); - // Start Wifi connection with WEP - void begink(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t key_len); + // Set the encryption type: WPA, WEP or NONE + void setEncryption(uint8_t encType); - // Start Wifi connection with WPA - void beginp(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + // Set the key used for the encryption + void setPassword(const char *key, const uint8_t keyLen, uint8_t keyIndex); //getSock get the first socket available static uint8_t getSocket(); // verify the completion of the scan command - wl_status_t get_status(); + wl_status_t getScanCmdStatus(); // get the result of the last operation - uint8_t get_result(uint8_t dummy = 0); + uint8_t getResult(uint8_t dummy = 0); // Disconnect from the network uint8_t disconnect(void); //Get the interface MAC address. - uint8_t getMacAddr(uint8_t* mac); + uint8_t* getMacAddr(); - //Get the DHCP infortion related to IP, netmas, gateway - void getIpAddr(uint8_t *ip, uint8_t *mask, uint8_t *gwip); + //Get the DHCP information related to IP + uint8_t* getIpAddr(); - // Get the list of currently known networks. - wl_error_code_t wl_get_network_list(struct wl_network_t** network_list, uint8_t* network_cnt); + //Get the DHCP information related to IP + uint8_t* getNetMask(); - // Return a list of all SSID available - void getSSIDList(char** ssid_list, uint8_t* ssidListNum); + //Get the DHCP information related to IP + uint8_t* getGatewayIp(); + + // Return SSID item available + char* getSSIDListItem(uint8_t ssidListItem); + + // Return the number of SSID discovered + uint8_t getSSIDListNum(); // Return the current SSID associated with the network - void getCurrSSID(char* ssid); + char* getSSID(); // Return the current BSSID associated with the network - void getCurrBSSID(uint8_t* bssid); + uint8_t* getBSSID(); // Return the current RSSI associated with the network - void getCurrRSSI(int32_t* rssi); + int32_t getRSSI(); // Return the current Encryption Type associated with the network - void getCurrEncType(uint8_t* enc_type); + uint8_t getCurrEncType(); friend class Client; friend class Server; From 9d60bbb0dc0ae6116a51be7736f049cd9779ef18 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Sat, 5 Mar 2011 00:02:04 +0100 Subject: [PATCH 03/98] Add overloads of begin()-Add IpAddress class --- WiFi/WiFi.h | 78 +++++++++++++++++++---------------------------------- 1 file changed, 28 insertions(+), 50 deletions(-) diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 3f9051205..5cea90e4e 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -9,24 +9,10 @@ extern "C" { #include "utility/debug.h" // only for test, not released } +#include "IPAddress.h" #include "Client.h" #include "Server.h" -// location in EEPROM to store key information -#define EE_WIFI_DATA_ADDR 0x10 -#define EE_WIFI_SSID_LEN EE_WIFI_DATA_ADDR -#define EE_WIFI_ENC_TYPE EE_WIFI_SSID_LEN + 1 -#define EE_WIFI_KEY_LEN EE_WIFI_ENC_TYPE + 1 -#define EE_WIFI_SSID_DATA EE_WIFI_KEY_LEN + 1 -#define EE_WIFI_KEY_DATA EE_WIFI_SSID_DATA + WL_SSID_MAX_LENGTH -#define EE_WIFI_END_DATA EE_WIFI_KEY_DATA + WL_KEY_MAX_LENGTH - -// Wifi Encryption selection type -#define WIFI_ENC_NONE 0 -#define WIFI_ENC_WEP 1 -#define WIFI_ENC_WPA 2 - - class WiFiClass { private: @@ -47,59 +33,51 @@ public: WiFiClass(); - // Start Wifi connection - void begin(); + // Start Wifi connection with latest settings + int begin(); - // Set SSID name - void setSSID(char* ssid); + // Start Wifi connection with no encryption + int begin(char* ssid, uint8_t ssid_len); - // Set the encryption type: WPA, WEP or NONE - void setEncryption(uint8_t encType); + // Start Wifi connection with WEP encryption + int begin(char* ssid, uint8_t ssid_len, uint8_t key_idx, const char* key, const uint8_t key_len); - // Set the key used for the encryption - void setPassword(const char *key, const uint8_t keyLen, uint8_t keyIndex); - - //getSock get the first socket available - static uint8_t getSocket(); - - // verify the completion of the scan command - wl_status_t getScanCmdStatus(); - - // get the result of the last operation - uint8_t getResult(uint8_t dummy = 0); + // Start Wifi connection with passphrase + // the most secure supported mode will be automatically selected + int begin(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); // Disconnect from the network - uint8_t disconnect(void); + int disconnect(void); //Get the interface MAC address. - uint8_t* getMacAddr(); + uint8_t* macAddress(); //Get the DHCP information related to IP - uint8_t* getIpAddr(); + IPAddress localIp(); - //Get the DHCP information related to IP - uint8_t* getNetMask(); + //Get the DHCP information related to subnetMask + IPAddress subnetMask(); - //Get the DHCP information related to IP - uint8_t* getGatewayIp(); - - // Return SSID item available - char* getSSIDListItem(uint8_t ssidListItem); - - // Return the number of SSID discovered - uint8_t getSSIDListNum(); + //Get the DHCP information related to gateway IP + IPAddress gatewayIP(); // Return the current SSID associated with the network - char* getSSID(); + char* SSID(); // Return the current BSSID associated with the network - uint8_t* getBSSID(); + uint8_t* BSSID(); - // Return the current RSSI associated with the network - int32_t getRSSI(); + // Start scan SSIDs available and return the number of SSID discovered + uint8_t scanSSID(); + + // Return SSID item available + char* SSIDListItem(uint8_t ssidListItem); // Return the current Encryption Type associated with the network - uint8_t getCurrEncType(); + uint8_t encType(uint8_t ssidListItem); + + // Return the current RSSI /Received Signal Strength in dBm) associated with the network + int32_t RSSI(uint8_t ssidListItem); friend class Client; friend class Server; From 765e848fdb35f94217f9773bf092fe344920cb04 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Tue, 8 Mar 2011 22:13:54 +0100 Subject: [PATCH 04/98] Changes on WiFi API after review. Add driver utility implementation --- WiFi/Client.cpp | 70 ++- WiFi/Client.h | 32 +- WiFi/Server.cpp | 2 - WiFi/Server.h | 2 +- WiFi/WiFi.cpp | 238 +++++----- WiFi/WiFi.h | 54 ++- .../wifi_Server_example.pde | 157 +++++++ WiFi/examples/wifi_example/wifi_example.pde | 79 +++- WiFi/examples/wifi_example2/wifi_example2.pde | 107 ----- WiFi/keywords.txt | 21 +- WiFi/utility/debug.h | 67 +++ WiFi/utility/server_drv.cpp | 187 ++++++++ WiFi/utility/server_drv.h | 28 ++ WiFi/utility/socket.c | 20 + WiFi/utility/socket.h | 87 ++++ WiFi/utility/spi_drv.cpp | 434 ++++++++++++++++++ WiFi/utility/spi_drv.h | 70 +++ WiFi/utility/wifi_drv.cpp | 351 ++++++++++++++ WiFi/utility/wifi_drv.h | 68 +++ WiFi/utility/wifi_spi.h | 101 ++++ WiFi/utility/wl_definitions.h | 37 ++ WiFi/utility/wl_types.h | 42 ++ 22 files changed, 1904 insertions(+), 350 deletions(-) create mode 100644 WiFi/examples/wifi_Server_example/wifi_Server_example.pde delete mode 100755 WiFi/examples/wifi_example2/wifi_example2.pde 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/Client.cpp b/WiFi/Client.cpp index 36f54cde7..770fb8d49 100755 --- a/WiFi/Client.cpp +++ b/WiFi/Client.cpp @@ -1,5 +1,6 @@ extern "C" { - #include "wl_types.h" + #include "utility/wl_definitions.h" + #include "utility/wl_types.h" #include "socket.h" #include "string.h" } @@ -11,45 +12,28 @@ extern "C" { #include "Server.h" #include "server_drv.h" -uint16_t Client::_srcport = 0; +uint16_t Client::_srcport = 1024; -Client::Client(uint8_t sock) { - _sock = sock; +Client::Client(uint8_t sock) : _sock(sock) { } -Client::Client(uint8_t *ip, uint16_t port) { - _ip = ip; - _port = port; - _sock = 255; -} - -uint8_t Client::getFirstSock() -{ - for (int i = 0; i < MAX_SOCK_NUM; i++) { - if (WiFiClass::_state[i] < 0) - { - return i; - } - } - return SOCK_NOT_AVAIL; +Client::Client(IPAddress& ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) { } uint8_t Client::connect() { - _sock = getFirstSock(); - -// _srcport++; -// if (_srcport + 1024 == 0) _srcport = 0; + _sock = getFirstSocket(); +//TODO implementation _socket = socket(TCP_SOCKET); if (_socket<0) { - return 0; + return 0; }else{ WiFiClass::_state[_sock] = _socket; } - - if (!::connect(_socket, _ip, _port)) { - return 0; - } +// +// if (!::connect(_socket, _ip, _port)) { +// return 0; +// } return 1; } @@ -97,8 +81,8 @@ int Client::read() { } -int Client::readBuf(uint8_t* buf, uint16_t* len) { - if (!ServerDrv::getDataBuf(_sock, buf, len)) +int Client::read(uint8_t* buf, size_t size) { + if (!ServerDrv::getDataBuf(_sock, buf, &size)) return -1; return 0; } @@ -146,19 +130,19 @@ uint8_t Client::status() { } } -// the next three functions are a hack so we can compare the client returned -// by Server::available() to null, or use it as the condition in an -// if-statement. this lets us stay compatible with the Processing network -// library. - -uint8_t Client::operator==(int p) { - return _sock == 255; -} - -uint8_t Client::operator!=(int p) { - return _sock != 255; -} - Client::operator bool() { return _sock != 255; } + +// Private Methods +uint8_t Client::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/Client.h b/WiFi/Client.h index 621229155..277476230 100755 --- a/WiFi/Client.h +++ b/WiFi/Client.h @@ -1,35 +1,37 @@ #ifndef Client_h #define Client_h +#include "IPAddress.h" #include "Print.h" class Client : public Print { -private: - static uint16_t _srcport; - uint8_t _sock; //not used - uint8_t *_ip; - uint16_t _port; - uint16_t _socket; - public: - Client(uint8_t); - Client(uint8_t *, uint16_t); + Client(uint8_t sock); + Client(IPAddress& ip, uint16_t port); + uint8_t status(); uint8_t connect(); virtual void write(uint8_t); virtual void write(const char *str); virtual void write(const uint8_t *buf, size_t size); - int available(); - int read(); - int readBuf(uint8_t* buf, uint16_t* len); + virtual int available(); + virtual int read(); + virtual int read(uint8_t* buf, size_t size); void flush(); void stop(); uint8_t connected(); - uint8_t operator==(int); - uint8_t operator!=(int); operator bool(); - uint8_t getFirstSock(); + friend class Server; + +private: + static uint16_t _srcport; + uint8_t _sock; //not used + IPAddress _ip; + uint16_t _port; + uint16_t _socket; + + uint8_t getFirstSocket(); }; #endif diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index e9b58137e..ba7cd2697 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -1,6 +1,4 @@ #include -#include "HardwareSerial.h" -#include "wifi_spi.h" #include "Server.h" #include "Client.h" #include "WiFi.h" diff --git a/WiFi/Server.h b/WiFi/Server.h index fdab7945b..e48c6a55b 100755 --- a/WiFi/Server.h +++ b/WiFi/Server.h @@ -2,7 +2,7 @@ #define Server_h extern "C" { - #include "utility/wl_types.h" + #include "utility/wl_definitions.h" } #include "Print.h" diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index e68a98d42..59bbad9b8 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -1,42 +1,32 @@ -#include "WProgram.h" -#include "WiFi.h" -#include -#include "HardwareSerial.h" #include "wifi_drv.h" +#include "WiFi.h" +#include "wiring.h" +extern "C" { + #include "utility/wl_definitions.h" + #include "utility/wl_types.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 }; +int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; +uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; -char WiFiClass::ssid[WL_SSID_MAX_LENGTH] = { 0 }; -uint8_t WiFiClass::ssid_len; -char WiFiClass::key[13] = { 0 }; -uint8_t WiFiClass::key_len; -char WiFiClass::passph[63] = { 0 }; -uint8_t WiFiClass::passph_len; -wl_status_t WiFiClass::status; +char WiFiClass::_ssid[] = { 0 }; +char WiFiClass::_key[] = { 0 }; +char WiFiClass::_passph[] = { 0 }; +wl_status_t WiFiClass::_status; -void readEEdata(int addr, uint8_t* data, uint8_t len) -{ - for (int i = 0; i < len; ++i) - { - data[i]= EEPROM.read(addr); - } -} WiFiClass::WiFiClass() { + init(); } void WiFiClass::init() { - Serial.begin(9600); - Serial.println("WiFi initializing..."); - WiFiDrv::wifi_drv_init(); + WiFiDrv::wifiDriverInit(); } - uint8_t WiFiClass::getSocket() { for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i) @@ -49,136 +39,118 @@ uint8_t WiFiClass::getSocket() return NO_SOCKET_AVAIL; } -void WiFiClass::begin() +int WiFiClass::begin() { - init(); - Serial.println("WiFi Starting..."); - ssid_len = EEPROM.read(EE_WIFI_SSID_LEN); - if ((ssid_len == 0)||(ssid_len > WL_SSID_MAX_LENGTH)) - { - Serial.println("No SSID in EEPROM"); - - status = WL_NO_SSID_AVAIL; - return; - } - readEEdata(EE_WIFI_SSID_DATA, (uint8_t*)&ssid[0], ssid_len); - - Serial.print("SSID: "); - Serial.print(ssid_len, 10); - Serial.print(" - "); - Serial.print(ssid[0]); - Serial.println(""); - key_len = EEPROM.read(EE_WIFI_KEY_LEN); - if (key_len == 0) - { - Serial.println("No PASSPHRASE in EEPROM"); - - passph_len = EEPROM.read(EE_WIFI_PASSPH_LEN); - if (passph_len == 0) - { - begin(ssid, ssid_len); - }else{ - readEEdata(EE_WIFI_PASSPH_DATA, (uint8_t*)&passph[0], passph_len); - beginp(ssid,ssid_len,&passph[0],passph_len); - } - }else{ - Serial.println("No KEY in EEPROM"); - - readEEdata(EE_WIFI_KEY_DATA, (uint8_t*)&key[0], key_len); - begink(ssid,ssid_len, 0, &key[0],key_len); - } } -void WiFiClass::begin(char* ssid, uint8_t ssid_len) +int WiFiClass::begin(char* ssid) { - wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_net(ssid, ssid_len); - if (result == WL_SUCCESS) + if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE) { - status = WL_CONNECTED; - Serial.println("WiFi Connected!"); - }else{ - status = WL_CONNECT_FAILED; - Serial.println("WiFi Connection failed!"); + delay(WL_DELAY_START_CONNECTION); + return WiFiDrv::getConnectionStatus(); + }else + { + return WL_CONNECT_FAILED; } } -void WiFiClass::begink(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t key_len) +int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) { // set encryption key - wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_key(ssid, ssid_len, key_idx, key, key_len); - if (result == WL_SUCCESS) - { - //begin(ssid, ssid_len); - }else{ - // Error setting passphrase - } - + if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) + { + delay(WL_DELAY_START_CONNECTION); + return WiFiDrv::getConnectionStatus(); + }else{ + return WL_CONNECT_FAILED; + } } -void WiFiClass::beginp(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len) +int WiFiClass::begin(char* ssid, const char *passphrase) { // set passphrase - wl_error_code_t result = (wl_error_code_t)WiFiDrv::wifi_set_passpharse(ssid, ssid_len, passphrase, len); - if (result == WL_SUCCESS) + if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) { - //begin(ssid, ssid_len); - }else{ - // Error setting passphrase + delay(WL_DELAY_START_CONNECTION); + return WiFiDrv::getConnectionStatus(); + }else{ + return WL_CONNECT_FAILED; } } -wl_status_t WiFiClass::get_status() -{ - return status; -} - -uint8_t WiFiClass::get_result(uint8_t dummy) -{ - uint8_t result = WiFiDrv::wifi_get_result(dummy); - return result; -} - - -void WiFiClass::getIpAddr(uint8_t *ip, uint8_t *mask, uint8_t *gwip) -{ - WiFiDrv::getIpAddr(ip,mask,gwip); -} - - -uint8_t WiFiClass::getMacAddr(uint8_t* mac) -{ - return WiFiDrv::wl_get_mac_addr(mac); -} - -void WiFiClass::getSSIDList(char** ssid_list, uint8_t* ssidListNum) -{ - WiFiDrv::getSSIDList(ssid_list, ssidListNum); -} - -void WiFiClass::getCurrSSID(char* ssid) -{ - WiFiDrv::getCurrSSID(ssid); -} - -void WiFiClass::getCurrBSSID(uint8_t* bssid) -{ - WiFiDrv::getCurrBSSID(bssid); -} - -void WiFiClass::getCurrRSSI(int32_t* rssi) -{ - WiFiDrv::getCurrRSSI(rssi); -} - -void WiFiClass::getCurrEncType(uint8_t* enc_type) -{ - WiFiDrv::getCurrEncType(enc_type); -} - -uint8_t WiFiClass::disconnect() +int WiFiClass::disconnect() { return WiFiDrv::disconnect(); } +uint8_t* WiFiClass::macAddress(uint8_t* mac) +{ + mac = WiFiDrv::getMacAddress(); + return mac; +} + +IPAddress WiFiClass::localIp() +{ + IPAddress ret; + WiFiDrv::getIpAddress(ret.raw_address()); + return ret; +} + +IPAddress WiFiClass::subnetMask() +{ + IPAddress ret; + WiFiDrv::getSubnetMask(ret.raw_address()); + return ret; +} + +IPAddress WiFiClass::gatewayIP() +{ + IPAddress ret; + WiFiDrv::getGatewayIP(ret.raw_address()); + return ret; +} + +char* WiFiClass::SSID() +{ + return WiFiDrv::getCurrentSSID(); +} + +uint8_t* WiFiClass::BSSID(uint8_t* bssid) +{ + bssid = WiFiDrv::getCurrentBSSID(); + return bssid; +} + +int32_t WiFiClass::RSSI() +{ + return WiFiDrv::getCurrentRSSI(); +} + +uint8_t WiFiClass::encryptionType() +{ + return WiFiDrv::getCurrentEncryptionType(); +} + + +uint8_t WiFiClass::scanNetworks() +{ + return WiFiDrv::scanNetworks(); +} + +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); +} WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 5cea90e4e..3a97ffcec 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -4,9 +4,7 @@ #include extern "C" { - #include "utility/wl_types.h" - #include "utility/wifi_spi.h" - #include "utility/debug.h" // only for test, not released + #include "utility/wl_definitions.h" } #include "IPAddress.h" @@ -18,39 +16,39 @@ class WiFiClass private: // this data are stored in EEPROM and loaded at begin // The next connect overwrite these values - static char _ssid[WL_SSID_MAX_LENGTH]; - static uint8_t _ssid_len; - static char _key[WL_WEP_KEY_MAX_LENGTH]; - static uint8_t _key_len; - static char _passph[WL_WPA_KEY_MAX_LENGTH]; - static uint8_t _passph_len; + static char _ssid[WL_SSID_MAX_LENGTH]; + static char _key[WL_WEP_KEY_MAX_LENGTH]; + static char _passph[WL_WPA_KEY_MAX_LENGTH]; static wl_status_t _status; void init(); public: - static int16_t _state[MAX_SOCK_NUM]; + static int16_t _state[MAX_SOCK_NUM]; static uint16_t _server_port[MAX_SOCK_NUM]; WiFiClass(); + // Get thefirst socket available + static uint8_t getSocket(); + // Start Wifi connection with latest settings int begin(); // Start Wifi connection with no encryption - int begin(char* ssid, uint8_t ssid_len); + int begin(char* ssid); // Start Wifi connection with WEP encryption - int begin(char* ssid, uint8_t ssid_len, uint8_t key_idx, const char* key, const uint8_t key_len); + 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 - int begin(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + int begin(char* ssid, const char *passphrase); // Disconnect from the network int disconnect(void); //Get the interface MAC address. - uint8_t* macAddress(); + uint8_t* macAddress(uint8_t* mac); //Get the DHCP information related to IP IPAddress localIp(); @@ -65,19 +63,25 @@ public: char* SSID(); // Return the current BSSID associated with the network - uint8_t* BSSID(); - - // Start scan SSIDs available and return the number of SSID discovered - uint8_t scanSSID(); - - // Return SSID item available - char* SSIDListItem(uint8_t ssidListItem); - - // Return the current Encryption Type associated with the network - uint8_t encType(uint8_t ssidListItem); + uint8_t* BSSID(uint8_t* bssid); // Return the current RSSI /Received Signal Strength in dBm) associated with the network - int32_t RSSI(uint8_t ssidListItem); + int32_t RSSI(); + + // Return the Encryption Type associated with the network + uint8_t encryptionType(); + + // Start scan WiFi networks available and return the discovered number + uint8_t scanNetworks(); + + // Return SSID item associated with the network identified with networkItem + char* SSID(uint8_t networkItem); + + // Return the Encryption Type associated with the network identified with networkItem + uint8_t encryptionType(uint8_t networkItem); + + // Return the current RSSI /Received Signal Strength in dBm) associated with the network identified with networkItem + int32_t RSSI(uint8_t networkItem); friend class Client; friend class Server; diff --git a/WiFi/examples/wifi_Server_example/wifi_Server_example.pde b/WiFi/examples/wifi_Server_example/wifi_Server_example.pde new file mode 100644 index 000000000..2d8166e6c --- /dev/null +++ b/WiFi/examples/wifi_Server_example/wifi_Server_example.pde @@ -0,0 +1,157 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + //WiFi.getCurrSSID(&ssid[0]); + //Serial.print("SSID:"); + //Serial.println(ssid); + byte bssid[6]; + WiFi.BSSID(bssid); + //delay(200); + Serial.print("BSSID:"); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); + + char ssid[20][32] = { {0} }; + byte numSsid = WiFi.scanNetworks(); + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + //Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } +} + diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/wifi_example/wifi_example.pde index a96c5fc32..c86df452e 100755 --- a/WiFi/examples/wifi_example/wifi_example.pde +++ b/WiFi/examples/wifi_example/wifi_example.pde @@ -1,26 +1,81 @@ /* WiFi example - - A simple connection with WiFi AP with Wireless Security - information loaded in EEPROM - + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + created 13 July 2010 by Domenico La Fauci */ #include -#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; + void setup() -{ - Serial.begin(9600); - WiFi.begin(); - if (WiFi.get_status() == WL_NO_SSID_AVAIL) +{ + Serial.begin(9600); + Serial.println("Setup WiFi..."); + char ssid[] = "Cariddi"; + Serial.println(ssid); + int status = WiFi.begin(ssid); + if ( status != WL_CONNECTED) { - // SSID not present in EEPROM - char ssid[] = "Cariddi"; - WiFi.begin(ssid, strlen(ssid)); + // Using WPA + Serial.println("Trying with Passphrase..."); + const char *pass = "12345678"; + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) + { + // using WEP + Serial.println("Trying with Key..."); + uint8_t key_idx = 0; + const char *key = "12345678"; + status = WiFi.begin(ssid,key_idx, key); + if ( status != WL_CONNECTED) + { + Serial.println("Wifi Connection failed!"); + return; + } + } } + Serial.println("Wifi Connected!"); + + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); } void loop() diff --git a/WiFi/examples/wifi_example2/wifi_example2.pde b/WiFi/examples/wifi_example2/wifi_example2.pde deleted file mode 100755 index d16908300..000000000 --- a/WiFi/examples/wifi_example2/wifi_example2.pde +++ /dev/null @@ -1,107 +0,0 @@ -/* - WiFi example - - A simple connection with WiFi AP with Wireless Security - information loaded in EEPROM, if not available - try to access with WPA or WEP security keys - - created 13 July 2010 - by Domenico La Fauci - */ - -#include -#include -#include - -// network configuration. gateway and subnet are optional. -byte mac[6] = { 0 }; -byte ip[4] = { 0 }; -byte gateway[4] = { 0 }; -byte subnet[4] = { 0 }; - - -void setup() -{ - Serial.begin(9600); - Serial.println("Setup WiFi..."); - WiFi.begin(); - if (WiFi.get_status() == WL_NO_SSID_AVAIL) - { - //uint8_t result = WiFi.get_result(); - //Serial.println(result,16); - - // SSID not present in EEPROM - char ssid[] = "Cariddi"; - Serial.println(ssid); - delay(2000); - - // Using WPA - WiFi.begin(ssid, strlen(ssid)); - delay(10000); - if (WiFi.get_result() == WL_CONNECT_FAILED) - { - Serial.println("Trying with Passphrase..."); - uint8_t key_idx = 0; - const char *pass = "12345678"; - uint8_t pass_len = strlen(pass); - WiFi.beginp(ssid, strlen(ssid), pass, pass_len); - } - // wait to trying connection... - delay(5000); - - // using WEP - if (WiFi.get_result() == WL_CONNECT_FAILED) - { - Serial.println("Trying with Key..."); - uint8_t key_idx = 0; - const char *key = "12345678"; - uint8_t key_len = strlen(key); - WiFi.begink(ssid, strlen(ssid),key_idx, key, key_len); - } - // wait to trying connection... - delay(5000); - - { - WiFi.getIpAddr(ip,subnet,gateway); - Serial.print("IP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.getMacAddr(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); - - } - } -} - -void loop() -{ - static uint8_t count = 0; - while (WiFi.get_status() != WL_CONNECTED) - { - uint8_t result = WiFi.get_result(count++); - Serial.println(result); - delay(3000); - } -} - diff --git a/WiFi/keywords.txt b/WiFi/keywords.txt index ce613bdbb..ef4148621 100755 --- a/WiFi/keywords.txt +++ b/WiFi/keywords.txt @@ -23,19 +23,16 @@ flush KEYWORD2 stop KEYWORD2 connected KEYWORD2 begin KEYWORD2 -begink KEYWORD2 -beginp KEYWORD2 disconnect KEYWORD2 -getMacAddr KEYWORD2 -getIpAddr KEYWORD2 -getSSIDList KEYWORD2 -getCurrSSID KEYWORD2 -getCurrBSSID KEYWORD2 -getCurrRSSI KEYWORD2 -getCurrEncType KEYWORD2 -wl_get_network_list KEYWORD2 -get_result KEYWORD2 -get_status KEYWORD2 +macAddress KEYWORD2 +localIp KEYWORD2 +subnetMask KEYWORD2 +gatewayIP KEYWORD2 +SSID KEYWORD2 +BSSID KEYWORD2 +RSSI KEYWORD2 +encryptionType KEYWORD2 +getResult KEYWORD2 getSocket KEYWORD2 diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h new file mode 100644 index 000000000..1f1296686 --- /dev/null +++ b/WiFi/utility/debug.h @@ -0,0 +1,67 @@ +//*********************************************/ +// +// File: debug.h +// +// Author: Domenico La Fauci +// +//********************************************/ + + +#ifndef Debug_H +#define Debug_H + +#include +#include + +#define INFO_0 1 +#define INFO_1 2 +#define INFO_2 4 +#define INFO_3 8 +#define INFO_4 16 +#define INFO_5 32 +#define INFO_D (1<<0xD) // Debug +#define INFO_E (1<<0xE) // Error +#define INFO_F (1<<0xF) // Warning + +#define PRINT_FILE_LINE() do { \ + Serial.print("[");Serial.print(__FILE__); \ + Serial.print("::");Serial.print(__LINE__);Serial.print("]");\ +}while (0); + +#ifdef _DEBUG_ + +#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-W-");\ + Serial.println(x); \ +}while (0); + + +#define INFO2(x,y) do { PRINT_FILE_LINE() Serial.print("-I-");\ + Serial.print(x);Serial.print(",");Serial.println(y); \ +}while (0); + + +#else +#define INFO1(x) do {} while(0); +#define INFO(format, args...) do {} while(0); +#endif + +#define WARN(args) do { PRINT_FILE_LINE() \ + Serial.print("-W-"); Serial.println(args); \ +}while (0); + +#define DBG_PIN2 3 +#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() + +#endif diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp new file mode 100644 index 000000000..d88703b88 --- /dev/null +++ b/WiFi/utility/server_drv.cpp @@ -0,0 +1,187 @@ +#include "server_drv.h" + +#include "WProgram.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) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendParam(port); + SpiDrv::sendParam(&sock, 1, LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); +} + + +uint8_t ServerDrv::getState(uint8_t sock) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + + delayMicroseconds(DELAY_POST_CMD); + return _data; +} + + +uint8_t ServerDrv::availData(uint8_t sock) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + + delayMicroseconds(DELAY_POST_CMD); + + if (_dataLen!=0) + { + return (_data == 1); + } + return false; +} + +bool ServerDrv::getData(uint8_t sock, uint8_t *data) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(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) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + if (!SpiDrv::waitResponse(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) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2); + SpiDrv::sendParam(&sock, sizeof(sock)); + SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(SEND_DATA_TCP_CMD, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + if (_dataLen!=0) + { + return (_data == 1); + } + return false; +} + + +uint8_t ServerDrv::isDataSent(uint8_t sock) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse isDataSent"); + } + SpiDrv::spiSlaveDeselect(); + + delayMicroseconds(DELAY_POST_CMD); + return _data; +} + +ServerDrv serverDrv; diff --git a/WiFi/utility/server_drv.h b/WiFi/utility/server_drv.h new file mode 100644 index 000000000..51dcadfa3 --- /dev/null +++ b/WiFi/utility/server_drv.h @@ -0,0 +1,28 @@ +#ifndef Server_Drv_h +#define Server_Drv_h + +#include +#include "wifi_spi.h" + +class ServerDrv +{ +public: + // Start server TCP on port specified + static void StartServer(uint16_t port, uint8_t sock); + + static uint8_t getState(uint8_t sock); + + static bool getData(uint8_t sock, uint8_t *data); + + 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 isDataSent(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..02d0f4c5f --- /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) {} // Opens a socket(TCP or UDP or IP_RAW mode) +void close(SOCKET s) {} // Close socket +uint8 connect(SOCKET s, uint8 * addr, uint16 port) {} // Establish TCP connection (Active connection) +void disconnect(SOCKET s) {} // disconnect the connection +uint8 listen(SOCKET s) {} // Establish TCP connection (Passive connection) +uint16 send(SOCKET s, const uint8 * buf, uint16 len) {} // Send data (TCP) +uint16 recv(SOCKET s, uint8 * buf, uint16 len) {} // Receive data (TCP) +uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port) {} // Send data (UDP/IP RAW) +uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port) {} // Receive data (UDP/IP RAW) + +uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len) {} 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..86af01a0e --- /dev/null +++ b/WiFi/utility/spi_drv.cpp @@ -0,0 +1,434 @@ + +#include "WProgram.h" +#include "spi_drv.h" + +extern "C" { +#include "debug.h" +} + +#define DATAOUT 11//MOSI +#define DATAIN 12//MISO +#define SPICLOCK 13//sck +#define SLAVESELECT 10//ss + + +void SpiDrv::spiSetup() +{ + int clr = 0; + pinMode(DATAOUT, OUTPUT); + pinMode(DATAIN, INPUT); + pinMode(SPICLOCK,OUTPUT); + pinMode(SLAVESELECT,OUTPUT); + digitalWrite(SLAVESELECT,HIGH); //disable device + // SPCR = 01010000 + //interrupt disabled,spi enabled,msb 1st,master,clk low when idle, + //sample on leading edge of clk,system clock/4 rate (fastest) + SPCR = (1< 0) && (_readChar != waitChar)); + + if ((_readChar != waitChar)&&(timeout >=0)) + { + INFO1("*C*"); + Serial.println(_readChar,16); + }else if (timeout == 0) + { + INFO1("*T*"); + } + + return (_readChar == waitChar); +} + +int SpiDrv::waitSpiChar(char waitChar, char* readChar) +{ + int timeout = TIMEOUT_CHAR; + do{ + *readChar = spiTransfer(DUMMY_DATA); //get data byte + if (*readChar == WAIT_CMD) + { + INFO1("WAIT"); + delayMicroseconds(WAIT_CHAR_DELAY); + } + }while((timeout-- > 0) && (*readChar != waitChar)); + + return (*readChar == waitChar); +} + + +int SpiDrv::readAndCheckChar(char checkChar, char* readChar) +{ + *readChar = spiTransfer(DUMMY_DATA); //get data byte + + return (*readChar == checkChar); +} + +char SpiDrv::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) \ + if (!WAIT_START_CMD(_data)) \ + { \ + TOGGLE_TRIGGER() \ + WARN("Error waiting START_CMD"); \ + Serial.println(cmd, 16); \ + return 0; \ + }else \ + +#define CHECK_DATA(check, x) \ + if (!readAndCheckChar(check, &x)) \ + { \ + WARN("Reply error"); \ + return 0; \ + }else \ + +int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_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); + { + 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::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::waitResponse(uint8_t cmd, 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){}; + + 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::waitResponse(uint8_t cmd, uint8_t* param, uint8_t* param_len) +{ + 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) + { + 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::waitResponse(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>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) +{ + int 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); + + // 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..f214df637 --- /dev/null +++ b/WiFi/utility/spi_drv.h @@ -0,0 +1,70 @@ +#ifndef SPI_Drv_h +#define SPI_Drv_h + +#include +#include "wifi_spi.h" + +#define WAIT_CHAR_DELAY 100 +#define TIMEOUT_CHAR_DELAY 10 +#define SPI_TX_DELAY 2 + +#define NO_LAST_PARAM 0 +#define LAST_PARAM 1 + +#define DUMMY_DATA 0xFF + + +class SpiDrv +{ +public: + + static void spiSetup(); + + static void spiDriverInit(); + + static void spiSlaveSelect(); + + static void spiSlaveDeselect(); + + static char spiTransfer(volatile char data); + + 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 waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams); + + static int waitResponse(uint8_t cmd, uint8_t numParam, tParam* params); + + static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len); + + static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len); + + static int waitResponse(uint8_t cmd, uint8_t* param, uint8_t* param_len); + + static int waitResponse(uint8_t cmd, uint8_t* param, uint16_t* param_len); + + 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..40fe75017 --- /dev/null +++ b/WiFi/utility/wifi_drv.cpp @@ -0,0 +1,351 @@ +#include +#include +#include + +#include "WProgram.h" +#include "spi_drv.h" +#include "wifi_drv.h" + +#define _DEBUG_ + +extern "C" { +#include "wifi_spi.h" +#include "wl_types.h" +#include "debug.h" +} + +char WiFiDrv::_networkSsid[] = {0}; +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}}; + + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + SpiDrv::waitResponse(GET_IPADDR_CMD, PARAM_NUMS_3, params); + + SpiDrv::spiSlaveDeselect(); +} + +// Public Methods + + +void WiFiDrv::wifiDriverInit() +{ + SpiDrv::spiDriverInit(); +} + +// If ssid == NULL execute a wifi scan, otherwise try to connect to the network specified +uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1); + SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + 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) +{ + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + 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) +{ + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponse(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + +uint8_t WiFiDrv::disconnect() +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponse(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return result; +} + +uint8_t WiFiDrv::getConnectionStatus() +{ + SpiDrv::spiSlaveSelect(); + + // Send Command + SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + SpiDrv::waitResponse(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _data; +} + +uint8_t* WiFiDrv::getMacAddress() +{ + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponse(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _mac; +} + +void WiFiDrv::getIpAddress(uint8_t *ip) +{ + getNetworkData(_localIp, _subnetMask, _gatewayIp); + memcpy(ip, _localIp, WL_IPV4_LENGTH); +} + + void WiFiDrv::getSubnetMask(uint8_t *ip) + { + getNetworkData(_localIp, _subnetMask, _gatewayIp); + memcpy(ip, _subnetMask, WL_IPV4_LENGTH); + } + + void WiFiDrv::getGatewayIP(uint8_t *ip) + { + getNetworkData(_localIp, _subnetMask, _gatewayIp); + memcpy(ip, _gatewayIp, WL_IPV4_LENGTH); + } + +char* WiFiDrv::getCurrentSSID() +{ + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponse(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _ssid; +} + +uint8_t* WiFiDrv::getCurrentBSSID() +{ + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _dataLen = 0; + uint8_t result = SpiDrv::waitResponse(GET_CURR_BSSID_CMD, PARAM_NUMS_1, (uint8_t*)_bssid, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _bssid; +} + +int32_t WiFiDrv::getCurrentRSSI() +{ + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t _dataLen = 0; + int32_t rssi = 0; + uint8_t result = SpiDrv::waitResponse(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)rssi, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return rssi; +} + +uint8_t WiFiDrv::getCurrentEncryptionType() +{ + SpiDrv::spiSlaveSelect(); + // 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 + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint8_t dataLen = 0; + uint8_t encType = 0; + uint8_t result = SpiDrv::waitResponse(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)encType, &dataLen); + + SpiDrv::spiSlaveDeselect(); + + return encType; +} + +uint8_t WiFiDrv::scanNetworks() +{ + SpiDrv::spiSlaveSelect(); + // Send Command + SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_1); + + uint8_t _dummy = DUMMY_DATA; + SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); + + //Wait the reply elaboration + delayMicroseconds(DELAY_RX_TX); + + // Wait for reply + uint16_t _dataLen = 0; + + tParam params[WL_NETWORKS_LIST_MAXNUM]; + uint8_t ssidListNum = 0; + uint8_t result = SpiDrv::waitResponse(SCAN_NETWORKS, params, &ssidListNum, WL_NETWORKS_LIST_MAXNUM); + + SpiDrv::spiSlaveDeselect(); + + return ssidListNum; +} + +char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + + //TODO make an RPC call to get the ssid associated with networkItem + return _networkSsid; +} + +uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + uint8_t networkEncType = 0; + + //TODO make an RPC call to get the encryption type associated with networkItem + return networkEncType; +} + +int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) +{ + if (networkItem >= WL_NETWORKS_LIST_MAXNUM) + return NULL; + int32_t networkRssi = 0; + + //TODO make an RPC call to get the rssi associated with networkItem + return networkRssi; +} + +WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h new file mode 100644 index 000000000..45f895ec4 --- /dev/null +++ b/WiFi/utility/wifi_drv.h @@ -0,0 +1,68 @@ +#ifndef WiFi_Drv_h +#define WiFi_Drv_h + +#include +#include "wifi_spi.h" + +#define KEY_IDX_LEN 1 +#define WL_DELAY_START_CONNECTION 5000 + +class WiFiDrv +{ +private: + // settings of requested network + static char _networkSsid[WL_SSID_MAX_LENGTH]; + + // 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]; + + static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip); + +public: + + static void wifiDriverInit(); + + static uint8_t wifiSetNetwork(char* ssid, uint8_t ssid_len); + + static uint8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len); + + static uint8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len); + + static uint8_t disconnect(); + + static uint8_t getConnectionStatus(); + + static uint8_t* getMacAddress(); + + static void getIpAddress(uint8_t *ip); + + static void getSubnetMask(uint8_t *ip); + + static void getGatewayIP(uint8_t *ip); + + static char* getCurrentSSID(); + + static uint8_t* getCurrentBSSID(); + + static int32_t getCurrentRSSI(); + + static uint8_t getCurrentEncryptionType(); + + static uint8_t scanNetworks(); + + static char* getSSIDNetoworks(uint8_t networkItem); + + static int32_t getRSSINetoworks(uint8_t networkItem); + + static uint8_t getEncTypeNetowrks(uint8_t networkItem); + +}; + +extern WiFiDrv wiFiDrv; + +#endif diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h new file mode 100644 index 000000000..50b92af00 --- /dev/null +++ b/WiFi/utility/wifi_spi.h @@ -0,0 +1,101 @@ +#ifndef WiFi_Spi_h +#define WiFi_Spi_h + +#include "wl_definitions.h" + +#define CMD_FLAG 0 +#define REPLY_FLAG 1<<7 + +#define WIFI_SPI_ACK 1 +#define WIFI_SPI_ERR 0xFF + +#define TIMEOUT_CHAR 1000 +#define DELAY_RX_TX 200 //usec +#define DELAY_POST_CMD 0 //usec + +//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */ +#define NO_SOCKET_AVAIL 255 + +#define START_CMD 0xE0 +#define WAIT_CMD 0xE1 +#define END_CMD 0xEE + +enum { + SET_NET_CMD = 0x10, + SET_PASSPHRASE_CMD = 0x11, + SET_KEY_CMD = 0x12, + + 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, + + + DISCONNECT_CMD = 0x30, + + START_SERVER_TCP_CMD = 0x40, + GET_STATE_TCP_CMD = 0x41, + GET_DATA_TCP_CMD = 0x42, + AVAIL_DATA_TCP_CMD = 0x43, + SEND_DATA_TCP_CMD = 0x44, + DATA_SENT_TCP_CMD = 0x45, + GET_DATABUF_TCP_CMD = 0x46, +}; + + +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__)) +{ + 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 totLen; + unsigned char nParam; +}tSpiHdr; + +#endif diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h new file mode 100644 index 000000000..a80fb82c5 --- /dev/null +++ b/WiFi/utility/wl_definitions.h @@ -0,0 +1,37 @@ +/* + * 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 5 +// Maxmium number of socket +#define MAX_SOCK_NUM 4 + +typedef enum { + WL_IDLE_STATUS, + WL_NO_SSID_AVAIL, + WL_SCAN_COMPLETED, + WL_CONNECTED, + WL_CONNECT_FAILED, + WL_CONNECTION_LOST, + WL_DISCONNECTED +} wl_status_t; + + +#endif /* WL_DEFINITIONS_H_ */ diff --git a/WiFi/utility/wl_types.h b/WiFi/utility/wl_types.h new file mode 100644 index 000000000..7f6e8f22c --- /dev/null +++ b/WiFi/utility/wl_types.h @@ -0,0 +1,42 @@ +/* + * 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 +}; + + +/* 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_TYPES_H_ From d640f9c77ad6d31b840932a68be4fe9e2dce40c3 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Tue, 5 Apr 2011 23:24:17 +0200 Subject: [PATCH 05/98] Bugfix for Open and WEP nets --- WiFi/WiFi.cpp | 58 ++++-- WiFi/WiFi.h | 3 + .../wifi_Server_example.pde | 4 +- .../wifi_WEP_example/wifi_WEP_example.pde | 172 +++++++++++++++++ .../wifi_WPA_example/wifi_WPA_example.pde | 174 ++++++++++++++++++ WiFi/examples/wifi_example/wifi_example.pde | 155 ++++++++++++---- WiFi/utility/debug.h | 8 +- WiFi/utility/server_drv.cpp | 14 +- WiFi/utility/spi_drv.cpp | 114 ++++++++++-- WiFi/utility/spi_drv.h | 11 +- WiFi/utility/wifi_drv.cpp | 78 ++++---- WiFi/utility/wifi_drv.h | 3 +- WiFi/utility/wifi_spi.h | 2 +- WiFi/utility/wl_definitions.h | 2 +- 14 files changed, 684 insertions(+), 114 deletions(-) create mode 100644 WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde create mode 100644 WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 59bbad9b8..cb433d2bc 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -2,9 +2,12 @@ #include "WiFi.h" #include "wiring.h" +#define _DEBUG_ + extern "C" { #include "utility/wl_definitions.h" #include "utility/wl_types.h" + #include "debug.h" } // XXX: don't make assumptions about the value of MAX_SOCK_NUM. @@ -19,7 +22,6 @@ wl_status_t WiFiClass::_status; WiFiClass::WiFiClass() { - init(); } void WiFiClass::init() @@ -41,42 +43,66 @@ uint8_t WiFiClass::getSocket() int WiFiClass::begin() { + init(); } int WiFiClass::begin(char* ssid) { + uint8_t status = WL_IDLE_STATUS; + init(); + if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE) { - delay(WL_DELAY_START_CONNECTION); - return WiFiDrv::getConnectionStatus(); + + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); }else { - return WL_CONNECT_FAILED; + status = WL_CONNECT_FAILED; } + return status; } int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) { + uint8_t status = WL_IDLE_STATUS; + init(); // set encryption key if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) { - delay(WL_DELAY_START_CONNECTION); - return WiFiDrv::getConnectionStatus(); + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); }else{ - return WL_CONNECT_FAILED; + status = WL_CONNECT_FAILED; } + return status; } int WiFiClass::begin(char* ssid, const char *passphrase) { + uint8_t status = WL_IDLE_STATUS; + init(); // set passphrase if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) { - delay(WL_DELAY_START_CONNECTION); - return WiFiDrv::getConnectionStatus(); + do + { + delay(WL_DELAY_START_CONNECTION); + status = WiFiDrv::getConnectionStatus(); + } + while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); }else{ - return WL_CONNECT_FAILED; + status = WL_CONNECT_FAILED; } + return status; } int WiFiClass::disconnect() @@ -86,7 +112,8 @@ int WiFiClass::disconnect() uint8_t* WiFiClass::macAddress(uint8_t* mac) { - mac = WiFiDrv::getMacAddress(); + uint8_t* _mac = WiFiDrv::getMacAddress(); + memcpy(mac, _mac, WL_MAC_ADDR_LENGTH); return mac; } @@ -118,7 +145,8 @@ char* WiFiClass::SSID() uint8_t* WiFiClass::BSSID(uint8_t* bssid) { - bssid = WiFiDrv::getCurrentBSSID(); + uint8_t* _bssid = WiFiDrv::getCurrentBSSID(); + memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH); return bssid; } @@ -153,4 +181,10 @@ uint8_t WiFiClass::encryptionType(uint8_t networkItem) return WiFiDrv::getEncTypeNetowrks(networkItem); } +uint8_t WiFiClass::status() +{ + return WiFiDrv::getConnectionStatus(); +} + + WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 3a97ffcec..d97209b63 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -83,6 +83,9 @@ public: // Return the current RSSI /Received Signal Strength in dBm) associated with the network identified with networkItem int32_t RSSI(uint8_t networkItem); + // Return Connection status + uint8_t status(); + friend class Client; friend class Server; }; diff --git a/WiFi/examples/wifi_Server_example/wifi_Server_example.pde b/WiFi/examples/wifi_Server_example/wifi_Server_example.pde index 2d8166e6c..2a88a5617 100644 --- a/WiFi/examples/wifi_Server_example/wifi_Server_example.pde +++ b/WiFi/examples/wifi_Server_example/wifi_Server_example.pde @@ -94,7 +94,7 @@ void setup() { Serial.begin(9600); Serial.println("Setup WiFi..."); - char ssid[] = "Cariddi"; + strcpy(ssid, "Vodafone-10289870"); Serial.println(ssid); int status = WiFi.begin(ssid); if ( status != WL_CONNECTED) @@ -131,6 +131,8 @@ void setup() void loop() { static uint8_t count = 0; + Serial.println("Retry connect..."); + status = WiFi.begin(ssid); if (status == WL_CONNECTED) { byte status = 0; diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde new file mode 100644 index 000000000..557c5039c --- /dev/null +++ b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde @@ -0,0 +1,172 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); +} + +void scanNetworks() +{ + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + //Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } + */ +} + + diff --git a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde new file mode 100644 index 000000000..498cf1191 --- /dev/null +++ b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde @@ -0,0 +1,174 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); +} + +void scanNetworks() +{ + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + //Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } + */ +} + + diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/wifi_example/wifi_example.pde index c86df452e..6606c2b1f 100755 --- a/WiFi/examples/wifi_example/wifi_example.pde +++ b/WiFi/examples/wifi_example/wifi_example.pde @@ -1,13 +1,13 @@ /* - WiFi example + WiFi Server example A simple connection with WiFi AP with Wireless Security information try to access with WPA or WEP security keys + A simple server is setup to exchange data. created 13 July 2010 by Domenico La Fauci */ - #include #include @@ -15,52 +15,32 @@ byte mac[6] = { 0 }; IPAddress ip; IPAddress gateway; IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + +Server server(23); -void setup() +void printIpData() { - Serial.begin(9600); - Serial.println("Setup WiFi..."); - char ssid[] = "Cariddi"; - Serial.println(ssid); - int status = WiFi.begin(ssid); - if ( status != WL_CONNECTED) - { - // Using WPA - Serial.println("Trying with Passphrase..."); - const char *pass = "12345678"; - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) - { - // using WEP - Serial.println("Trying with Key..."); - uint8_t key_idx = 0; - const char *key = "12345678"; - status = WiFi.begin(ssid,key_idx, key); - if ( status != WL_CONNECTED) - { - Serial.println("Wifi Connection failed!"); - return; - } - } - } - Serial.println("Wifi Connected!"); - ip = WiFi.localIp(); - + Serial.print("IP: "); Serial.print(ip[3],10);Serial.print("."); Serial.print(ip[2],10);Serial.print("."); Serial.print(ip[1],10);Serial.print("."); Serial.println(ip[0],10); - + subnet = WiFi.subnetMask(); Serial.print("NETMASK: "); Serial.print(subnet[3],10);Serial.print("."); Serial.print(subnet[2],10);Serial.print("."); Serial.print(subnet[1],10);Serial.print("."); Serial.println(subnet[0],10); - + gateway = WiFi.gatewayIP(); Serial.print("GATEWAY: "); Serial.print(gateway[3],10);Serial.print("."); @@ -78,7 +58,114 @@ void setup() Serial.println(mac[0],16); } -void loop() +void printCurrNet() { + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); } +void scanNetworks() +{ + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + //Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } + */ +} + + diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 1f1296686..7647417c6 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -13,6 +13,8 @@ #include #include +#include "HardwareSerial.h" + #define INFO_0 1 #define INFO_1 2 #define INFO_2 4 @@ -30,13 +32,13 @@ #ifdef _DEBUG_ -#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-W-");\ +#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-I-");\ Serial.println(x); \ }while (0); #define INFO2(x,y) do { PRINT_FILE_LINE() Serial.print("-I-");\ - Serial.print(x);Serial.print(",");Serial.println(y); \ + Serial.print(x,16);Serial.print(",");Serial.println(y,16); \ }while (0); @@ -49,7 +51,7 @@ Serial.print("-W-"); Serial.println(args); \ }while (0); -#define DBG_PIN2 3 +#define DBG_PIN2 5 #define DBG_PIN 4 #define START() digitalWrite(DBG_PIN2, HIGH); diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index d88703b88..6de08b207 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -19,7 +19,7 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) SpiDrv::sendParam(&sock, 1, LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -40,7 +40,7 @@ uint8_t ServerDrv::getState(uint8_t sock) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -64,7 +64,7 @@ uint8_t ServerDrv::availData(uint8_t sock) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -92,7 +92,7 @@ bool ServerDrv::getData(uint8_t sock, uint8_t *data) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -118,7 +118,7 @@ bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply if (!SpiDrv::waitResponse(GET_DATABUF_TCP_CMD, _data, _dataLen)) @@ -143,7 +143,7 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; @@ -169,7 +169,7 @@ uint8_t ServerDrv::isDataSent(uint8_t sock) SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration - delayMicroseconds(DELAY_RX_TX); + SpiDrv::waitForSlaveReady(); // Wait for reply uint8_t _data = 0; diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index 86af01a0e..e2a487d80 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -1,7 +1,8 @@ #include "WProgram.h" #include "spi_drv.h" - +#include "pins_arduino.h" +#define _DEBUG_ extern "C" { #include "debug.h" } @@ -9,31 +10,40 @@ extern "C" { #define DATAOUT 11//MOSI #define DATAIN 12//MISO #define SPICLOCK 13//sck -#define SLAVESELECT 10//ss +#define SLAVESELECT 2//ss +#define SLAVEREADY 3 -void SpiDrv::spiSetup() +void SpiDrv::begin() { - int clr = 0; - pinMode(DATAOUT, OUTPUT); - pinMode(DATAIN, INPUT); - pinMode(SPICLOCK,OUTPUT); - pinMode(SLAVESELECT,OUTPUT); - digitalWrite(SLAVESELECT,HIGH); //disable device - // SPCR = 01010000 - //interrupt disabled,spi enabled,msb 1st,master,clk low when idle, - //sample on leading edge of clk,system clock/4 rate (fastest) - SPCR = (1< maxNumParams) + { + numParam = maxNumParams; + } + *numParamRead = numParam; + if (numParam != 0) + { + for (i=0; i Date: Thu, 7 Apr 2011 23:55:10 +0200 Subject: [PATCH 06/98] Add WEP/WAP connection example and server example --- .../wifi_Server_example_WEP.pde | 171 +++++++++++++++++ .../wifi_Server_example_WPA.pde | 172 ++++++++++++++++++ .../wifi_WEP_example/wifi_WEP_example.pde | 25 ++- .../wifi_WPA_example/wifi_WPA_example.pde | 7 +- WiFi/utility/server_drv.cpp | 20 +- WiFi/utility/spi_drv.h | 3 + WiFi/utility/wifi_drv.cpp | 7 - WiFi/utility/wifi_spi.h | 2 - 8 files changed, 370 insertions(+), 37 deletions(-) create mode 100644 WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde create mode 100644 WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde diff --git a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde new file mode 100644 index 000000000..5a74d235e --- /dev/null +++ b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde @@ -0,0 +1,171 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); +} + +void scanNetworks() +{ + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } +} + + diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde new file mode 100644 index 000000000..deacbbb23 --- /dev/null +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde @@ -0,0 +1,172 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); +} + +void scanNetworks() +{ + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i0) + { + dataBuf[idx]=0; + Serial.println((char*)&dataBuf[0]); + server.write((char*)&dataBuf[0]); + } + return; + } + } +} + + diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde index 557c5039c..5a74d235e 100644 --- a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde +++ b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde @@ -85,12 +85,15 @@ void printCurrNet() void scanNetworks() { + Serial.println("** Scan Networks **"); byte numSsid = WiFi.scanNetworks(); if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; Serial.print("SSID List:"); Serial.println(numSsid, 10); for (int i = 0; i0) { dataBuf[idx]=0; - //Serial.println((char*)&dataBuf[0]); + Serial.println((char*)&dataBuf[0]); server.write((char*)&dataBuf[0]); } return; } } - */ } diff --git a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde index 498cf1191..916e020c7 100644 --- a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde +++ b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde @@ -101,7 +101,7 @@ void scanNetworks() int startWiFiWpa() { Serial.println("Setup WiFi Wpa..."); - strcpy(ssid, "Cariddiwep"); + strcpy(ssid, "Cariddi"); Serial.print("SSID: "); Serial.println(ssid); const char *pass = "1234567890"; @@ -117,9 +117,10 @@ void setup() { Serial.begin(9600); Serial.println("*** Start WiFi example ***"); - - //scanNetworks(); delay(3000); + WiFi.begin(); + scanNetworks(); + int _status = startWiFiWpa(); diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index 6de08b207..ae9429347 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -12,10 +12,10 @@ extern "C" { // Start server TCP on port specified void ServerDrv::StartServer(uint16_t port, uint8_t sock) { - SpiDrv::spiSlaveSelect(); + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2); - SpiDrv::sendParam(port); + SpiDrv::sendParam(port);SpiDrv::spiSlaveSelect(); SpiDrv::sendParam(&sock, 1, LAST_PARAM); //Wait the reply elaboration @@ -34,7 +34,7 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) uint8_t ServerDrv::getState(uint8_t sock) { - SpiDrv::spiSlaveSelect(); + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); @@ -51,14 +51,13 @@ uint8_t ServerDrv::getState(uint8_t sock) } SpiDrv::spiSlaveDeselect(); - delayMicroseconds(DELAY_POST_CMD); return _data; } uint8_t ServerDrv::availData(uint8_t sock) { - SpiDrv::spiSlaveSelect(); + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); @@ -75,8 +74,6 @@ uint8_t ServerDrv::availData(uint8_t sock) } SpiDrv::spiSlaveDeselect(); - delayMicroseconds(DELAY_POST_CMD); - if (_dataLen!=0) { return (_data == 1); @@ -86,7 +83,7 @@ uint8_t ServerDrv::availData(uint8_t sock) bool ServerDrv::getData(uint8_t sock, uint8_t *data) { - SpiDrv::spiSlaveSelect(); + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); @@ -112,7 +109,7 @@ bool ServerDrv::getData(uint8_t sock, uint8_t *data) bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen) { - SpiDrv::spiSlaveSelect(); + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); @@ -136,7 +133,7 @@ bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen) bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) { - SpiDrv::spiSlaveSelect(); + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2); SpiDrv::sendParam(&sock, sizeof(sock)); @@ -163,7 +160,7 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) uint8_t ServerDrv::isDataSent(uint8_t sock) { - SpiDrv::spiSlaveSelect(); + WAIT_FOR_SLAVE_SELECT(); // Send Command SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); @@ -180,7 +177,6 @@ uint8_t ServerDrv::isDataSent(uint8_t sock) } SpiDrv::spiSlaveDeselect(); - delayMicroseconds(DELAY_POST_CMD); return _data; } diff --git a/WiFi/utility/spi_drv.h b/WiFi/utility/spi_drv.h index 5f9ed37a2..4511f15dd 100644 --- a/WiFi/utility/spi_drv.h +++ b/WiFi/utility/spi_drv.h @@ -14,6 +14,9 @@ #define DUMMY_DATA 0xFF +#define WAIT_FOR_SLAVE_SELECT() \ + SpiDrv::waitForSlaveReady(); \ + SpiDrv::spiSlaveSelect(); class SpiDrv { diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index c204e2232..fd5050594 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -25,11 +25,6 @@ uint8_t WiFiDrv::_gatewayIp[] = {0}; // Private Methods -#define WAIT_FOR_SLAVE_SELECT() \ - SpiDrv::waitForSlaveReady(); \ - SpiDrv::spiSlaveSelect(); - - 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}}; @@ -161,7 +156,6 @@ uint8_t WiFiDrv::getConnectionStatus() //Wait the reply elaboration SpiDrv::waitForSlaveReady(); - //delayMicroseconds(DELAY_RX_TX); // Wait for reply uint8_t _data = 0; @@ -334,7 +328,6 @@ char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem) if (networkItem >= WL_NETWORKS_LIST_MAXNUM) return NULL; - //TODO make an RPC call to get the ssid associated with networkItem return _networkSsid[networkItem]; } diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 2d0b1e2cd..841aff0f7 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -10,8 +10,6 @@ #define WIFI_SPI_ERR 0xFF #define TIMEOUT_CHAR 1000 -#define DELAY_RX_TX 200 //usec -#define DELAY_POST_CMD 0 //usec //#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */ #define NO_SOCKET_AVAIL 255 From 49912f241fcac902778a21cf18312e6a41c0fdf2 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Thu, 21 Apr 2011 07:47:54 +0200 Subject: [PATCH 07/98] Debug on Server TCP --- WiFi/Server.cpp | 3 - WiFi/WiFi.cpp | 19 +++-- .../wifi_Server_example_WEP.pde | 6 +- .../wifi_Server_example_WPA.pde | 69 +++++++++++++------ .../wifi_WEP_example/wifi_WEP_example.pde | 36 ++-------- WiFi/utility/debug.h | 1 + WiFi/utility/server_drv.cpp | 25 +++---- WiFi/utility/spi_drv.cpp | 24 +++++-- WiFi/utility/spi_drv.h | 25 ++++--- WiFi/utility/wifi_drv.cpp | 22 +++--- WiFi/utility/wifi_spi.h | 31 +++++++-- WiFi/utility/wl_definitions.h | 2 + 12 files changed, 153 insertions(+), 110 deletions(-) diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index ba7cd2697..8dd091496 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -35,10 +35,7 @@ Client Server::available(byte* status) *status == ESTABLISHED) { return client; //TODO - }else{ - delayMicroseconds(100); } - } } diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index cb433d2bc..1716bc90b 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -49,17 +49,18 @@ int WiFiClass::begin() int WiFiClass::begin(char* ssid) { uint8_t status = WL_IDLE_STATUS; - init(); + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + begin(); 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)); + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); }else { status = WL_CONNECT_FAILED; @@ -70,7 +71,9 @@ int WiFiClass::begin(char* ssid) int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) { uint8_t status = WL_IDLE_STATUS; - init(); + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + begin(); // set encryption key if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) { @@ -79,7 +82,7 @@ int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) delay(WL_DELAY_START_CONNECTION); status = WiFiDrv::getConnectionStatus(); } - while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); }else{ status = WL_CONNECT_FAILED; } @@ -89,7 +92,9 @@ int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key) int WiFiClass::begin(char* ssid, const char *passphrase) { uint8_t status = WL_IDLE_STATUS; - init(); + uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; + + begin(); // set passphrase if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) { @@ -98,7 +103,7 @@ int WiFiClass::begin(char* ssid, const char *passphrase) delay(WL_DELAY_START_CONNECTION); status = WiFiDrv::getConnectionStatus(); } - while (( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED)); + while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); }else{ status = WL_CONNECT_FAILED; } diff --git a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde index 5a74d235e..9191d63e3 100644 --- a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde +++ b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde @@ -149,8 +149,9 @@ void loop() byte _status = 0; Client client = server.available(&_status); if (client) { - //Serial.print("Status: "); - //Serial.println(status, 16); + Serial.print("Status: "); + Serial.println(status, 16); + /* byte idx = 0; while (client.available()) { @@ -164,6 +165,7 @@ void loop() server.write((char*)&dataBuf[0]); } return; + */ } } } diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde index deacbbb23..6ea176fa3 100644 --- a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde @@ -23,12 +23,13 @@ char ssidList[MAX_NUM_SSID][32] = { {0} }; Server server(23); +boolean gotAMessage = false; // whether or not you got a message from the client yet void printIpData() { ip = WiFi.localIp(); - Serial.print("IP: "); + Serial.print("\nIP: "); Serial.print(ip[3],10);Serial.print("."); Serial.print(ip[2],10);Serial.print("."); Serial.print(ip[1],10);Serial.print("."); @@ -85,7 +86,7 @@ void printCurrNet() void scanNetworks() { - Serial.println("** Scan Networks **"); + Serial.println("\n** Scan Networks **"); byte numSsid = WiFi.scanNetworks(); if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; Serial.print("SSID List:"); @@ -100,7 +101,8 @@ void scanNetworks() int startWiFiWpa() { - Serial.println("Setup WiFi Wpa..."); + Serial.println("\nSetup WiFi Wpa..."); + //strcpy(ssid, "AndroidAP9647"); strcpy(ssid, "Cariddi"); Serial.print("SSID: "); Serial.println(ssid); @@ -127,44 +129,71 @@ void setup() if ( _status == WL_CONNECTED) { - Serial.println("Wifi Connected!"); + Serial.println("\nWifi Connected!"); printIpData(); printCurrNet(); - - scanNetworks(); - Serial.println("Starting server..."); + Serial.println("\nStarting server..."); server.begin(); delay(1000); } } +void execCmd(char* buf) +{ + Serial.print("Executing command: "); + Serial.println(buf); + server.write(buf); +} + + void loop() { - if (status == WL_CONNECTED) { byte _status = 0; Client client = server.available(&_status); + //delay(2000); if (client) { - //Serial.print("Status: "); - //Serial.println(status, 16); - byte idx = 0; + if (!gotAMessage) { + Serial.println("We have a new client"); + client.println("Hello, client!"); + gotAMessage = true; + } +/* + // 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.print(thisChar); + */ + /* + Serial.print("Status: "); + Serial.println(_status, 16); + delay(2000); + */ + + static byte idx = 0; + while (client.available()) { - dataBuf[idx++] = client.read(); + delay(10); + dataBuf[idx] = client.read(); + Serial.print(dataBuf[idx]); + if (dataBuf[idx] = 0xa) + { + dataBuf[idx+1]=0; + //Serial.println((char*)dataBuf); + //execCmd((char*)dataBuf); + idx=0; + }else{ + ++idx; + } } - - if (idx>0) - { - dataBuf[idx]=0; - Serial.println((char*)&dataBuf[0]); - server.write((char*)&dataBuf[0]); - } - return; } } } diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde index 5a74d235e..9208e2276 100644 --- a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde +++ b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde @@ -119,8 +119,10 @@ void setup() { Serial.begin(9600); Serial.println("*** Start WiFi example ***"); - delay(3000); + + WiFi.begin(); + scanNetworks(); startWiFiWep(); @@ -130,42 +132,12 @@ void setup() printIpData(); - printCurrNet(); - - scanNetworks(); - - Serial.println("Starting server..."); - server.begin(); - delay(1000); - + printCurrNet(); } } void loop() { - - if (status == WL_CONNECTED) - { - byte _status = 0; - Client client = server.available(&_status); - if (client) { - //Serial.print("Status: "); - //Serial.println(status, 16); - byte idx = 0; - while (client.available()) - { - dataBuf[idx++] = client.read(); - } - - if (idx>0) - { - dataBuf[idx]=0; - Serial.println((char*)&dataBuf[0]); - server.write((char*)&dataBuf[0]); - } - return; - } - } } diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 7647417c6..b6228d4ed 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -44,6 +44,7 @@ #else #define INFO1(x) do {} while(0); +#define INFO2(x,y) do {} while(0); #define INFO(format, args...) do {} while(0); #endif diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index ae9429347..53bb2f7d5 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -3,6 +3,8 @@ #include "WProgram.h" #include "spi_drv.h" +#define _DEBUG_ + extern "C" { #include "wl_types.h" #include "debug.h" @@ -15,7 +17,7 @@ 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::spiSlaveSelect(); + SpiDrv::sendParam(port); SpiDrv::sendParam(&sock, 1, LAST_PARAM); //Wait the reply elaboration @@ -24,7 +26,7 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + if (!SpiDrv::waitResponseCmd(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); } @@ -45,13 +47,12 @@ uint8_t ServerDrv::getState(uint8_t sock) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + if (!SpiDrv::waitResponseCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); } SpiDrv::spiSlaveDeselect(); - - return _data; + return _data; } @@ -68,7 +69,7 @@ uint8_t ServerDrv::availData(uint8_t sock) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + if (!SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); } @@ -94,7 +95,7 @@ bool ServerDrv::getData(uint8_t sock, uint8_t *data) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(GET_DATA_TCP_CMD, &_data, &_dataLen)) + if (!SpiDrv::waitResponseData8(GET_DATA_TCP_CMD, &_data, &_dataLen)) { WARN("error waitResponse"); } @@ -112,13 +113,13 @@ 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::sendParam(&sock, sizeof(sock), LAST_PARAM); + SpiDrv::sendBuffer(&sock, sizeof(sock), LAST_PARAM); //Wait the reply elaboration SpiDrv::waitForSlaveReady(); // Wait for reply - if (!SpiDrv::waitResponse(GET_DATABUF_TCP_CMD, _data, _dataLen)) + if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen)) { WARN("error waitResponse"); } @@ -136,7 +137,7 @@ 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::sendParam(&sock, sizeof(sock)); + SpiDrv::sendBuffer(&sock, sizeof(sock)); SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM); //Wait the reply elaboration @@ -145,7 +146,7 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(SEND_DATA_TCP_CMD, &_data, &_dataLen)) + if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen)) { WARN("error waitResponse"); } @@ -171,7 +172,7 @@ uint8_t ServerDrv::isDataSent(uint8_t sock) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse isDataSent"); } diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index e2a487d80..6a818a420 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -33,6 +33,10 @@ void SpiDrv::begin() digitalWrite(SS, HIGH); digitalWrite(SLAVESELECT, HIGH); +#ifdef _DEBUG_ + INIT_TRIGGER() +#endif + // Warning: if the SS pin ever becomes a LOW INPUT then SPI // automatically switches to Slave, so the data direction of // the SS pin MUST be kept as OUTPUT. @@ -75,6 +79,9 @@ int SpiDrv::waitSpiChar(unsigned char waitChar) if (_readChar == WAIT_CMD) { delayMicroseconds(WAIT_CHAR_DELAY); + }else if (_readChar == ERR_CMD) + { + return -1; }else { delayMicroseconds(TIMEOUT_CHAR_DELAY); @@ -164,7 +171,7 @@ void SpiDrv::waitForSlaveReady() #endif } -int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len) +int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len) { char _data = 0; int i =0, ii = 0; @@ -188,7 +195,7 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* return 1; } - +/* int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len) { char _data = 0; @@ -213,9 +220,9 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t return 1; } +*/ - -int SpiDrv::waitResponse(uint8_t cmd, uint8_t* param, uint16_t* param_len) +int SpiDrv::waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len) { char _data = 0; int i =0, ii = 0; @@ -241,7 +248,7 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* param, uint16_t* param_len) return 1; } -int SpiDrv::waitResponse(uint8_t cmd, uint8_t* param, uint8_t* param_len) +int SpiDrv::waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len) { char _data = 0; int i =0, ii = 0; @@ -267,7 +274,7 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* param, uint8_t* param_len) return 1; } -int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, tParam* params) +int SpiDrv::waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params) { char _data = 0; int i =0, ii = 0; @@ -308,6 +315,7 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, tParam* params) return 1; } +/* int SpiDrv::waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams) { char _data = 0; @@ -346,6 +354,7 @@ int SpiDrv::waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uin } return 1; } +*/ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams) { @@ -492,6 +501,9 @@ void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam) // Send Spi START CMD spiTransfer(START_CMD); + //wait the interrupt trigger on slave + delayMicroseconds(SPI_START_CMD_DELAY); + // Send Spi C + cmd spiTransfer(cmd & ~(REPLY_FLAG)); diff --git a/WiFi/utility/spi_drv.h b/WiFi/utility/spi_drv.h index 4511f15dd..0b0871572 100644 --- a/WiFi/utility/spi_drv.h +++ b/WiFi/utility/spi_drv.h @@ -7,7 +7,9 @@ #define WAIT_CHAR_DELAY 100 #define TIMEOUT_CHAR_DELAY 10 #define TIMEOUT_READY_SLAVE 1000 -#define SPI_TX_DELAY 2 +#define SPI_TX_DELAY 5 +#define SPI_START_CMD_DELAY 10 + #define NO_LAST_PARAM 0 #define LAST_PARAM 1 @@ -16,7 +18,10 @@ #define WAIT_FOR_SLAVE_SELECT() \ SpiDrv::waitForSlaveReady(); \ - SpiDrv::spiSlaveSelect(); + SpiDrv::spiSlaveSelect(); \ + delayMicroseconds(SPI_TX_DELAY); + + class SpiDrv { @@ -46,18 +51,18 @@ public: static char readChar(); - static int waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams); - - static int waitResponse(uint8_t cmd, uint8_t numParam, tParam* params); + static int waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params); - static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len); + static int waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len); - static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len); - - static int waitResponse(uint8_t cmd, uint8_t* param, uint8_t* param_len); + static int waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len); - static int waitResponse(uint8_t cmd, uint8_t* param, uint16_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); diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index fd5050594..13cadc7f3 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -43,7 +43,7 @@ void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - SpiDrv::waitResponse(GET_IPADDR_CMD, PARAM_NUMS_3, params); + SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params); SpiDrv::spiSlaveDeselect(); } @@ -70,7 +70,7 @@ uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); } @@ -93,7 +93,7 @@ uint8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *pas // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); } @@ -117,7 +117,7 @@ uint8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - if (!SpiDrv::waitResponse(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) + if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen)) { WARN("error waitResponse"); } @@ -140,7 +140,7 @@ uint8_t WiFiDrv::disconnect() // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponse(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); + uint8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -160,7 +160,7 @@ uint8_t WiFiDrv::getConnectionStatus() // Wait for reply uint8_t _data = 0; uint8_t _dataLen = 0; - SpiDrv::waitResponse(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); + SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -182,7 +182,7 @@ uint8_t* WiFiDrv::getMacAddress() // Wait for reply uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponse(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); + uint8_t result = SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -222,7 +222,7 @@ char* WiFiDrv::getCurrentSSID() // Wait for reply uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponse(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); + uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -244,7 +244,7 @@ uint8_t* WiFiDrv::getCurrentBSSID() // Wait for reply uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponse(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); + uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -267,7 +267,7 @@ int32_t WiFiDrv::getCurrentRSSI() // Wait for reply uint8_t _dataLen = 0; int32_t rssi = 0; - uint8_t result = SpiDrv::waitResponse(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); + uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -290,7 +290,7 @@ uint8_t WiFiDrv::getCurrentEncryptionType() // Wait for reply uint8_t dataLen = 0; uint8_t encType = 0; - uint8_t result = SpiDrv::waitResponse(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)encType, &dataLen); + uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)encType, &dataLen); SpiDrv::spiSlaveDeselect(); diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 841aff0f7..dfcb36d1a 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -5,6 +5,7 @@ #define CMD_FLAG 0 #define REPLY_FLAG 1<<7 +#define DATA_FLAG 0x40 #define WIFI_SPI_ACK 1 #define WIFI_SPI_ERR 0xFF @@ -17,6 +18,7 @@ #define START_CMD 0xE0 #define WAIT_CMD 0xE1 #define END_CMD 0xEE +#define ERR_CMD 0xEF enum { SET_NET_CMD = 0x10, @@ -31,17 +33,17 @@ enum { 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, DISCONNECT_CMD = 0x30, + // All command with DATA_FLAG 0x40 send a 16bit Len - START_SERVER_TCP_CMD = 0x40, - GET_STATE_TCP_CMD = 0x41, - GET_DATA_TCP_CMD = 0x42, - AVAIL_DATA_TCP_CMD = 0x43, SEND_DATA_TCP_CMD = 0x44, - DATA_SENT_TCP_CMD = 0x45, - GET_DATABUF_TCP_CMD = 0x46, + GET_DATABUF_TCP_CMD = 0x45, }; @@ -79,6 +81,13 @@ typedef struct __attribute__((__packed__)) char* param; }tParam; +typedef struct __attribute__((__packed__)) +{ + uint16_t dataLen; + char* data; +}tDataParam; + + typedef struct __attribute__((__packed__)) { unsigned char cmd; @@ -87,6 +96,14 @@ typedef struct __attribute__((__packed__)) 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__)) { diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h index df20637ea..a1dd6d525 100644 --- a/WiFi/utility/wl_definitions.h +++ b/WiFi/utility/wl_definitions.h @@ -22,6 +22,8 @@ #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 5 typedef enum { WL_IDLE_STATUS, From ca1a1d8a9d451db55b30c463d1355253e4570178 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Thu, 21 Apr 2011 19:16:39 +0200 Subject: [PATCH 08/98] Added WebServer --- WiFi/Server.cpp | 6 +- WiFi/Server.h | 2 +- WiFi/examples/WebServer/WebServer.pde | 97 +++++++++++++++++++ .../wifi_Server_example_WPA.pde | 10 +- WiFi/utility/wl_definitions.h | 2 +- 5 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 WiFi/examples/WebServer/WebServer.pde diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index 8dd091496..90ef0a321 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -29,10 +29,12 @@ Client Server::available(byte* status) if (WiFiClass::_server_port[sock] != 0) { Client client(sock); - *status = client.status(); + int _status = client.status(); + if (status != NULL) + *status = _status; if (WiFiClass::_server_port[sock] == _port && - *status == ESTABLISHED) + _status == ESTABLISHED) { return client; //TODO } diff --git a/WiFi/Server.h b/WiFi/Server.h index e48c6a55b..cf586c6ad 100755 --- a/WiFi/Server.h +++ b/WiFi/Server.h @@ -15,7 +15,7 @@ private: void* pcb; public: Server(uint16_t); - Client available(uint8_t*); + Client available(uint8_t* status = NULL); void begin(); virtual void write(uint8_t); virtual void write(const char *str); diff --git a/WiFi/examples/WebServer/WebServer.pde b/WiFi/examples/WebServer/WebServer.pde new file mode 100644 index 000000000..83869073f --- /dev/null +++ b/WiFi/examples/WebServer/WebServer.pde @@ -0,0 +1,97 @@ +/* + Web Server + + A simple web server that shows the value of the analog input pins. + using a WiFi shield. + + Circuit: + * WiFi shield attached + * Analog inputs attached to pins A0 through A5 (optional) + + created 13 July 2010 + by Domenico La Fauci + */ + +#include +#include + +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; + +Server server(80); + +int startWiFiWpa() +{ + Serial.println("\nSetup WiFi Wpa..."); + //strcpy(ssid, "AndroidAP9647"); + strcpy(ssid, "Cariddi"); + Serial.print("SSID: "); + Serial.println(ssid); + const char *pass = "1234567890"; + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) + { + Serial.println("Connection Failed"); + } + return status; +} + +void setup() +{ + // start the WiFi connection and the server: + Serial.begin(9600); + Serial.println("*** Start WebServer WiFi example ***"); + + int _status = startWiFiWpa(); + if ( _status == WL_CONNECTED) + { + Serial.println("\nStarting server..."); + server.begin(); + } +} + +void loop() +{ + // listen for incoming clients + Client client = server.available(); + if (client) { + // an http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + // 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(); + + // output the value of each analog input pin + for (int analogChannel = 0; analogChannel < 6; analogChannel++) { + client.print("analog input "); + client.print(analogChannel); + client.print(" is "); + client.print(analogRead(analogChannel)); + 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(); + } +} diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde index 6ea176fa3..e9b27b15d 100644 --- a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde @@ -144,7 +144,7 @@ void setup() void execCmd(char* buf) { - Serial.print("Executing command: "); + Serial.print("\nExecuting command: "); Serial.println(buf); server.write(buf); } @@ -159,7 +159,7 @@ void loop() //delay(2000); if (client) { if (!gotAMessage) { - Serial.println("We have a new client"); + Serial.println("\nWe have a new client\n"); client.println("Hello, client!"); gotAMessage = true; } @@ -181,14 +181,14 @@ void loop() while (client.available()) { - delay(10); dataBuf[idx] = client.read(); + if (idx == 0) Serial.print("Client chatting...: "); Serial.print(dataBuf[idx]); - if (dataBuf[idx] = 0xa) + if ((dataBuf[idx] == 0xa)/*||(dataBuf[idx] == 0xd)*/) { dataBuf[idx+1]=0; //Serial.println((char*)dataBuf); - //execCmd((char*)dataBuf); + execCmd((char*)dataBuf); idx=0; }else{ ++idx; diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h index a1dd6d525..e28064a3d 100644 --- a/WiFi/utility/wl_definitions.h +++ b/WiFi/utility/wl_definitions.h @@ -23,7 +23,7 @@ // Maxmium number of socket #define MAX_SOCK_NUM 4 //Maximum number of attempts to establish wifi connection -#define WL_MAX_ATTEMPT_CONNECTION 5 +#define WL_MAX_ATTEMPT_CONNECTION 10 typedef enum { WL_IDLE_STATUS, From c10210a881edb69ecf1f152cb72618f947ebeffe Mon Sep 17 00:00:00 2001 From: mlafauci Date: Sun, 24 Apr 2011 00:45:47 +0200 Subject: [PATCH 09/98] Added Client connection and WebClient example --- WiFi/Client.cpp | 17 ++--- WiFi/examples/WebClient/WebClient.pde | 89 +++++++++++++++++++++++++++ WiFi/utility/server_drv.cpp | 23 +++++++ WiFi/utility/server_drv.h | 2 + WiFi/utility/spi_drv.cpp | 2 - WiFi/utility/wifi_spi.h | 19 ++++++ 6 files changed, 139 insertions(+), 13 deletions(-) create mode 100644 WiFi/examples/WebClient/WebClient.pde diff --git a/WiFi/Client.cpp b/WiFi/Client.cpp index 770fb8d49..f33cea114 100755 --- a/WiFi/Client.cpp +++ b/WiFi/Client.cpp @@ -22,19 +22,14 @@ Client::Client(IPAddress& ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_S uint8_t Client::connect() { _sock = getFirstSocket(); -//TODO implementation - _socket = socket(TCP_SOCKET); - if (_socket<0) + if (_sock != NO_SOCKET_AVAIL) { - return 0; + ServerDrv::StartClient(uint32_t(_ip), _port, _sock); + WiFiClass::_state[_sock] = _socket; }else{ - WiFiClass::_state[_sock] = _socket; + return 0; } -// -// if (!::connect(_socket, _ip, _port)) { -// return 0; -// } - return 1; + return 1; } void Client::write(uint8_t b) { @@ -138,7 +133,7 @@ Client::operator bool() { uint8_t Client::getFirstSocket() { for (int i = 0; i < MAX_SOCK_NUM; i++) { - if (WiFiClass::_state[i] < 0) + if (WiFiClass::_state[i] == 0) { return i; } diff --git a/WiFi/examples/WebClient/WebClient.pde b/WiFi/examples/WebClient/WebClient.pde new file mode 100644 index 000000000..feabd0b38 --- /dev/null +++ b/WiFi/examples/WebClient/WebClient.pde @@ -0,0 +1,89 @@ +/* + Web client + + This sketch connects to a website (http://www.google.com) + using a WiFi shield. + + Circuit: + * WiFi shield attached + * Analog inputs attached to pins A0 through A5 (optional) + + created 13 July 2010 + by Domenico La Fauci + */ + + +#include +#include + +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +IPAddress server(74,125,232,115); // Google +//byte server[] = { 173,194,33,104 }; // Google + +// 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): +Client client(server, 80); + +int startWiFiWpa() +{ + Serial.println("\nSetup WiFi Wpa..."); + //strcpy(ssid, "AndroidAP9647"); + strcpy(ssid, "Cariddi"); + Serial.print("SSID: "); + Serial.println(ssid); + const char *pass = "1234567890"; + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) + { + Serial.println("Connection Failed"); + } + return status; +} + + +void setup() { + // start the WiFi connection and the server: + Serial.begin(9600); + Serial.println("*** Start WebClient WiFi example ***"); + + int _status = startWiFiWpa(); + if ( _status == WL_CONNECTED) + { + Serial.println("\nStarting connection..."); + // if you get a connection, report back via serial: + if (client.connect()) { + Serial.println("connected"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.0"); + client.println(); + } + else { + // kf you didn't get a connection to the server: + Serial.println("connection failed"); + } + } +} + +void loop() +{ + // if there are incoming bytes available + // from the server, read them and print them: + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + + // do nothing forevermore: + for(;;) + ; + } +} + diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index 53bb2f7d5..eee1be243 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -33,6 +33,29 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) SpiDrv::spiSlaveDeselect(); } +// Start server TCP on port specified +void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock) +{ + WAIT_FOR_SLAVE_SELECT(); + INFO2(ipAddress,port); + // 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(); +} uint8_t ServerDrv::getState(uint8_t sock) { diff --git a/WiFi/utility/server_drv.h b/WiFi/utility/server_drv.h index 51dcadfa3..6d28d13f1 100644 --- a/WiFi/utility/server_drv.h +++ b/WiFi/utility/server_drv.h @@ -9,6 +9,8 @@ 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 uint8_t getState(uint8_t sock); diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index 6a818a420..c48ce8ba9 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -426,7 +426,6 @@ void SpiDrv::sendParamLen8(uint8_t param_len) spiTransfer(param_len); } - void SpiDrv::sendParamLen16(uint16_t param_len) { // Send Spi paramLen @@ -434,7 +433,6 @@ void SpiDrv::sendParamLen16(uint16_t param_len) spiTransfer((uint8_t)(param_len & 0xff)); } - uint8_t SpiDrv::readParamLen8(uint8_t* param_len) { uint8_t _param_len = spiTransfer(DUMMY_DATA); diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index dfcb36d1a..c4e573366 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -38,6 +38,7 @@ enum { DATA_SENT_TCP_CMD = 0x2A, AVAIL_DATA_TCP_CMD = 0x2B, GET_DATA_TCP_CMD = 0x2C, + START_CLIENT_TCP_CMD= 0x2D, DISCONNECT_CMD = 0x30, // All command with DATA_FLAG 0x40 send a 16bit Len @@ -113,4 +114,22 @@ typedef struct __attribute__((__packed__)) 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 From 8d1761f0097a1d74173f717ed009e840fdc42d43 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Sat, 30 Apr 2011 07:48:10 +0200 Subject: [PATCH 10/98] Cleaning the code --- .../wifi_Server_example_WPA.pde | 20 ++---- WiFi/utility/spi_drv.cpp | 65 +++++-------------- WiFi/utility/spi_drv.h | 9 ++- WiFi/utility/wifi_drv.cpp | 6 +- WiFi/utility/wifi_spi.h | 1 - 5 files changed, 29 insertions(+), 72 deletions(-) diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde index e9b27b15d..a3a5c8b0d 100644 --- a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde @@ -119,10 +119,12 @@ void setup() { Serial.begin(9600); Serial.println("*** Start WiFi example ***"); - delay(3000); WiFi.begin(); + // Wait for initialize WiFi + delay(3000); + scanNetworks(); int _status = startWiFiWpa(); @@ -156,27 +158,13 @@ void loop() { byte _status = 0; Client client = server.available(&_status); - //delay(2000); if (client) { if (!gotAMessage) { Serial.println("\nWe have a new client\n"); client.println("Hello, client!"); gotAMessage = true; } -/* - // 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.print(thisChar); - */ - /* - Serial.print("Status: "); - Serial.println(_status, 16); - delay(2000); - */ - + static byte idx = 0; while (client.available()) diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index c48ce8ba9..bd7dd16a1 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -42,6 +42,7 @@ void SpiDrv::begin() // the SS pin MUST be kept as OUTPUT. SPCR |= _BV(MSTR); SPCR |= _BV(SPE); + //SPSR |= _BV(SPI2X); } void SpiDrv::end() { @@ -76,48 +77,29 @@ int SpiDrv::waitSpiChar(unsigned char waitChar) unsigned char _readChar = 0; do{ _readChar = readChar(); //get data byte - if (_readChar == WAIT_CMD) - { - delayMicroseconds(WAIT_CHAR_DELAY); - }else if (_readChar == ERR_CMD) + if (_readChar == ERR_CMD) { + WARN("Err cmd received\n"); return -1; - }else - { - delayMicroseconds(TIMEOUT_CHAR_DELAY); } -// else if (_readChar != waitChar) -// { -// Serial.println(_readChar,16); -// } }while((timeout-- > 0) && (_readChar != waitChar)); - - if ((_readChar != waitChar)&&(timeout >=0)) - { - INFO1("*C*"); - Serial.println(_readChar,16); - }else if (timeout == 0) - { - INFO1("*T*"); - } - return (_readChar == waitChar); } -int SpiDrv::waitSpiChar(char waitChar, char* readChar) -{ - int timeout = TIMEOUT_CHAR; - do{ - *readChar = spiTransfer(DUMMY_DATA); //get data byte - if (*readChar == WAIT_CMD) - { - INFO1("WAIT"); - delayMicroseconds(WAIT_CHAR_DELAY); - } - }while((timeout-- > 0) && (*readChar != waitChar)); - - return (*readChar == waitChar); -} +//int SpiDrv::waitSpiChar(char waitChar, char* readChar) +//{ +// int timeout = TIMEOUT_CHAR; +// do{ +// *readChar = spiTransfer(DUMMY_DATA); //get data byte +// if (*readChar == WAIT_CMD) +// { +// INFO1("WAIT"); +// delayMicroseconds(WAIT_CHAR_DELAY); +// } +// }while((timeout-- > 0) && (*readChar != waitChar)); +// +// return (*readChar == waitChar); +//} int SpiDrv::readAndCheckChar(char checkChar, char* readChar) @@ -153,22 +135,11 @@ char SpiDrv::readChar() return 0; \ }else \ -bool SpiDrv::waitSlaveReady() -{ - return (digitalRead(SLAVEREADY) == LOW); -} +#define waitSlaveReady() (digitalRead(SLAVEREADY) == LOW) void SpiDrv::waitForSlaveReady() { -#if 0 - int count = 0; - while (!waitSlaveReady() && (++count Date: Wed, 18 May 2011 08:39:47 +0200 Subject: [PATCH 11/98] Bugfix and fix compilation erro with new-extensions branch --- WiFi/Client.cpp | 10 ++-- WiFi/Server.cpp | 3 -- WiFi/WiFi.cpp | 6 +++ WiFi/WiFi.h | 3 ++ .../wifi_Server_example_WPA.pde | 21 ++++++-- WiFi/examples/wifi_example/wifi_example.pde | 46 ++++++++++++++++-- WiFi/utility/debug.h | 2 - WiFi/utility/spi_drv.cpp | 48 +++++++++++++++++-- WiFi/utility/spi_drv.h | 11 ++--- WiFi/utility/wifi_drv.cpp | 21 ++++++++ WiFi/utility/wifi_drv.h | 1 + WiFi/utility/wifi_spi.h | 1 + 12 files changed, 144 insertions(+), 29 deletions(-) diff --git a/WiFi/Client.cpp b/WiFi/Client.cpp index f33cea114..82c2b3e7a 100755 --- a/WiFi/Client.cpp +++ b/WiFi/Client.cpp @@ -3,6 +3,7 @@ extern "C" { #include "utility/wl_types.h" #include "socket.h" #include "string.h" + #include "utility/debug.h" } #include "WProgram.h" @@ -35,25 +36,28 @@ uint8_t Client::connect() { void Client::write(uint8_t b) { if (_sock != 255) { - while (!ServerDrv::isDataSent(_sock)); + START(); ServerDrv::sendData(_sock, &b, 1); + while (!ServerDrv::isDataSent(_sock)); + END(); + } } void Client::write(const char *str) { if (_sock != 255) { - while (!ServerDrv::isDataSent(_sock)); unsigned int len = strlen(str); ServerDrv::sendData(_sock, (const uint8_t *)str, len); + while (!ServerDrv::isDataSent(_sock)); } } void Client::write(const uint8_t *buf, size_t size) { if (_sock != 255) { - while (!ServerDrv::isDataSent(_sock)); ServerDrv::sendData(_sock, buf, size); + while (!ServerDrv::isDataSent(_sock)); } } diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index 90ef0a321..695c55d25 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -66,10 +66,7 @@ void Server::write(const uint8_t *buffer, size_t size) client.status() == ESTABLISHED) { client.write(buffer, size); - }else{ - delay(20); } - } } } diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 1716bc90b..337c0bb93 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -191,5 +191,11 @@ uint8_t WiFiClass::status() return WiFiDrv::getConnectionStatus(); } +uint8_t WiFiClass::test() +{ + return WiFiDrv::testCmd(); +} + + WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index d97209b63..738f6f256 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -86,6 +86,9 @@ public: // Return Connection status uint8_t status(); + // function used for test + uint8_t test(); + friend class Client; friend class Server; }; diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde index a3a5c8b0d..fa79002f1 100644 --- a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde @@ -10,6 +10,7 @@ */ #include #include +#define _PRINT_ byte mac[6] = { 0 }; IPAddress ip; @@ -103,11 +104,13 @@ int startWiFiWpa() { Serial.println("\nSetup WiFi Wpa..."); //strcpy(ssid, "AndroidAP9647"); + //strcpy(ssid, "AndroidAP3551"); strcpy(ssid, "Cariddi"); Serial.print("SSID: "); Serial.println(ssid); const char *pass = "1234567890"; status = WiFi.begin(ssid, pass); + //status = WiFi.begin(ssid); if ( status != WL_CONNECTED) { Serial.println("Connection Failed"); @@ -146,9 +149,12 @@ void setup() void execCmd(char* buf) { + #ifdef _PRINT_ Serial.print("\nExecuting command: "); Serial.println(buf); - server.write(buf); + #endif + //server.write(buf); + server.print(buf); } @@ -166,12 +172,18 @@ void loop() } static byte idx = 0; + int c = 0; - while (client.available()) + do { - dataBuf[idx] = client.read(); + c = client.read(); + if (c!=-1) + { + dataBuf[idx] = c; + #ifdef _PRINT_ if (idx == 0) Serial.print("Client chatting...: "); Serial.print(dataBuf[idx]); + #endif if ((dataBuf[idx] == 0xa)/*||(dataBuf[idx] == 0xd)*/) { dataBuf[idx+1]=0; @@ -181,7 +193,8 @@ void loop() }else{ ++idx; } - } + } + }while (c!=-1); } } } diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/wifi_example/wifi_example.pde index 6606c2b1f..4a05228b9 100755 --- a/WiFi/examples/wifi_example/wifi_example.pde +++ b/WiFi/examples/wifi_example/wifi_example.pde @@ -114,12 +114,12 @@ void setup() { Serial.begin(9600); Serial.println("*** Start WiFi example ***"); - + WiFi.begin(); //scanNetworks(); delay(3000); - int _status = startWiFi(); - + //int _status = startWiFi(); +/* if ( _status == WL_CONNECTED) { Serial.println("Wifi Connected!"); @@ -129,17 +129,53 @@ void setup() printCurrNet(); scanNetworks(); - /* + Serial.println("Starting server..."); server.begin(); delay(1000); - */ + } + */ } void loop() { + static boolean free = false; + +// if (free) Serial.println(WiFi.test(),10); + if (free) WiFi.test(); + + byte c = Serial.read(); + if (c!=255) + { + switch (c) + { + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + { + int i = 0; + for (;i<(c-0x30);++i) + { + int num = WiFi.test(); + //Serial.println(num,10); + } + break; + } + case 'c': + free = true; + break; + case 's': + free = false; + break; + + } + } /* + Client client = server.available(); static uint8_t count = 0; Serial.println("Retry connect..."); status = WiFi.begin(ssid); diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index b6228d4ed..ef3ba8c22 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -13,8 +13,6 @@ #include #include -#include "HardwareSerial.h" - #define INFO_0 1 #define INFO_1 2 #define INFO_2 4 diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index bd7dd16a1..8688b9b56 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -12,7 +12,8 @@ extern "C" { #define SPICLOCK 13//sck #define SLAVESELECT 2//ss #define SLAVEREADY 3 - + +#define DELAY_100NS asm volatile("nop") void SpiDrv::begin() { @@ -60,6 +61,16 @@ void SpiDrv::spiSlaveDeselect() digitalWrite(SLAVESELECT,HIGH); } +void delaySpi() +{ + int i = 0; + const int DELAY = 1000; + for (;i #include "wifi_spi.h" -#define WAIT_CHAR_DELAY 100 -#define TIMEOUT_CHAR_DELAY 1 //10 -#define TIMEOUT_READY_SLAVE 1000 -#define SPI_TX_DELAY 1 -#define SPI_START_CMD_DELAY 10 - +#define SPI_START_CMD_DELAY 10 #define NO_LAST_PARAM 0 #define LAST_PARAM 1 @@ -25,7 +20,9 @@ class SpiDrv { private: - static bool waitSlaveReady(); + //static bool waitSlaveReady(); + static void waitForSlaveSign(); + static void getParam(uint8_t* param); public: static void begin(); diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index a979659a6..a8dffe351 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -351,4 +351,25 @@ int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) return networkRssi; } +uint8_t WiFiDrv::testCmd() +{ + WAIT_FOR_SLAVE_SELECT(); + + // Send Command + SpiDrv::sendCmd(TEST_CMD, PARAM_NUMS_0); + + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + SpiDrv::waitResponseCmd(TEST_CMD, PARAM_NUMS_1, &_data, &_dataLen); + + SpiDrv::spiSlaveDeselect(); + + return _data; +} + + WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 74af7837b..661e0e45f 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -62,6 +62,7 @@ public: static uint8_t getEncTypeNetowrks(uint8_t networkItem); + static uint8_t testCmd(); }; extern WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 4f0b30855..576c5fb11 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -23,6 +23,7 @@ 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, From 0b8d3f6421332e393335cc05b7a815f86630fc9a Mon Sep 17 00:00:00 2001 From: mlafauci Date: Wed, 18 May 2011 15:37:29 +0200 Subject: [PATCH 12/98] Fix compilation errors on examples and adapt API to new Ethernet functions --- WiFi/Client.cpp | 33 ++++- WiFi/Client.h | 22 +-- WiFi/Server.cpp | 10 +- WiFi/Server.h | 4 +- WiFi/WiFi.cpp | 7 +- .../PachubeClientString.ino | 138 ++++++++++++++++++ .../{WebClient.pde => WebClient.ino} | 5 +- .../{WebServer.pde => WebServer.ino} | 0 ...er_example.pde => wifi_Server_example.ino} | 0 ...le_WEP.pde => wifi_Server_example_WEP.ino} | 0 ...le_WPA.pde => wifi_Server_example_WPA.ino} | 0 ...i_WPA_example.pde => wifi_WPA_example.ino} | 0 WiFi/utility/server_drv.cpp | 2 +- WiFi/utility/spi_drv.cpp | 2 +- WiFi/utility/wifi_drv.cpp | 17 ++- WiFi/utility/wifi_drv.h | 7 +- 16 files changed, 205 insertions(+), 42 deletions(-) create mode 100644 WiFi/examples/PachubeClientString/PachubeClientString.ino rename WiFi/examples/WebClient/{WebClient.pde => WebClient.ino} (94%) rename WiFi/examples/WebServer/{WebServer.pde => WebServer.ino} (100%) rename WiFi/examples/wifi_Server_example/{wifi_Server_example.pde => wifi_Server_example.ino} (100%) rename WiFi/examples/wifi_Server_example_WEP/{wifi_Server_example_WEP.pde => wifi_Server_example_WEP.ino} (100%) rename WiFi/examples/wifi_Server_example_WPA/{wifi_Server_example_WPA.pde => wifi_Server_example_WPA.ino} (100%) rename WiFi/examples/wifi_WPA_example/{wifi_WPA_example.pde => wifi_WPA_example.ino} (100%) diff --git a/WiFi/Client.cpp b/WiFi/Client.cpp index 82c2b3e7a..afdb31d7c 100755 --- a/WiFi/Client.cpp +++ b/WiFi/Client.cpp @@ -6,8 +6,6 @@ extern "C" { #include "utility/debug.h" } -#include "WProgram.h" - #include "WiFi.h" #include "Client.h" #include "Server.h" @@ -15,18 +13,36 @@ extern "C" { uint16_t Client::_srcport = 1024; +Client::Client() : _sock(MAX_SOCK_NUM) { +} + Client::Client(uint8_t sock) : _sock(sock) { } -Client::Client(IPAddress& ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM) { +int Client::connect(const char* host, uint16_t port) { + /* TODO Add DNS wifi spi function to resolve DNS */ +#if 0 + // Look up the host first + int ret = 0; + DNSClient dns; + IPAddress remote_addr; + + dns.begin(Ethernet.dnsServerIP()); + ret = dns.getHostByName(host, remote_addr); + if (ret == 1) { + return connect(remote_addr, port); + } else { + return ret; + } +#endif } -uint8_t Client::connect() { +int Client::connect(IPAddress ip, uint16_t port) { _sock = getFirstSocket(); if (_sock != NO_SOCKET_AVAIL) { - ServerDrv::StartClient(uint32_t(_ip), _port, _sock); - WiFiClass::_state[_sock] = _socket; + ServerDrv::StartClient(uint32_t(ip), port, _sock); + WiFiClass::_state[_sock] = _sock; }else{ return 0; } @@ -86,6 +102,11 @@ int Client::read(uint8_t* buf, size_t size) { return 0; } +int Client::peek() { + //TODO to be implemented + return 0; +} + void Client::flush() { while (available()) read(); diff --git a/WiFi/Client.h b/WiFi/Client.h index 277476230..c812089c1 100755 --- a/WiFi/Client.h +++ b/WiFi/Client.h @@ -1,23 +1,25 @@ -#ifndef Client_h -#define Client_h - -#include "IPAddress.h" +#ifndef client_h +#define client_h +#include "Arduino.h" #include "Print.h" -class Client : public Print { +class Client : public Stream { + public: + Client(); Client(uint8_t sock); - Client(IPAddress& ip, uint16_t port); uint8_t status(); - uint8_t connect(); + int connect(IPAddress ip, uint16_t port); + int connect(const char *host, uint16_t port); virtual void write(uint8_t); virtual void write(const char *str); virtual void write(const uint8_t *buf, size_t size); virtual int available(); virtual int read(); - virtual int read(uint8_t* buf, size_t size); - void flush(); + virtual int read(uint8_t *buf, size_t size); + virtual int peek(); + virtual void flush(); void stop(); uint8_t connected(); operator bool(); @@ -27,8 +29,6 @@ public: private: static uint16_t _srcport; uint8_t _sock; //not used - IPAddress _ip; - uint16_t _port; uint16_t _socket; uint8_t getFirstSocket(); diff --git a/WiFi/Server.cpp b/WiFi/Server.cpp index 695c55d25..a8e236029 100755 --- a/WiFi/Server.cpp +++ b/WiFi/Server.cpp @@ -1,9 +1,11 @@ #include -#include "Server.h" -#include "Client.h" -#include "WiFi.h" #include "server_drv.h" -#include "wiring.h" + +#include "WiFi.h" +#include "Client.h" +#include "Server.h" + + Server::Server(uint16_t port) { diff --git a/WiFi/Server.h b/WiFi/Server.h index cf586c6ad..34124af09 100755 --- a/WiFi/Server.h +++ b/WiFi/Server.h @@ -1,5 +1,5 @@ -#ifndef Server_h -#define Server_h +#ifndef server_h +#define server_h extern "C" { #include "utility/wl_definitions.h" diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 337c0bb93..5e766e0f5 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -1,6 +1,5 @@ #include "wifi_drv.h" #include "WiFi.h" -#include "wiring.h" #define _DEBUG_ @@ -125,21 +124,21 @@ uint8_t* WiFiClass::macAddress(uint8_t* mac) IPAddress WiFiClass::localIp() { IPAddress ret; - WiFiDrv::getIpAddress(ret.raw_address()); + WiFiDrv::getIpAddress(ret); return ret; } IPAddress WiFiClass::subnetMask() { IPAddress ret; - WiFiDrv::getSubnetMask(ret.raw_address()); + WiFiDrv::getSubnetMask(ret); return ret; } IPAddress WiFiClass::gatewayIP() { IPAddress ret; - WiFiDrv::getGatewayIP(ret.raw_address()); + WiFiDrv::getGatewayIP(ret); return ret; } diff --git a/WiFi/examples/PachubeClientString/PachubeClientString.ino b/WiFi/examples/PachubeClientString/PachubeClientString.ino new file mode 100644 index 000000000..9a38ec15a --- /dev/null +++ b/WiFi/examples/PachubeClientString/PachubeClientString.ino @@ -0,0 +1,138 @@ +/* + Pachube sensor client with Strings + + This sketch connects an analog sensor to Pachube (http://www.pachube.com) + using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or + the Adafruit Ethernet shield, either one will work, as long as it's got + a Wiznet Ethernet module on board. + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * Analog sensor attached to analog in 0 + * Ethernet shield attached to pins 10, 11, 12, 13 + + created 15 March 2010 + updated 4 Sep 2010 + by Tom Igoe + + This code is in the public domain. + + */ + +#include +#include + +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; + +// The address of the server you want to connect to (pachube.com): +IPAddress server(173,203,98,29); + +// initialize the library instance: +Client client(server, 80); + +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 int postingInterval = 10000; //delay between updates to Pachube.com + +int startWiFiWpa() +{ + Serial.println("\nSetup WiFi Wpa..."); + //strcpy(ssid, "AndroidAP9647"); + strcpy(ssid, "Cariddi"); + Serial.print("SSID: "); + Serial.println(ssid); + const char *pass = "1234567890"; + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) + { + Serial.println("Connection Failed"); + } + return status; +} + + + +void setup() { + // start the WiFi connection and the server: + Serial.begin(9600); + Serial.println("*** Start WebClient WiFi example ***"); + + int _status = startWiFiWpa(); + if ( _status == WL_CONNECTED) + { + Serial.println("\nWiFi Connected."); + } + + delay(1000); +} + +void loop() { + // read the analog sensor: + int sensorReading = analogRead(A0); + // convert the data to a String to send it: + String dataString = String(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 += ","; + dataString += String(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()) { + Serial.println("connecting..."); + // send the HTTP PUT request. + // fill in your feed address here: + client.print("PUT /api/http://api.pachube.com/v2/feeds/24196.csv HTTP/1.1\n"); + client.print("Host: www.pachube.com\n"); + // fill in your Pachube API key here: + client.print("X-PachubeApiKey: gw0L2A-J5ACRGQccX59tCYt0IEzyecr-SoiuC47U1-8\n"); + client.print("Content-Length: "); + client.println(thisData.length(), DEC); + + // last pieces of the HTTP PUT request: + client.print("Content-Type: text/csv\n"); + client.println("Connection: close\n"); + + // here's the actual content of the PUT request: + client.println(thisData); + + // note the time that the connection was made: + lastConnectionTime = millis(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + } +} diff --git a/WiFi/examples/WebClient/WebClient.pde b/WiFi/examples/WebClient/WebClient.ino similarity index 94% rename from WiFi/examples/WebClient/WebClient.pde rename to WiFi/examples/WebClient/WebClient.ino index feabd0b38..8959d7e0b 100644 --- a/WiFi/examples/WebClient/WebClient.pde +++ b/WiFi/examples/WebClient/WebClient.ino @@ -19,12 +19,11 @@ char ssid[32] = { 0 }; int status = WL_IDLE_STATUS; IPAddress server(74,125,232,115); // Google -//byte server[] = { 173,194,33,104 }; // Google // 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): -Client client(server, 80); +Client client; int startWiFiWpa() { @@ -53,7 +52,7 @@ void setup() { { Serial.println("\nStarting connection..."); // if you get a connection, report back via serial: - if (client.connect()) { + if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.0"); diff --git a/WiFi/examples/WebServer/WebServer.pde b/WiFi/examples/WebServer/WebServer.ino similarity index 100% rename from WiFi/examples/WebServer/WebServer.pde rename to WiFi/examples/WebServer/WebServer.ino diff --git a/WiFi/examples/wifi_Server_example/wifi_Server_example.pde b/WiFi/examples/wifi_Server_example/wifi_Server_example.ino similarity index 100% rename from WiFi/examples/wifi_Server_example/wifi_Server_example.pde rename to WiFi/examples/wifi_Server_example/wifi_Server_example.ino diff --git a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino similarity index 100% rename from WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.pde rename to WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino similarity index 100% rename from WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.pde rename to WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino diff --git a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino similarity index 100% rename from WiFi/examples/wifi_WPA_example/wifi_WPA_example.pde rename to WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index eee1be243..005ba4c76 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -1,6 +1,6 @@ #include "server_drv.h" -#include "WProgram.h" +#include "Arduino.h" #include "spi_drv.h" #define _DEBUG_ diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index 8688b9b56..43c5dd251 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -1,5 +1,5 @@ -#include "WProgram.h" +#include "Arduino.h" #include "spi_drv.h" #include "pins_arduino.h" #define _DEBUG_ diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index a8dffe351..04c44c257 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -2,7 +2,7 @@ #include #include -#include "WProgram.h" +#include "Arduino.h" #include "spi_drv.h" #include "wifi_drv.h" @@ -189,22 +189,25 @@ uint8_t* WiFiDrv::getMacAddress() return _mac; } -void WiFiDrv::getIpAddress(uint8_t *ip) +void WiFiDrv::getIpAddress(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); - memcpy(ip, _localIp, WL_IPV4_LENGTH); + ip = _localIp; + //memcpy(ip, _localIp, WL_IPV4_LENGTH); } - void WiFiDrv::getSubnetMask(uint8_t *ip) + void WiFiDrv::getSubnetMask(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); - memcpy(ip, _subnetMask, WL_IPV4_LENGTH); + ip = _subnetMask; + //memcpy(ip, _subnetMask, WL_IPV4_LENGTH); } - void WiFiDrv::getGatewayIP(uint8_t *ip) + void WiFiDrv::getGatewayIP(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); - memcpy(ip, _gatewayIp, WL_IPV4_LENGTH); + ip = _gatewayIp; + //memcpy(ip, _gatewayIp, WL_IPV4_LENGTH); } char* WiFiDrv::getCurrentSSID() diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 661e0e45f..80cc2d8a0 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -3,6 +3,7 @@ #include #include "wifi_spi.h" +#include "IPAddress.h" #define KEY_IDX_LEN 1 #define WL_DELAY_START_CONNECTION 5000 @@ -40,11 +41,11 @@ public: static uint8_t* getMacAddress(); - static void getIpAddress(uint8_t *ip); + static void getIpAddress(IPAddress& ip); - static void getSubnetMask(uint8_t *ip); + static void getSubnetMask(IPAddress& ip); - static void getGatewayIP(uint8_t *ip); + static void getGatewayIP(IPAddress& ip); static char* getCurrentSSID(); From 444fa5eba57b5f29610461018ab2152fa5a00902 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sat, 21 May 2011 10:35:02 -0400 Subject: [PATCH 13/98] Added new examples --- .../WifiChatServer/WifiChatServer.ino | 99 ++++++++++++++ .../WifiTwitterClient/WifiTwitterClient.ino | 129 ++++++++++++++++++ WiFi/examples/WifiWebClient/WifiWebClient.ino | 78 +++++++++++ 3 files changed, 306 insertions(+) create mode 100644 WiFi/examples/WifiChatServer/WifiChatServer.ino create mode 100644 WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino create mode 100644 WiFi/examples/WifiWebClient/WifiWebClient.ino diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino new file mode 100644 index 000000000..06d3973ee --- /dev/null +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -0,0 +1,99 @@ +/* + Wifi Chat Server + + A simple server that distributes any incoming messages to all + connected clients. To use telnet to your device's IP address and type. + You can see the client's input in the serial monitor as well. + + This example obtains an IP address from the Wifi router and + reports it. + + Circuit: + * WiFi shield attached + + created 21 May 2011 + by Tom Igoe + + */ + +#include +#include +#include + +char ssid[] = "tigoenet"; +char pass[] = "m30w-m30w"; +int status = WL_IDLE_STATUS; +// telnet defaults to port 23 +Server server(23); +boolean gotAMessage = false; // whether or not you got a message from the client yet + +void setup() { + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status == WL_CONNECTED) { + // start listening for clients + server.begin(); + Serial.println("Wifi shield is idle"); + // report the IP address: + IPAddress myIPAddress = WiFi.localIp(); + Serial.print("My IP Address: "); + Serial.print(WiFi.localIp()[3]); + Serial.print("."); + Serial.print(WiFi.localIp()[2]); + Serial.print("."); + Serial.print(WiFi.localIp()[1]); + Serial.print("."); + Serial.println(WiFi.localIp()[0]); + } else { + // if you didn't get a wifi connection, report what happened: + switch (status) { + case WL_IDLE_STATUS: + Serial.println("Wifi connection succeeded"); + break; + case WL_NO_SSID_AVAIL: + Serial.println("No Wifi network available"); + break; + case WL_CONNECT_FAILED: + Serial.println("Wifi connection failed"); + break; + case WL_CONNECTION_LOST: + Serial.println("Wifi connection lost"); + break; + case WL_DISCONNECTED: + Serial.println("Wifi disconnected"); + break; + } + } +} + +void loop() { + // wait for a new client: + Client client = server.available(); + + // when the client sends the first byte, say hello: + if (client) { + if (!gotAMessage) { + Serial.println("I have a new client"); + client.println("Hello, client!"); + gotAMessage = true; + } + + // 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.print(thisChar); + } +} + + + + + + + diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino new file mode 100644 index 000000000..8dc8b1190 --- /dev/null +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -0,0 +1,129 @@ +/* + Twitter Client with Strings + + This sketch connects to Twitter using an Ethernet shield. It parses the XML + returned, and looks for this is a tweet + + You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield, + either one will work, as long as it's got a Wiznet Ethernet module on board. + + This example uses the DHCP routines in the Ethernet library which is part of the + Arduino core from version 1.0 beta 1 + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * Ethernet shield attached to pins 10, 11, 12, 13 + + created 21 May 2011 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include +#include + +char ssid[] = "tigoenet"; +char pass[] = "m30w-m30w"; +int status = WL_IDLE_STATUS; + +// initialize the library instance: +Client client; + +const int requestInterval = 60000; // delay between requests + +char serverName[] = "api.twitter.com"; // twitter URL + +boolean requested; // whether you've made a request since connecting +long lastAttemptTime = 0; // last time you connected to the server, in milliseconds + +String currentLine = ""; // string to hold the text from server +String tweet = ""; // string to hold the tweet +boolean readingTweet = false; // if you're currently reading the tweet + +void setup() { + // reserve space for the strings: + currentLine.reserve(256); + tweet.reserve(150); + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + connectToServer(); + } +} + + + +void loop() +{ + if (client.connected()) { + if (client.available()) { + // read incoming bytes: + char inChar = client.read(); +Serial.write(inChar); + + // add incoming byte to end of line: + currentLine += inChar; + + // if you get a newline, clear the line: + if (inChar == '\n') { + currentLine = ""; + } + // if the current line ends with , it will + // be followed by the tweet: + if ( currentLine.endsWith("")) { + // tweet is beginning. Clear the tweet string: + readingTweet = true; + tweet = ""; + } + // if you're currently reading the bytes of a tweet, + // add them to the tweet String: + if (readingTweet) { + if (inChar != '<') { + tweet += inChar; + } + else { + // if you got a "<" character, + // you've reached the end of the tweet: + readingTweet = false; + Serial.println(tweet); + // close the connection to the server: + client.stop(); + } + } + } + } + else if (millis() - lastAttemptTime > requestInterval) { + // if you're not connected, and two minutes have passed since + // your last connection, then attempt to connect again: + connectToServer(); + } +} + +void connectToServer() { + // attempt to connect, and wait a millisecond: + Serial.println("connecting to server..."); + if (client.connect(serverName, 80)) { + Serial.println("making HTTP request..."); + // make HTTP GET request to twitter: + client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1"); + client.println("HOST: api.twitter.com"); + client.println(); + } + // note the time of this connect attempt: + lastAttemptTime = millis(); +} + diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino new file mode 100644 index 000000000..566a0b454 --- /dev/null +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -0,0 +1,78 @@ +#include + +/* + Web client + + This sketch connects to a website (http://www.google.com) + using a WiFi shield. + + Circuit: + * WiFi shield attached + * Analog inputs attached to pins A0 through A5 (optional) + + created 13 July 2010 + by Domenico La Fauci + modified 21 May 2011 + by Tom Igoe + */ + + +#include +#include +#include + +char ssid[] = "tigoenet"; +char pass[] = "m30w-m30w"; +int status = WL_IDLE_STATUS; +IPAddress server(74,125,115,105); // Google + +// 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): +Client client; + +void setup() { + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + Serial.println("\nStarting connection..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.0"); + client.println(); + } + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + + // do nothing forevermore: + for(;;) + ; + } +} + + From e49786b423a9d5009f8783451becc45f68eff6cd Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sat, 21 May 2011 10:37:50 -0400 Subject: [PATCH 14/98] updated new wifi examples --- WiFi/examples/WifiChatServer/WifiChatServer.ino | 4 ++-- WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino | 4 ++-- WiFi/examples/WifiWebClient/WifiWebClient.ino | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 06d3973ee..0495aefbd 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -20,8 +20,8 @@ #include #include -char ssid[] = "tigoenet"; -char pass[] = "m30w-m30w"; +char ssid[] = "yourWifiNetwork"; +char pass[] = "seekrit-password"; int status = WL_IDLE_STATUS; // telnet defaults to port 23 Server server(23); diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 8dc8b1190..9792121ae 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -26,8 +26,8 @@ #include #include -char ssid[] = "tigoenet"; -char pass[] = "m30w-m30w"; +char ssid[] = "yourWifiNetwork"; +char pass[] = "seekrit-password"; int status = WL_IDLE_STATUS; // initialize the library instance: diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 566a0b454..d1883c2f9 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -21,8 +21,8 @@ #include #include -char ssid[] = "tigoenet"; -char pass[] = "m30w-m30w"; +char ssid[] = "yourWifiNetwork"; +char pass[] = "seekrit-password"; int status = WL_IDLE_STATUS; IPAddress server(74,125,115,105); // Google From aa5af88ea6e507f97d0baf71d46c346fb03c90b3 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Thu, 2 Jun 2011 16:44:29 +0200 Subject: [PATCH 15/98] Del wifi_WEP_example.pde --- .../wifi_WEP_example/wifi_WEP_example.pde | 143 ------------------ 1 file changed, 143 deletions(-) delete mode 100644 WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde deleted file mode 100644 index 9208e2276..000000000 --- a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.pde +++ /dev/null @@ -1,143 +0,0 @@ -/* - WiFi Server example - - A simple connection with WiFi AP with Wireless Security - information try to access with WPA or WEP security keys - A simple server is setup to exchange data. - - created 13 July 2010 - by Domenico La Fauci - */ -#include -#include - -byte mac[6] = { 0 }; -IPAddress ip; -IPAddress gateway; -IPAddress subnet; -byte dataBuf[80] = { 0 }; -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; -#define MAX_NUM_SSID 10 -char ssidList[MAX_NUM_SSID][32] = { {0} }; - - -Server server(23); - -void printIpData() -{ - ip = WiFi.localIp(); - - Serial.print("IP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); -} - -void printCurrNet() -{ - char* ssid = WiFi.SSID(); - Serial.print("SSID: "); - Serial.println(ssid); - - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],16);Serial.print(":"); - Serial.print(bssid[4],16);Serial.print(":"); - Serial.print(bssid[3],16);Serial.print(":"); - Serial.print(bssid[2],16);Serial.print(":"); - Serial.print(bssid[1],16);Serial.print(":"); - Serial.println(bssid[0],16); - - int32_t rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi,10); - - uint8_t enct = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(enct,16); -} - -void scanNetworks() -{ - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; - Serial.print("SSID List:"); - Serial.println(numSsid, 10); - for (int i = 0; i Date: Mon, 6 Jun 2011 01:27:45 +0200 Subject: [PATCH 16/98] Changed localIP function name --- WiFi/WiFi.cpp | 2 +- WiFi/WiFi.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 5e766e0f5..a7ccc2047 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -121,7 +121,7 @@ uint8_t* WiFiClass::macAddress(uint8_t* mac) return mac; } -IPAddress WiFiClass::localIp() +IPAddress WiFiClass::localIP() { IPAddress ret; WiFiDrv::getIpAddress(ret); diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 738f6f256..27380b9d2 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -51,7 +51,7 @@ public: uint8_t* macAddress(uint8_t* mac); //Get the DHCP information related to IP - IPAddress localIp(); + IPAddress localIP(); //Get the DHCP information related to subnetMask IPAddress subnetMask(); From 65d27780876a2f3f97aca847054db3c029bc65d5 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Mon, 6 Jun 2011 01:28:16 +0200 Subject: [PATCH 17/98] delete some debug print --- WiFi/utility/server_drv.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index 005ba4c76..b403a31b3 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -37,7 +37,6 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock) { WAIT_FOR_SLAVE_SELECT(); - INFO2(ipAddress,port); // Send Command SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_3); SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress)); From 0b2d19663c990fc0c4e09b812a93dea6625235ce Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 5 Jun 2011 21:59:07 -0400 Subject: [PATCH 18/98] Added new examples in Arduino style. Added IPAddress.cpp and .h --- .../wifi_WEP_example/wifi_WEP_example.ino | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino new file mode 100644 index 000000000..9208e2276 --- /dev/null +++ b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino @@ -0,0 +1,143 @@ +/* + WiFi Server example + + A simple connection with WiFi AP with Wireless Security + information try to access with WPA or WEP security keys + A simple server is setup to exchange data. + + created 13 July 2010 + by Domenico La Fauci + */ +#include +#include + +byte mac[6] = { 0 }; +IPAddress ip; +IPAddress gateway; +IPAddress subnet; +byte dataBuf[80] = { 0 }; +char ssid[32] = { 0 }; +int status = WL_IDLE_STATUS; +#define MAX_NUM_SSID 10 +char ssidList[MAX_NUM_SSID][32] = { {0} }; + + +Server server(23); + +void printIpData() +{ + ip = WiFi.localIp(); + + Serial.print("IP: "); + Serial.print(ip[3],10);Serial.print("."); + Serial.print(ip[2],10);Serial.print("."); + Serial.print(ip[1],10);Serial.print("."); + Serial.println(ip[0],10); + + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3],10);Serial.print("."); + Serial.print(subnet[2],10);Serial.print("."); + Serial.print(subnet[1],10);Serial.print("."); + Serial.println(subnet[0],10); + + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3],10);Serial.print("."); + Serial.print(gateway[2],10);Serial.print("."); + Serial.print(gateway[1],10);Serial.print("."); + Serial.println(gateway[0],10); + + WiFi.macAddress(mac); + Serial.print("MAC: "); + Serial.print(mac[5],16);Serial.print(":"); + Serial.print(mac[4],16);Serial.print(":"); + Serial.print(mac[3],16);Serial.print(":"); + Serial.print(mac[2],16);Serial.print(":"); + Serial.print(mac[1],16);Serial.print(":"); + Serial.println(mac[0],16); +} + +void printCurrNet() +{ + char* ssid = WiFi.SSID(); + Serial.print("SSID: "); + Serial.println(ssid); + + byte bssid[6]; + WiFi.BSSID(bssid); + Serial.print("BSSID: "); + Serial.print(bssid[5],16);Serial.print(":"); + Serial.print(bssid[4],16);Serial.print(":"); + Serial.print(bssid[3],16);Serial.print(":"); + Serial.print(bssid[2],16);Serial.print(":"); + Serial.print(bssid[1],16);Serial.print(":"); + Serial.println(bssid[0],16); + + int32_t rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi,10); + + uint8_t enct = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(enct,16); +} + +void scanNetworks() +{ + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; + Serial.print("SSID List:"); + Serial.println(numSsid, 10); + for (int i = 0; i Date: Sun, 5 Jun 2011 22:02:42 -0400 Subject: [PATCH 19/98] Added new examples in Arduino style, added IPAddress files. For real this time. --- WiFi/IPAddress.cpp | 44 +++++ WiFi/IPAddress.h | 72 ++++++++ .../Wifi_Open_RSSI/Wifi_Open_RSSI.ino | 50 ++++++ .../Wifi_Open_ScanNetworks.ino | 155 ++++++++++++++++ .../Wifi_WEP_ScanNetworks.ino | 164 +++++++++++++++++ .../Wifi_WPA_ChatServer.ino | 79 +++++++++ .../Wifi_WPA_PachubeClientString.ino | 140 +++++++++++++++ .../Wifi_WPA_ScanNetworks.ino | 167 ++++++++++++++++++ .../Wifi_WPA_TwitterClient.ino | 121 +++++++++++++ .../Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino | 77 ++++++++ .../Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino | 100 +++++++++++ 11 files changed, 1169 insertions(+) create mode 100644 WiFi/IPAddress.cpp create mode 100644 WiFi/IPAddress.h create mode 100644 WiFi/examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino create mode 100644 WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino create mode 100644 WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino create mode 100644 WiFi/examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino create mode 100644 WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino create mode 100644 WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino create mode 100644 WiFi/examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino create mode 100644 WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino create mode 100644 WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino diff --git a/WiFi/IPAddress.cpp b/WiFi/IPAddress.cpp new file mode 100644 index 000000000..610ff4c53 --- /dev/null +++ b/WiFi/IPAddress.cpp @@ -0,0 +1,44 @@ + +#include +#include + +IPAddress::IPAddress() +{ + memset(_address, 0, sizeof(_address)); +} + +IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) +{ + _address[0] = first_octet; + _address[1] = second_octet; + _address[2] = third_octet; + _address[3] = fourth_octet; +} + +IPAddress::IPAddress(uint32_t address) +{ + memcpy(_address, &address, sizeof(_address)); +} + +IPAddress::IPAddress(const uint8_t *address) +{ + memcpy(_address, address, sizeof(_address)); +} + +IPAddress& IPAddress::operator=(const uint8_t *address) +{ + memcpy(_address, address, sizeof(_address)); + return *this; +} + +IPAddress& IPAddress::operator=(uint32_t address) +{ + memcpy(_address, (const uint8_t *)&address, sizeof(_address)); + return *this; +} + +bool IPAddress::operator==(const uint8_t* addr) +{ + return memcmp(addr, _address, sizeof(_address)) == 0; +} + diff --git a/WiFi/IPAddress.h b/WiFi/IPAddress.h new file mode 100644 index 000000000..487e420bd --- /dev/null +++ b/WiFi/IPAddress.h @@ -0,0 +1,72 @@ +/* + * + * MIT License: + * Copyright (c) 2011 Adrian McEwen + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * adrianm@mcqn.com 1/1/2011 + */ + +#ifndef IPAddress_h +#define IPAddress_h + +// A class to make it easier to handle and pass around IP addresses + +class IPAddress { +private: + uint8_t _address[4]; // IPv4 address + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() { return _address; }; + +public: + // Constructors + IPAddress(); + IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); + IPAddress(uint32_t address); + IPAddress(const uint8_t *address); + + // Overloaded cast operator to allow IPAddress objects to be used where a pointer + // to a four-byte uint8_t array is expected + operator uint32_t() { return *((uint32_t*)_address); }; + bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; + bool operator==(const uint8_t* addr); + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const { return _address[index]; }; + uint8_t& operator[](int index) { return _address[index]; }; + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(uint32_t address); + + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; +}; + +const IPAddress INADDR_NONE(0,0,0,0); + + +#endif diff --git a/WiFi/examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino b/WiFi/examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino new file mode 100644 index 000000000..e0e8ee103 --- /dev/null +++ b/WiFi/examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino @@ -0,0 +1,50 @@ +/* + + Open connection using the WiFi shield. Attempts to connect + and prints out the signal strength. + + Circuit: + * WiFi shield attached + created 5 June 2011 + by Tom Igoe + */ +#include +#include + +char ssid[] = "yourNetwork"; // the name of your network +int status = WL_IDLE_STATUS; // the Wifi radio's status + + +byte mac[6]; // the MAC address of your Wifi shield +IPAddress ip; // the IP address of your shield + +void setup() { + // initialize serial: + Serial.begin(9600); + + // attempt to connect using WEP encryption: + Serial.println("Attempting to connect to open network..."); + status = WiFi.begin(ssid); + + Serial.print("SSID: "); + Serial.println(ssid); + + // if you're not connected, stop here: + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } +} + +void loop() { + // if you're connected, print out the signal strength: + if ( status != WL_CONNECTED) { + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("RSSI:"); + Serial.println(rssi); + delay(250); + } +} + + diff --git a/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino b/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino new file mode 100644 index 000000000..5a85b98a6 --- /dev/null +++ b/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino @@ -0,0 +1,155 @@ +/* + + Open connection using the WiFi shield. Attempts to connect + and prints out info about the network + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by Domenico La Fauci + modified 5 June 2011 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "yourNetwork"; // the name of your network +int status = WL_IDLE_STATUS; // the Wifi radio's status + + +byte mac[6]; // the MAC address of your Wifi shield +IPAddress ip; // the IP address of your shield +IPAddress gateway; // the router's address +IPAddress subnet; // the subnet mask + +void setup() { + // initialize serial: + Serial.begin(9600); + + // attempt to connect using WEP encryption: + Serial.println("Attempting to connect to open network..."); + status = WiFi.begin(ssid); + + Serial.print("SSID: "); + Serial.println(ssid); + + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); + + // if you're not connected, stop here: + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + // if you are connected, print out info about the connection: + else { + printIpData(); + printCurrentNet(); + } +} + +void loop() { + // do nothing +} + +void printIpData() { + // print your WiFi shield's IP address: + ip = WiFi.localIP(); + Serial.print("IP: "); + Serial.print(ip[0]); + Serial.print("."); + Serial.print(ip[1]); + Serial.print("."); + Serial.print(ip[2]); + Serial.print("."); + Serial.println(ip[3]); + + // print your subnet mask: + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[0]); + Serial.print("."); + Serial.print(subnet[1]); + Serial.print("."); + Serial.print(subnet[2]); + Serial.print("."); + Serial.println(subnet[3]); + + // print your gateway address: + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[0]); + Serial.print("."); + Serial.print(gateway[1]); + Serial.print("."); + Serial.print(gateway[2]); + Serial.print("."); + Serial.println(gateway[3]); + + // 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 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("RSSI:"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); +} + +void scanNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + + // print the list of networks seen: + Serial.print("SSID List:"); + Serial.println(numSsid); + // print the network number and name for each network found: + for (int thisNet = 0; thisNet +#include + +char ssid[] = "yourNetwork"; // the name of your network +char keyIndex = 0; // WEP networks can have multiple keys. +char key[] = "BAE4B2EDB9171646AA0DC8ED19"; // the key you're using to connect +int status = WL_IDLE_STATUS; // the Wifi radio's status + + +byte mac[6]; // the MAC address of your Wifi shield +IPAddress ip; // the IP address of your shield +IPAddress gateway; // the router's address +IPAddress subnet; // the subnet mask + +void setup() { + // initialize serial: + Serial.begin(9600); + + // attempt to connect using WEP encryption: + Serial.println("Attempting to connect to WEP-128 network..."); + status = WiFi.begin(ssid, keyIndex, key); + + + Serial.print("SSID: "); + Serial.println(ssid); + + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); + + // if you're not connected, stop here: + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + // if you are connected, print out info about the connection: + else { + printIpData(); + printCurrentNet(); + } +} + +void loop() { + // do nothing +} + +void printIpData() { + // print your WiFi shield's IP address: + ip = WiFi.localIp(); + Serial.print("IP: "); + Serial.print(ip[3]); + Serial.print("."); + Serial.print(ip[2]); + Serial.print("."); + Serial.print(ip[1]); + Serial.print("."); + Serial.println(ip[0]); + + // print your subnet mask: + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[3]); + Serial.print("."); + Serial.print(subnet[2]); + Serial.print("."); + Serial.print(subnet[1]); + Serial.print("."); + Serial.println(subnet[0]); + + // print your gateway address: + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[3]); + Serial.print("."); + Serial.print(gateway[2]); + Serial.print("."); + Serial.print(gateway[1]); + Serial.print("."); + Serial.println(gateway[0]); + + // 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 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("RSSI:"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); +} + +void scanNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + + // print the list of networks seen: + Serial.print("SSID List:"); + Serial.println(numSsid); + // print the network number and name for each network found: + for (int thisNet = 0; thisNet +#include + +char ssid[] = "yourNetwork"; // the name of your network +char pass[] = "secretPassword"; // the WPA2 password for your network +int status = WL_IDLE_STATUS; // the Wifi radio's status + +// telnet defaults to port 23 +Server server(23); + +boolean gotAMessage = false; // whether or not you got a message from the client yet + +void setup() { + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + server.begin(); + Serial.print("Connected to wifi. My address:"); + IPAddress myAddress = WiFi.localIP(); + Serial.print(myAddress[0]); + Serial.print("."); + Serial.print(myAddress[1]); + Serial.print("."); + Serial.print(myAddress[2]); + Serial.print("."); + Serial.println(myAddress[3]); + } +} + +void loop() { + // wait for a new client: + Client client = server.available(); + + // when the client sends the first byte, say hello: + if (client) { + if (!gotAMessage) { + Serial.println("I have a new client"); + client.println("Hello, client!"); + gotAMessage = true; + } + // read the bytes incoming from the client: + char thisChar = client.read(); + // echo the bytes back to the client: + client.write(thisChar); + // echo the bytes to the server as well: + Serial.print(thisChar); + } +} + + + + diff --git a/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino b/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino new file mode 100644 index 000000000..227f43b9b --- /dev/null +++ b/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino @@ -0,0 +1,140 @@ +/* + Pachube sensor client with Strings + + This sketch connects an analog sensor to Pachube (http://www.pachube.com) + using an Arduino WiFi shield. + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * Analog sensors attached to analog in 0 and 1 + * WoFo shield attached to pins 10, 11, 12, 13 + + created 15 March 2010 + updated 5 June 2011 + by Tom Igoe + + This code is in the public domain. + + */ + + +#include +#include + +char ssid[] = "yourNetwork"; +char pass[] = "secretPassword"; +int status = WL_IDLE_STATUS; + +// The address of the server you want to connect to (pachube.com): +IPAddress server(173,203,98,29); + +// initialize the library instance: +Client client; + +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 int postingInterval = 30000; //delay between updates to Pachube.com + +int startWiFiWpa() +{ + Serial.println("\nSetup WiFi Wpa..."); + //strcpy(ssid, "AndroidAP9647"); + strcpy(ssid, "tigoenet"); + Serial.print("SSID: "); + Serial.println(ssid); + const char *pass = "m30w-m30w"; + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) + { + Serial.println("Connection Failed"); + } + return status; +} + + + +void setup() { + // start the WiFi connection and the server: + Serial.begin(9600); + Serial.println("*** Start WebClient WiFi example ***"); + + int _status = startWiFiWpa(); + if ( _status == WL_CONNECTED) + { + Serial.println("\nWiFi Connected."); + } + + delay(1000); +} + + +void loop() { + // read the analog sensor: + int sensorReading = analogRead(A0); + // convert the data to a String to send it: + String dataString = String(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 += ","; + dataString += String(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. + // fill in your feed address here: + client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n"); + client.print("Host: www.pachube.com\n"); + // fill in your Pachube API key here: + client.print("X-PachubeApiKey: YOUR_KEY_HERE\n"); + client.print("Content-Length: "); + client.println(thisData.length(), DEC); + + // last pieces of the HTTP PUT request: + client.print("Content-Type: text/csv\n"); + client.println("Connection: close\n"); + + // here's the actual content of the PUT request: + client.println(thisData); + + // note the time that the connection was made: + lastConnectionTime = millis(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + } +} + diff --git a/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino b/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino new file mode 100644 index 000000000..0c3bbf9fb --- /dev/null +++ b/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino @@ -0,0 +1,167 @@ +/* + + WPA2 conection using the WiFi shield. Attempts to connect + and prints out info about the network + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by Domenico La Fauci + modified 5 June 2011 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "yourNetwork"; // the name of your network +char pass[] = "secretPassword"; // the WPA2 password for your network + +int status = WL_IDLE_STATUS; // the Wifi radio's status + +byte mac[6]; // the MAC address of your Wifi shield +IPAddress ip; // the IP address of your shield +IPAddress gateway; // the router's address +IPAddress subnet; // the subnet mask + +void setup() { + // initialize serial: + Serial.begin(9600); + + // attempt to connect using WEP encryption: +// Serial.println("Attempting to connect to WEP-128 network..."); +// status = WiFi.begin(ssid, keyIndex, key); + + // attempt to connect using WPA2 encryption: + Serial.println("Attempting to connect to WPA network..."); + status = WiFi.begin(ssid, pass); + + + Serial.print("SSID: "); + Serial.println(ssid); + + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); + + // if you're not connected, stop here: + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + // if you are connected, print out info about the connection: + else { + printIpData(); + printCurrentNet(); + } +} + +void loop() { + // do nothing +} + +void printIpData() { + // print your WiFi shield's IP address: + ip = WiFi.localIP(); + Serial.print("IP: "); + Serial.print(ip[0]); + Serial.print("."); + Serial.print(ip[1]); + Serial.print("."); + Serial.print(ip[2]); + Serial.print("."); + Serial.println(ip[3]); + + // print your subnet mask: + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.print(subnet[0]); + Serial.print("."); + Serial.print(subnet[1]); + Serial.print("."); + Serial.print(subnet[2]); + Serial.print("."); + Serial.println(subnet[3]); + + // print your gateway address: + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.print(gateway[0]); + Serial.print("."); + Serial.print(gateway[1]); + Serial.print("."); + Serial.print(gateway[2]); + Serial.print("."); + Serial.println(gateway[3]); + + // 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 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("RSSI:"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); +} + +void scanNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + + // print the list of networks seen: + Serial.print("SSID List:"); + Serial.println(numSsid); + // print the network number and name for each network found: + for (int thisNet = 0; thisNetthis is a tweet + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * WiFi shield attached to pins 10, 11, 12, 13 + + created 21 May 2011 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include + +char ssid[] = "yourNetwork"; +char pass[] = "secretpassword"; +int status = WL_IDLE_STATUS; + +// initialize the library instance: +Client client; + +const int requestInterval = 30*1000; // delay between requests; 30 seconds + +IPAddress server(199,59,149,200); // api.twitter.com + +boolean requested; // whether you've made a request since connecting +long lastAttemptTime = 0; // last time you connected to the server, in milliseconds + +String currentLine = ""; // string to hold the text from server +String tweet = ""; // string to hold the tweet +boolean readingTweet = false; // if you're currently reading the tweet + +void setup() { + // reserve space for the strings: + currentLine.reserve(256); + tweet.reserve(150); + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + connectToServer(); + } +} + + + +void loop() +{ + if (client.connected()) { + if (client.available()) { + // read incoming bytes: + char inChar = client.read(); + + // add incoming byte to end of line: + currentLine += inChar; + + // if you get a newline, clear the line: + if (inChar == '\n') { + currentLine = ""; + } + // if the current line ends with , it will + // be followed by the tweet: + if ( currentLine.endsWith("")) { + // tweet is beginning. Clear the tweet string: + readingTweet = true; + tweet = ""; + } + // if you're currently reading the bytes of a tweet, + // add them to the tweet String: + if (readingTweet) { + if (inChar != '<') { + tweet += inChar; + } + else { + // if you got a "<" character, + // you've reached the end of the tweet: + readingTweet = false; + Serial.println(tweet); + // close the connection to the server: + client.stop(); + } + } + } + } + else if (millis() - lastAttemptTime > requestInterval) { + // if you're not connected, and two minutes have passed since + // your last connection, then attempt to connect again: + connectToServer(); + } +} + +void connectToServer() { + // attempt to connect, and wait a millisecond: + Serial.println("connecting to server..."); + if (client.connect(server, 80)) { + Serial.println("making HTTP request..."); + // make HTTP GET request to twitter: + client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); + client.println("HOST: api.twitter.com"); + client.println(); + } + // note the time of this connect attempt: + lastAttemptTime = millis(); +} + diff --git a/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino b/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino new file mode 100644 index 000000000..5e1bd277b --- /dev/null +++ b/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino @@ -0,0 +1,77 @@ + +/* + Web client + + This sketch connects to a website (http://www.google.com) + using a WiFi shield. + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by Domenico La Fauci + modified 21 May 2011 + by Tom Igoe + */ + + +#include +#include +#include + +char ssid[] = "yourNetwork"; +char pass[] = "secretPassword"; +int status = WL_IDLE_STATUS; +IPAddress server(74,125,115,105); // Google + +// 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): +Client client; + +void setup() { + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + // don't do anything else: + while(true); + } + else { + Serial.println("Connected to wifi"); + Serial.println("\nStarting connection..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.0"); + client.println(); + } + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + + // do nothing forevermore: + for(;;) + ; + } +} + + diff --git a/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino b/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino new file mode 100644 index 000000000..9f121ad0b --- /dev/null +++ b/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino @@ -0,0 +1,100 @@ +/* + Web Server + + A simple web server that shows the value of the analog input pins. + using a WiFi shield. + + Circuit: + * WiFi shield attached + * Analog inputs attached to pins A0 through A5 (optional) + + created 13 July 2010 + by Domenico La Fauci + modified 5 June 2011 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "yourNetwork"; +char pass[] = "secretpassword"; +int status = WL_IDLE_STATUS; + +Server server(80); + +void setup() { + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + server.begin(); + Serial.print("Connected to wifi. My address:"); + IPAddress myAddress = WiFi.localIp(); + Serial.print(myAddress[0]); + Serial.print("."); + Serial.print(myAddress[1]); + Serial.print("."); + Serial.print(myAddress[2]); + Serial.print("."); + Serial.println(myAddress[3]); + } +} + + +void loop() { + // listen for incoming clients + Client client = server.available(); + if (client) { + // an http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + // 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(); + client.println(""); + // output the value of each analog input pin + for (int analogChannel = 0; analogChannel < 6; analogChannel++) { + client.print("analog input "); + client.print(analogChannel); + client.print(" is "); + client.print(analogRead(analogChannel)); + 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(); + } +} + + From f4d452090cac1e08029a4b440cde184a6223c6fe Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 5 Jun 2011 22:06:54 -0400 Subject: [PATCH 20/98] Deleting ole examples in progress --- WiFi/examples/WebClient/WebClient.ino | 88 --------- WiFi/examples/WebServer/WebServer.ino | 97 ---------- .../WifiChatServer/WifiChatServer.ino | 99 ---------- .../WifiTwitterClient/WifiTwitterClient.ino | 129 ------------- WiFi/examples/WifiWebClient/WifiWebClient.ino | 78 -------- .../wifi_WPA_example/wifi_WPA_example.ino | 175 ------------------ 6 files changed, 666 deletions(-) delete mode 100644 WiFi/examples/WebClient/WebClient.ino delete mode 100644 WiFi/examples/WebServer/WebServer.ino delete mode 100644 WiFi/examples/WifiChatServer/WifiChatServer.ino delete mode 100644 WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino delete mode 100644 WiFi/examples/WifiWebClient/WifiWebClient.ino delete mode 100644 WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino diff --git a/WiFi/examples/WebClient/WebClient.ino b/WiFi/examples/WebClient/WebClient.ino deleted file mode 100644 index 8959d7e0b..000000000 --- a/WiFi/examples/WebClient/WebClient.ino +++ /dev/null @@ -1,88 +0,0 @@ -/* - Web client - - This sketch connects to a website (http://www.google.com) - using a WiFi shield. - - Circuit: - * WiFi shield attached - * Analog inputs attached to pins A0 through A5 (optional) - - created 13 July 2010 - by Domenico La Fauci - */ - - -#include -#include - -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; -IPAddress server(74,125,232,115); // Google - -// 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): -Client client; - -int startWiFiWpa() -{ - Serial.println("\nSetup WiFi Wpa..."); - //strcpy(ssid, "AndroidAP9647"); - strcpy(ssid, "Cariddi"); - Serial.print("SSID: "); - Serial.println(ssid); - const char *pass = "1234567890"; - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) - { - Serial.println("Connection Failed"); - } - return status; -} - - -void setup() { - // start the WiFi connection and the server: - Serial.begin(9600); - Serial.println("*** Start WebClient WiFi example ***"); - - int _status = startWiFiWpa(); - if ( _status == WL_CONNECTED) - { - Serial.println("\nStarting connection..."); - // if you get a connection, report back via serial: - if (client.connect(server, 80)) { - Serial.println("connected"); - // Make a HTTP request: - client.println("GET /search?q=arduino HTTP/1.0"); - client.println(); - } - else { - // kf you didn't get a connection to the server: - Serial.println("connection failed"); - } - } -} - -void loop() -{ - // if there are incoming bytes available - // from the server, read them and print them: - if (client.available()) { - char c = client.read(); - Serial.print(c); - } - - // if the server's disconnected, stop the client: - if (!client.connected()) { - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - - // do nothing forevermore: - for(;;) - ; - } -} - diff --git a/WiFi/examples/WebServer/WebServer.ino b/WiFi/examples/WebServer/WebServer.ino deleted file mode 100644 index 83869073f..000000000 --- a/WiFi/examples/WebServer/WebServer.ino +++ /dev/null @@ -1,97 +0,0 @@ -/* - Web Server - - A simple web server that shows the value of the analog input pins. - using a WiFi shield. - - Circuit: - * WiFi shield attached - * Analog inputs attached to pins A0 through A5 (optional) - - created 13 July 2010 - by Domenico La Fauci - */ - -#include -#include - -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; - -Server server(80); - -int startWiFiWpa() -{ - Serial.println("\nSetup WiFi Wpa..."); - //strcpy(ssid, "AndroidAP9647"); - strcpy(ssid, "Cariddi"); - Serial.print("SSID: "); - Serial.println(ssid); - const char *pass = "1234567890"; - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) - { - Serial.println("Connection Failed"); - } - return status; -} - -void setup() -{ - // start the WiFi connection and the server: - Serial.begin(9600); - Serial.println("*** Start WebServer WiFi example ***"); - - int _status = startWiFiWpa(); - if ( _status == WL_CONNECTED) - { - Serial.println("\nStarting server..."); - server.begin(); - } -} - -void loop() -{ - // listen for incoming clients - Client client = server.available(); - if (client) { - // an http request ends with a blank line - boolean currentLineIsBlank = true; - while (client.connected()) { - if (client.available()) { - char c = client.read(); - // 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(); - - // output the value of each analog input pin - for (int analogChannel = 0; analogChannel < 6; analogChannel++) { - client.print("analog input "); - client.print(analogChannel); - client.print(" is "); - client.print(analogRead(analogChannel)); - 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(); - } -} diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino deleted file mode 100644 index 0495aefbd..000000000 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ /dev/null @@ -1,99 +0,0 @@ -/* - Wifi Chat Server - - A simple server that distributes any incoming messages to all - connected clients. To use telnet to your device's IP address and type. - You can see the client's input in the serial monitor as well. - - This example obtains an IP address from the Wifi router and - reports it. - - Circuit: - * WiFi shield attached - - created 21 May 2011 - by Tom Igoe - - */ - -#include -#include -#include - -char ssid[] = "yourWifiNetwork"; -char pass[] = "seekrit-password"; -int status = WL_IDLE_STATUS; -// telnet defaults to port 23 -Server server(23); -boolean gotAMessage = false; // whether or not you got a message from the client yet - -void setup() { - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status == WL_CONNECTED) { - // start listening for clients - server.begin(); - Serial.println("Wifi shield is idle"); - // report the IP address: - IPAddress myIPAddress = WiFi.localIp(); - Serial.print("My IP Address: "); - Serial.print(WiFi.localIp()[3]); - Serial.print("."); - Serial.print(WiFi.localIp()[2]); - Serial.print("."); - Serial.print(WiFi.localIp()[1]); - Serial.print("."); - Serial.println(WiFi.localIp()[0]); - } else { - // if you didn't get a wifi connection, report what happened: - switch (status) { - case WL_IDLE_STATUS: - Serial.println("Wifi connection succeeded"); - break; - case WL_NO_SSID_AVAIL: - Serial.println("No Wifi network available"); - break; - case WL_CONNECT_FAILED: - Serial.println("Wifi connection failed"); - break; - case WL_CONNECTION_LOST: - Serial.println("Wifi connection lost"); - break; - case WL_DISCONNECTED: - Serial.println("Wifi disconnected"); - break; - } - } -} - -void loop() { - // wait for a new client: - Client client = server.available(); - - // when the client sends the first byte, say hello: - if (client) { - if (!gotAMessage) { - Serial.println("I have a new client"); - client.println("Hello, client!"); - gotAMessage = true; - } - - // 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.print(thisChar); - } -} - - - - - - - diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino deleted file mode 100644 index 9792121ae..000000000 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ /dev/null @@ -1,129 +0,0 @@ -/* - Twitter Client with Strings - - This sketch connects to Twitter using an Ethernet shield. It parses the XML - returned, and looks for this is a tweet - - You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield, - either one will work, as long as it's got a Wiznet Ethernet module on board. - - This example uses the DHCP routines in the Ethernet library which is part of the - Arduino core from version 1.0 beta 1 - - This example uses the String library, which is part of the Arduino core from - version 0019. - - Circuit: - * Ethernet shield attached to pins 10, 11, 12, 13 - - created 21 May 2011 - by Tom Igoe - - This code is in the public domain. - - */ -#include -#include -#include - -char ssid[] = "yourWifiNetwork"; -char pass[] = "seekrit-password"; -int status = WL_IDLE_STATUS; - -// initialize the library instance: -Client client; - -const int requestInterval = 60000; // delay between requests - -char serverName[] = "api.twitter.com"; // twitter URL - -boolean requested; // whether you've made a request since connecting -long lastAttemptTime = 0; // last time you connected to the server, in milliseconds - -String currentLine = ""; // string to hold the text from server -String tweet = ""; // string to hold the tweet -boolean readingTweet = false; // if you're currently reading the tweet - -void setup() { - // reserve space for the strings: - currentLine.reserve(256); - tweet.reserve(150); - // initialize serial: - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - else { - Serial.println("Connected to wifi"); - connectToServer(); - } -} - - - -void loop() -{ - if (client.connected()) { - if (client.available()) { - // read incoming bytes: - char inChar = client.read(); -Serial.write(inChar); - - // add incoming byte to end of line: - currentLine += inChar; - - // if you get a newline, clear the line: - if (inChar == '\n') { - currentLine = ""; - } - // if the current line ends with , it will - // be followed by the tweet: - if ( currentLine.endsWith("")) { - // tweet is beginning. Clear the tweet string: - readingTweet = true; - tweet = ""; - } - // if you're currently reading the bytes of a tweet, - // add them to the tweet String: - if (readingTweet) { - if (inChar != '<') { - tweet += inChar; - } - else { - // if you got a "<" character, - // you've reached the end of the tweet: - readingTweet = false; - Serial.println(tweet); - // close the connection to the server: - client.stop(); - } - } - } - } - else if (millis() - lastAttemptTime > requestInterval) { - // if you're not connected, and two minutes have passed since - // your last connection, then attempt to connect again: - connectToServer(); - } -} - -void connectToServer() { - // attempt to connect, and wait a millisecond: - Serial.println("connecting to server..."); - if (client.connect(serverName, 80)) { - Serial.println("making HTTP request..."); - // make HTTP GET request to twitter: - client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1"); - client.println("HOST: api.twitter.com"); - client.println(); - } - // note the time of this connect attempt: - lastAttemptTime = millis(); -} - diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino deleted file mode 100644 index d1883c2f9..000000000 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ /dev/null @@ -1,78 +0,0 @@ -#include - -/* - Web client - - This sketch connects to a website (http://www.google.com) - using a WiFi shield. - - Circuit: - * WiFi shield attached - * Analog inputs attached to pins A0 through A5 (optional) - - created 13 July 2010 - by Domenico La Fauci - modified 21 May 2011 - by Tom Igoe - */ - - -#include -#include -#include - -char ssid[] = "yourWifiNetwork"; -char pass[] = "seekrit-password"; -int status = WL_IDLE_STATUS; -IPAddress server(74,125,115,105); // Google - -// 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): -Client client; - -void setup() { - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - else { - Serial.println("Connected to wifi"); - Serial.println("\nStarting connection..."); - // if you get a connection, report back via serial: - if (client.connect(server, 80)) { - Serial.println("connected"); - // Make a HTTP request: - client.println("GET /search?q=arduino HTTP/1.0"); - client.println(); - } - } -} - -void loop() { - // if there are incoming bytes available - // from the server, read them and print them: - if (client.available()) { - char c = client.read(); - Serial.print(c); - } - - // if the server's disconnected, stop the client: - if (!client.connected()) { - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - - // do nothing forevermore: - for(;;) - ; - } -} - - diff --git a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino b/WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino deleted file mode 100644 index 916e020c7..000000000 --- a/WiFi/examples/wifi_WPA_example/wifi_WPA_example.ino +++ /dev/null @@ -1,175 +0,0 @@ -/* - WiFi Server example - - A simple connection with WiFi AP with Wireless Security - information try to access with WPA or WEP security keys - A simple server is setup to exchange data. - - created 13 July 2010 - by Domenico La Fauci - */ -#include -#include - -byte mac[6] = { 0 }; -IPAddress ip; -IPAddress gateway; -IPAddress subnet; -byte dataBuf[80] = { 0 }; -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; -#define MAX_NUM_SSID 10 -char ssidList[MAX_NUM_SSID][32] = { {0} }; - - -Server server(23); - -void printIpData() -{ - ip = WiFi.localIp(); - - Serial.print("IP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); -} - -void printCurrNet() -{ - char* ssid = WiFi.SSID(); - Serial.print("SSID: "); - Serial.println(ssid); - - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],16);Serial.print(":"); - Serial.print(bssid[4],16);Serial.print(":"); - Serial.print(bssid[3],16);Serial.print(":"); - Serial.print(bssid[2],16);Serial.print(":"); - Serial.print(bssid[1],16);Serial.print(":"); - Serial.println(bssid[0],16); - - int32_t rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi,10); - - uint8_t enct = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(enct,16); -} - -void scanNetworks() -{ - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; - Serial.print("SSID List:"); - Serial.println(numSsid, 10); - for (int i = 0; i0) - { - dataBuf[idx]=0; - //Serial.println((char*)&dataBuf[0]); - server.write((char*)&dataBuf[0]); - } - return; - } - } - */ -} - - From f98da1ba55eb3f458eacef52aa918fb5c1b72b68 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 6 Jun 2011 15:00:54 -0400 Subject: [PATCH 21/98] Corrected note in the WEP ScanNetworks example --- WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino b/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino index 4ede09eb1..fd9f6fdaf 100644 --- a/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino +++ b/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino @@ -1,6 +1,6 @@ /* - WPA2 connection using the WiFi shield. Attempts to connect + WEP connection using the WiFi shield. Attempts to connect and prints out info about the network Circuit: From 535309233292631d6b84bbf4ef43c0f0bd1e225f Mon Sep 17 00:00:00 2001 From: mlafauci Date: Wed, 8 Jun 2011 00:54:14 +0200 Subject: [PATCH 22/98] Moved WiFi driver initialization to default constructor Moved scanNetworks in the example before wifi connection setup in order to show the list of possible WiFi APs. --- WiFi/WiFi.cpp | 11 +++++------ WiFi/WiFi.h | 2 +- .../Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino | 10 +++++----- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index a7ccc2047..3ed9d686c 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -21,6 +21,8 @@ wl_status_t WiFiClass::_status; WiFiClass::WiFiClass() { + // Driver initialization + init(); } void WiFiClass::init() @@ -42,7 +44,8 @@ uint8_t WiFiClass::getSocket() int WiFiClass::begin() { - init(); + // Add procedure to read the latest configuration from eeprom/dataflash + // and start the wifi connection } int WiFiClass::begin(char* ssid) @@ -50,8 +53,6 @@ int WiFiClass::begin(char* ssid) uint8_t status = WL_IDLE_STATUS; uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; - begin(); - if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE) { do @@ -72,8 +73,7 @@ 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; - begin(); - // set encryption key + // set encryption key if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE) { do @@ -93,7 +93,6 @@ int WiFiClass::begin(char* ssid, const char *passphrase) uint8_t status = WL_IDLE_STATUS; uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION; - begin(); // set passphrase if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE) { diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index 27380b9d2..ac88bd02a 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -21,7 +21,7 @@ private: static char _passph[WL_WPA_KEY_MAX_LENGTH]; static wl_status_t _status; - void init(); + static void init(); public: static int16_t _state[MAX_SOCK_NUM]; static uint16_t _server_port[MAX_SOCK_NUM]; diff --git a/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino b/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino index 5a85b98a6..87c5ffe02 100644 --- a/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino +++ b/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino @@ -28,7 +28,11 @@ IPAddress subnet; // the subnet mask void setup() { // initialize serial: Serial.begin(9600); - + + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); + // attempt to connect using WEP encryption: Serial.println("Attempting to connect to open network..."); status = WiFi.begin(ssid); @@ -36,10 +40,6 @@ void setup() { Serial.print("SSID: "); Serial.println(ssid); - // scan for existing networks: - Serial.println("Scanning available networks..."); - scanNetworks(); - // if you're not connected, stop here: if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); From 9785b99cba7e42ec2ec49d7333c62f9b101591bd Mon Sep 17 00:00:00 2001 From: mlafauci Date: Wed, 31 Aug 2011 09:34:56 +0200 Subject: [PATCH 23/98] Fix CS for proto1 --- 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 43c5dd251..b8cd18b28 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -10,7 +10,7 @@ extern "C" { #define DATAOUT 11//MOSI #define DATAIN 12//MISO #define SPICLOCK 13//sck -#define SLAVESELECT 2//ss +#define SLAVESELECT 10//ss #define SLAVEREADY 3 #define DELAY_100NS asm volatile("nop") From 00b945d86985955f5d9151fce0d933b484334c96 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Wed, 31 Aug 2011 10:34:51 +0200 Subject: [PATCH 24/98] Changes to integrate latest core updates to WiFly branch --- WiFi/Client.h | 37 ---------- WiFi/IPAddress.cpp | 44 ------------ WiFi/IPAddress.h | 72 ------------------- WiFi/WiFi.h | 8 +-- WiFi/{Client.cpp => WiFiClient.cpp} | 48 +++++++------ WiFi/WiFiClient.h | 39 ++++++++++ WiFi/{Server.cpp => WiFiServer.cpp} | 22 +++--- WiFi/{Server.h => WiFiServer.h} | 14 ++-- .../PachubeClientString.ino | 15 ++-- .../Wifi_Open_ScanNetworks.ino | 2 +- .../Wifi_WEP_ScanNetworks.ino | 4 +- .../Wifi_WPA_ChatServer.ino | 4 +- .../Wifi_WPA_PachubeClientString.ino | 2 +- .../Wifi_WPA_ScanNetworks.ino | 13 ++-- .../Wifi_WPA_TwitterClient.ino | 2 +- .../Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino | 5 +- .../Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino | 8 +-- .../wifi_Server_example.ino | 9 ++- .../wifi_Server_example_WEP.ino | 10 ++- .../wifi_Server_example_WPA.ino | 13 ++-- .../wifi_WEP_example/wifi_WEP_example.ino | 7 +- WiFi/examples/wifi_example/wifi_example.pde | 7 +- WiFi/utility/debug.h | 2 +- 23 files changed, 136 insertions(+), 251 deletions(-) delete mode 100755 WiFi/Client.h delete mode 100644 WiFi/IPAddress.cpp delete mode 100644 WiFi/IPAddress.h rename WiFi/{Client.cpp => WiFiClient.cpp} (74%) create mode 100755 WiFi/WiFiClient.h rename WiFi/{Server.cpp => WiFiServer.cpp} (74%) rename WiFi/{Server.h => WiFiServer.h} (58%) diff --git a/WiFi/Client.h b/WiFi/Client.h deleted file mode 100755 index c812089c1..000000000 --- a/WiFi/Client.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef client_h -#define client_h -#include "Arduino.h" -#include "Print.h" - -class Client : public Stream { - -public: - Client(); - Client(uint8_t sock); - - uint8_t status(); - int connect(IPAddress ip, uint16_t port); - int connect(const char *host, uint16_t port); - virtual void write(uint8_t); - virtual void write(const char *str); - virtual void 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(); - void stop(); - uint8_t connected(); - operator bool(); - - friend class Server; - -private: - static uint16_t _srcport; - uint8_t _sock; //not used - uint16_t _socket; - - uint8_t getFirstSocket(); -}; - -#endif diff --git a/WiFi/IPAddress.cpp b/WiFi/IPAddress.cpp deleted file mode 100644 index 610ff4c53..000000000 --- a/WiFi/IPAddress.cpp +++ /dev/null @@ -1,44 +0,0 @@ - -#include -#include - -IPAddress::IPAddress() -{ - memset(_address, 0, sizeof(_address)); -} - -IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) -{ - _address[0] = first_octet; - _address[1] = second_octet; - _address[2] = third_octet; - _address[3] = fourth_octet; -} - -IPAddress::IPAddress(uint32_t address) -{ - memcpy(_address, &address, sizeof(_address)); -} - -IPAddress::IPAddress(const uint8_t *address) -{ - memcpy(_address, address, sizeof(_address)); -} - -IPAddress& IPAddress::operator=(const uint8_t *address) -{ - memcpy(_address, address, sizeof(_address)); - return *this; -} - -IPAddress& IPAddress::operator=(uint32_t address) -{ - memcpy(_address, (const uint8_t *)&address, sizeof(_address)); - return *this; -} - -bool IPAddress::operator==(const uint8_t* addr) -{ - return memcmp(addr, _address, sizeof(_address)) == 0; -} - diff --git a/WiFi/IPAddress.h b/WiFi/IPAddress.h deleted file mode 100644 index 487e420bd..000000000 --- a/WiFi/IPAddress.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * - * MIT License: - * Copyright (c) 2011 Adrian McEwen - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * adrianm@mcqn.com 1/1/2011 - */ - -#ifndef IPAddress_h -#define IPAddress_h - -// A class to make it easier to handle and pass around IP addresses - -class IPAddress { -private: - uint8_t _address[4]; // IPv4 address - // Access the raw byte array containing the address. Because this returns a pointer - // to the internal structure rather than a copy of the address this function should only - // be used when you know that the usage of the returned uint8_t* will be transient and not - // stored. - uint8_t* raw_address() { return _address; }; - -public: - // Constructors - IPAddress(); - IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); - IPAddress(uint32_t address); - IPAddress(const uint8_t *address); - - // Overloaded cast operator to allow IPAddress objects to be used where a pointer - // to a four-byte uint8_t array is expected - operator uint32_t() { return *((uint32_t*)_address); }; - bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; - bool operator==(const uint8_t* addr); - - // Overloaded index operator to allow getting and setting individual octets of the address - uint8_t operator[](int index) const { return _address[index]; }; - uint8_t& operator[](int index) { return _address[index]; }; - - // Overloaded copy operators to allow initialisation of IPAddress objects from other types - IPAddress& operator=(const uint8_t *address); - IPAddress& operator=(uint32_t address); - - friend class EthernetClass; - friend class UDP; - friend class Client; - friend class Server; - friend class DhcpClass; - friend class DNSClient; -}; - -const IPAddress INADDR_NONE(0,0,0,0); - - -#endif diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index ac88bd02a..fbb1225d2 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -8,8 +8,8 @@ extern "C" { } #include "IPAddress.h" -#include "Client.h" -#include "Server.h" +#include "WiFiClient.h" +#include "WiFiServer.h" class WiFiClass { @@ -89,8 +89,8 @@ public: // function used for test uint8_t test(); - friend class Client; - friend class Server; + friend class WiFiClient; + friend class WiFiServer; }; extern WiFiClass WiFi; diff --git a/WiFi/Client.cpp b/WiFi/WiFiClient.cpp similarity index 74% rename from WiFi/Client.cpp rename to WiFi/WiFiClient.cpp index afdb31d7c..9aa0e64cd 100755 --- a/WiFi/Client.cpp +++ b/WiFi/WiFiClient.cpp @@ -7,19 +7,19 @@ extern "C" { } #include "WiFi.h" -#include "Client.h" -#include "Server.h" +#include "WiFiClient.h" +#include "WiFiServer.h" #include "server_drv.h" -uint16_t Client::_srcport = 1024; +uint16_t WiFiClient::_srcport = 1024; -Client::Client() : _sock(MAX_SOCK_NUM) { +WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) { } -Client::Client(uint8_t sock) : _sock(sock) { +WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) { } -int Client::connect(const char* host, uint16_t port) { +int WiFiClient::connect(const char* host, uint16_t port) { /* TODO Add DNS wifi spi function to resolve DNS */ #if 0 // Look up the host first @@ -37,7 +37,7 @@ int Client::connect(const char* host, uint16_t port) { #endif } -int Client::connect(IPAddress ip, uint16_t port) { +int WiFiClient::connect(IPAddress ip, uint16_t port) { _sock = getFirstSocket(); if (_sock != NO_SOCKET_AVAIL) { @@ -49,36 +49,40 @@ int Client::connect(IPAddress ip, uint16_t port) { return 1; } -void Client::write(uint8_t b) { +size_t WiFiClient::write(uint8_t b) { if (_sock != 255) { START(); ServerDrv::sendData(_sock, &b, 1); while (!ServerDrv::isDataSent(_sock)); END(); - + return 1; } + return 0; } -void Client::write(const char *str) { +size_t WiFiClient::write(const char *str) { if (_sock != 255) { unsigned int len = strlen(str); ServerDrv::sendData(_sock, (const uint8_t *)str, len); while (!ServerDrv::isDataSent(_sock)); + return len; } + return 0; } -void Client::write(const uint8_t *buf, size_t size) { +size_t WiFiClient::write(const uint8_t *buf, size_t size) { if (_sock != 255) { ServerDrv::sendData(_sock, buf, size); while (!ServerDrv::isDataSent(_sock)); + return size; } - + return 0; } -int Client::available() { +int WiFiClient::available() { if (_sock != 255) { return ServerDrv::availData(_sock); @@ -87,7 +91,7 @@ int Client::available() { return 0; } -int Client::read() { +int WiFiClient::read() { uint8_t b; if (!available()) return -1; @@ -96,23 +100,23 @@ int Client::read() { } -int Client::read(uint8_t* buf, size_t size) { +int WiFiClient::read(uint8_t* buf, size_t size) { if (!ServerDrv::getDataBuf(_sock, buf, &size)) return -1; return 0; } -int Client::peek() { +int WiFiClient::peek() { //TODO to be implemented return 0; } -void Client::flush() { +void WiFiClient::flush() { while (available()) read(); } -void Client::stop() { +void WiFiClient::stop() { if (_sock == 255) return; @@ -132,7 +136,7 @@ void Client::stop() { _sock = 255; } -uint8_t Client::connected() { +uint8_t WiFiClient::connected() { if (_sock == 255) { return 0; } else { @@ -142,7 +146,7 @@ uint8_t Client::connected() { } } -uint8_t Client::status() { +uint8_t WiFiClient::status() { if (_sock == 255) { return CLOSED; } else { @@ -150,12 +154,12 @@ uint8_t Client::status() { } } -Client::operator bool() { +WiFiClient::operator bool() { return _sock != 255; } // Private Methods -uint8_t Client::getFirstSocket() +uint8_t WiFiClient::getFirstSocket() { for (int i = 0; i < MAX_SOCK_NUM; i++) { if (WiFiClass::_state[i] == 0) diff --git a/WiFi/WiFiClient.h b/WiFi/WiFiClient.h new file mode 100755 index 000000000..597bcdb56 --- /dev/null +++ b/WiFi/WiFiClient.h @@ -0,0 +1,39 @@ +#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 char *str); + 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; + +private: + static uint16_t _srcport; + uint8_t _sock; //not used + uint16_t _socket; + + uint8_t getFirstSocket(); +}; + +#endif diff --git a/WiFi/Server.cpp b/WiFi/WiFiServer.cpp similarity index 74% rename from WiFi/Server.cpp rename to WiFi/WiFiServer.cpp index a8e236029..52277eac8 100755 --- a/WiFi/Server.cpp +++ b/WiFi/WiFiServer.cpp @@ -2,17 +2,17 @@ #include "server_drv.h" #include "WiFi.h" -#include "Client.h" -#include "Server.h" +#include "WiFiClient.h" +#include "WiFiServer.h" -Server::Server(uint16_t port) +WiFiServer::WiFiServer(uint16_t port) { _port = port; } -void Server::begin() +void WiFiServer::begin() { uint8_t _sock = WiFiClass::getSocket(); if (_sock != NO_SOCKET_AVAIL) @@ -22,7 +22,7 @@ void Server::begin() } } -Client Server::available(byte* status) +WiFiClient WiFiServer::available(byte* status) { //accept(); @@ -30,7 +30,7 @@ Client Server::available(byte* status) { if (WiFiClass::_server_port[sock] != 0) { - Client client(sock); + WiFiClient client(sock); int _status = client.status(); if (status != NULL) *status = _status; @@ -43,26 +43,26 @@ Client Server::available(byte* status) } } - return Client(255); + return WiFiClient(255); } -void Server::write(uint8_t b) +void WiFiServer::write(uint8_t b) { write(&b, 1); } -void Server::write(const char *str) +void WiFiServer::write(const char *str) { write((const uint8_t *)str, strlen(str)); } -void Server::write(const uint8_t *buffer, size_t size) +void WiFiServer::write(const uint8_t *buffer, size_t size) { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { if (WiFiClass::_server_port[sock] != 0) { - Client client(sock); + WiFiClient client(sock); if (WiFiClass::_server_port[sock] == _port && client.status() == ESTABLISHED) diff --git a/WiFi/Server.h b/WiFi/WiFiServer.h similarity index 58% rename from WiFi/Server.h rename to WiFi/WiFiServer.h index 34124af09..7eb3dd14e 100755 --- a/WiFi/Server.h +++ b/WiFi/WiFiServer.h @@ -1,21 +1,21 @@ -#ifndef server_h -#define server_h +#ifndef wifiserver_h +#define wifiserver_h extern "C" { #include "utility/wl_definitions.h" } -#include "Print.h" +#include "Server.h" -class Client; +class WiFiClient; -class Server : public Print { +class WiFiServer : public Server { private: uint16_t _port; void* pcb; public: - Server(uint16_t); - Client available(uint8_t* status = NULL); + WiFiServer(uint16_t); + WiFiClient available(uint8_t* status = NULL); void begin(); virtual void write(uint8_t); virtual void write(const char *str); diff --git a/WiFi/examples/PachubeClientString/PachubeClientString.ino b/WiFi/examples/PachubeClientString/PachubeClientString.ino index 9a38ec15a..1e007761b 100644 --- a/WiFi/examples/PachubeClientString/PachubeClientString.ino +++ b/WiFi/examples/PachubeClientString/PachubeClientString.ino @@ -22,16 +22,16 @@ */ #include -#include -char ssid[32] = { 0 }; +char ssid[] = "yourNetwork"; +char pass[] = "secretPassword"; int status = WL_IDLE_STATUS; // The address of the server you want to connect to (pachube.com): IPAddress server(173,203,98,29); // initialize the library instance: -Client client(server, 80); +WiFiClient client; 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 @@ -40,11 +40,10 @@ const int postingInterval = 10000; //delay between updates to Pachube.com int startWiFiWpa() { Serial.println("\nSetup WiFi Wpa..."); - //strcpy(ssid, "AndroidAP9647"); - strcpy(ssid, "Cariddi"); + strcpy(ssid, "tigoenet"); Serial.print("SSID: "); Serial.println(ssid); - const char *pass = "1234567890"; + const char *pass = "m30w-m30w"; status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { @@ -110,11 +109,11 @@ void loop() { // this method makes a HTTP connection to the server: void sendData(String thisData) { // if there's a successful connection: - if (client.connect()) { + if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request. // fill in your feed address here: - client.print("PUT /api/http://api.pachube.com/v2/feeds/24196.csv HTTP/1.1\n"); + client.print("PUT /api/24196.csv HTTP/1.1\n"); client.print("Host: www.pachube.com\n"); // fill in your Pachube API key here: client.print("X-PachubeApiKey: gw0L2A-J5ACRGQccX59tCYt0IEzyecr-SoiuC47U1-8\n"); diff --git a/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino b/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino index 87c5ffe02..64d27fbd0 100644 --- a/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino +++ b/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino @@ -7,7 +7,7 @@ * WiFi shield attached created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) modified 5 June 2011 by Tom Igoe */ diff --git a/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino b/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino index fd9f6fdaf..1198346bb 100644 --- a/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino +++ b/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino @@ -7,7 +7,7 @@ * WiFi shield attached created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) modified 5 June 2011 by Tom Igoe */ @@ -61,7 +61,7 @@ void loop() { void printIpData() { // print your WiFi shield's IP address: - ip = WiFi.localIp(); + ip = WiFi.localIP(); Serial.print("IP: "); Serial.print(ip[3]); Serial.print("."); diff --git a/WiFi/examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino b/WiFi/examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino index 75dab1936..6cbb4a736 100644 --- a/WiFi/examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino +++ b/WiFi/examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino @@ -25,7 +25,7 @@ char pass[] = "secretPassword"; // the WPA2 password for your network int status = WL_IDLE_STATUS; // the Wifi radio's status // telnet defaults to port 23 -Server server(23); +WiFiServer server(23); boolean gotAMessage = false; // whether or not you got a message from the client yet @@ -56,7 +56,7 @@ void setup() { void loop() { // wait for a new client: - Client client = server.available(); + WiFiClient client = server.available(); // when the client sends the first byte, say hello: if (client) { diff --git a/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino b/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino index 227f43b9b..88e44e849 100644 --- a/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino +++ b/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino @@ -31,7 +31,7 @@ int status = WL_IDLE_STATUS; IPAddress server(173,203,98,29); // initialize the library instance: -Client client; +WiFiClient client; 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 diff --git a/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino b/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino index 0c3bbf9fb..4a217f0cd 100644 --- a/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino +++ b/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino @@ -7,7 +7,7 @@ * WiFi shield attached created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) modified 5 June 2011 by Tom Igoe */ @@ -16,9 +16,8 @@ #include #include -char ssid[] = "yourNetwork"; // the name of your network -char pass[] = "secretPassword"; // the WPA2 password for your network - +char ssid[] = "yourNetwork"; +char pass[] = "secretpassword"; int status = WL_IDLE_STATUS; // the Wifi radio's status byte mac[6]; // the MAC address of your Wifi shield @@ -29,7 +28,11 @@ IPAddress subnet; // the subnet mask void setup() { // initialize serial: Serial.begin(9600); - + // scan for existing networks: + Serial.println("Scanning available networks..."); + //WiFi.begin(); + scanNetworks(); + // attempt to connect using WEP encryption: // Serial.println("Attempting to connect to WEP-128 network..."); // status = WiFi.begin(ssid, keyIndex, key); diff --git a/WiFi/examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino b/WiFi/examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino index f65d6a7b0..cc0cd9e38 100644 --- a/WiFi/examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino +++ b/WiFi/examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino @@ -24,7 +24,7 @@ char pass[] = "secretpassword"; int status = WL_IDLE_STATUS; // initialize the library instance: -Client client; +WiFiClient client; const int requestInterval = 30*1000; // delay between requests; 30 seconds diff --git a/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino b/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino index 5e1bd277b..57bc5d265 100644 --- a/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino +++ b/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino @@ -9,7 +9,7 @@ * WiFi shield attached created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) modified 21 May 2011 by Tom Igoe */ @@ -17,7 +17,6 @@ #include #include -#include char ssid[] = "yourNetwork"; char pass[] = "secretPassword"; @@ -27,7 +26,7 @@ IPAddress server(74,125,115,105); // Google // 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): -Client client; +WiFiClient client; void setup() { Serial.begin(9600); diff --git a/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino b/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino index 9f121ad0b..eb0479a37 100644 --- a/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino +++ b/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino @@ -9,7 +9,7 @@ * Analog inputs attached to pins A0 through A5 (optional) created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) modified 5 June 2011 by Tom Igoe */ @@ -22,7 +22,7 @@ char ssid[] = "yourNetwork"; char pass[] = "secretpassword"; int status = WL_IDLE_STATUS; -Server server(80); +WiFiServer server(80); void setup() { // initialize serial: @@ -39,7 +39,7 @@ void setup() { else { server.begin(); Serial.print("Connected to wifi. My address:"); - IPAddress myAddress = WiFi.localIp(); + IPAddress myAddress = WiFi.localIP(); Serial.print(myAddress[0]); Serial.print("."); Serial.print(myAddress[1]); @@ -53,7 +53,7 @@ void setup() { void loop() { // listen for incoming clients - Client client = server.available(); + WiFiClient client = server.available(); if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; diff --git a/WiFi/examples/wifi_Server_example/wifi_Server_example.ino b/WiFi/examples/wifi_Server_example/wifi_Server_example.ino index 2a88a5617..7fc82afd8 100644 --- a/WiFi/examples/wifi_Server_example/wifi_Server_example.ino +++ b/WiFi/examples/wifi_Server_example/wifi_Server_example.ino @@ -6,10 +6,9 @@ A simple server is setup to exchange data. created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) */ #include -#include byte mac[6] = { 0 }; IPAddress ip; @@ -19,11 +18,11 @@ byte dataBuf[80] = { 0 }; char ssid[32] = { 0 }; int status = WL_IDLE_STATUS; -Server server(23); +WiFiServer server(23); void printIpData() { - ip = WiFi.localIp(); + ip = WiFi.localIP(); Serial.print("IP: "); Serial.print(ip[3],10);Serial.print("."); @@ -136,7 +135,7 @@ void loop() if (status == WL_CONNECTED) { byte status = 0; - Client client = server.available(&status); + WiFiClient client = server.available(&status); if (client) { //Serial.print("Status: "); //Serial.println(status, 16); diff --git a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino index 9191d63e3..5c11b9580 100644 --- a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino +++ b/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino @@ -6,10 +6,9 @@ A simple server is setup to exchange data. created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) */ #include -#include byte mac[6] = { 0 }; IPAddress ip; @@ -21,12 +20,11 @@ int status = WL_IDLE_STATUS; #define MAX_NUM_SSID 10 char ssidList[MAX_NUM_SSID][32] = { {0} }; - -Server server(23); +WiFiServer server(23); void printIpData() { - ip = WiFi.localIp(); + ip = WiFi.localIP(); Serial.print("IP: "); Serial.print(ip[3],10);Serial.print("."); @@ -147,7 +145,7 @@ void loop() if (status == WL_CONNECTED) { byte _status = 0; - Client client = server.available(&_status); + WiFiClient client = server.available(&_status); if (client) { Serial.print("Status: "); Serial.println(status, 16); diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino index fa79002f1..bf0f731e7 100644 --- a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino +++ b/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino @@ -6,10 +6,10 @@ A simple server is setup to exchange data. created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) */ #include -#include + #define _PRINT_ byte mac[6] = { 0 }; @@ -23,12 +23,12 @@ int status = WL_IDLE_STATUS; char ssidList[MAX_NUM_SSID][32] = { {0} }; -Server server(23); +WiFiServer server(23); boolean gotAMessage = false; // whether or not you got a message from the client yet void printIpData() { - ip = WiFi.localIp(); + ip = WiFi.localIP(); Serial.print("\nIP: "); Serial.print(ip[3],10);Serial.print("."); @@ -153,8 +153,7 @@ void execCmd(char* buf) Serial.print("\nExecuting command: "); Serial.println(buf); #endif - //server.write(buf); - server.print(buf); + server.write(buf); } @@ -163,7 +162,7 @@ void loop() if (status == WL_CONNECTED) { byte _status = 0; - Client client = server.available(&_status); + WiFiClient client = server.available(&_status); if (client) { if (!gotAMessage) { Serial.println("\nWe have a new client\n"); diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino index 9208e2276..8849f7129 100644 --- a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino +++ b/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino @@ -6,10 +6,9 @@ A simple server is setup to exchange data. created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) */ #include -#include byte mac[6] = { 0 }; IPAddress ip; @@ -22,11 +21,11 @@ int status = WL_IDLE_STATUS; char ssidList[MAX_NUM_SSID][32] = { {0} }; -Server server(23); +WiFiServer server(23); void printIpData() { - ip = WiFi.localIp(); + ip = WiFi.localIP(); Serial.print("IP: "); Serial.print(ip[3],10);Serial.print("."); diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/wifi_example/wifi_example.pde index 4a05228b9..8db9d2412 100755 --- a/WiFi/examples/wifi_example/wifi_example.pde +++ b/WiFi/examples/wifi_example/wifi_example.pde @@ -6,10 +6,9 @@ A simple server is setup to exchange data. created 13 July 2010 - by Domenico La Fauci + by dlf (Metodo2 srl) */ #include -#include byte mac[6] = { 0 }; IPAddress ip; @@ -22,11 +21,11 @@ int status = WL_IDLE_STATUS; char ssidList[MAX_NUM_SSID][32] = { {0} }; -Server server(23); +WiFiServer server(23); void printIpData() { - ip = WiFi.localIp(); + ip = WiFi.localIP(); Serial.print("IP: "); Serial.print(ip[3],10);Serial.print("."); diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index ef3ba8c22..b9cd3c2aa 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -2,7 +2,7 @@ // // File: debug.h // -// Author: Domenico La Fauci +// Author: dlf (Metodo2 srl) // //********************************************/ From 20bea5fcc06c12361d79c05d4896ad8aec17504b Mon Sep 17 00:00:00 2001 From: mlafauci Date: Tue, 13 Sep 2011 21:48:09 +0200 Subject: [PATCH 25/98] Making Print::write(char *) non-virtual on WiFi lib --- WiFi/WiFiClient.cpp | 11 ----------- WiFi/WiFiClient.h | 1 - WiFi/WiFiServer.cpp | 5 ----- WiFi/WiFiServer.h | 1 - 4 files changed, 18 deletions(-) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index 9aa0e64cd..e11b086e2 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -61,17 +61,6 @@ size_t WiFiClient::write(uint8_t b) { return 0; } -size_t WiFiClient::write(const char *str) { - if (_sock != 255) - { - unsigned int len = strlen(str); - ServerDrv::sendData(_sock, (const uint8_t *)str, len); - while (!ServerDrv::isDataSent(_sock)); - return len; - } - return 0; -} - size_t WiFiClient::write(const uint8_t *buf, size_t size) { if (_sock != 255) { diff --git a/WiFi/WiFiClient.h b/WiFi/WiFiClient.h index 597bcdb56..919532f41 100755 --- a/WiFi/WiFiClient.h +++ b/WiFi/WiFiClient.h @@ -15,7 +15,6 @@ public: 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 char *str); virtual size_t write(const uint8_t *buf, size_t size); virtual int available(); virtual int read(); diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp index 52277eac8..8461583aa 100755 --- a/WiFi/WiFiServer.cpp +++ b/WiFi/WiFiServer.cpp @@ -51,11 +51,6 @@ void WiFiServer::write(uint8_t b) write(&b, 1); } -void WiFiServer::write(const char *str) -{ - write((const uint8_t *)str, strlen(str)); -} - void WiFiServer::write(const uint8_t *buffer, size_t size) { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) diff --git a/WiFi/WiFiServer.h b/WiFi/WiFiServer.h index 7eb3dd14e..2871d06da 100755 --- a/WiFi/WiFiServer.h +++ b/WiFi/WiFiServer.h @@ -18,7 +18,6 @@ public: WiFiClient available(uint8_t* status = NULL); void begin(); virtual void write(uint8_t); - virtual void write(const char *str); virtual void write(const uint8_t *buf, size_t size); }; From 7c484e3cb3fc66713459bc7d3123e64be6f4acb3 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 13 Sep 2011 17:36:01 -0400 Subject: [PATCH 26/98] Changed the return type on write() from void to size_t to match Print.h --- WiFi/WiFiServer.cpp | 4 ++-- WiFi/WiFiServer.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) mode change 100755 => 100644 WiFi/WiFiServer.cpp mode change 100755 => 100644 WiFi/WiFiServer.h diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp old mode 100755 new mode 100644 index 8461583aa..e03f9c393 --- a/WiFi/WiFiServer.cpp +++ b/WiFi/WiFiServer.cpp @@ -46,12 +46,12 @@ WiFiClient WiFiServer::available(byte* status) return WiFiClient(255); } -void WiFiServer::write(uint8_t b) +size_t WiFiServer::write(uint8_t b) { write(&b, 1); } -void WiFiServer::write(const uint8_t *buffer, size_t size) +size_t WiFiServer::write(const uint8_t *buffer, size_t size) { for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { diff --git a/WiFi/WiFiServer.h b/WiFi/WiFiServer.h old mode 100755 new mode 100644 index 2871d06da..849f84cac --- a/WiFi/WiFiServer.h +++ b/WiFi/WiFiServer.h @@ -17,8 +17,8 @@ public: WiFiServer(uint16_t); WiFiClient available(uint8_t* status = NULL); void begin(); - virtual void write(uint8_t); - virtual void write(const uint8_t *buf, size_t size); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buf, size_t size); }; #endif From af0dd1926af1920a0dc10e9cf20d3e1036391398 Mon Sep 17 00:00:00 2001 From: mlafauci Date: Wed, 14 Sep 2011 00:13:31 +0200 Subject: [PATCH 27/98] Change the return type of write() from void to size_t on WiFiServer class --- WiFi/WiFiClient.h | 2 ++ WiFi/WiFiServer.cpp | 11 +++++++---- WiFi/WiFiServer.h | 6 ++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/WiFi/WiFiClient.h b/WiFi/WiFiClient.h index 919532f41..5a7f0f3b8 100755 --- a/WiFi/WiFiClient.h +++ b/WiFi/WiFiClient.h @@ -27,6 +27,8 @@ public: friend class WiFiServer; + using Print::write; + private: static uint16_t _srcport; uint8_t _sock; //not used diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp index 8461583aa..860759383 100755 --- a/WiFi/WiFiServer.cpp +++ b/WiFi/WiFiServer.cpp @@ -46,13 +46,15 @@ WiFiClient WiFiServer::available(byte* status) return WiFiClient(255); } -void WiFiServer::write(uint8_t b) +size_t WiFiServer::write(uint8_t b) { - write(&b, 1); + return write(&b, 1); } -void WiFiServer::write(const uint8_t *buffer, size_t size) +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) @@ -62,8 +64,9 @@ void WiFiServer::write(const uint8_t *buffer, size_t size) if (WiFiClass::_server_port[sock] == _port && client.status() == ESTABLISHED) { - client.write(buffer, size); + n+=client.write(buffer, size); } } } + return n; } diff --git a/WiFi/WiFiServer.h b/WiFi/WiFiServer.h index 2871d06da..d93b0ced1 100755 --- a/WiFi/WiFiServer.h +++ b/WiFi/WiFiServer.h @@ -17,8 +17,10 @@ public: WiFiServer(uint16_t); WiFiClient available(uint8_t* status = NULL); void begin(); - virtual void write(uint8_t); - virtual void write(const uint8_t *buf, size_t size); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t *buf, size_t size); + + using Print::write; }; #endif From c518f50035bcce1df23f6dd5c4ed78571a05bbe2 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 13 Sep 2011 21:11:19 -0400 Subject: [PATCH 28/98] Updated keywords.txt --- WiFi/keywords.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/WiFi/keywords.txt b/WiFi/keywords.txt index ef4148621..47704cd00 100755 --- a/WiFi/keywords.txt +++ b/WiFi/keywords.txt @@ -25,7 +25,7 @@ connected KEYWORD2 begin KEYWORD2 disconnect KEYWORD2 macAddress KEYWORD2 -localIp KEYWORD2 +localIP KEYWORD2 subnetMask KEYWORD2 gatewayIP KEYWORD2 SSID KEYWORD2 @@ -34,7 +34,8 @@ RSSI KEYWORD2 encryptionType KEYWORD2 getResult KEYWORD2 getSocket KEYWORD2 - +WiFiClient KEYWORD2 +WiFiServer KEYWORD2 ####################################### # Constants (LITERAL1) From 301c1d71d8d538949c54e06884c2582575e374ad Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 16 Sep 2011 21:12:38 -0400 Subject: [PATCH 29/98] Cleaned up wifi library examples moved old examples to libraries/Wifi/examples/old_examples, to finish later. Added five new examples which work in 1.0 beta4 --- .../ScanNetworksOpen/ScanNetworksOpen.ino | 137 +++++++++++++++++ .../ScanNetworksWPA/ScanNetworksWPA.ino | 143 ++++++++++++++++++ .../TwitterClientWPA/TwitterClientWPA.ino | 121 +++++++++++++++ WiFi/examples/WebClientWPA/WebClientWPA.ino | 77 ++++++++++ WiFi/examples/WebServerWPA/WebServerWPA.ino | 95 ++++++++++++ .../PachubeClientString.ino | 0 .../Wifi_Open_RSSI/Wifi_Open_RSSI.ino | 0 .../Wifi_Open_ScanNetworks.ino | 0 .../Wifi_WEP_ScanNetworks.ino | 0 .../Wifi_WPA_ChatServer.ino | 0 .../Wifi_WPA_PachubeClientString.ino | 0 .../Wifi_WPA_ScanNetworks.ino | 0 .../Wifi_WPA_TwitterClient.ino | 0 .../Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino | 0 .../Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino | 0 .../wifi_Server_example.ino | 0 .../wifi_Server_example_WEP.ino | 0 .../wifi_Server_example_WPA.ino | 0 .../wifi_WEP_example/wifi_WEP_example.ino | 0 .../wifi_example/wifi_example.pde | 0 20 files changed, 573 insertions(+) create mode 100644 WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino create mode 100644 WiFi/examples/ScanNetworksWPA/ScanNetworksWPA.ino create mode 100644 WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino create mode 100644 WiFi/examples/WebClientWPA/WebClientWPA.ino create mode 100644 WiFi/examples/WebServerWPA/WebServerWPA.ino rename WiFi/examples/{ => old_examples}/PachubeClientString/PachubeClientString.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_Open_RSSI/Wifi_Open_RSSI.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino (100%) rename WiFi/examples/{ => old_examples}/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino (100%) rename WiFi/examples/{ => old_examples}/wifi_Server_example/wifi_Server_example.ino (100%) rename WiFi/examples/{ => old_examples}/wifi_Server_example_WEP/wifi_Server_example_WEP.ino (100%) rename WiFi/examples/{ => old_examples}/wifi_Server_example_WPA/wifi_Server_example_WPA.ino (100%) rename WiFi/examples/{ => old_examples}/wifi_WEP_example/wifi_WEP_example.ino (100%) rename WiFi/examples/{ => old_examples}/wifi_example/wifi_example.pde (100%) diff --git a/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino b/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino new file mode 100644 index 000000000..3e5f07534 --- /dev/null +++ b/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino @@ -0,0 +1,137 @@ +/* + + Open connection using the WiFi shield. Attempts to connect + and prints out info about the network + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 15 Sep 2011 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "yourNetwork"; // the name of your network +int status = WL_IDLE_STATUS; // the Wifi radio's status + +byte mac[6]; // the MAC address of your Wifi shield +IPAddress ip; // the IP address of your shield +IPAddress gateway; // the router's address +IPAddress subnet; // the subnet mask + +void setup() { + // initialize serial: + Serial.begin(9600); + + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); + + // attempt to connect using WEP encryption: + Serial.println("Attempting to connect to open network..."); + status = WiFi.begin(ssid); + + Serial.print("SSID: "); + Serial.println(ssid); + + // if you're not connected, stop here: + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + // if you are connected, print out info about the connection: + else { + printIpData(); + printCurrentNet(); + } +} + +void loop() { + // do nothing +} + +void printIpData() { + // print your WiFi shield's IP address: + ip = WiFi.localIP(); + Serial.println(ip); + + + // print your subnet mask: + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.println(subnet); + + + // print your gateway address: + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.println(gateway); + + // 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 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("RSSI:"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); +} + +void scanNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + + // print the list of networks seen: + Serial.print("SSID List:"); + 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 +int status = WL_IDLE_STATUS; // the Wifi radio's status + +byte mac[6]; // the MAC address of your Wifi shield +IPAddress ip; // the IP address of your shield +IPAddress gateway; // the router's address +IPAddress subnet; // the subnet mask + +void setup() { + // initialize serial: + Serial.begin(9600); + // scan for existing networks: + Serial.println("Scanning available networks..."); + //WiFi.begin(); + scanNetworks(); + + // attempt to connect using WEP encryption: +// Serial.println("Attempting to connect to WEP-128 network..."); +// status = WiFi.begin(ssid, keyIndex, key); + + // attempt to connect using WPA2 encryption: + Serial.println("Attempting to connect to WPA network..."); + status = WiFi.begin(ssid, pass); + + + Serial.print("SSID: "); + Serial.println(ssid); + + // if you're not connected, stop here: + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + // if you are connected, print out info about the connection: + else { + printIpData(); + printCurrentNet(); + } +} + +void loop() { + // do nothing +} + +void printIpData() { + // print your WiFi shield's IP address: + ip = WiFi.localIP(); + Serial.println(ip); + + + // print your subnet mask: + subnet = WiFi.subnetMask(); + Serial.print("NETMASK: "); + Serial.println(subnet); + + + // print your gateway address: + gateway = WiFi.gatewayIP(); + Serial.print("GATEWAY: "); + Serial.println(gateway); + + // 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 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("RSSI:"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); +} + +void scanNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + + // print the list of networks seen: + Serial.print("SSID List:"); + Serial.println(numSsid); + // print the network number and name for each network found: + for (int thisNet = 0; thisNetthis is a tweet + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * WiFi shield attached to pins 10, 11, 12, 13 + + created 15 Sep 2011 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include + +char ssid[] = "yourNetwork"; // your network SSID (name) +char pass[] = "secretPassword"; // your network password +int status = WL_IDLE_STATUS; + +// initialize the library instance: +WiFiClient client; + +const int requestInterval = 30*1000; // delay between requests; 30 seconds + +IPAddress server(199,59,149,200); // api.twitter.com + +boolean requested; // whether you've made a request since connecting +long lastAttemptTime = 0; // last time you connected to the server, in milliseconds + +String currentLine = ""; // string to hold the text from server +String tweet = ""; // string to hold the tweet +boolean readingTweet = false; // if you're currently reading the tweet + +void setup() { + // reserve space for the strings: + currentLine.reserve(256); + tweet.reserve(150); + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + connectToServer(); + } +} + + + +void loop() +{ + if (client.connected()) { + if (client.available()) { + // read incoming bytes: + char inChar = client.read(); + + // add incoming byte to end of line: + currentLine += inChar; + + // if you get a newline, clear the line: + if (inChar == '\n') { + currentLine = ""; + } + // if the current line ends with , it will + // be followed by the tweet: + if ( currentLine.endsWith("")) { + // tweet is beginning. Clear the tweet string: + readingTweet = true; + tweet = ""; + } + // if you're currently reading the bytes of a tweet, + // add them to the tweet String: + if (readingTweet) { + if (inChar != '<') { + tweet += inChar; + } + else { + // if you got a "<" character, + // you've reached the end of the tweet: + readingTweet = false; + Serial.println(tweet); + // close the connection to the server: + client.stop(); + } + } + } + } + else if (millis() - lastAttemptTime > requestInterval) { + // if you're not connected, and two minutes have passed since + // your last connection, then attempt to connect again: + connectToServer(); + } +} + +void connectToServer() { + // attempt to connect, and wait a millisecond: + Serial.println("connecting to server..."); + if (client.connect(server, 80)) { + Serial.println("making HTTP request..."); + // make HTTP GET request to twitter: + client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); + client.println("HOST: api.twitter.com"); + client.println(); + } + // note the time of this connect attempt: + lastAttemptTime = millis(); +} + diff --git a/WiFi/examples/WebClientWPA/WebClientWPA.ino b/WiFi/examples/WebClientWPA/WebClientWPA.ino new file mode 100644 index 000000000..4bfca16b8 --- /dev/null +++ b/WiFi/examples/WebClientWPA/WebClientWPA.ino @@ -0,0 +1,77 @@ + +/* + Web client + + This sketch connects to a website (http://www.google.com) + using a WiFi shield. + + Circuit: + * WiFi shield attached + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 15 Sep 2011 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "lamaison"; // your network SSID (name) +char pass[] = "tenantaccess247"; // your network password + +int status = WL_IDLE_STATUS; +IPAddress server(74,125,115,105); // Google + +// 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); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + // don't do anything else: + while(true); + } + else { + Serial.println("Connected to wifi"); + Serial.println("\nStarting connection..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected"); + // Make a HTTP request: + client.println("GET /search?q=arduino HTTP/1.0"); + client.println(); + } + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + if (client.available()) { + char c = client.read(); + Serial.print(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + + // do nothing forevermore: + for(;;) + ; + } +} + + diff --git a/WiFi/examples/WebServerWPA/WebServerWPA.ino b/WiFi/examples/WebServerWPA/WebServerWPA.ino new file mode 100644 index 000000000..6dd3138d8 --- /dev/null +++ b/WiFi/examples/WebServerWPA/WebServerWPA.ino @@ -0,0 +1,95 @@ +/* + Web Server + + A simple web server that shows the value of the analog input pins. + using a WiFi shield. + + Circuit: + * WiFi shield attached + * Analog inputs attached to pins A0 through A5 (optional) + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 15 Sep 2011 + by Tom Igoe + */ + + +#include +#include + +char ssid[] = "lamaison"; // your network SSID (name) +char pass[] = "tenantaccess247"; // your network password +int status = WL_IDLE_STATUS; + +WiFiServer server(80); + +void setup() { + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + server.begin(); + Serial.print("Connected to wifi. My address:"); + IPAddress myAddress = WiFi.localIP(); + Serial.println(myAddress); + + } +} + + +void loop() { + // listen for incoming clients + WiFiClient client = server.available(); + if (client) { + // an http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + // 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(); + client.println(""); + // output the value of each analog input pin + for (int analogChannel = 0; analogChannel < 6; analogChannel++) { + client.print("analog input "); + client.print(analogChannel); + client.print(" is "); + client.print(analogRead(analogChannel)); + 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(); + } +} + + diff --git a/WiFi/examples/PachubeClientString/PachubeClientString.ino b/WiFi/examples/old_examples/PachubeClientString/PachubeClientString.ino similarity index 100% rename from WiFi/examples/PachubeClientString/PachubeClientString.ino rename to WiFi/examples/old_examples/PachubeClientString/PachubeClientString.ino diff --git a/WiFi/examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino b/WiFi/examples/old_examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino similarity index 100% rename from WiFi/examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino rename to WiFi/examples/old_examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino diff --git a/WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino b/WiFi/examples/old_examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino similarity index 100% rename from WiFi/examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino rename to WiFi/examples/old_examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino diff --git a/WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino b/WiFi/examples/old_examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino similarity index 100% rename from WiFi/examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino rename to WiFi/examples/old_examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino diff --git a/WiFi/examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino b/WiFi/examples/old_examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino similarity index 100% rename from WiFi/examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino rename to WiFi/examples/old_examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino diff --git a/WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino b/WiFi/examples/old_examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino similarity index 100% rename from WiFi/examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino rename to WiFi/examples/old_examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino diff --git a/WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino b/WiFi/examples/old_examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino similarity index 100% rename from WiFi/examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino rename to WiFi/examples/old_examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino diff --git a/WiFi/examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino b/WiFi/examples/old_examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino similarity index 100% rename from WiFi/examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino rename to WiFi/examples/old_examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino diff --git a/WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino b/WiFi/examples/old_examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino similarity index 100% rename from WiFi/examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino rename to WiFi/examples/old_examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino diff --git a/WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino b/WiFi/examples/old_examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino similarity index 100% rename from WiFi/examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino rename to WiFi/examples/old_examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino diff --git a/WiFi/examples/wifi_Server_example/wifi_Server_example.ino b/WiFi/examples/old_examples/wifi_Server_example/wifi_Server_example.ino similarity index 100% rename from WiFi/examples/wifi_Server_example/wifi_Server_example.ino rename to WiFi/examples/old_examples/wifi_Server_example/wifi_Server_example.ino diff --git a/WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino b/WiFi/examples/old_examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino similarity index 100% rename from WiFi/examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino rename to WiFi/examples/old_examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino diff --git a/WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino b/WiFi/examples/old_examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino similarity index 100% rename from WiFi/examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino rename to WiFi/examples/old_examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino diff --git a/WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino b/WiFi/examples/old_examples/wifi_WEP_example/wifi_WEP_example.ino similarity index 100% rename from WiFi/examples/wifi_WEP_example/wifi_WEP_example.ino rename to WiFi/examples/old_examples/wifi_WEP_example/wifi_WEP_example.ino diff --git a/WiFi/examples/wifi_example/wifi_example.pde b/WiFi/examples/old_examples/wifi_example/wifi_example.pde similarity index 100% rename from WiFi/examples/wifi_example/wifi_example.pde rename to WiFi/examples/old_examples/wifi_example/wifi_example.pde From 7d3629302e3ee64160ec36aa2212a23ed29a5cb1 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 10 Jan 2012 08:46:46 -0500 Subject: [PATCH 30/98] Updated ScanNetworks examples to make sure scanning happened after wifi.Begin() --- .../ScanNetworksOpen/ScanNetworksOpen.ino | 9 +++--- .../ScanNetworksWPA/ScanNetworksWPA.ino | 29 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino b/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino index 3e5f07534..155a263b9 100644 --- a/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino +++ b/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino @@ -8,7 +8,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 15 Sep 2011 + modified 10 Jan 2012 by Tom Igoe */ @@ -28,13 +28,14 @@ void setup() { // initialize serial: Serial.begin(9600); - // scan for existing networks: - Serial.println("Scanning available networks..."); - scanNetworks(); // attempt to connect using WEP encryption: Serial.println("Attempting to connect to open network..."); status = WiFi.begin(ssid); + + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); Serial.print("SSID: "); Serial.println(ssid); diff --git a/WiFi/examples/ScanNetworksWPA/ScanNetworksWPA.ino b/WiFi/examples/ScanNetworksWPA/ScanNetworksWPA.ino index d209888a4..d811842f7 100644 --- a/WiFi/examples/ScanNetworksWPA/ScanNetworksWPA.ino +++ b/WiFi/examples/ScanNetworksWPA/ScanNetworksWPA.ino @@ -8,7 +8,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 15 Sep 2011 + modified 10 Jan 2012 by Tom Igoe */ @@ -16,8 +16,8 @@ #include #include -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password +char ssid[] = "networkName"; // your network SSID (name) +char pass[] = "yourPassword"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status byte mac[6]; // the MAC address of your Wifi shield @@ -28,20 +28,19 @@ IPAddress subnet; // the subnet mask void setup() { // initialize serial: Serial.begin(9600); - // scan for existing networks: - Serial.println("Scanning available networks..."); - //WiFi.begin(); - scanNetworks(); - + // attempt to connect using WEP encryption: -// Serial.println("Attempting to connect to WEP-128 network..."); -// status = WiFi.begin(ssid, keyIndex, key); - - // attempt to connect using WPA2 encryption: + // Serial.println("Attempting to connect to WEP-128 network..."); + // status = WiFi.begin(ssid, keyIndex, key); + + // attempt to connect using WPA2 encryption: Serial.println("Attempting to connect to WPA network..."); status = WiFi.begin(ssid, pass); - + Serial.println("Scanning available networks..."); + // scan for existing networks: + scanNetworks(); + Serial.print("SSID: "); Serial.println(ssid); @@ -52,6 +51,7 @@ void setup() { } // if you are connected, print out info about the connection: else { + printIpData(); printCurrentNet(); } @@ -71,7 +71,7 @@ void printIpData() { subnet = WiFi.subnetMask(); Serial.print("NETMASK: "); Serial.println(subnet); - + // print your gateway address: gateway = WiFi.gatewayIP(); @@ -141,3 +141,4 @@ void scanNetworks() { Serial.println(WiFi.SSID(thisNet)); } } + From 97177fe51a44038d41308ad05d990cdd987a86ca Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 10 Jan 2012 08:46:58 -0500 Subject: [PATCH 31/98] Deleted old passwords --- WiFi/examples/WebClientWPA/WebClientWPA.ino | 6 +++--- WiFi/examples/WebServerWPA/WebServerWPA.ino | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/WiFi/examples/WebClientWPA/WebClientWPA.ino b/WiFi/examples/WebClientWPA/WebClientWPA.ino index 4bfca16b8..519ea0d67 100644 --- a/WiFi/examples/WebClientWPA/WebClientWPA.ino +++ b/WiFi/examples/WebClientWPA/WebClientWPA.ino @@ -10,7 +10,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 15 Sep 2011 + modified 10 Jan 2012 by Tom Igoe */ @@ -18,8 +18,8 @@ #include #include -char ssid[] = "lamaison"; // your network SSID (name) -char pass[] = "tenantaccess247"; // your network password +char ssid[] = "networkName"; // your network SSID (name) +char pass[] = "yourPassword"; // your network password int status = WL_IDLE_STATUS; IPAddress server(74,125,115,105); // Google diff --git a/WiFi/examples/WebServerWPA/WebServerWPA.ino b/WiFi/examples/WebServerWPA/WebServerWPA.ino index 6dd3138d8..f89ba8445 100644 --- a/WiFi/examples/WebServerWPA/WebServerWPA.ino +++ b/WiFi/examples/WebServerWPA/WebServerWPA.ino @@ -10,7 +10,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 15 Sep 2011 + modified 10 Jan 2012 by Tom Igoe */ @@ -18,8 +18,8 @@ #include #include -char ssid[] = "lamaison"; // your network SSID (name) -char pass[] = "tenantaccess247"; // your network password +char ssid[] = "networkName"; // your network SSID (name) +char pass[] = "yourPassword"; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); From b61427ddc8dd6425cb58b9bd24411b979fafff0f Mon Sep 17 00:00:00 2001 From: Domenico La Fauci Date: Mon, 30 Jan 2012 00:07:39 +0100 Subject: [PATCH 32/98] Bugfix on Test Report 12.01.2012 --- WiFi/utility/debug.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index b9cd3c2aa..0cc05d49c 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -39,6 +39,11 @@ Serial.print(x,16);Serial.print(",");Serial.println(y,16); \ }while (0); +#define INFO(format, args...) do { \ + char buf[250]; \ + sprintf(buf, format, args); \ + Serial.println(buf); \ +} while(0); #else #define INFO1(x) do {} while(0); From 00c36dc56368813cd4aa97452d673b9797debe64 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Sat, 4 Feb 2012 12:29:12 +0100 Subject: [PATCH 33/98] WiFi libraries: Bugfix on TestReport 12.01.2012 --- WiFi/WiFiClient.cpp | 22 +++++++++++------ WiFi/WiFiServer.cpp | 35 +++++++++++++++++++++++---- WiFi/WiFiServer.h | 1 + WiFi/utility/debug.h | 11 +++++---- WiFi/utility/server_drv.cpp | 48 ++++++++++++++++++++++++++++++++++--- WiFi/utility/server_drv.h | 10 +++++--- WiFi/utility/wifi_spi.h | 2 ++ 7 files changed, 106 insertions(+), 23 deletions(-) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index e11b086e2..56802fd14 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -11,6 +11,7 @@ extern "C" { #include "WiFiServer.h" #include "server_drv.h" + uint16_t WiFiClient::_srcport = 1024; WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) { @@ -41,7 +42,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { _sock = getFirstSocket(); if (_sock != NO_SOCKET_AVAIL) { - ServerDrv::StartClient(uint32_t(ip), port, _sock); + ServerDrv::startClient(uint32_t(ip), port, _sock); WiFiClass::_state[_sock] = _sock; }else{ return 0; @@ -106,11 +107,13 @@ void WiFiClient::flush() { } void WiFiClient::stop() { + + INFO("1)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status()); if (_sock == 255) return; - - // attempt to close the connection gracefully (send a FIN to other side) - disconnect(WiFiClass::_state[_sock]); + + ServerDrv::stopClient(_sock); + unsigned long start = millis(); // wait a second for the connection to close @@ -119,9 +122,13 @@ void WiFiClient::stop() { // if it hasn't closed, close it forcefully if (status() != CLOSED) - close(_sock); + { + //TODO force close + //close(_sock); + } + - WiFiClass::_server_port[_sock] = 0; + INFO("2)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status()); _sock = 255; } @@ -130,6 +137,7 @@ uint8_t WiFiClient::connected() { return 0; } else { uint8_t s = status(); + INFO("Client status: %d", s); return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || (s == CLOSE_WAIT && !available())); } @@ -139,7 +147,7 @@ uint8_t WiFiClient::status() { if (_sock == 255) { return CLOSED; } else { - return ServerDrv::getState(_sock); + return ServerDrv::getClientState(_sock); } } diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp index 860759383..cd0a0f5ab 100644 --- a/WiFi/WiFiServer.cpp +++ b/WiFi/WiFiServer.cpp @@ -1,6 +1,10 @@ #include #include "server_drv.h" +extern "C" { + #include "utility/debug.h" +} + #include "WiFi.h" #include "WiFiClient.h" #include "WiFiServer.h" @@ -17,7 +21,7 @@ void WiFiServer::begin() uint8_t _sock = WiFiClass::getSocket(); if (_sock != NO_SOCKET_AVAIL) { - ServerDrv::StartServer(_port, _sock); + ServerDrv::startServer(_port, _sock); WiFiClass::_server_port[_sock] = _port; } } @@ -25,18 +29,34 @@ void WiFiServer::begin() WiFiClient WiFiServer::available(byte* status) { //accept(); + static byte tmp_cli_status = 0; + static byte tmp_ser_status = 0; + static int cycle = 0; for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { - if (WiFiClass::_server_port[sock] != 0) + if (WiFiClass::_server_port[sock] == _port) { WiFiClient client(sock); - int _status = client.status(); + uint8_t _status = client.status(); + uint8_t _ser_status = this->status(); + if ((tmp_cli_status != _status)||(tmp_ser_status != _ser_status)) + { + INFO("%d)Sock: %d Client Status: %d Server Status: %d port: %d", cycle, sock, _status, _ser_status, WiFiClass::_server_port[sock]); + tmp_cli_status = _status; + tmp_ser_status = _ser_status; + cycle = 0; + }else{ + ++cycle; + } if (status != NULL) *status = _status; - if (WiFiClass::_server_port[sock] == _port && - _status == ESTABLISHED) + //server not in listen state, restart it + if (this->status()==0) + ServerDrv::startServer(_port, sock); + + if (_status == ESTABLISHED) { return client; //TODO } @@ -46,6 +66,11 @@ WiFiClient WiFiServer::available(byte* status) return WiFiClient(255); } +uint8_t WiFiServer::status() { + return ServerDrv::getServerState(0); +} + + size_t WiFiServer::write(uint8_t b) { return write(&b, 1); diff --git a/WiFi/WiFiServer.h b/WiFi/WiFiServer.h index d93b0ced1..68b574c29 100755 --- a/WiFi/WiFiServer.h +++ b/WiFi/WiFiServer.h @@ -19,6 +19,7 @@ public: 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; }; diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 0cc05d49c..089103cca 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -30,6 +30,12 @@ #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); @@ -39,11 +45,6 @@ Serial.print(x,16);Serial.print(",");Serial.println(y,16); \ }while (0); -#define INFO(format, args...) do { \ - char buf[250]; \ - sprintf(buf, format, args); \ - Serial.println(buf); \ -} while(0); #else #define INFO1(x) do {} while(0); diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index b403a31b3..30bc5187c 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -12,7 +12,7 @@ extern "C" { // Start server TCP on port specified -void ServerDrv::StartServer(uint16_t port, uint8_t sock) +void ServerDrv::startServer(uint16_t port, uint8_t sock) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -34,7 +34,7 @@ void ServerDrv::StartServer(uint16_t port, uint8_t sock) } // Start server TCP on port specified -void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock) +void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock) { WAIT_FOR_SLAVE_SELECT(); // Send Command @@ -56,7 +56,29 @@ void ServerDrv::StartClient(uint32_t ipAddress, uint16_t port, uint8_t sock) SpiDrv::spiSlaveDeselect(); } -uint8_t ServerDrv::getState(uint8_t sock) +// 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 @@ -77,6 +99,26 @@ uint8_t ServerDrv::getState(uint8_t sock) 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) { diff --git a/WiFi/utility/server_drv.h b/WiFi/utility/server_drv.h index 6d28d13f1..c7afcff23 100644 --- a/WiFi/utility/server_drv.h +++ b/WiFi/utility/server_drv.h @@ -8,11 +8,15 @@ class ServerDrv { public: // Start server TCP on port specified - static void StartServer(uint16_t port, uint8_t sock); + static void startServer(uint16_t port, uint8_t sock); - static void StartClient(uint32_t ipAddress, 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 getState(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); diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index 576c5fb11..cdcc02fea 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -39,6 +39,8 @@ enum { 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, // All command with DATA_FLAG 0x40 send a 16bit Len From 4558c021b7cd620cb4e8e0e8c6d4e5323263a22f Mon Sep 17 00:00:00 2001 From: Domenico La Fauci Date: Sat, 4 Feb 2012 17:01:24 +0100 Subject: [PATCH 34/98] WiFi libraries: fix compiler warning --- WiFi/WiFi.cpp | 1 + WiFi/WiFiClient.cpp | 1 + WiFi/utility/socket.c | 16 ++++++++-------- WiFi/utility/spi_drv.cpp | 9 ++++----- WiFi/utility/wifi_drv.cpp | 20 ++++++-------------- 5 files changed, 20 insertions(+), 27 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 3ed9d686c..0f10babec 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -46,6 +46,7 @@ int WiFiClass::begin() { // Add procedure to read the latest configuration from eeprom/dataflash // and start the wifi connection + return WL_IDLE_STATUS; } int WiFiClass::begin(char* ssid) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index 56802fd14..c3d67a5b7 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -36,6 +36,7 @@ int WiFiClient::connect(const char* host, uint16_t port) { return ret; } #endif + return 0; } int WiFiClient::connect(IPAddress ip, uint16_t port) { diff --git a/WiFi/utility/socket.c b/WiFi/utility/socket.c index 02d0f4c5f..665073b04 100644 --- a/WiFi/utility/socket.c +++ b/WiFi/utility/socket.c @@ -7,14 +7,14 @@ #include #include "socket.h" -SOCKET socket(uint8 protocol) {} // Opens a socket(TCP or UDP or IP_RAW mode) +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) {} // Establish TCP connection (Active connection) +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) {} // Establish TCP connection (Passive connection) -uint16 send(SOCKET s, const uint8 * buf, uint16 len) {} // Send data (TCP) -uint16 recv(SOCKET s, uint8 * buf, uint16 len) {} // Receive data (TCP) -uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port) {} // Send data (UDP/IP RAW) -uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port) {} // Receive data (UDP/IP RAW) +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) {} +uint16 igmpsend(SOCKET s, const uint8 * buf, uint16 len) {return 0;} diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index b8cd18b28..dc0104218 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -181,7 +181,7 @@ void SpiDrv::getParam(uint8_t* param) int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len) { char _data = 0; - int i =0, ii = 0; + int ii = 0; IF_CHECK_START_CMD(_data) { @@ -233,7 +233,7 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t int SpiDrv::waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len) { char _data = 0; - int i =0, ii = 0; + uint16_t ii = 0; IF_CHECK_START_CMD(_data) { @@ -259,7 +259,7 @@ int SpiDrv::waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len) int SpiDrv::waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len) { char _data = 0; - int i =0, ii = 0; + int ii = 0; IF_CHECK_START_CMD(_data) { @@ -464,7 +464,7 @@ uint16_t SpiDrv::readParamLen16(uint16_t* param_len) void SpiDrv::sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam) { - int i = 0; + uint16_t i = 0; // Send Spi paramLen sendParamLen16(param_len); @@ -483,7 +483,6 @@ void SpiDrv::sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam) void SpiDrv::sendParam(uint16_t param, uint8_t lastParam) { - int i = 0; // Send Spi paramLen sendParamLen8(2); diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 04c44c257..24d160674 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -41,8 +41,6 @@ void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip) SpiDrv::waitForSlaveReady(); // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params); SpiDrv::spiSlaveDeselect(); @@ -182,7 +180,7 @@ uint8_t* WiFiDrv::getMacAddress() // Wait for reply uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); + SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -200,14 +198,12 @@ void WiFiDrv::getIpAddress(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); ip = _subnetMask; - //memcpy(ip, _subnetMask, WL_IPV4_LENGTH); } void WiFiDrv::getGatewayIP(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); ip = _gatewayIp; - //memcpy(ip, _gatewayIp, WL_IPV4_LENGTH); } char* WiFiDrv::getCurrentSSID() @@ -225,7 +221,7 @@ char* WiFiDrv::getCurrentSSID() // Wait for reply uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); + SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -247,7 +243,7 @@ uint8_t* WiFiDrv::getCurrentBSSID() // Wait for reply uint8_t _dataLen = 0; - uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); + SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -270,7 +266,7 @@ int32_t WiFiDrv::getCurrentRSSI() // Wait for reply uint8_t _dataLen = 0; int32_t rssi = 0; - uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); + SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen); SpiDrv::spiSlaveDeselect(); @@ -293,7 +289,7 @@ uint8_t WiFiDrv::getCurrentEncryptionType() // Wait for reply uint8_t dataLen = 0; uint8_t encType = 0; - uint8_t result = SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)encType, &dataLen); + SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)encType, &dataLen); SpiDrv::spiSlaveDeselect(); @@ -314,12 +310,8 @@ uint8_t WiFiDrv::scanNetworks() SpiDrv::waitForSlaveReady(); // Wait for reply - uint16_t _dataLen = 0; - - tParam params[WL_NETWORKS_LIST_MAXNUM]; - uint8_t ssidListNum = 0; - uint8_t result = SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM); + SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM); SpiDrv::spiSlaveDeselect(); From fa9393f7c439051883c9153ee75c0d43226b77cf Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Fri, 10 Feb 2012 00:49:42 +0100 Subject: [PATCH 35/98] Avoid to print Warning Messages on Arduino monitor. Add debug command on wifi shield to enable debug msgs. Solved issue on repeat quick refresh or stop and go web server --- WiFi/WiFi.cpp | 3 +-- WiFi/WiFiClient.cpp | 46 ++++++++++++++++------------------ WiFi/WiFiServer.cpp | 23 ++++++----------- WiFi/utility/debug.h | 8 ++++-- WiFi/utility/server_drv.cpp | 49 +++++++++++++++++++++++-------------- WiFi/utility/server_drv.h | 2 +- 6 files changed, 67 insertions(+), 64 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 0f10babec..9fd4bba98 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -1,8 +1,6 @@ #include "wifi_drv.h" #include "WiFi.h" -#define _DEBUG_ - extern "C" { #include "utility/wl_definitions.h" #include "utility/wl_types.h" @@ -101,6 +99,7 @@ int WiFiClass::begin(char* ssid, const char *passphrase) { delay(WL_DELAY_START_CONNECTION); status = WiFiDrv::getConnectionStatus(); + INFO("Status: %d", status); } while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); }else{ diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index c3d67a5b7..075af56c9 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -52,25 +52,29 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { } size_t WiFiClient::write(uint8_t b) { - if (_sock != 255) - { - START(); - ServerDrv::sendData(_sock, &b, 1); - while (!ServerDrv::isDataSent(_sock)); - END(); - return 1; - } - return 0; + return write(&b, 1); } size_t WiFiClient::write(const uint8_t *buf, size_t size) { - if (_sock != 255) + if (_sock >= MAX_SOCK_NUM) { - ServerDrv::sendData(_sock, buf, size); - while (!ServerDrv::isDataSent(_sock)); - return size; + setWriteError(); + return 0; } - 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() { @@ -86,6 +90,7 @@ int WiFiClient::read() { uint8_t b; if (!available()) return -1; + ServerDrv::getData(_sock, &b); return b; } @@ -109,7 +114,6 @@ void WiFiClient::flush() { void WiFiClient::stop() { - INFO("1)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status()); if (_sock == 255) return; @@ -121,24 +125,16 @@ void WiFiClient::stop() { while (status() != CLOSED && millis() - start < 1000) delay(1); - // if it hasn't closed, close it forcefully - if (status() != CLOSED) - { - //TODO force close - //close(_sock); - } - - - INFO("2)Stop WiFi client sock:%d state:%d status:%d", _sock, WiFiClass::_state[_sock], status()); _sock = 255; } uint8_t WiFiClient::connected() { + if (_sock == 255) { return 0; } else { uint8_t s = status(); - INFO("Client status: %d", s); + return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || (s == CLOSE_WAIT && !available())); } diff --git a/WiFi/WiFiServer.cpp b/WiFi/WiFiServer.cpp index cd0a0f5ab..77dbac0b9 100644 --- a/WiFi/WiFiServer.cpp +++ b/WiFi/WiFiServer.cpp @@ -9,8 +9,6 @@ extern "C" { #include "WiFiClient.h" #include "WiFiServer.h" - - WiFiServer::WiFiServer(uint16_t port) { _port = port; @@ -28,10 +26,8 @@ void WiFiServer::begin() WiFiClient WiFiServer::available(byte* status) { - //accept(); - static byte tmp_cli_status = 0; - static byte tmp_ser_status = 0; - static int cycle = 0; + static int cycle_server_down = 0; + const int TH_SERVER_DOWN = 50; for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { @@ -40,21 +36,16 @@ WiFiClient WiFiServer::available(byte* status) WiFiClient client(sock); uint8_t _status = client.status(); uint8_t _ser_status = this->status(); - if ((tmp_cli_status != _status)||(tmp_ser_status != _ser_status)) - { - INFO("%d)Sock: %d Client Status: %d Server Status: %d port: %d", cycle, sock, _status, _ser_status, WiFiClass::_server_port[sock]); - tmp_cli_status = _status; - tmp_ser_status = _ser_status; - cycle = 0; - }else{ - ++cycle; - } + if (status != NULL) *status = _status; //server not in listen state, restart it - if (this->status()==0) + if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN)) + { ServerDrv::startServer(_port, sock); + cycle_server_down = 0; + } if (_status == ESTABLISHED) { diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 089103cca..29d786f59 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -52,9 +52,13 @@ #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 #define DBG_PIN2 5 #define DBG_PIN 4 @@ -65,9 +69,9 @@ #define RST_TRIGGER() digitalWrite(DBG_PIN, LOW); #define INIT_TRIGGER() pinMode(DBG_PIN, OUTPUT); \ - pinMode(DBG_PIN2, OUTPUT); \ + pinMode(DBG_PIN2, OUTPUT); \ RST_TRIGGER() -#define TOGGLE_TRIGGER() SET_TRIGGER() \ +#define TOGGLE_TRIGGER() SET_TRIGGER() \ delayMicroseconds(2); \ RST_TRIGGER() diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index 30bc5187c..7b5194089 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -3,8 +3,6 @@ #include "Arduino.h" #include "spi_drv.h" -#define _DEBUG_ - extern "C" { #include "wl_types.h" #include "debug.h" @@ -223,26 +221,41 @@ bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) } -uint8_t ServerDrv::isDataSent(uint8_t sock) +uint8_t ServerDrv::checkDataSent(uint8_t sock) { - WAIT_FOR_SLAVE_SELECT(); - // Send Command - SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); - SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); + const uint16_t TIMEOUT_DATA_SENT = 250; + static uint16_t timeout = 0; + uint8_t _data = 0; + uint8_t _dataLen = 0; - //Wait the reply elaboration - SpiDrv::waitForSlaveReady(); + do { + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1); + SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM); - // Wait for reply - uint8_t _data = 0; - uint8_t _dataLen = 0; - if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen)) - { - WARN("error waitResponse isDataSent"); - } - SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); - return _data; + // 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; + if (timeout > TIMEOUT_DATA_SENT) + { + timeout = 0; + INFO1("Timeout wainting for data sent"); + } + } + }while((_data==0)&&(timeout Date: Fri, 10 Feb 2012 01:00:11 +0100 Subject: [PATCH 36/98] Delete some prints --- WiFi/WiFi.cpp | 1 - WiFi/utility/debug.h | 10 ---------- WiFi/utility/spi_drv.cpp | 4 ---- 3 files changed, 15 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 9fd4bba98..9d30c0ade 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -99,7 +99,6 @@ int WiFiClass::begin(char* ssid, const char *passphrase) { delay(WL_DELAY_START_CONNECTION); status = WiFiDrv::getConnectionStatus(); - INFO("Status: %d", status); } while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0)); }else{ diff --git a/WiFi/utility/debug.h b/WiFi/utility/debug.h index 29d786f59..437ec6c94 100644 --- a/WiFi/utility/debug.h +++ b/WiFi/utility/debug.h @@ -13,16 +13,6 @@ #include #include -#define INFO_0 1 -#define INFO_1 2 -#define INFO_2 4 -#define INFO_3 8 -#define INFO_4 16 -#define INFO_5 32 -#define INFO_D (1<<0xD) // Debug -#define INFO_E (1<<0xE) // Error -#define INFO_F (1<<0xF) // Warning - #define PRINT_FILE_LINE() do { \ Serial.print("[");Serial.print(__FILE__); \ Serial.print("::");Serial.print(__LINE__);Serial.print("]");\ diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index dc0104218..93c693149 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -140,7 +140,6 @@ char SpiDrv::readChar() { \ TOGGLE_TRIGGER() \ WARN("Error waiting START_CMD"); \ - Serial.println(cmd, 16); \ return 0; \ }else \ @@ -307,14 +306,12 @@ int SpiDrv::waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params) } else { WARN("Error numParam == 0"); - Serial.println(cmd, 16); return 0; } if (numParam != _numParam) { WARN("Mismatch numParam"); - Serial.println(cmd, 16); return 0; } @@ -402,7 +399,6 @@ int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, u } else { WARN("Error numParams == 0"); - Serial.println(cmd, 16); return 0; } readAndCheckChar(END_CMD, &_data); From 47f082800c5e20e6333126bfa6a3aa9b53914f56 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 1 Mar 2012 09:34:42 -0500 Subject: [PATCH 37/98] Small change in Twitter example --- WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino b/WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino index 1a5a9c397..f28da4ac0 100644 --- a/WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino +++ b/WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino @@ -112,7 +112,7 @@ void connectToServer() { Serial.println("making HTTP request..."); // make HTTP GET request to twitter: client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); - client.println("HOST: api.twitter.com"); + client.println("HOST:api.twitter.com"); client.println(); } // note the time of this connect attempt: From 63a462862a9d71040089bb27457b6dd72c5d8f68 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 2 Mar 2012 13:30:57 -0500 Subject: [PATCH 38/98] Added ScanNetworks example, for all encryption types Since this example doesn't need a connection to a network to see what nets are out there, I went with one example rather than three. --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 79 +++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 WiFi/examples/ScanNetworks/ScanNetworks.ino diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino new file mode 100644 index 000000000..99a490b8e --- /dev/null +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -0,0 +1,79 @@ +/* + + 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 1 Mar 2012 + by Tom Igoe + */ + + +#include +#include + +void setup() { + // initialize serial: + Serial.begin(9600); + + // attempt to connect using WEP encryption: + Serial.println("Initializing Wifi..."); + WiFi.begin("networName"); + printMacAddress(); + + + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); +} + +void loop() { + delay(10000); + // scan for existing networks: + Serial.println("Scanning available networks..."); + scanNetworks(); +} + +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 scanNetworks() { + // scan for nearby networks: + Serial.println("** Scan Networks **"); + byte numSsid = WiFi.scanNetworks(); + + // print the list of networks seen: + Serial.print("SSID List:"); + Serial.println(numSsid); + // print the network number and name for each network found: + for (int thisNet = 0; thisNet Date: Fri, 2 Mar 2012 13:31:22 -0500 Subject: [PATCH 39/98] Deleted old ScanNetworks example in favor of generic one. --- .../ScanNetworksOpen/ScanNetworksOpen.ino | 138 ----------------- .../ScanNetworksWPA/ScanNetworksWPA.ino | 144 ------------------ 2 files changed, 282 deletions(-) delete mode 100644 WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino delete mode 100644 WiFi/examples/ScanNetworksWPA/ScanNetworksWPA.ino diff --git a/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino b/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino deleted file mode 100644 index 155a263b9..000000000 --- a/WiFi/examples/ScanNetworksOpen/ScanNetworksOpen.ino +++ /dev/null @@ -1,138 +0,0 @@ -/* - - Open connection using the WiFi shield. Attempts to connect - and prints out info about the network - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 10 Jan 2012 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "yourNetwork"; // the name of your network -int status = WL_IDLE_STATUS; // the Wifi radio's status - -byte mac[6]; // the MAC address of your Wifi shield -IPAddress ip; // the IP address of your shield -IPAddress gateway; // the router's address -IPAddress subnet; // the subnet mask - -void setup() { - // initialize serial: - Serial.begin(9600); - - - // attempt to connect using WEP encryption: - Serial.println("Attempting to connect to open network..."); - status = WiFi.begin(ssid); - - // scan for existing networks: - Serial.println("Scanning available networks..."); - scanNetworks(); - - Serial.print("SSID: "); - Serial.println(ssid); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - // if you are connected, print out info about the connection: - else { - printIpData(); - printCurrentNet(); - } -} - -void loop() { - // do nothing -} - -void printIpData() { - // print your WiFi shield's IP address: - ip = WiFi.localIP(); - Serial.println(ip); - - - // print your subnet mask: - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.println(subnet); - - - // print your gateway address: - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.println(gateway); - - // 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 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("RSSI:"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption,HEX); -} - -void scanNetworks() { - // scan for nearby networks: - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - - // print the list of networks seen: - Serial.print("SSID List:"); - Serial.println(numSsid); - // print the network number and name for each network found: - for (int thisNet = 0; thisNet -#include - -char ssid[] = "networkName"; // your network SSID (name) -char pass[] = "yourPassword"; // your network password -int status = WL_IDLE_STATUS; // the Wifi radio's status - -byte mac[6]; // the MAC address of your Wifi shield -IPAddress ip; // the IP address of your shield -IPAddress gateway; // the router's address -IPAddress subnet; // the subnet mask - -void setup() { - // initialize serial: - Serial.begin(9600); - - // attempt to connect using WEP encryption: - // Serial.println("Attempting to connect to WEP-128 network..."); - // status = WiFi.begin(ssid, keyIndex, key); - - // attempt to connect using WPA2 encryption: - Serial.println("Attempting to connect to WPA network..."); - status = WiFi.begin(ssid, pass); - - Serial.println("Scanning available networks..."); - // scan for existing networks: - scanNetworks(); - - Serial.print("SSID: "); - Serial.println(ssid); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - // if you are connected, print out info about the connection: - else { - - printIpData(); - printCurrentNet(); - } -} - -void loop() { - // do nothing -} - -void printIpData() { - // print your WiFi shield's IP address: - ip = WiFi.localIP(); - Serial.println(ip); - - - // print your subnet mask: - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.println(subnet); - - - // print your gateway address: - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.println(gateway); - - // 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 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("RSSI:"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption,HEX); -} - -void scanNetworks() { - // scan for nearby networks: - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - - // print the list of networks seen: - Serial.print("SSID List:"); - Serial.println(numSsid); - // print the network number and name for each network found: - for (int thisNet = 0; thisNet Date: Fri, 2 Mar 2012 13:31:48 -0500 Subject: [PATCH 40/98] Added basic connection examples for all three encryption schemes --- .../ConnectNoEncryption.ino | 112 ++++++++++++++++++ .../ConnectWithWEP/ConnectWithWEP.ino | 105 ++++++++++++++++ .../ConnectWithWPA/ConnectWithWPA.ino | 105 ++++++++++++++++ 3 files changed, 322 insertions(+) create mode 100644 WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino create mode 100644 WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino create mode 100644 WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino new file mode 100644 index 000000000..85207eac0 --- /dev/null +++ b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -0,0 +1,112 @@ +/* + + 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 29 Feb 2012 + by Scott Fitzgerald + */ + #include + +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); + + // 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"); + while(true); + } + // if you are connected : + else { + 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); + + // 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); + + // print your subnet mask: + IPAddress subnet = WiFi.subnetMask(); + Serial.print("NetMask: "); + Serial.println(subnet); + + // print your gateway address: + IPAddress gateway = WiFi.gatewayIP(); + Serial.print("Gateway: "); + Serial.println(gateway); +} + +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 sgtrength (RSSI):"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); +} + diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino new file mode 100644 index 000000000..e97871d23 --- /dev/null +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -0,0 +1,105 @@ +/* + + 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 29 Feb 2012 + by Scott Fitzgerald + */ + #include + +char ssid[] = "YourNetwork"; // your network SSID (name) +char key[] = "725d223132"; // 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); + + // attempt to connect to an open network: + Serial.print("Attempting to connect to WEP network: "); + Serial.println(ssid); + status = WiFi.begin(ssid, pass, key); + + // if you're not connected, stop here: + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + // if you are connected : + else { + 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); + + // 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 sgtrength (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..7813ba8e1 --- /dev/null +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -0,0 +1,105 @@ +/* + + 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 29 Feb 2012 + by Scott Fitzgerald + */ + #include + +char ssid[] = "networkName"; // your network SSID (name) +char pass[] = "yourPassword"; // 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"); + while(true); + } + // if you are connected : + else { + 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); + + // 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 sgtrength (RSSI):"); + Serial.println(rssi); + + // print the encryption type: + byte encryption = WiFi.encryptionType(); + Serial.print("Encryption Type:"); + Serial.println(encryption,HEX); + Serial.println(); +} + From 3e894fdcc042617c343bf7c9573039ce85c68e2e Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 2 Mar 2012 13:34:17 -0500 Subject: [PATCH 41/98] Fixed spelling errror in Wifi connect examples. --- WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino | 2 +- WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino | 2 +- WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino index 85207eac0..efedf6c73 100644 --- a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino +++ b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -101,7 +101,7 @@ void printCurrentNet() { // print the received signal strength: long rssi = WiFi.RSSI(); - Serial.print("signal sgtrength (RSSI):"); + Serial.print("signal strength (RSSI):"); Serial.println(rssi); // print the encryption type: diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino index e97871d23..4cad6f085 100644 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -93,7 +93,7 @@ void printCurrentNet() { // print the received signal strength: long rssi = WiFi.RSSI(); - Serial.print("signal sgtrength (RSSI):"); + Serial.print("signal strength (RSSI):"); Serial.println(rssi); // print the encryption type: diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino index 7813ba8e1..a01496c85 100644 --- a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -93,7 +93,7 @@ void printCurrentNet() { // print the received signal strength: long rssi = WiFi.RSSI(); - Serial.print("signal sgtrength (RSSI):"); + Serial.print("signal strength (RSSI):"); Serial.println(rssi); // print the encryption type: From 1d1f647ed65fafc97a6fab273e8d6daa06984807 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 2 Mar 2012 16:30:06 -0500 Subject: [PATCH 42/98] Added Pachube clients (untested) --- .../WifiPachubeClient/WifiPachubeClient.ino | 146 ++++++++++++++++++ .../WifiPachubeClientString.ino | 127 +++++++++++++++ 2 files changed, 273 insertions(+) create mode 100644 WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino create mode 100644 WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino new file mode 100644 index 000000000..20cf86bc0 --- /dev/null +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -0,0 +1,146 @@ +/* + Wifi Pachube sensor client + + This sketch connects an analog sensor to Pachube (http://www.pachube.com) + using an Arduino Wifi shield. + + This example has been updated to use version 2.0 of the Pachube.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 2 March 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 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; + +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 int postingInterval = 10000; //delay between updates to Pachube.com + +void setup() { + // start serial port: + Serial.begin(9600); + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + } +} + + +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("www.pachube.com", 80)) { + Serial.println("connecting..."); + // send the HTTP PUT request: + client.print("PUT /v2/feeds/"); + client.print(FEEDID); + client.println(".csv HTTP/1.1"); + client.print("Host: api.pachube.com\n"); + client.print("X-PachubeApiKey: "); + 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.print("Content-Type: text/csv\n"); + client.println("Connection: close\n"); + + // here's the actual content of the PUT request: + client.print("sensor1,"); + client.println(thisData); + + // note the time that the connection was made: + lastConnectionTime = millis(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + lastConnected = client.connected(); + } +} + + +// 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; +} + diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino new file mode 100644 index 000000000..0f0697e0b --- /dev/null +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -0,0 +1,127 @@ +/* + Wifi Pachube sensor client with Strings + + This sketch connects an analog sensor to Pachube (http://www.pachube.com) + using a Arduino Wifi shield. + + 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 2 March 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 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; + +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 int postingInterval = 10000; //delay between updates to Pachube.com + +void setup() { + // start serial port: + Serial.begin(9600); + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + } +} + +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("api.pachube.com", 80)) { + Serial.println("connecting..."); + // send the HTTP PUT request: + client.print("PUT /v2/feeds/"); + client.print(FEEDID); + client.println(".csv HTTP/1.1"); + client.print("Host: api.pachube.com\n"); + client.print("X-PachubeApiKey: "); + client.println(APIKEY); + client.print("User-Agent: "); + client.println(USERAGENT); + client.print("Content-Length: "); + client.println(thisData.length(), DEC); + + // last pieces of the HTTP PUT request: + client.print("Content-Type: text/csv\n"); + client.println("Connection: close\n"); + + // here's the actual content of the PUT request: + client.println(thisData); + + // note the time that the connection was made: + lastConnectionTime = millis(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + lastConnected = client.connected(); + } +} From e6dbda6825341ef8ec3ad806677cbcd25204f139 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 2 Mar 2012 20:01:57 -0500 Subject: [PATCH 43/98] Added Twitter client (in progress, not working) --- WiFi/examples/TwitterClient/TwitterClient.ino | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 WiFi/examples/TwitterClient/TwitterClient.ino diff --git a/WiFi/examples/TwitterClient/TwitterClient.ino b/WiFi/examples/TwitterClient/TwitterClient.ino new file mode 100644 index 000000000..2e1e9dd6e --- /dev/null +++ b/WiFi/examples/TwitterClient/TwitterClient.ino @@ -0,0 +1,199 @@ +/* + Twitter Client with Strings + + This sketch connects to Twitter using using an Arduino WiFi shield. + It parses the XML returned, and looks for this is a tweet + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * WiFi shield attached to pins 10, 11, 12, 13 + + created 15 Sep 2011 + modified 2 Mar 2012 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include + +char ssid[] = "itpsandbox"; // your network SSID (name) +char pass[] = "NYU+s0a!+P?"; // 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; + +// initialize the library instance: +WiFiClient client; + +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 +//char server[] = "api.twitter.com"; +IPAddress server(199,59,148,20); +//IPAddress server(128,122,151,128); + +String currentLine = ""; // string to hold the text from server +String tweet = ""; // string to hold the tweet +boolean readingTweet = false; // if you're currently reading the tweet + +void setup() { + // reserve space for the strings: + currentLine.reserve(256); + tweet.reserve(150); + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + while ( status != WL_CONNECTED) { + status = WiFi.begin(ssid, pass); + Serial.println("Couldn't get a wifi connection"); + delay(5000); + } + Serial.println("Connected to wifi"); + printWifiData(); + printCurrentNet(); + connectToServer(); +} + + + +void loop() +{ + if (client.connected()) { + Serial.println("requested"); + if (client.available()) { + // read incoming bytes: + char inChar = client.read(); + + // add incoming byte to end of line: + currentLine += inChar; + + // if you get a newline, clear the line: + if (inChar == '\n') { + Serial.println(currentLine); + currentLine = ""; + } + // if the current line ends with , it will + // be followed by the tweet: + if ( currentLine.endsWith("")) { + // tweet is beginning. Clear the tweet string: + readingTweet = true; + tweet = ""; + } + // if you're currently reading the bytes of a tweet, + // add them to the tweet String: + if (readingTweet) { + if (inChar != '<') { + tweet += inChar; + } + else { + // if you got a "<" character, + // you've reached the end of the tweet: + readingTweet = false; + Serial.println(tweet); + // close the connection to the server: + client.stop(); + } + } + } + } + else if (millis() - lastConnectionTime > postingInterval) { + // if you're not connected, and two minutes have passed since + // your last connection, then attempt to connect again: + connectToServer(); + } +} + +void connectToServer() { + // attempt to connect, and wait a millisecond: + Serial.println("connecting to server..."); + if (client.connect(server, 80)) { + Serial.println("making HTTP request..."); + // make HTTP GET request to twitter: + delay(10); + client.print("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1\n"); + client.print("Host:api.twitter.com\n"); + client.print("Connection:close\n\n"); + // client.println(); + } + else { + // if you couldn't make a connection: + Serial.println("connection failed"); + Serial.println(); + Serial.println("disconnecting."); + client.stop(); + } + // note the time of this connect attempt: + lastConnectionTime = millis(); + lastConnected = client.connected(); +} + + +void printWifiData() { + // print your WiFi shield's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + 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(); +} + + From 514a694c8a979ef6e0d926d589d9bb74c5716f18 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Sun, 4 Mar 2012 11:42:48 +0100 Subject: [PATCH 44/98] Fix issues on get encryptionType for the current associated network. Fix issue related to retrieve encription type and RSSI for the available networks --- WiFi/WiFi.cpp | 14 ----------- WiFi/WiFi.h | 40 ++++++++++++++++++----------- WiFi/utility/spi_drv.cpp | 2 +- WiFi/utility/wifi_drv.cpp | 47 +++++++++++++++++++++-------------- WiFi/utility/wifi_drv.h | 3 ++- WiFi/utility/wifi_spi.h | 5 +++- WiFi/utility/wl_definitions.h | 10 ++++++++ WiFi/utility/wl_types.h | 11 -------- 8 files changed, 71 insertions(+), 61 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 9d30c0ade..e613bc2f2 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -40,13 +40,6 @@ uint8_t WiFiClass::getSocket() return NO_SOCKET_AVAIL; } -int WiFiClass::begin() -{ - // Add procedure to read the latest configuration from eeprom/dataflash - // and start the wifi connection - return WL_IDLE_STATUS; -} - int WiFiClass::begin(char* ssid) { uint8_t status = WL_IDLE_STATUS; @@ -188,11 +181,4 @@ uint8_t WiFiClass::status() return WiFiDrv::getConnectionStatus(); } -uint8_t WiFiClass::test() -{ - return WiFiDrv::testCmd(); -} - - - WiFiClass WiFi; diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index fbb1225d2..cabfd139a 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -4,9 +4,10 @@ #include extern "C" { - #include "utility/wl_definitions.h" + #include "utility/wl_definitions.h" + #include "utility/wl_types.h" } - + #include "IPAddress.h" #include "WiFiClient.h" #include "WiFiServer.h" @@ -28,20 +29,31 @@ public: WiFiClass(); - // Get thefirst socket available + // Get the first socket available static uint8_t getSocket(); - // Start Wifi connection with latest settings - int begin(); - - // Start Wifi connection with no encryption + /* Start Wifi connection for OPEN networks + * + * param ssid: Pointer to the SSID string. + */ int begin(char* ssid); - // Start Wifi connection with WEP encryption + /* 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 + /* Start Wifi connection with passphrase + * the most secure supported mode will be automatically selected + * + * 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 @@ -83,12 +95,12 @@ public: // Return the current RSSI /Received Signal Strength in dBm) associated with the network identified with networkItem int32_t RSSI(uint8_t networkItem); - // Return Connection status + /* Return Connection status. + * + * return: one of the value defined in wl_status_t + */ uint8_t status(); - // function used for test - uint8_t test(); - friend class WiFiClient; friend class WiFiServer; }; diff --git a/WiFi/utility/spi_drv.cpp b/WiFi/utility/spi_drv.cpp index 93c693149..aaa596fcf 100644 --- a/WiFi/utility/spi_drv.cpp +++ b/WiFi/utility/spi_drv.cpp @@ -2,7 +2,7 @@ #include "Arduino.h" #include "spi_drv.h" #include "pins_arduino.h" -#define _DEBUG_ +//#define _DEBUG_ extern "C" { #include "debug.h" } diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 24d160674..6065107d6 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -15,6 +15,9 @@ extern "C" { } 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 }; + char WiFiDrv::_ssid[] = {0}; uint8_t WiFiDrv::_bssid[] = {0}; uint8_t WiFiDrv::_mac[] = {0}; @@ -289,7 +292,7 @@ uint8_t WiFiDrv::getCurrentEncryptionType() // 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::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen); SpiDrv::spiSlaveDeselect(); @@ -303,9 +306,6 @@ uint8_t WiFiDrv::scanNetworks() // Send Command SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0); -// uint8_t _dummy = DUMMY_DATA; -// SpiDrv::sendParam(&_dummy, 1, LAST_PARAM); - //Wait the reply elaboration SpiDrv::waitForSlaveReady(); @@ -330,10 +330,25 @@ uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem) { if (networkItem >= WL_NETWORKS_LIST_MAXNUM) return NULL; - uint8_t networkEncType = 0; - //TODO make an RPC call to get the encryption type associated with networkItem - return networkEncType; + 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) @@ -342,29 +357,23 @@ int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) return NULL; int32_t networkRssi = 0; - //TODO make an RPC call to get the rssi associated with networkItem - return networkRssi; -} - -uint8_t WiFiDrv::testCmd() -{ WAIT_FOR_SLAVE_SELECT(); // Send Command - SpiDrv::sendCmd(TEST_CMD, PARAM_NUMS_0); + 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 _data = 0; - uint8_t _dataLen = 0; - SpiDrv::waitResponseCmd(TEST_CMD, PARAM_NUMS_1, &_data, &_dataLen); + uint8_t dataLen = 0; + SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen); SpiDrv::spiSlaveDeselect(); - return _data; + return networkRssi; } - WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 80cc2d8a0..6a3e77dd4 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -14,6 +14,8 @@ 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]; @@ -63,7 +65,6 @@ public: static uint8_t getEncTypeNetowrks(uint8_t networkItem); - static uint8_t testCmd(); }; extern WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index cdcc02fea..daa06ea45 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -41,8 +41,11 @@ enum { 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, + // All command with DATA_FLAG 0x40 send a 16bit Len SEND_DATA_TCP_CMD = 0x44, diff --git a/WiFi/utility/wl_definitions.h b/WiFi/utility/wl_definitions.h index e28064a3d..e3db8b608 100644 --- a/WiFi/utility/wl_definitions.h +++ b/WiFi/utility/wl_definitions.h @@ -35,5 +35,15 @@ typedef enum { 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 index 7f6e8f22c..82b309d7f 100644 --- a/WiFi/utility/wl_types.h +++ b/WiFi/utility/wl_types.h @@ -28,15 +28,4 @@ enum wl_auth_mode { AUTH_MODE_WPA2_PSK }; - -/* 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_TYPES_H_ From 0ddbfd0e9562835b41b69f249d909263eb4b3001 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 4 Mar 2012 15:14:12 -0500 Subject: [PATCH 45/98] Updated Wifi Connect with WEP example to explain 40- vs 128-bit --- .../ConnectWithWEP/ConnectWithWEP.ino | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino index 4cad6f085..77e3eb098 100644 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -1,23 +1,33 @@ /* - This example connects to an unencrypted Wifi network. + 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 29 Feb 2012 - by Scott Fitzgerald + modified 4 Mar 2012 + by Tom Igoe */ - #include +#include -char ssid[] = "YourNetwork"; // your network SSID (name) -char key[] = "725d223132"; // your network key -int keyIndex = 0; // your network key Index number -int status = WL_IDLE_STATUS; // the Wifi radio's status +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: @@ -26,8 +36,8 @@ void setup() { // attempt to connect to an open network: Serial.print("Attempting to connect to WEP network: "); Serial.println(ssid); - status = WiFi.begin(ssid, pass, key); - + 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"); @@ -35,9 +45,9 @@ void setup() { } // if you are connected : else { - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); + Serial.print("You're connected to the network"); + printCurrentNet(); + printWifiData(); } } @@ -52,7 +62,7 @@ void printWifiData() { IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); - + // print your MAC address: byte mac[6]; WiFi.macAddress(mac); @@ -103,3 +113,4 @@ void printCurrentNet() { Serial.println(); } + From 45ab1504ac18c713e503b2a8645d07af6dc08216 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 4 Mar 2012 16:59:16 -0500 Subject: [PATCH 46/98] Updated ScanNetworks to include RSSI and Encryption type for each network --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index 99a490b8e..d12ae09dd 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 1 Mar 2012 + modified 4 Mar 2012 by Tom Igoe */ @@ -24,10 +24,9 @@ void setup() { // attempt to connect using WEP encryption: Serial.println("Initializing Wifi..."); - WiFi.begin("networName"); + //WiFi.begin("no network"); printMacAddress(); - // scan for existing networks: Serial.println("Scanning available networks..."); scanNetworks(); @@ -68,11 +67,16 @@ void scanNetworks() { // print the list of networks seen: Serial.print("SSID List:"); Serial.println(numSsid); + Serial.println("#\tNetwork:\tRSSI:\tEncryption:"); // print the network number and name for each network found: for (int thisNet = 0; thisNet Date: Sun, 4 Mar 2012 16:59:52 -0500 Subject: [PATCH 47/98] Deleted old examples --- WiFi/examples/TwitterClient/TwitterClient.ino | 199 ------------------ .../TwitterClientWPA/TwitterClientWPA.ino | 121 ----------- WiFi/examples/WebClientWPA/WebClientWPA.ino | 77 ------- WiFi/examples/WebServerWPA/WebServerWPA.ino | 95 --------- 4 files changed, 492 deletions(-) delete mode 100644 WiFi/examples/TwitterClient/TwitterClient.ino delete mode 100644 WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino delete mode 100644 WiFi/examples/WebClientWPA/WebClientWPA.ino delete mode 100644 WiFi/examples/WebServerWPA/WebServerWPA.ino diff --git a/WiFi/examples/TwitterClient/TwitterClient.ino b/WiFi/examples/TwitterClient/TwitterClient.ino deleted file mode 100644 index 2e1e9dd6e..000000000 --- a/WiFi/examples/TwitterClient/TwitterClient.ino +++ /dev/null @@ -1,199 +0,0 @@ -/* - Twitter Client with Strings - - This sketch connects to Twitter using using an Arduino WiFi shield. - It parses the XML returned, and looks for this is a tweet - - This example is written for a network using WPA encryption. For - WEP or WPA, change the Wifi.begin() call accordingly. - - This example uses the String library, which is part of the Arduino core from - version 0019. - - Circuit: - * WiFi shield attached to pins 10, 11, 12, 13 - - created 15 Sep 2011 - modified 2 Mar 2012 - by Tom Igoe - - This code is in the public domain. - - */ -#include -#include - -char ssid[] = "itpsandbox"; // your network SSID (name) -char pass[] = "NYU+s0a!+P?"; // 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; - -// initialize the library instance: -WiFiClient client; - -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 -//char server[] = "api.twitter.com"; -IPAddress server(199,59,148,20); -//IPAddress server(128,122,151,128); - -String currentLine = ""; // string to hold the text from server -String tweet = ""; // string to hold the tweet -boolean readingTweet = false; // if you're currently reading the tweet - -void setup() { - // reserve space for the strings: - currentLine.reserve(256); - tweet.reserve(150); - // initialize serial: - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - while ( status != WL_CONNECTED) { - status = WiFi.begin(ssid, pass); - Serial.println("Couldn't get a wifi connection"); - delay(5000); - } - Serial.println("Connected to wifi"); - printWifiData(); - printCurrentNet(); - connectToServer(); -} - - - -void loop() -{ - if (client.connected()) { - Serial.println("requested"); - if (client.available()) { - // read incoming bytes: - char inChar = client.read(); - - // add incoming byte to end of line: - currentLine += inChar; - - // if you get a newline, clear the line: - if (inChar == '\n') { - Serial.println(currentLine); - currentLine = ""; - } - // if the current line ends with , it will - // be followed by the tweet: - if ( currentLine.endsWith("")) { - // tweet is beginning. Clear the tweet string: - readingTweet = true; - tweet = ""; - } - // if you're currently reading the bytes of a tweet, - // add them to the tweet String: - if (readingTweet) { - if (inChar != '<') { - tweet += inChar; - } - else { - // if you got a "<" character, - // you've reached the end of the tweet: - readingTweet = false; - Serial.println(tweet); - // close the connection to the server: - client.stop(); - } - } - } - } - else if (millis() - lastConnectionTime > postingInterval) { - // if you're not connected, and two minutes have passed since - // your last connection, then attempt to connect again: - connectToServer(); - } -} - -void connectToServer() { - // attempt to connect, and wait a millisecond: - Serial.println("connecting to server..."); - if (client.connect(server, 80)) { - Serial.println("making HTTP request..."); - // make HTTP GET request to twitter: - delay(10); - client.print("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1\n"); - client.print("Host:api.twitter.com\n"); - client.print("Connection:close\n\n"); - // client.println(); - } - else { - // if you couldn't make a connection: - Serial.println("connection failed"); - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - } - // note the time of this connect attempt: - lastConnectionTime = millis(); - lastConnected = client.connected(); -} - - -void printWifiData() { - // print your WiFi shield's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - 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/TwitterClientWPA/TwitterClientWPA.ino b/WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino deleted file mode 100644 index f28da4ac0..000000000 --- a/WiFi/examples/TwitterClientWPA/TwitterClientWPA.ino +++ /dev/null @@ -1,121 +0,0 @@ -/* - Twitter Client with Strings - - This sketch connects to Twitter using using an Arduino WiFi shield. - It parses the XML returned, and looks for this is a tweet - - This example uses the String library, which is part of the Arduino core from - version 0019. - - Circuit: - * WiFi shield attached to pins 10, 11, 12, 13 - - created 15 Sep 2011 - by Tom Igoe - - This code is in the public domain. - - */ -#include -#include - -char ssid[] = "yourNetwork"; // your network SSID (name) -char pass[] = "secretPassword"; // your network password -int status = WL_IDLE_STATUS; - -// initialize the library instance: -WiFiClient client; - -const int requestInterval = 30*1000; // delay between requests; 30 seconds - -IPAddress server(199,59,149,200); // api.twitter.com - -boolean requested; // whether you've made a request since connecting -long lastAttemptTime = 0; // last time you connected to the server, in milliseconds - -String currentLine = ""; // string to hold the text from server -String tweet = ""; // string to hold the tweet -boolean readingTweet = false; // if you're currently reading the tweet - -void setup() { - // reserve space for the strings: - currentLine.reserve(256); - tweet.reserve(150); - // initialize serial: - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - else { - Serial.println("Connected to wifi"); - connectToServer(); - } -} - - - -void loop() -{ - if (client.connected()) { - if (client.available()) { - // read incoming bytes: - char inChar = client.read(); - - // add incoming byte to end of line: - currentLine += inChar; - - // if you get a newline, clear the line: - if (inChar == '\n') { - currentLine = ""; - } - // if the current line ends with , it will - // be followed by the tweet: - if ( currentLine.endsWith("")) { - // tweet is beginning. Clear the tweet string: - readingTweet = true; - tweet = ""; - } - // if you're currently reading the bytes of a tweet, - // add them to the tweet String: - if (readingTweet) { - if (inChar != '<') { - tweet += inChar; - } - else { - // if you got a "<" character, - // you've reached the end of the tweet: - readingTweet = false; - Serial.println(tweet); - // close the connection to the server: - client.stop(); - } - } - } - } - else if (millis() - lastAttemptTime > requestInterval) { - // if you're not connected, and two minutes have passed since - // your last connection, then attempt to connect again: - connectToServer(); - } -} - -void connectToServer() { - // attempt to connect, and wait a millisecond: - Serial.println("connecting to server..."); - if (client.connect(server, 80)) { - Serial.println("making HTTP request..."); - // make HTTP GET request to twitter: - client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); - client.println("HOST:api.twitter.com"); - client.println(); - } - // note the time of this connect attempt: - lastAttemptTime = millis(); -} - diff --git a/WiFi/examples/WebClientWPA/WebClientWPA.ino b/WiFi/examples/WebClientWPA/WebClientWPA.ino deleted file mode 100644 index 519ea0d67..000000000 --- a/WiFi/examples/WebClientWPA/WebClientWPA.ino +++ /dev/null @@ -1,77 +0,0 @@ - -/* - Web client - - This sketch connects to a website (http://www.google.com) - using a WiFi shield. - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 10 Jan 2012 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "networkName"; // your network SSID (name) -char pass[] = "yourPassword"; // your network password - -int status = WL_IDLE_STATUS; -IPAddress server(74,125,115,105); // Google - -// 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); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - // don't do anything else: - while(true); - } - else { - Serial.println("Connected to wifi"); - Serial.println("\nStarting connection..."); - // if you get a connection, report back via serial: - if (client.connect(server, 80)) { - Serial.println("connected"); - // Make a HTTP request: - client.println("GET /search?q=arduino HTTP/1.0"); - client.println(); - } - } -} - -void loop() { - // if there are incoming bytes available - // from the server, read them and print them: - if (client.available()) { - char c = client.read(); - Serial.print(c); - } - - // if the server's disconnected, stop the client: - if (!client.connected()) { - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - - // do nothing forevermore: - for(;;) - ; - } -} - - diff --git a/WiFi/examples/WebServerWPA/WebServerWPA.ino b/WiFi/examples/WebServerWPA/WebServerWPA.ino deleted file mode 100644 index f89ba8445..000000000 --- a/WiFi/examples/WebServerWPA/WebServerWPA.ino +++ /dev/null @@ -1,95 +0,0 @@ -/* - Web Server - - A simple web server that shows the value of the analog input pins. - using a WiFi shield. - - Circuit: - * WiFi shield attached - * Analog inputs attached to pins A0 through A5 (optional) - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 10 Jan 2012 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "networkName"; // your network SSID (name) -char pass[] = "yourPassword"; // your network password -int status = WL_IDLE_STATUS; - -WiFiServer server(80); - -void setup() { - // initialize serial: - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - else { - server.begin(); - Serial.print("Connected to wifi. My address:"); - IPAddress myAddress = WiFi.localIP(); - Serial.println(myAddress); - - } -} - - -void loop() { - // listen for incoming clients - WiFiClient client = server.available(); - if (client) { - // an http request ends with a blank line - boolean currentLineIsBlank = true; - while (client.connected()) { - if (client.available()) { - char c = client.read(); - // 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(); - client.println(""); - // output the value of each analog input pin - for (int analogChannel = 0; analogChannel < 6; analogChannel++) { - client.print("analog input "); - client.print(analogChannel); - client.print(" is "); - client.print(analogRead(analogChannel)); - 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(); - } -} - - From 1236c15e09b0ed3ef4ca0ed36019c06ba1545294 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 4 Mar 2012 17:04:51 -0500 Subject: [PATCH 48/98] Updated all clients to unify style, and include printWifiStatus() where useful --- .../WifiPachubeClient/WifiPachubeClient.ino | 43 +++++- .../WifiPachubeClientString.ino | 17 ++- .../WifiTwitterClient/WifiTwitterClient.ino | 143 ++++++++++++++++++ WiFi/examples/WifiWebClient/WifiWebClient.ino | 103 +++++++++++++ WiFi/examples/WifiWebServer/WifiWebServer.ino | 113 ++++++++++++++ 5 files changed, 408 insertions(+), 11 deletions(-) create mode 100644 WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino create mode 100644 WiFi/examples/WifiWebClient/WifiWebClient.ino create mode 100644 WiFi/examples/WifiWebServer/WifiWebServer.ino diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index 20cf86bc0..2b0e3ae11 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -4,6 +4,9 @@ This sketch connects an analog sensor to Pachube (http://www.pachube.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 Pachube.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. @@ -12,7 +15,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 2 March 2012 + created 4 March 2012 by Tom Igoe This code is in the public domain. @@ -22,6 +25,7 @@ #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 Project" // user agent is the project name @@ -38,15 +42,20 @@ boolean lastConnected = false; // state of the connection last time through const int postingInterval = 10000; //delay between updates to Pachube.com void setup() { - // start serial port: Serial.begin(9600); - status = WiFi.begin(ssid, pass); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); + // don't do anything else: while(true); } else { Serial.println("Connected to wifi"); + printWifiStatus(); } } @@ -86,7 +95,7 @@ void sendData(int thisData) { // if there's a successful connection: if (client.connect("www.pachube.com", 80)) { Serial.println("connecting..."); - // send the HTTP PUT request: + // send the HTTP PUT request: client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); @@ -107,20 +116,20 @@ void sendData(int thisData) { client.println("Connection: close\n"); // here's the actual content of the PUT request: - client.print("sensor1,"); + client.print("sensor1,"); client.println(thisData); // note the time that the connection was made: lastConnectionTime = millis(); } else { - // if you couldn't make a connection: + // if you couldn't make a connection: Serial.println("connection failed"); Serial.println(); Serial.println("disconnecting."); client.stop(); - lastConnected = client.connected(); } + lastConnected = client.connected(); } @@ -144,3 +153,23 @@ int getLength(int someValue) { 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: "); + + // 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/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index 0f0697e0b..8c655d9d2 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -4,6 +4,13 @@ This sketch connects an analog sensor to Pachube (http://www.pachube.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 Pachube.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. @@ -11,7 +18,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 2 March 2012 + created 4 March 2012 by Tom Igoe This code is in the public domain. @@ -39,7 +46,7 @@ const int postingInterval = 10000; //delay between updates to Pachube.com void setup() { // start serial port: Serial.begin(9600); - status = WiFi.begin(ssid, pass); + status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); @@ -53,9 +60,9 @@ void loop() { // read the analog sensor: int sensorReading = analogRead(A0); // convert the data to a String to send it: - + String dataString = "sensor1,"; - dataString += sensorReading; + dataString += sensorReading; // you can append multiple readings to this String if your // pachube feed is set up to handle multiple values: @@ -125,3 +132,5 @@ void sendData(String thisData) { lastConnected = client.connected(); } } + + diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino new file mode 100644 index 000000000..4758041ac --- /dev/null +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -0,0 +1,143 @@ +/* + Wifi Twitter Client with Strings + + This sketch connects to Twitter using using an Arduino WiFi shield. + It parses the XML returned, and looks for this is a tweet + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + This example uses the String library, which is part of the Arduino core from + version 0019. + + Circuit: + * WiFi shield attached to pins 10, 11, 12, 13 + + created 4 Mar 2012 + by Tom Igoe + + This code is in the public domain. + + */ +#include +#include + +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) + + +// initialize the library instance: +WiFiClient client; + +const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds + +IPAddress server(199,59,149,200); // api.twitter.com + +boolean requested; // whether you've made a request since connecting +unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds + +String currentLine = ""; // string to hold the text from server +String tweet = ""; // string to hold the tweet +boolean readingTweet = false; // if you're currently reading the tweet + +void setup() { + // reserve space for the strings: + currentLine.reserve(256); + tweet.reserve(150); + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to WPA network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + Serial.println("Connected to wifi"); + printWifiStatus(); + connectToServer(); + } +} + + + +void loop() +{ + if (client.connected()) { + if (client.available()) { + // read incoming bytes: + char inChar = client.read(); + + // add incoming byte to end of line: + currentLine += inChar; + + // if you get a newline, clear the line: + if (inChar == '\n') { + currentLine = ""; + } + // if the current line ends with , it will + // be followed by the tweet: + if ( currentLine.endsWith("")) { + // tweet is beginning. Clear the tweet string: + readingTweet = true; + tweet = ""; + } + // if you're currently reading the bytes of a tweet, + // add them to the tweet String: + if (readingTweet) { + if (inChar != '<') { + tweet += inChar; + } + else { + // if you got a "<" character, + // you've reached the end of the tweet: + readingTweet = false; + Serial.println(tweet); + // close the connection to the server: + client.stop(); + } + } + } + } + else if (millis() - lastAttemptTime > requestInterval) { + // if you're not connected, and two minutes have passed since + // your last connection, then attempt to connect again: + connectToServer(); + } +} + +void connectToServer() { + // attempt to connect, and wait a millisecond: + Serial.println("connecting to server..."); + if (client.connect(server, 80)) { + Serial.println("making HTTP request..."); + // make HTTP GET request to twitter: + client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); + client.println("HOST:api.twitter.com"); + client.println(); + } + // note the time of this connect attempt: + lastAttemptTime = 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: "); + + // 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..d4f1ce5a1 --- /dev/null +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -0,0 +1,103 @@ + +/* + 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 4 Mar 2012 + by Tom Igoe + */ + + +#include +#include + +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) + +int status = WL_IDLE_STATUS; +IPAddress server(74,125,115,105); // Google + +// 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); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + // don't do anything else: + while(true); + } + else { + 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.0"); + client.println(); + } + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + if (client.available()) { + char c = client.read(); + Serial.print(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: "); + + // 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..11c3c686d --- /dev/null +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -0,0 +1,113 @@ +/* + 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 4 Mar 2012 + by Tom Igoe + */ + + +#include +#include + +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) + +int status = WL_IDLE_STATUS; + +WiFiServer server(80); + +void setup() { + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + server.begin(); + Serial.print("Connected to wifi."); + printWifiStatus(); + } +} + + +void loop() { + // listen for incoming clients + WiFiClient client = server.available(); + if (client) { + // an http request ends with a blank line + boolean currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + // 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(); + client.println(""); + // output the value of each analog input pin + for (int analogChannel = 0; analogChannel < 6; analogChannel++) { + client.print("analog input "); + client.print(analogChannel); + client.print(" is "); + client.print(analogRead(analogChannel)); + 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(); + } +} + + +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: "); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} From 1d7d421cef9c6c4850985d9be9a775975c046ae2 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 4 Mar 2012 17:12:14 -0500 Subject: [PATCH 49/98] updated ScanNetworks to include RSSI and encryption (really this time!) --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index d12ae09dd..e6de67e63 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -24,19 +24,19 @@ void setup() { // attempt to connect using WEP encryption: Serial.println("Initializing Wifi..."); - //WiFi.begin("no network"); printMacAddress(); + // scan for existing networks: Serial.println("Scanning available networks..."); - scanNetworks(); + listNetworks(); } void loop() { delay(10000); // scan for existing networks: Serial.println("Scanning available networks..."); - scanNetworks(); + listNetworks(); } void printMacAddress() { @@ -59,19 +59,20 @@ void printMacAddress() { Serial.println(mac[0],HEX); } -void scanNetworks() { +void listNetworks() { // scan for nearby networks: Serial.println("** Scan Networks **"); byte numSsid = WiFi.scanNetworks(); // print the list of networks seen: - Serial.print("SSID List:"); + Serial.print("number of available networks:"); Serial.println(numSsid); - Serial.println("#\tNetwork:\tRSSI:\tEncryption:"); + + Serial.println("#\tName\tRSSI\tencryptionType:"); // print the network number and name for each network found: for (int thisNet = 0; thisNet Date: Sun, 4 Mar 2012 17:19:24 -0500 Subject: [PATCH 50/98] Corrected error in printWifiStatus --- WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino | 3 ++- WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino | 3 ++- WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino | 3 ++- WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino | 3 ++- WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino | 3 ++- WiFi/examples/WifiWebClient/WifiWebClient.ino | 3 ++- WiFi/examples/WifiWebServer/WifiWebServer.ino | 3 ++- 7 files changed, 14 insertions(+), 7 deletions(-) diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino index efedf6c73..cf1eb432d 100644 --- a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino +++ b/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino @@ -48,7 +48,8 @@ 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); // print your MAC address: diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino index 77e3eb098..1159e37f4 100644 --- a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino +++ b/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino @@ -60,7 +60,8 @@ 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); // print your MAC address: diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino index a01496c85..7b5752cc7 100644 --- a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino +++ b/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino @@ -49,7 +49,8 @@ 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); // print your MAC address: diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index 2b0e3ae11..980d84945 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -160,7 +160,8 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); + Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 4758041ac..7be583634 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -132,7 +132,8 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); + Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index d4f1ce5a1..5ad5582d0 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -89,7 +89,8 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); + Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index 11c3c686d..721b3d8ad 100644 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -103,7 +103,8 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); + Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); From 0c81e70581982b8b97e2c7a830b4ea0379951399 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 4 Mar 2012 17:22:24 -0500 Subject: [PATCH 51/98] Added Wifi version of the chat server --- .../WifiChatServer/WifiChatServer.ino | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 WiFi/examples/WifiChatServer/WifiChatServer.ino diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino new file mode 100644 index 000000000..2470799d6 --- /dev/null +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -0,0 +1,95 @@ +/* + Chat Server + + A simple server that distributes any incoming messages to all + connected clients. To use telnet to your device's IP address and type. + You can see the client's input in the serial monitor as well. + + 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 18 Dec 2009 + by David A. Mellis + modified 4 Mar 2012 + by Tom Igoe + + */ + + +#include +#include +char ssid[] = "tigoenet"; // your network SSID (name) +char pass[] = "m30w-m30w"; // your network password (use for WPA, or use as key for WEP) + + +//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) + +int status = WL_IDLE_STATUS; + +WiFiServer server(23); + +boolean gotAMessage = false; // whether or not you got a message from the client yet + +void setup() { + // initialize serial: + Serial.begin(9600); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + if ( status != WL_CONNECTED) { + Serial.println("Couldn't get a wifi connection"); + while(true); + } + else { + server.begin(); + Serial.print("Connected to wifi."); + printWifiStatus(); + } +} +void loop() { + // wait for a new client: + WiFiClient client = server.available(); + + // when the client sends the first byte, say hello: + if (client) { + if (!gotAMessage) { + Serial.println("We have a new client"); + client.println("Hello, client!"); + gotAMessage = true; + } + + // 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.print(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"); +} + From 43e38bcacce0b7fcc65aec45a07c1975b13c4d73 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 4 Mar 2012 17:23:01 -0500 Subject: [PATCH 52/98] Updated WIfi chat server --- WiFi/examples/WifiChatServer/WifiChatServer.ino | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 2470799d6..441491f13 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -22,12 +22,9 @@ #include #include -char ssid[] = "tigoenet"; // your network SSID (name) -char pass[] = "m30w-m30w"; // your network password (use for WPA, or use as key for WEP) - -//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[] = "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) int status = WL_IDLE_STATUS; From cd0c0f261643d1cf104cd54fa9a8532b02811db9 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Wed, 7 Mar 2012 00:05:54 +0100 Subject: [PATCH 53/98] Added comments --- WiFi/WiFi.cpp | 8 +-- WiFi/WiFi.h | 102 ++++++++++++++++++++++++------- WiFi/utility/wifi_drv.cpp | 8 +-- WiFi/utility/wifi_drv.h | 122 +++++++++++++++++++++++++++++++++++++- 4 files changed, 204 insertions(+), 36 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index e613bc2f2..2dd59c76d 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -4,19 +4,13 @@ extern "C" { #include "utility/wl_definitions.h" #include "utility/wl_types.h" - #include "debug.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 }; -char WiFiClass::_ssid[] = { 0 }; -char WiFiClass::_key[] = { 0 }; -char WiFiClass::_passph[] = { 0 }; -wl_status_t WiFiClass::_status; - - WiFiClass::WiFiClass() { // Driver initialization diff --git a/WiFi/WiFi.h b/WiFi/WiFi.h index cabfd139a..1bdaf9fe0 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -15,12 +15,6 @@ extern "C" { class WiFiClass { private: - // this data are stored in EEPROM and loaded at begin - // The next connect overwrite these values - static char _ssid[WL_SSID_MAX_LENGTH]; - static char _key[WL_WEP_KEY_MAX_LENGTH]; - static char _passph[WL_WPA_KEY_MAX_LENGTH]; - static wl_status_t _status; static void init(); public: @@ -29,7 +23,9 @@ public: WiFiClass(); - // Get the first socket available + /* + * Get the first socket available + */ static uint8_t getSocket(); /* Start Wifi connection for OPEN networks @@ -51,51 +47,113 @@ public: /* 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 + /* + * Disconnect from the network + * + * return: one value of wl_status_t enum + */ int disconnect(void); - //Get the interface MAC address. + /* + * 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 DHCP information related to IP + /* + * Get the interface IP address. + * + * return: Ip address value + */ IPAddress localIP(); - //Get the DHCP information related to subnetMask + /* + * Get the interface subnet mask address. + * + * return: subnet mask address value + */ IPAddress subnetMask(); - //Get the DHCP information related to gateway IP - IPAddress gatewayIP(); + /* + * Get the gateway ip address. + * + * return: gateway ip address value + */ + IPAddress gatewayIP(); - // Return the current SSID associated with the network + /* + * Return the current SSID associated with the network + * + * return: ssid string + */ char* SSID(); - // Return the current BSSID associated with the network + /* + * 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 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 the Encryption Type associated with the network + * + * return: one value of wl_enc_type enum + */ uint8_t encryptionType(); - // Start scan WiFi networks available and return the discovered number + /* + * Start scan WiFi networks available + * + * return: Number of discovered networks + */ uint8_t scanNetworks(); - // Return SSID item associated with the network identified with networkItem + /* + * 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 associated with the network identified with 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 current RSSI /Received Signal Strength in dBm) associated with the network identified with 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 Connection status. * * return: one of the value defined in wl_status_t */ diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 6065107d6..00a52eb4b 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -14,10 +14,12 @@ extern "C" { #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}; @@ -57,7 +59,6 @@ void WiFiDrv::wifiDriverInit() SpiDrv::begin(); } -// If ssid == NULL execute a wifi scan, otherwise try to connect to the network specified uint8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len) { WAIT_FOR_SLAVE_SELECT(); @@ -194,13 +195,12 @@ void WiFiDrv::getIpAddress(IPAddress& ip) { getNetworkData(_localIp, _subnetMask, _gatewayIp); ip = _localIp; - //memcpy(ip, _localIp, WL_IPV4_LENGTH); } - void WiFiDrv::getSubnetMask(IPAddress& ip) + void WiFiDrv::getSubnetMask(IPAddress& mask) { getNetworkData(_localIp, _subnetMask, _gatewayIp); - ip = _subnetMask; + mask = _subnetMask; } void WiFiDrv::getGatewayIP(IPAddress& ip) diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index 6a3e77dd4..a1a8a8383 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -5,9 +5,10 @@ #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 -#define WL_DELAY_RETRY_START_CONNECTION 1000 class WiFiDrv { @@ -25,44 +26,159 @@ private: 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); 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 uint8_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 uint8_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 uint8_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(); - + + /* + * 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); - static void getSubnetMask(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 uint8_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 + */ 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); }; From 2b6169e2a23e20d93f6531a053b3f9ba7b75729a Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 8 Mar 2012 12:10:50 -0500 Subject: [PATCH 54/98] Fixed variable error in WifiTwitterClient --- WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 7be583634..e0c2d9f9c 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -13,7 +13,7 @@ Circuit: * WiFi shield attached to pins 10, 11, 12, 13 - created 4 Mar 2012 + created 8 Mar 2012 by Tom Igoe This code is in the public domain. @@ -25,7 +25,7 @@ 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) - +int status = WL_IDLE_STATUS; // status of the wifi connection // initialize the library instance: WiFiClient client; From 1e2a51a30277f5ce782dba93f9df9763093ac43f Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 8 Mar 2012 12:12:48 -0500 Subject: [PATCH 55/98] Deleted old examples --- .../PachubeClientString.ino | 137 ------------ .../Wifi_Open_RSSI/Wifi_Open_RSSI.ino | 50 ----- .../Wifi_Open_ScanNetworks.ino | 155 ------------- .../Wifi_WEP_ScanNetworks.ino | 164 -------------- .../Wifi_WPA_ChatServer.ino | 79 ------- .../Wifi_WPA_PachubeClientString.ino | 140 ------------ .../Wifi_WPA_ScanNetworks.ino | 170 --------------- .../Wifi_WPA_TwitterClient.ino | 121 ---------- .../Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino | 76 ------- .../Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino | 100 --------- .../wifi_Server_example.ino | 158 -------------- .../wifi_Server_example_WEP.ino | 171 --------------- .../wifi_Server_example_WPA.ino | 201 ----------------- .../wifi_WEP_example/wifi_WEP_example.ino | 142 ------------ .../wifi_example/wifi_example.pde | 206 ------------------ 15 files changed, 2070 deletions(-) delete mode 100644 WiFi/examples/old_examples/PachubeClientString/PachubeClientString.ino delete mode 100644 WiFi/examples/old_examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino delete mode 100644 WiFi/examples/old_examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino delete mode 100644 WiFi/examples/old_examples/Wifi_WEP_ScanNetworks/Wifi_WEP_ScanNetworks.ino delete mode 100644 WiFi/examples/old_examples/Wifi_WPA_ChatServer/Wifi_WPA_ChatServer.ino delete mode 100644 WiFi/examples/old_examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino delete mode 100644 WiFi/examples/old_examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino delete mode 100644 WiFi/examples/old_examples/Wifi_WPA_TwitterClient/Wifi_WPA_TwitterClient.ino delete mode 100644 WiFi/examples/old_examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino delete mode 100644 WiFi/examples/old_examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino delete mode 100644 WiFi/examples/old_examples/wifi_Server_example/wifi_Server_example.ino delete mode 100644 WiFi/examples/old_examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino delete mode 100644 WiFi/examples/old_examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino delete mode 100644 WiFi/examples/old_examples/wifi_WEP_example/wifi_WEP_example.ino delete mode 100755 WiFi/examples/old_examples/wifi_example/wifi_example.pde diff --git a/WiFi/examples/old_examples/PachubeClientString/PachubeClientString.ino b/WiFi/examples/old_examples/PachubeClientString/PachubeClientString.ino deleted file mode 100644 index 1e007761b..000000000 --- a/WiFi/examples/old_examples/PachubeClientString/PachubeClientString.ino +++ /dev/null @@ -1,137 +0,0 @@ -/* - Pachube sensor client with Strings - - This sketch connects an analog sensor to Pachube (http://www.pachube.com) - using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or - the Adafruit Ethernet shield, either one will work, as long as it's got - a Wiznet Ethernet module on board. - - This example uses the String library, which is part of the Arduino core from - version 0019. - - Circuit: - * Analog sensor attached to analog in 0 - * Ethernet shield attached to pins 10, 11, 12, 13 - - created 15 March 2010 - updated 4 Sep 2010 - by Tom Igoe - - This code is in the public domain. - - */ - -#include - -char ssid[] = "yourNetwork"; -char pass[] = "secretPassword"; -int status = WL_IDLE_STATUS; - -// The address of the server you want to connect to (pachube.com): -IPAddress server(173,203,98,29); - -// initialize the library instance: -WiFiClient client; - -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 int postingInterval = 10000; //delay between updates to Pachube.com - -int startWiFiWpa() -{ - Serial.println("\nSetup WiFi Wpa..."); - strcpy(ssid, "tigoenet"); - Serial.print("SSID: "); - Serial.println(ssid); - const char *pass = "m30w-m30w"; - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) - { - Serial.println("Connection Failed"); - } - return status; -} - - - -void setup() { - // start the WiFi connection and the server: - Serial.begin(9600); - Serial.println("*** Start WebClient WiFi example ***"); - - int _status = startWiFiWpa(); - if ( _status == WL_CONNECTED) - { - Serial.println("\nWiFi Connected."); - } - - delay(1000); -} - -void loop() { - // read the analog sensor: - int sensorReading = analogRead(A0); - // convert the data to a String to send it: - String dataString = String(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 += ","; - dataString += String(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. - // fill in your feed address here: - client.print("PUT /api/24196.csv HTTP/1.1\n"); - client.print("Host: www.pachube.com\n"); - // fill in your Pachube API key here: - client.print("X-PachubeApiKey: gw0L2A-J5ACRGQccX59tCYt0IEzyecr-SoiuC47U1-8\n"); - client.print("Content-Length: "); - client.println(thisData.length(), DEC); - - // last pieces of the HTTP PUT request: - client.print("Content-Type: text/csv\n"); - client.println("Connection: close\n"); - - // here's the actual content of the PUT request: - client.println(thisData); - - // note the time that the connection was made: - lastConnectionTime = millis(); - } - else { - // if you couldn't make a connection: - Serial.println("connection failed"); - } -} diff --git a/WiFi/examples/old_examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino b/WiFi/examples/old_examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino deleted file mode 100644 index e0e8ee103..000000000 --- a/WiFi/examples/old_examples/Wifi_Open_RSSI/Wifi_Open_RSSI.ino +++ /dev/null @@ -1,50 +0,0 @@ -/* - - Open connection using the WiFi shield. Attempts to connect - and prints out the signal strength. - - Circuit: - * WiFi shield attached - created 5 June 2011 - by Tom Igoe - */ -#include -#include - -char ssid[] = "yourNetwork"; // the name of your network -int status = WL_IDLE_STATUS; // the Wifi radio's status - - -byte mac[6]; // the MAC address of your Wifi shield -IPAddress ip; // the IP address of your shield - -void setup() { - // initialize serial: - Serial.begin(9600); - - // attempt to connect using WEP encryption: - Serial.println("Attempting to connect to open network..."); - status = WiFi.begin(ssid); - - Serial.print("SSID: "); - Serial.println(ssid); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } -} - -void loop() { - // if you're connected, print out the signal strength: - if ( status != WL_CONNECTED) { - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi); - delay(250); - } -} - - diff --git a/WiFi/examples/old_examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino b/WiFi/examples/old_examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino deleted file mode 100644 index 64d27fbd0..000000000 --- a/WiFi/examples/old_examples/Wifi_Open_ScanNetworks/Wifi_Open_ScanNetworks.ino +++ /dev/null @@ -1,155 +0,0 @@ -/* - - Open connection using the WiFi shield. Attempts to connect - and prints out info about the network - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 5 June 2011 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "yourNetwork"; // the name of your network -int status = WL_IDLE_STATUS; // the Wifi radio's status - - -byte mac[6]; // the MAC address of your Wifi shield -IPAddress ip; // the IP address of your shield -IPAddress gateway; // the router's address -IPAddress subnet; // the subnet mask - -void setup() { - // initialize serial: - Serial.begin(9600); - - // scan for existing networks: - Serial.println("Scanning available networks..."); - scanNetworks(); - - // attempt to connect using WEP encryption: - Serial.println("Attempting to connect to open network..."); - status = WiFi.begin(ssid); - - Serial.print("SSID: "); - Serial.println(ssid); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - // if you are connected, print out info about the connection: - else { - printIpData(); - printCurrentNet(); - } -} - -void loop() { - // do nothing -} - -void printIpData() { - // print your WiFi shield's IP address: - ip = WiFi.localIP(); - Serial.print("IP: "); - Serial.print(ip[0]); - Serial.print("."); - Serial.print(ip[1]); - Serial.print("."); - Serial.print(ip[2]); - Serial.print("."); - Serial.println(ip[3]); - - // print your subnet mask: - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[0]); - Serial.print("."); - Serial.print(subnet[1]); - Serial.print("."); - Serial.print(subnet[2]); - Serial.print("."); - Serial.println(subnet[3]); - - // print your gateway address: - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[0]); - Serial.print("."); - Serial.print(gateway[1]); - Serial.print("."); - Serial.print(gateway[2]); - Serial.print("."); - Serial.println(gateway[3]); - - // 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 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("RSSI:"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption,HEX); -} - -void scanNetworks() { - // scan for nearby networks: - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - - // print the list of networks seen: - Serial.print("SSID List:"); - Serial.println(numSsid); - // print the network number and name for each network found: - for (int thisNet = 0; thisNet -#include - -char ssid[] = "yourNetwork"; // the name of your network -char keyIndex = 0; // WEP networks can have multiple keys. -char key[] = "BAE4B2EDB9171646AA0DC8ED19"; // the key you're using to connect -int status = WL_IDLE_STATUS; // the Wifi radio's status - - -byte mac[6]; // the MAC address of your Wifi shield -IPAddress ip; // the IP address of your shield -IPAddress gateway; // the router's address -IPAddress subnet; // the subnet mask - -void setup() { - // initialize serial: - Serial.begin(9600); - - // attempt to connect using WEP encryption: - Serial.println("Attempting to connect to WEP-128 network..."); - status = WiFi.begin(ssid, keyIndex, key); - - - Serial.print("SSID: "); - Serial.println(ssid); - - // scan for existing networks: - Serial.println("Scanning available networks..."); - scanNetworks(); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - // if you are connected, print out info about the connection: - else { - printIpData(); - printCurrentNet(); - } -} - -void loop() { - // do nothing -} - -void printIpData() { - // print your WiFi shield's IP address: - ip = WiFi.localIP(); - Serial.print("IP: "); - Serial.print(ip[3]); - Serial.print("."); - Serial.print(ip[2]); - Serial.print("."); - Serial.print(ip[1]); - Serial.print("."); - Serial.println(ip[0]); - - // print your subnet mask: - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3]); - Serial.print("."); - Serial.print(subnet[2]); - Serial.print("."); - Serial.print(subnet[1]); - Serial.print("."); - Serial.println(subnet[0]); - - // print your gateway address: - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3]); - Serial.print("."); - Serial.print(gateway[2]); - Serial.print("."); - Serial.print(gateway[1]); - Serial.print("."); - Serial.println(gateway[0]); - - // 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 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("RSSI:"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption,HEX); -} - -void scanNetworks() { - // scan for nearby networks: - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - - // print the list of networks seen: - Serial.print("SSID List:"); - Serial.println(numSsid); - // print the network number and name for each network found: - for (int thisNet = 0; thisNet -#include - -char ssid[] = "yourNetwork"; // the name of your network -char pass[] = "secretPassword"; // the WPA2 password for your network -int status = WL_IDLE_STATUS; // the Wifi radio's status - -// telnet defaults to port 23 -WiFiServer server(23); - -boolean gotAMessage = false; // whether or not you got a message from the client yet - -void setup() { - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - else { - server.begin(); - Serial.print("Connected to wifi. My address:"); - IPAddress myAddress = WiFi.localIP(); - Serial.print(myAddress[0]); - Serial.print("."); - Serial.print(myAddress[1]); - Serial.print("."); - Serial.print(myAddress[2]); - Serial.print("."); - Serial.println(myAddress[3]); - } -} - -void loop() { - // wait for a new client: - WiFiClient client = server.available(); - - // when the client sends the first byte, say hello: - if (client) { - if (!gotAMessage) { - Serial.println("I have a new client"); - client.println("Hello, client!"); - gotAMessage = true; - } - // read the bytes incoming from the client: - char thisChar = client.read(); - // echo the bytes back to the client: - client.write(thisChar); - // echo the bytes to the server as well: - Serial.print(thisChar); - } -} - - - - diff --git a/WiFi/examples/old_examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino b/WiFi/examples/old_examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino deleted file mode 100644 index 88e44e849..000000000 --- a/WiFi/examples/old_examples/Wifi_WPA_PachubeClientString/Wifi_WPA_PachubeClientString.ino +++ /dev/null @@ -1,140 +0,0 @@ -/* - Pachube sensor client with Strings - - This sketch connects an analog sensor to Pachube (http://www.pachube.com) - using an Arduino WiFi shield. - - This example uses the String library, which is part of the Arduino core from - version 0019. - - Circuit: - * Analog sensors attached to analog in 0 and 1 - * WoFo shield attached to pins 10, 11, 12, 13 - - created 15 March 2010 - updated 5 June 2011 - by Tom Igoe - - This code is in the public domain. - - */ - - -#include -#include - -char ssid[] = "yourNetwork"; -char pass[] = "secretPassword"; -int status = WL_IDLE_STATUS; - -// The address of the server you want to connect to (pachube.com): -IPAddress server(173,203,98,29); - -// initialize the library instance: -WiFiClient client; - -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 int postingInterval = 30000; //delay between updates to Pachube.com - -int startWiFiWpa() -{ - Serial.println("\nSetup WiFi Wpa..."); - //strcpy(ssid, "AndroidAP9647"); - strcpy(ssid, "tigoenet"); - Serial.print("SSID: "); - Serial.println(ssid); - const char *pass = "m30w-m30w"; - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) - { - Serial.println("Connection Failed"); - } - return status; -} - - - -void setup() { - // start the WiFi connection and the server: - Serial.begin(9600); - Serial.println("*** Start WebClient WiFi example ***"); - - int _status = startWiFiWpa(); - if ( _status == WL_CONNECTED) - { - Serial.println("\nWiFi Connected."); - } - - delay(1000); -} - - -void loop() { - // read the analog sensor: - int sensorReading = analogRead(A0); - // convert the data to a String to send it: - String dataString = String(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 += ","; - dataString += String(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. - // fill in your feed address here: - client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n"); - client.print("Host: www.pachube.com\n"); - // fill in your Pachube API key here: - client.print("X-PachubeApiKey: YOUR_KEY_HERE\n"); - client.print("Content-Length: "); - client.println(thisData.length(), DEC); - - // last pieces of the HTTP PUT request: - client.print("Content-Type: text/csv\n"); - client.println("Connection: close\n"); - - // here's the actual content of the PUT request: - client.println(thisData); - - // note the time that the connection was made: - lastConnectionTime = millis(); - } - else { - // if you couldn't make a connection: - Serial.println("connection failed"); - } -} - diff --git a/WiFi/examples/old_examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino b/WiFi/examples/old_examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino deleted file mode 100644 index 4a217f0cd..000000000 --- a/WiFi/examples/old_examples/Wifi_WPA_ScanNetworks/Wifi_WPA_ScanNetworks.ino +++ /dev/null @@ -1,170 +0,0 @@ -/* - - WPA2 conection using the WiFi shield. Attempts to connect - and prints out info about the network - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 5 June 2011 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "yourNetwork"; -char pass[] = "secretpassword"; -int status = WL_IDLE_STATUS; // the Wifi radio's status - -byte mac[6]; // the MAC address of your Wifi shield -IPAddress ip; // the IP address of your shield -IPAddress gateway; // the router's address -IPAddress subnet; // the subnet mask - -void setup() { - // initialize serial: - Serial.begin(9600); - // scan for existing networks: - Serial.println("Scanning available networks..."); - //WiFi.begin(); - scanNetworks(); - - // attempt to connect using WEP encryption: -// Serial.println("Attempting to connect to WEP-128 network..."); -// status = WiFi.begin(ssid, keyIndex, key); - - // attempt to connect using WPA2 encryption: - Serial.println("Attempting to connect to WPA network..."); - status = WiFi.begin(ssid, pass); - - - Serial.print("SSID: "); - Serial.println(ssid); - - // scan for existing networks: - Serial.println("Scanning available networks..."); - scanNetworks(); - - // if you're not connected, stop here: - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - // if you are connected, print out info about the connection: - else { - printIpData(); - printCurrentNet(); - } -} - -void loop() { - // do nothing -} - -void printIpData() { - // print your WiFi shield's IP address: - ip = WiFi.localIP(); - Serial.print("IP: "); - Serial.print(ip[0]); - Serial.print("."); - Serial.print(ip[1]); - Serial.print("."); - Serial.print(ip[2]); - Serial.print("."); - Serial.println(ip[3]); - - // print your subnet mask: - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[0]); - Serial.print("."); - Serial.print(subnet[1]); - Serial.print("."); - Serial.print(subnet[2]); - Serial.print("."); - Serial.println(subnet[3]); - - // print your gateway address: - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[0]); - Serial.print("."); - Serial.print(gateway[1]); - Serial.print("."); - Serial.print(gateway[2]); - Serial.print("."); - Serial.println(gateway[3]); - - // 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 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("RSSI:"); - Serial.println(rssi); - - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption,HEX); -} - -void scanNetworks() { - // scan for nearby networks: - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - - // print the list of networks seen: - Serial.print("SSID List:"); - Serial.println(numSsid); - // print the network number and name for each network found: - for (int thisNet = 0; thisNetthis is a tweet
- - This example uses the String library, which is part of the Arduino core from - version 0019. - - Circuit: - * WiFi shield attached to pins 10, 11, 12, 13 - - created 21 May 2011 - by Tom Igoe - - This code is in the public domain. - - */ -#include -#include - -char ssid[] = "yourNetwork"; -char pass[] = "secretpassword"; -int status = WL_IDLE_STATUS; - -// initialize the library instance: -WiFiClient client; - -const int requestInterval = 30*1000; // delay between requests; 30 seconds - -IPAddress server(199,59,149,200); // api.twitter.com - -boolean requested; // whether you've made a request since connecting -long lastAttemptTime = 0; // last time you connected to the server, in milliseconds - -String currentLine = ""; // string to hold the text from server -String tweet = ""; // string to hold the tweet -boolean readingTweet = false; // if you're currently reading the tweet - -void setup() { - // reserve space for the strings: - currentLine.reserve(256); - tweet.reserve(150); - // initialize serial: - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - else { - Serial.println("Connected to wifi"); - connectToServer(); - } -} - - - -void loop() -{ - if (client.connected()) { - if (client.available()) { - // read incoming bytes: - char inChar = client.read(); - - // add incoming byte to end of line: - currentLine += inChar; - - // if you get a newline, clear the line: - if (inChar == '\n') { - currentLine = ""; - } - // if the current line ends with , it will - // be followed by the tweet: - if ( currentLine.endsWith("")) { - // tweet is beginning. Clear the tweet string: - readingTweet = true; - tweet = ""; - } - // if you're currently reading the bytes of a tweet, - // add them to the tweet String: - if (readingTweet) { - if (inChar != '<') { - tweet += inChar; - } - else { - // if you got a "<" character, - // you've reached the end of the tweet: - readingTweet = false; - Serial.println(tweet); - // close the connection to the server: - client.stop(); - } - } - } - } - else if (millis() - lastAttemptTime > requestInterval) { - // if you're not connected, and two minutes have passed since - // your last connection, then attempt to connect again: - connectToServer(); - } -} - -void connectToServer() { - // attempt to connect, and wait a millisecond: - Serial.println("connecting to server..."); - if (client.connect(server, 80)) { - Serial.println("making HTTP request..."); - // make HTTP GET request to twitter: - client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); - client.println("HOST: api.twitter.com"); - client.println(); - } - // note the time of this connect attempt: - lastAttemptTime = millis(); -} - diff --git a/WiFi/examples/old_examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino b/WiFi/examples/old_examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino deleted file mode 100644 index 57bc5d265..000000000 --- a/WiFi/examples/old_examples/Wifi_WPA_WebClient/Wifi_WPA_WebClient.ino +++ /dev/null @@ -1,76 +0,0 @@ - -/* - Web client - - This sketch connects to a website (http://www.google.com) - using a WiFi shield. - - Circuit: - * WiFi shield attached - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 21 May 2011 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "yourNetwork"; -char pass[] = "secretPassword"; -int status = WL_IDLE_STATUS; -IPAddress server(74,125,115,105); // Google - -// 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); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - // don't do anything else: - while(true); - } - else { - Serial.println("Connected to wifi"); - Serial.println("\nStarting connection..."); - // if you get a connection, report back via serial: - if (client.connect(server, 80)) { - Serial.println("connected"); - // Make a HTTP request: - client.println("GET /search?q=arduino HTTP/1.0"); - client.println(); - } - } -} - -void loop() { - // if there are incoming bytes available - // from the server, read them and print them: - if (client.available()) { - char c = client.read(); - Serial.print(c); - } - - // if the server's disconnected, stop the client: - if (!client.connected()) { - Serial.println(); - Serial.println("disconnecting."); - client.stop(); - - // do nothing forevermore: - for(;;) - ; - } -} - - diff --git a/WiFi/examples/old_examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino b/WiFi/examples/old_examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino deleted file mode 100644 index eb0479a37..000000000 --- a/WiFi/examples/old_examples/Wifi_WPA_WebServer/Wifi_WPA_WebServer.ino +++ /dev/null @@ -1,100 +0,0 @@ -/* - Web Server - - A simple web server that shows the value of the analog input pins. - using a WiFi shield. - - Circuit: - * WiFi shield attached - * Analog inputs attached to pins A0 through A5 (optional) - - created 13 July 2010 - by dlf (Metodo2 srl) - modified 5 June 2011 - by Tom Igoe - */ - - -#include -#include - -char ssid[] = "yourNetwork"; -char pass[] = "secretpassword"; -int status = WL_IDLE_STATUS; - -WiFiServer server(80); - -void setup() { - // initialize serial: - Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - while(true); - } - else { - server.begin(); - Serial.print("Connected to wifi. My address:"); - IPAddress myAddress = WiFi.localIP(); - Serial.print(myAddress[0]); - Serial.print("."); - Serial.print(myAddress[1]); - Serial.print("."); - Serial.print(myAddress[2]); - Serial.print("."); - Serial.println(myAddress[3]); - } -} - - -void loop() { - // listen for incoming clients - WiFiClient client = server.available(); - if (client) { - // an http request ends with a blank line - boolean currentLineIsBlank = true; - while (client.connected()) { - if (client.available()) { - char c = client.read(); - // 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(); - client.println(""); - // output the value of each analog input pin - for (int analogChannel = 0; analogChannel < 6; analogChannel++) { - client.print("analog input "); - client.print(analogChannel); - client.print(" is "); - client.print(analogRead(analogChannel)); - 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(); - } -} - - diff --git a/WiFi/examples/old_examples/wifi_Server_example/wifi_Server_example.ino b/WiFi/examples/old_examples/wifi_Server_example/wifi_Server_example.ino deleted file mode 100644 index 7fc82afd8..000000000 --- a/WiFi/examples/old_examples/wifi_Server_example/wifi_Server_example.ino +++ /dev/null @@ -1,158 +0,0 @@ -/* - WiFi Server example - - A simple connection with WiFi AP with Wireless Security - information try to access with WPA or WEP security keys - A simple server is setup to exchange data. - - created 13 July 2010 - by dlf (Metodo2 srl) - */ -#include - -byte mac[6] = { 0 }; -IPAddress ip; -IPAddress gateway; -IPAddress subnet; -byte dataBuf[80] = { 0 }; -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; - -WiFiServer server(23); - -void printIpData() -{ - ip = WiFi.localIP(); - - Serial.print("IP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); -} - -void printCurrNet() -{ - //WiFi.getCurrSSID(&ssid[0]); - //Serial.print("SSID:"); - //Serial.println(ssid); - byte bssid[6]; - WiFi.BSSID(bssid); - //delay(200); - Serial.print("BSSID:"); - Serial.print(bssid[5],16);Serial.print(":"); - Serial.print(bssid[4],16);Serial.print(":"); - Serial.print(bssid[3],16);Serial.print(":"); - Serial.print(bssid[2],16);Serial.print(":"); - Serial.print(bssid[1],16);Serial.print(":"); - Serial.println(bssid[0],16); - - int32_t rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi,10); - - uint8_t enct = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(enct,16); - - char ssid[20][32] = { {0} }; - byte numSsid = WiFi.scanNetworks(); - Serial.print("SSID List:"); - Serial.println(numSsid, 10); - for (int i = 0; i0) - { - dataBuf[idx]=0; - //Serial.println((char*)&dataBuf[0]); - server.write((char*)&dataBuf[0]); - } - return; - } - } -} - diff --git a/WiFi/examples/old_examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino b/WiFi/examples/old_examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino deleted file mode 100644 index 5c11b9580..000000000 --- a/WiFi/examples/old_examples/wifi_Server_example_WEP/wifi_Server_example_WEP.ino +++ /dev/null @@ -1,171 +0,0 @@ -/* - WiFi Server example - - A simple connection with WiFi AP with Wireless Security - information try to access with WPA or WEP security keys - A simple server is setup to exchange data. - - created 13 July 2010 - by dlf (Metodo2 srl) - */ -#include - -byte mac[6] = { 0 }; -IPAddress ip; -IPAddress gateway; -IPAddress subnet; -byte dataBuf[80] = { 0 }; -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; -#define MAX_NUM_SSID 10 -char ssidList[MAX_NUM_SSID][32] = { {0} }; - -WiFiServer server(23); - -void printIpData() -{ - ip = WiFi.localIP(); - - Serial.print("IP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); -} - -void printCurrNet() -{ - char* ssid = WiFi.SSID(); - Serial.print("SSID: "); - Serial.println(ssid); - - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],16);Serial.print(":"); - Serial.print(bssid[4],16);Serial.print(":"); - Serial.print(bssid[3],16);Serial.print(":"); - Serial.print(bssid[2],16);Serial.print(":"); - Serial.print(bssid[1],16);Serial.print(":"); - Serial.println(bssid[0],16); - - int32_t rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi,10); - - uint8_t enct = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(enct,16); -} - -void scanNetworks() -{ - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; - Serial.print("SSID List:"); - Serial.println(numSsid, 10); - for (int i = 0; i0) - { - dataBuf[idx]=0; - Serial.println((char*)&dataBuf[0]); - server.write((char*)&dataBuf[0]); - } - return; - */ - } - } -} - - diff --git a/WiFi/examples/old_examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino b/WiFi/examples/old_examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino deleted file mode 100644 index bf0f731e7..000000000 --- a/WiFi/examples/old_examples/wifi_Server_example_WPA/wifi_Server_example_WPA.ino +++ /dev/null @@ -1,201 +0,0 @@ -/* - WiFi Server example - - A simple connection with WiFi AP with Wireless Security - information try to access with WPA or WEP security keys - A simple server is setup to exchange data. - - created 13 July 2010 - by dlf (Metodo2 srl) - */ -#include - -#define _PRINT_ - -byte mac[6] = { 0 }; -IPAddress ip; -IPAddress gateway; -IPAddress subnet; -byte dataBuf[80] = { 0 }; -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; -#define MAX_NUM_SSID 10 -char ssidList[MAX_NUM_SSID][32] = { {0} }; - - -WiFiServer server(23); -boolean gotAMessage = false; // whether or not you got a message from the client yet - -void printIpData() -{ - ip = WiFi.localIP(); - - Serial.print("\nIP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); -} - -void printCurrNet() -{ - char* ssid = WiFi.SSID(); - Serial.print("SSID: "); - Serial.println(ssid); - - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],16);Serial.print(":"); - Serial.print(bssid[4],16);Serial.print(":"); - Serial.print(bssid[3],16);Serial.print(":"); - Serial.print(bssid[2],16);Serial.print(":"); - Serial.print(bssid[1],16);Serial.print(":"); - Serial.println(bssid[0],16); - - int32_t rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi,10); - - uint8_t enct = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(enct,16); -} - -void scanNetworks() -{ - Serial.println("\n** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; - Serial.print("SSID List:"); - Serial.println(numSsid, 10); - for (int i = 0; i - -byte mac[6] = { 0 }; -IPAddress ip; -IPAddress gateway; -IPAddress subnet; -byte dataBuf[80] = { 0 }; -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; -#define MAX_NUM_SSID 10 -char ssidList[MAX_NUM_SSID][32] = { {0} }; - - -WiFiServer server(23); - -void printIpData() -{ - ip = WiFi.localIP(); - - Serial.print("IP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); -} - -void printCurrNet() -{ - char* ssid = WiFi.SSID(); - Serial.print("SSID: "); - Serial.println(ssid); - - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],16);Serial.print(":"); - Serial.print(bssid[4],16);Serial.print(":"); - Serial.print(bssid[3],16);Serial.print(":"); - Serial.print(bssid[2],16);Serial.print(":"); - Serial.print(bssid[1],16);Serial.print(":"); - Serial.println(bssid[0],16); - - int32_t rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi,10); - - uint8_t enct = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(enct,16); -} - -void scanNetworks() -{ - Serial.println("** Scan Networks **"); - byte numSsid = WiFi.scanNetworks(); - if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; - Serial.print("SSID List:"); - Serial.println(numSsid, 10); - for (int i = 0; i - -byte mac[6] = { 0 }; -IPAddress ip; -IPAddress gateway; -IPAddress subnet; -byte dataBuf[80] = { 0 }; -char ssid[32] = { 0 }; -int status = WL_IDLE_STATUS; -#define MAX_NUM_SSID 10 -char ssidList[MAX_NUM_SSID][32] = { {0} }; - - -WiFiServer server(23); - -void printIpData() -{ - ip = WiFi.localIP(); - - Serial.print("IP: "); - Serial.print(ip[3],10);Serial.print("."); - Serial.print(ip[2],10);Serial.print("."); - Serial.print(ip[1],10);Serial.print("."); - Serial.println(ip[0],10); - - subnet = WiFi.subnetMask(); - Serial.print("NETMASK: "); - Serial.print(subnet[3],10);Serial.print("."); - Serial.print(subnet[2],10);Serial.print("."); - Serial.print(subnet[1],10);Serial.print("."); - Serial.println(subnet[0],10); - - gateway = WiFi.gatewayIP(); - Serial.print("GATEWAY: "); - Serial.print(gateway[3],10);Serial.print("."); - Serial.print(gateway[2],10);Serial.print("."); - Serial.print(gateway[1],10);Serial.print("."); - Serial.println(gateway[0],10); - - WiFi.macAddress(mac); - Serial.print("MAC: "); - Serial.print(mac[5],16);Serial.print(":"); - Serial.print(mac[4],16);Serial.print(":"); - Serial.print(mac[3],16);Serial.print(":"); - Serial.print(mac[2],16);Serial.print(":"); - Serial.print(mac[1],16);Serial.print(":"); - Serial.println(mac[0],16); -} - -void printCurrNet() -{ - char* ssid = WiFi.SSID(); - Serial.print("SSID: "); - Serial.println(ssid); - - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - Serial.print(bssid[5],16);Serial.print(":"); - Serial.print(bssid[4],16);Serial.print(":"); - Serial.print(bssid[3],16);Serial.print(":"); - Serial.print(bssid[2],16);Serial.print(":"); - Serial.print(bssid[1],16);Serial.print(":"); - Serial.println(bssid[0],16); - - int32_t rssi = WiFi.RSSI(); - Serial.print("RSSI:"); - Serial.println(rssi,10); - - uint8_t enct = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(enct,16); -} - -void scanNetworks() -{ - byte numSsid = WiFi.scanNetworks(); - if (numSsid > MAX_NUM_SSID) numSsid = MAX_NUM_SSID; - Serial.print("SSID List:"); - Serial.println(numSsid, 10); - for (int i = 0; i0) - { - dataBuf[idx]=0; - //Serial.println((char*)&dataBuf[0]); - server.write((char*)&dataBuf[0]); - } - return; - } - } - */ -} - - From e82ab0d0bc60b50f28ff976800a23cd1989a7dcb Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 9 Mar 2012 18:37:07 -0500 Subject: [PATCH 56/98] UpUpdatedWifiWebClient --- WiFi/examples/WifiWebClient/WifiWebClient.ino | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 5ad5582d0..7f9297a9d 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 4 Mar 2012 + modified 9 Mar 2012 by Tom Igoe */ @@ -29,7 +29,7 @@ char pass[] = "password"; // your network password (use for WPA, or use as ke int keyIndex = 0; // your network key Index number (needed only for WEP) int status = WL_IDLE_STATUS; -IPAddress server(74,125,115,105); // Google +IPAddress server(173,194,43,18); // Google // Initialize the Ethernet client library // with the IP address and port of the server @@ -56,7 +56,9 @@ void setup() { if (client.connect(server, 80)) { Serial.println("connected to server"); // Make a HTTP request: - client.println("GET /search?q=arduino HTTP/1.0"); + client.println("GET /search?q=arduino HTTP/1.1"); + client.println("Host:www.google.com"); + client.println("Connection:close"); client.println(); } } @@ -65,7 +67,7 @@ void setup() { void loop() { // if there are incoming bytes available // from the server, read them and print them: - if (client.available()) { + if (client.available()>0) { char c = client.read(); Serial.print(c); } From 44fb113fd3ca4bc1cbd03805c3045b207b368f7c Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 9 Mar 2012 21:42:57 -0500 Subject: [PATCH 57/98] Reformatted reporting on ScanNetworks --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index e6de67e63..c1267e672 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 4 Mar 2012 + modified 9 Mar 2012 by Tom Igoe */ @@ -68,16 +68,17 @@ void listNetworks() { Serial.print("number of available networks:"); Serial.println(numSsid); - Serial.println("#\tName\tRSSI\tencryptionType:"); // print the network number and name for each network found: for (int thisNet = 0; thisNet Date: Fri, 9 Mar 2012 21:43:49 -0500 Subject: [PATCH 58/98] Updated the PachubeClient and PachubeClientString sketches to make HTTP calling consistent --- .../WifiPachubeClient/WifiPachubeClient.ino | 31 ++++++++----------- .../WifiPachubeClientString.ino | 31 +++++++++---------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index 980d84945..0d2f4db8a 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -15,31 +15,31 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 4 March 2012 + created 9 March 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 Project" // user agent is the project name +#define FEEDID 00000 // replace your feed ID +#define USERAGENT "My Project" // user agent is the project name -char ssid[] = "yourNetwork"; // your network SSID (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; +IPAddress server(216,52,233,122); +//char server[] = "api.pachube.com"; -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 int postingInterval = 10000; //delay between updates to Pachube.com +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 Pachube.com void setup() { Serial.begin(9600); @@ -93,7 +93,7 @@ void loop() { // this method makes a HTTP connection to the server: void sendData(int thisData) { // if there's a successful connection: - if (client.connect("www.pachube.com", 80)) { + if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request: client.print("PUT /v2/feeds/"); @@ -119,8 +119,6 @@ void sendData(int thisData) { client.print("sensor1,"); client.println(thisData); - // note the time that the connection was made: - lastConnectionTime = millis(); } else { // if you couldn't make a connection: @@ -129,6 +127,8 @@ void sendData(int thisData) { Serial.println("disconnecting."); client.stop(); } + // note the time that the connection was made: + lastConnectionTime = millis(); lastConnected = client.connected(); } @@ -160,7 +160,7 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: @@ -169,8 +169,3 @@ void printWifiStatus() { Serial.print(rssi); Serial.println(" dBm"); } - - - - - diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index 8c655d9d2..674006571 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -18,7 +18,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 4 March 2012 + created 9 March 2012 by Tom Igoe This code is in the public domain. @@ -29,19 +29,21 @@ #include #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here -#define FEEDID 00000 // replace your feed ID -#define USERAGENT "My Project" // user agent is the project name +#define FEEDID 00000 // replace your feed ID +#define USERAGENT "My Project" // user agent is the project name -char ssid[] = "yourNetwork"; // your network SSID (name) +char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "secretPassword"; // your network password -int status = WL_IDLE_STATUS; +int status = WL_IDLE_STATUS // initialize the library instance: WiFiClient client; +IPAddress server(216,52,233,122); +//char server[] = "api.pachube.com"; -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 int postingInterval = 10000; //delay between updates to Pachube.com +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 Pachube.com void setup() { // start serial port: @@ -87,7 +89,7 @@ void loop() { } // if you're not connected, and ten seconds have passed since - // your last connection, then connect again and send data: + // your last connection, then connect again and send data: if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { sendData(dataString); } @@ -99,7 +101,7 @@ void loop() { // this method makes a HTTP connection to the server: void sendData(String thisData) { // if there's a successful connection: - if (client.connect("api.pachube.com", 80)) { + if (client.connect(server, 80)) { Serial.println("connecting..."); // send the HTTP PUT request: client.print("PUT /v2/feeds/"); @@ -119,9 +121,6 @@ void sendData(String thisData) { // here's the actual content of the PUT request: client.println(thisData); - - // note the time that the connection was made: - lastConnectionTime = millis(); } else { // if you couldn't make a connection: @@ -129,8 +128,8 @@ void sendData(String thisData) { Serial.println(); Serial.println("disconnecting."); client.stop(); - lastConnected = client.connected(); } + // note the time that the connection was made: + lastConnectionTime = millis(); + lastConnected = client.connected(); } - - From d6b768d579678a061b32e46082c3decce9fcca01 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 9 Mar 2012 21:44:15 -0500 Subject: [PATCH 59/98] Added raw output to TwitterClient for debugging --- .../examples/WifiTwitterClient/WifiTwitterClient.ino | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index e0c2d9f9c..853c9f92c 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -13,7 +13,7 @@ Circuit: * WiFi shield attached to pins 10, 11, 12, 13 - created 8 Mar 2012 + created 9 Mar 2012 by Tom Igoe This code is in the public domain. @@ -71,7 +71,9 @@ void loop() if (client.available()) { // read incoming bytes: char inChar = client.read(); - + // print the incoming byte (for debugging): + Serial.write(inChar); + // add incoming byte to end of line: currentLine += inChar; @@ -117,7 +119,8 @@ void connectToServer() { Serial.println("making HTTP request..."); // make HTTP GET request to twitter: client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); - client.println("HOST:api.twitter.com"); + client.println("Host:api.twitter.com"); + client.println("Connection:close"); client.println(); } // note the time of this connect attempt: @@ -132,7 +135,7 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: @@ -142,3 +145,4 @@ void printWifiStatus() { Serial.println(" dBm"); } + From bd4a9a68166937728865e2e37c81c59125df0ffe Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 9 Mar 2012 21:44:35 -0500 Subject: [PATCH 60/98] Updated Google numeric address in WifiWebClient --- WiFi/examples/WifiWebClient/WifiWebClient.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 7f9297a9d..913442496 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -26,10 +26,12 @@ 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) int status = WL_IDLE_STATUS; -IPAddress server(173,194,43,18); // Google +IPAddress server(173,194,73,105); // Google +//char server[] = "www.google.com"; // Initialize the Ethernet client library // with the IP address and port of the server From d09655d4780dbd444c737cbdb015c7eee268300d Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 12 Mar 2012 13:57:42 -0400 Subject: [PATCH 61/98] Updated WifiChatServer to clear out garbage characters --- .../WifiChatServer/WifiChatServer.ino | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 441491f13..22b9e546a 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -14,24 +14,24 @@ created 18 Dec 2009 by David A. Mellis - modified 4 Mar 2012 + modified 12 Mar 2012 by Tom Igoe */ - #include #include 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) int status = WL_IDLE_STATUS; WiFiServer server(23); -boolean gotAMessage = false; // whether or not you got a message from the client yet +boolean alreadyConnected = false; // whether or not the client was connected previously void setup() { // initialize serial: @@ -55,20 +55,25 @@ void loop() { // wait for a new client: WiFiClient client = server.available(); + // when the client sends the first byte, say hello: if (client) { - if (!gotAMessage) { + if (!alreadyConnected) { + // clead out the input buffer: + client.flush(); Serial.println("We have a new client"); client.println("Hello, client!"); - gotAMessage = true; - } + alreadyConnected = true; + } - // 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.print(thisChar); + 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); + } } } From 71ee20314fbdc15da75dda68f1e993881e90f61a Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 12 Mar 2012 14:35:02 -0400 Subject: [PATCH 62/98] Re-formatted ScanNetworks output --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index c1267e672..ecae9b300 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 9 Mar 2012 + modified 12 Mar 2012 by Tom Igoe */ @@ -26,7 +26,6 @@ void setup() { Serial.println("Initializing Wifi..."); printMacAddress(); - // scan for existing networks: Serial.println("Scanning available networks..."); listNetworks(); @@ -72,13 +71,12 @@ void listNetworks() { for (int thisNet = 0; thisNet Date: Tue, 13 Mar 2012 12:21:54 +0100 Subject: [PATCH 63/98] Fixed issue with WiFiWebClient, WiFiPachubeClient, WiFiTwitterClient --- WiFi/WiFiClient.cpp | 3 ++- WiFi/utility/server_drv.cpp | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index 075af56c9..c85ea4217 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -44,7 +44,8 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) { if (_sock != NO_SOCKET_AVAIL) { ServerDrv::startClient(uint32_t(ip), port, _sock); - WiFiClass::_state[_sock] = _sock; + WiFiClass::_state[_sock] = _sock; + while(!connected()); }else{ return 0; } diff --git a/WiFi/utility/server_drv.cpp b/WiFi/utility/server_drv.cpp index 7b5194089..c11e94109 100644 --- a/WiFi/utility/server_drv.cpp +++ b/WiFi/utility/server_drv.cpp @@ -1,3 +1,5 @@ +//#define _DEBUG_ + #include "server_drv.h" #include "Arduino.h" From 84403e25127a6a9e2b9e6c519fa4060c09fc5e9a Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Thu, 15 Mar 2012 01:18:43 +0100 Subject: [PATCH 64/98] Fixed DNS issue --- WiFi/WiFi.cpp | 5 ++++ WiFi/WiFi.h | 9 ++++++ WiFi/WiFiClient.cpp | 22 ++++---------- WiFi/utility/wifi_drv.cpp | 62 +++++++++++++++++++++++++++++++++++++++ WiFi/utility/wifi_drv.h | 13 ++++++++ WiFi/utility/wifi_spi.h | 2 ++ 6 files changed, 97 insertions(+), 16 deletions(-) diff --git a/WiFi/WiFi.cpp b/WiFi/WiFi.cpp index 2dd59c76d..8bb66f215 100755 --- a/WiFi/WiFi.cpp +++ b/WiFi/WiFi.cpp @@ -175,4 +175,9 @@ 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 index 1bdaf9fe0..16d6a0ee7 100755 --- a/WiFi/WiFi.h +++ b/WiFi/WiFi.h @@ -159,6 +159,15 @@ public: */ 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; }; diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index c85ea4217..ce325cc9f 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -21,22 +21,12 @@ WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) { } int WiFiClient::connect(const char* host, uint16_t port) { - /* TODO Add DNS wifi spi function to resolve DNS */ -#if 0 - // Look up the host first - int ret = 0; - DNSClient dns; - IPAddress remote_addr; - - dns.begin(Ethernet.dnsServerIP()); - ret = dns.getHostByName(host, remote_addr); - if (ret == 1) { - return connect(remote_addr, port); - } else { - return ret; - } -#endif - return 0; + IPAddress remote_addr; + if (WiFi.hostByName(host, remote_addr)) + { + return connect(remote_addr, port); + } + return 0; } int WiFiClient::connect(IPAddress ip, uint16_t port) { diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 00a52eb4b..0e3c2813c 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -376,4 +376,66 @@ int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem) 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(500); + } + }else{ + return 0; + } + return 1; +} + WiFiDrv wiFiDrv; diff --git a/WiFi/utility/wifi_drv.h b/WiFi/utility/wifi_drv.h index a1a8a8383..056e6126a 100644 --- a/WiFi/utility/wifi_drv.h +++ b/WiFi/utility/wifi_drv.h @@ -31,6 +31,10 @@ private: */ 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: /* @@ -181,6 +185,15 @@ public: */ 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; diff --git a/WiFi/utility/wifi_spi.h b/WiFi/utility/wifi_spi.h index daa06ea45..01e1acd0b 100644 --- a/WiFi/utility/wifi_spi.h +++ b/WiFi/utility/wifi_spi.h @@ -45,6 +45,8 @@ enum { 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, // All command with DATA_FLAG 0x40 send a 16bit Len From 01a0e7e3de3d3a728d1815e6bfcb58187184bab3 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 13 Mar 2012 13:00:14 -0400 Subject: [PATCH 65/98] Added option for DNS in all HTTP client examples --- .../WifiPachubeClient/WifiPachubeClient.ino | 32 ++++++++++-------- .../WifiPachubeClientString.ino | 33 ++++++++++++------- .../WifiTwitterClient/WifiTwitterClient.ino | 25 +++++++------- WiFi/examples/WifiWebClient/WifiWebClient.ino | 15 +++++---- 4 files changed, 61 insertions(+), 44 deletions(-) diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index 0d2f4db8a..de9e474c4 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -15,7 +15,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 9 March 2012 + created 13 March 2012 by Tom Igoe This code is in the public domain. @@ -26,20 +26,23 @@ #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here #define FEEDID 00000 // replace your feed ID -#define USERAGENT "My Project" // user agent is the project name +#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; -IPAddress server(216,52,233,122); -//char server[] = "api.pachube.com"; +// 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,122); // numeric IP for api.pachube.com +//char server[] = "api.pachube.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 Pachube.com +const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com void setup() { Serial.begin(9600); @@ -48,9 +51,9 @@ void setup() { Serial.println(ssid); status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { + if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); - // don't do anything else: + // stop here and do nothing: while(true); } else { @@ -99,7 +102,7 @@ void sendData(int thisData) { client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); - client.print("Host: api.pachube.com\n"); + client.println("Host: api.pachube.com"); client.print("X-PachubeApiKey: "); client.println(APIKEY); client.print("User-Agent: "); @@ -112,13 +115,14 @@ void sendData(int thisData) { client.println(thisLength); // last pieces of the HTTP PUT request: - client.print("Content-Type: text/csv\n"); - client.println("Connection: close\n"); + 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: @@ -127,9 +131,8 @@ void sendData(int thisData) { Serial.println("disconnecting."); client.stop(); } - // note the time that the connection was made: + // note the time that the connection was made or attempted: lastConnectionTime = millis(); - lastConnected = client.connected(); } @@ -169,3 +172,6 @@ void printWifiStatus() { Serial.print(rssi); Serial.println(" dBm"); } + + + diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index 674006571..30c0383ef 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -18,7 +18,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 9 March 2012 + created 13 March 2012 by Tom Igoe This code is in the public domain. @@ -30,27 +30,35 @@ #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here #define FEEDID 00000 // replace your feed ID -#define USERAGENT "My Project" // user agent is the project name +#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 + +int status = WL_IDLE_STATUS; // initialize the library instance: WiFiClient client; -IPAddress server(216,52,233,122); -//char server[] = "api.pachube.com"; + +// 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,122); // numeric IP for api.pachube.com +//char server[] = "api.pachube.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 Pachube.com void setup() { - // start serial port: Serial.begin(9600); + Serial.println("Attempting to connect to Wifi network..."); + Serial.print("SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); + // stop here and do nothing: while(true); } else { @@ -107,17 +115,18 @@ void sendData(String thisData) { client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); - client.print("Host: api.pachube.com\n"); + client.println("Host: api.pachube.com"); client.print("X-PachubeApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); client.print("Content-Length: "); - client.println(thisData.length(), DEC); + client.println(thisData.length()); // last pieces of the HTTP PUT request: - client.print("Content-Type: text/csv\n"); - client.println("Connection: close\n"); + client.println("Content-Type: text/csv"); + client.println("Connection: close"); + client.println(); // here's the actual content of the PUT request: client.println(thisData); @@ -129,7 +138,7 @@ void sendData(String thisData) { Serial.println("disconnecting."); client.stop(); } - // note the time that the connection was made: + // note the time that the connection was made or attempted: lastConnectionTime = millis(); - lastConnected = client.connected(); } + diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 853c9f92c..0465a0a2e 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -13,7 +13,7 @@ Circuit: * WiFi shield attached to pins 10, 11, 12, 13 - created 9 Mar 2012 + created 13 Mar 2012 by Tom Igoe This code is in the public domain. @@ -22,8 +22,9 @@ #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[] = "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) int status = WL_IDLE_STATUS; // status of the wifi connection @@ -32,10 +33,13 @@ WiFiClient client; const unsigned long requestInterval = 30*1000; // delay between requests; 30 seconds -IPAddress server(199,59,149,200); // api.twitter.com +// 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(199,59,149,200); // numeric IP for api.twitter.com +//char server[] = "api.twitter.com"; // name address for twitter API boolean requested; // whether you've made a request since connecting -unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds +unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds String currentLine = ""; // string to hold the text from server String tweet = ""; // string to hold the tweet @@ -54,25 +58,21 @@ void setup() { status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); + // stop here and do nothing: while(true); } - else { + else { Serial.println("Connected to wifi"); printWifiStatus(); connectToServer(); } } - - - void loop() { if (client.connected()) { if (client.available()) { // read incoming bytes: char inChar = client.read(); - // print the incoming byte (for debugging): - Serial.write(inChar); // add incoming byte to end of line: currentLine += inChar; @@ -118,7 +118,7 @@ void connectToServer() { if (client.connect(server, 80)) { Serial.println("making HTTP request..."); // make HTTP GET request to twitter: - client.println("GET /1/statuses/user_timeline.xml?screen_name=arduinoteam HTTP/1.1"); + client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino HTTP/1.1"); client.println("Host:api.twitter.com"); client.println("Connection:close"); client.println(); @@ -146,3 +146,4 @@ void printWifiStatus() { } + diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 913442496..c7d5b32b0 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 9 Mar 2012 + modified 13 Mar 2012 by Tom Igoe */ @@ -26,12 +26,13 @@ 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) int status = WL_IDLE_STATUS; -IPAddress server(173,194,73,105); // Google -//char server[] = "www.google.com"; +// 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 @@ -47,10 +48,10 @@ void setup() { status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); - // don't do anything else: + // stop here and do nothing: while(true); } - else { + else { Serial.println("Connected to wifi"); printWifiStatus(); Serial.println("\nStarting connection to server..."); @@ -60,7 +61,7 @@ void setup() { // 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("Connection: close"); client.println(); } } From 51d7a56ae25d3a9fe0421fee0f5949aaafdd80e5 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Thu, 15 Mar 2012 13:52:17 -0400 Subject: [PATCH 66/98] Updated twitter client --- .../WifiTwitterClient/WifiTwitterClient.ino | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 0465a0a2e..1f7c918d6 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -13,7 +13,7 @@ Circuit: * WiFi shield attached to pins 10, 11, 12, 13 - created 13 Mar 2012 + created 15 Mar 2012 by Tom Igoe This code is in the public domain. @@ -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[] = "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) int status = WL_IDLE_STATUS; // status of the wifi connection @@ -35,8 +35,8 @@ const unsigned long requestInterval = 30*1000; // delay between requests; 30 // 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(199,59,149,200); // numeric IP for api.twitter.com -//char server[] = "api.twitter.com"; // name address for twitter API +//IPAddress server(199,59,149,200); // numeric IP for api.twitter.com +char server[] = "api.twitter.com"; // name address for twitter API boolean requested; // whether you've made a request since connecting unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds @@ -52,9 +52,7 @@ void setup() { // initialize serial: Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); - Serial.print("SSID: "); - Serial.println(ssid); - + status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); @@ -87,6 +85,8 @@ void loop() // tweet is beginning. Clear the tweet string: readingTweet = true; tweet = ""; + // break out of the loop so this character isn't added to the tweet: + return; } // if you're currently reading the bytes of a tweet, // add them to the tweet String: From 533edc638775f121a5d35591373d6f6e01494a2b Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Fri, 16 Mar 2012 10:38:20 -0400 Subject: [PATCH 67/98] Updated WifiPachubeClientString to use DNS --- .../WifiPachubeClientString.ino | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index 30c0383ef..9ff287dfa 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -18,7 +18,7 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 13 March 2012 + created 16 March 2012 by Tom Igoe This code is in the public domain. @@ -42,8 +42,8 @@ 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,122); // numeric IP for api.pachube.com -//char server[] = "api.pachube.com"; // name address for pachube API +//IPAddress server(216,52,233,122); // numeric IP for api.pachube.com +char server[] = "api.pachube.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 @@ -63,6 +63,7 @@ void setup() { } else { Serial.println("Connected to wifi"); + printWifiStatus(); } } @@ -142,3 +143,21 @@ void sendData(String thisData) { 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"); +} + From bcfde7af9a2fdd9b11a951e0ad22f61c23efdc33 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Tue, 20 Mar 2012 10:36:33 -0400 Subject: [PATCH 68/98] Updated WifiWebServer to give more diagnostic info --- WiFi/examples/WifiWebServer/WifiWebServer.ino | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index 721b3d8ad..a27189a52 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 4 Mar 2012 + modified 20 Mar 2012 by Tom Igoe */ @@ -21,9 +21,10 @@ #include #include -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) + +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; @@ -53,11 +54,13 @@ 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 @@ -65,18 +68,23 @@ void loop() { // 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(analogRead(analogChannel)); + client.print(sensorReading); client.println("
"); } client.println(""); - break; + break; } if (c == '\n') { // you're starting a new line @@ -90,8 +98,9 @@ void loop() { } // give the web browser time to receive the data delay(1); - // close the connection: - client.stop(); + // close the connection: + client.stop(); + Serial.println("client disonnected"); } } @@ -103,7 +112,7 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: @@ -112,3 +121,4 @@ void printWifiStatus() { Serial.print(rssi); Serial.println(" dBm"); } + From 28da3b1d383260ac4a7c54ace04bda18c6cdd988 Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Sat, 24 Mar 2012 09:10:02 +0100 Subject: [PATCH 69/98] Added verbose debug command --- WiFi/WiFiClient.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WiFi/WiFiClient.cpp b/WiFi/WiFiClient.cpp index ce325cc9f..8f517cc19 100755 --- a/WiFi/WiFiClient.cpp +++ b/WiFi/WiFiClient.cpp @@ -126,7 +126,8 @@ uint8_t WiFiClient::connected() { } else { uint8_t s = status(); - return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || s == FIN_WAIT_2 || + return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 || + s == FIN_WAIT_2 || s == TIME_WAIT || (s == CLOSE_WAIT && !available())); } } From 5e9c9563387367c7f4fdeae400631be17a99cf26 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 2 Apr 2012 09:45:39 -0400 Subject: [PATCH 70/98] Added Serial open check for Leonardo to ScanNetworks example --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index ecae9b300..f0c379fe2 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 12 Mar 2012 + modified 2 April 2012 by Tom Igoe */ @@ -19,8 +19,9 @@ #include void setup() { - // initialize serial: + // initialize serial and wait for the port to open: Serial.begin(9600); + while(!Serial) ; // attempt to connect using WEP encryption: Serial.println("Initializing Wifi..."); @@ -41,7 +42,7 @@ void loop() { void printMacAddress() { // the MAC address of your Wifi shield byte mac[6]; - + // print your MAC address: WiFi.macAddress(mac); Serial.print("MAC: "); @@ -66,7 +67,7 @@ void listNetworks() { // 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 Date: Mon, 23 Apr 2012 11:26:27 -0400 Subject: [PATCH 71/98] Added Repeating Web Client example --- .../WifiWebClientRepeating.ino | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino new file mode 100644 index 000000000..f4c71c6c7 --- /dev/null +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -0,0 +1,128 @@ +/* + 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 Apr 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); + + // 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); + // wait 10 seconds for connection: + delay(10000); + } + // you're connected now, so print out the status: + printWifiStatus(); + // print the Wifi board/shield's IP address: + Serial.print("My IP address: "); + Serial.println(WiFi.localIP()); +} + +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"); +} + + + + + + From 78b5e3cba5ab29aecd1f34ca5d176cf557e3a338 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 23 Apr 2012 11:26:58 -0400 Subject: [PATCH 72/98] Updated all Wifi web client and server examples to attempt continual reconnects to the SSID --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 1 - .../WifiChatServer/WifiChatServer.ino | 35 ++++++------ .../WifiPachubeClient/WifiPachubeClient.ino | 30 ++++++----- .../WifiPachubeClientString.ino | 29 +++++----- .../WifiTwitterClient/WifiTwitterClient.ino | 34 ++++++------ WiFi/examples/WifiWebClient/WifiWebClient.ino | 54 +++++++++---------- WiFi/examples/WifiWebServer/WifiWebServer.ino | 33 ++++++------ 7 files changed, 114 insertions(+), 102 deletions(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index f0c379fe2..fda03a0f2 100644 --- a/WiFi/examples/ScanNetworks/ScanNetworks.ino +++ b/WiFi/examples/ScanNetworks/ScanNetworks.ino @@ -21,7 +21,6 @@ void setup() { // initialize serial and wait for the port to open: Serial.begin(9600); - while(!Serial) ; // attempt to connect using WEP encryption: Serial.println("Initializing Wifi..."); diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index 22b9e546a..d9112e625 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 12 Mar 2012 + modified 23 Apr 2012 by Tom Igoe */ @@ -34,28 +34,32 @@ WiFiServer server(23); boolean alreadyConnected = false; // whether or not the client was connected previously void setup() { - // initialize serial: + // start serial port: Serial.begin(9600); - Serial.println("Attempting to connect to Wifi network..."); - Serial.print("SSID: "); - Serial.println(ssid); - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - 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); + // wait 10 seconds for connection: + delay(10000); } - else { - server.begin(); - Serial.print("Connected to wifi."); - printWifiStatus(); - } + // start the server: + server.begin(); + // you're connected now, so print out the status: + printWifiStatus(); + // print the Wifi board/shield's IP address: + Serial.print("My IP address: "); + Serial.println(WiFi.localIP()); } + + void loop() { // wait for a new client: WiFiClient client = server.available(); - + // when the client sends the first byte, say hello: if (client) { if (!alreadyConnected) { @@ -95,3 +99,4 @@ void printWifiStatus() { Serial.println(" dBm"); } + diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index de9e474c4..09e968e0a 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -15,7 +15,8 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 13 March 2012 + created 13 Mar 2012 + modified 23 Apr 2012 by Tom Igoe This code is in the public domain. @@ -45,21 +46,22 @@ boolean lastConnected = false; // state of the connection last t const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com void setup() { + // start serial port: Serial.begin(9600); - Serial.println("Attempting to connect to Wifi network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - // stop here and do nothing: - 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); + // wait 10 seconds for connection: + delay(10000); } - else { - Serial.println("Connected to wifi"); - printWifiStatus(); - } + // you're connected now, so print out the status: + printWifiStatus(); + // print the Wifi board/shield's IP address: + Serial.print("My IP address: "); + Serial.println(WiFi.localIP()); } diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index 9ff287dfa..e347459f6 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -18,7 +18,8 @@ * Analog sensor attached to analog in 0 * Wifi shield attached to pins 10, 11, 12, 13 - created 16 March 2012 + created 16 Mar 2012 + modified 23 Apr 2012 by Tom Igoe This code is in the public domain. @@ -50,21 +51,22 @@ boolean lastConnected = false; // state of the connection last t const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com void setup() { + // start serial port: Serial.begin(9600); - Serial.println("Attempting to connect to Wifi network..."); - Serial.print("SSID: "); - Serial.println(ssid); - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - // stop here and do nothing: - 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); + // wait 10 seconds for connection: + delay(10000); } - else { - Serial.println("Connected to wifi"); - printWifiStatus(); - } + // you're connected now, so print out the status: + printWifiStatus(); + // print the Wifi board/shield's IP address: + Serial.print("My IP address: "); + Serial.println(WiFi.localIP()); } void loop() { @@ -161,3 +163,4 @@ void printWifiStatus() { Serial.println(" dBm"); } + diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index 1f7c918d6..b02da2103 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -13,7 +13,7 @@ Circuit: * WiFi shield attached to pins 10, 11, 12, 13 - created 15 Mar 2012 + created 23 apr 2012 by Tom Igoe This code is in the public domain. @@ -24,8 +24,8 @@ 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) + int status = WL_IDLE_STATUS; // status of the wifi connection // initialize the library instance: @@ -49,22 +49,25 @@ void setup() { // reserve space for the strings: currentLine.reserve(256); tweet.reserve(150); - // initialize serial: + // start serial port: Serial.begin(9600); - Serial.println("Attempting to connect to WPA network..."); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - // stop here and do nothing: - 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); + // wait 10 seconds for connection: + delay(10000); } - else { - Serial.println("Connected to wifi"); - printWifiStatus(); - connectToServer(); - } + // you're connected now, so print out the status: + printWifiStatus(); + // print the Wifi board/shield's IP address: + Serial.print("My IP address: "); + Serial.println(WiFi.localIP()); + connectToServer(); } + void loop() { if (client.connected()) { @@ -147,3 +150,4 @@ void printWifiStatus() { + diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index c7d5b32b0..783761135 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -5,9 +5,9 @@ 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 + 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. @@ -16,7 +16,7 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 13 Mar 2012 + modified 23 Apr 2012 by Tom Igoe */ @@ -41,38 +41,37 @@ WiFiClient client; void setup() { Serial.begin(9600); - Serial.println("Attempting to connect to Wifi network..."); - Serial.print("SSID: "); - Serial.println(ssid); - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - // stop here and do nothing: - 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); + // wait 10 seconds for connection: + delay(10000); } - else { - 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(); - } + 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: - if (client.available()>0) { + while (client.available()) { char c = client.read(); - Serial.print(c); + Serial.write(c); } // if the server's disconnected, stop the client: @@ -94,7 +93,7 @@ void printWifiStatus() { // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); + Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: @@ -107,3 +106,4 @@ void printWifiStatus() { + diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index a27189a52..b4cbce6c6 100644 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -13,11 +13,9 @@ created 13 July 2010 by dlf (Metodo2 srl) - modified 20 Mar 2012 + modified 23 Apr 2012 by Tom Igoe */ - - #include #include @@ -31,22 +29,23 @@ int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { - // initialize serial: + // start serial port: Serial.begin(9600); - Serial.println("Attempting to connect to Wifi network..."); - Serial.print("SSID: "); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - if ( status != WL_CONNECTED) { - Serial.println("Couldn't get a wifi connection"); - 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); + // wait 10 seconds for connection: + delay(10000); } - else { - server.begin(); - Serial.print("Connected to wifi."); - printWifiStatus(); - } + server.begin(); + // you're connected now, so print out the status: + printWifiStatus(); + // print the Wifi board/shield's IP address: + Serial.print("My IP address: "); + Serial.println(WiFi.localIP()); } From 7ce1019dd0aa65f47be7ee28d33073fffcdd7512 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 23 Apr 2012 11:33:42 -0400 Subject: [PATCH 73/98] Removed estraneous lines from Web examples --- WiFi/examples/WifiChatServer/WifiChatServer.ino | 5 +---- WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino | 3 --- .../WifiPachubeClientString/WifiPachubeClientString.ino | 3 --- WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino | 3 --- WiFi/examples/WifiWebClient/WifiWebClient.ino | 1 - .../WifiWebClientRepeating/WifiWebClientRepeating.ino | 3 --- WiFi/examples/WifiWebServer/WifiWebServer.ino | 3 --- 7 files changed, 1 insertion(+), 20 deletions(-) diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/WiFi/examples/WifiChatServer/WifiChatServer.ino index d9112e625..8b6698f40 100644 --- a/WiFi/examples/WifiChatServer/WifiChatServer.ino +++ b/WiFi/examples/WifiChatServer/WifiChatServer.ino @@ -49,10 +49,7 @@ void setup() { server.begin(); // you're connected now, so print out the status: printWifiStatus(); - // print the Wifi board/shield's IP address: - Serial.print("My IP address: "); - Serial.println(WiFi.localIP()); -} + } void loop() { diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index 09e968e0a..6e447416f 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -59,9 +59,6 @@ void setup() { } // you're connected now, so print out the status: printWifiStatus(); - // print the Wifi board/shield's IP address: - Serial.print("My IP address: "); - Serial.println(WiFi.localIP()); } diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index e347459f6..57f2a7530 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -64,9 +64,6 @@ void setup() { } // you're connected now, so print out the status: printWifiStatus(); - // print the Wifi board/shield's IP address: - Serial.print("My IP address: "); - Serial.println(WiFi.localIP()); } void loop() { diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino index b02da2103..8bf7bb2c2 100644 --- a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino +++ b/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino @@ -62,9 +62,6 @@ void setup() { } // you're connected now, so print out the status: printWifiStatus(); - // print the Wifi board/shield's IP address: - Serial.print("My IP address: "); - Serial.println(WiFi.localIP()); connectToServer(); } diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/WiFi/examples/WifiWebClient/WifiWebClient.ino index 783761135..f6a3cdf23 100644 --- a/WiFi/examples/WifiWebClient/WifiWebClient.ino +++ b/WiFi/examples/WifiWebClient/WifiWebClient.ino @@ -62,7 +62,6 @@ void setup() { client.println("Host:www.google.com"); client.println("Connection: close"); client.println(); - } } diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino index f4c71c6c7..8e5b88547 100644 --- a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino +++ b/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino @@ -48,9 +48,6 @@ void setup() { } // you're connected now, so print out the status: printWifiStatus(); - // print the Wifi board/shield's IP address: - Serial.print("My IP address: "); - Serial.println(WiFi.localIP()); } void loop() { diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/WiFi/examples/WifiWebServer/WifiWebServer.ino index b4cbce6c6..34a231b5c 100644 --- a/WiFi/examples/WifiWebServer/WifiWebServer.ino +++ b/WiFi/examples/WifiWebServer/WifiWebServer.ino @@ -43,9 +43,6 @@ void setup() { server.begin(); // you're connected now, so print out the status: printWifiStatus(); - // print the Wifi board/shield's IP address: - Serial.print("My IP address: "); - Serial.println(WiFi.localIP()); } From 6e149c4e323c4fd42afa79ea9e95192c62af5c3e Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 23 Apr 2012 11:39:15 -0400 Subject: [PATCH 74/98] Took Leonardo check out of ScanNetworks example --- WiFi/examples/ScanNetworks/ScanNetworks.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/WiFi/examples/ScanNetworks/ScanNetworks.ino index fda03a0f2..2c10fed33 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 2 April 2012 + modified 22 April 2012 by Tom Igoe */ From b114164af1485bd846ac0a9c01d4efe7604afa11 Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Mon, 14 May 2012 16:56:35 -0400 Subject: [PATCH 75/98] Pachube is dead. Long live Cosm. All Pachube examples changed to Cosm examples --- .../WifiCosmClient.ino} | 20 +++++++++---------- .../WifiCosmClientString.ino} | 18 ++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) rename WiFi/examples/{WifiPachubeClient/WifiPachubeClient.ino => WifiCosmClient/WifiCosmClient.ino} (91%) rename WiFi/examples/{WifiPachubeClientString/WifiPachubeClientString.ino => WifiCosmClientString/WifiCosmClientString.ino} (90%) diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino similarity index 91% rename from WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino rename to WiFi/examples/WifiCosmClient/WifiCosmClient.ino index 6e447416f..bd43d59fa 100644 --- a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino +++ b/WiFi/examples/WifiCosmClient/WifiCosmClient.ino @@ -1,13 +1,13 @@ /* - Wifi Pachube sensor client + Wifi Cosm sensor client - This sketch connects an analog sensor to Pachube (http://www.pachube.com) + 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 Pachube.com API. + 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. @@ -16,7 +16,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 13 Mar 2012 - modified 23 Apr 2012 + modified 14 May 2012 by Tom Igoe This code is in the public domain. @@ -25,7 +25,7 @@ #include #include -#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here +#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 @@ -38,12 +38,12 @@ int status = WL_IDLE_STATUS; 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,122); // numeric IP for api.pachube.com -//char server[] = "api.pachube.com"; // name address for pachube API +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 Pachube.com +const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com void setup() { // start serial port: @@ -101,8 +101,8 @@ void sendData(int thisData) { client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); - client.println("Host: api.pachube.com"); - client.print("X-PachubeApiKey: "); + client.println("Host: api.cosm.com"); + client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino similarity index 90% rename from WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino rename to WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino index 57f2a7530..ec3da3174 100644 --- a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino +++ b/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino @@ -1,13 +1,13 @@ /* - Wifi Pachube sensor client with Strings + Wifi Cosm sensor client with Strings - This sketch connects an analog sensor to Pachube (http://www.pachube.com) + 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 Pachube.com API. + 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. @@ -19,7 +19,7 @@ * Wifi shield attached to pins 10, 11, 12, 13 created 16 Mar 2012 - modified 23 Apr 2012 + modified 14 May 2012 by Tom Igoe This code is in the public domain. @@ -43,12 +43,12 @@ 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,122); // numeric IP for api.pachube.com -char server[] = "api.pachube.com"; // name address for pachube API +//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 Pachube.com +const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com void setup() { // start serial port: @@ -115,8 +115,8 @@ void sendData(String thisData) { client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); - client.println("Host: api.pachube.com"); - client.print("X-PachubeApiKey: "); + client.println("Host: api.cosm.com"); + client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); client.println(USERAGENT); From 47e6129e019ae10260eaa3a65b6188be8635bfee Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Sun, 20 May 2012 23:54:32 +0200 Subject: [PATCH 76/98] Porting HD API ver. 2.7 --- WiFi/utility/wifi_drv.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WiFi/utility/wifi_drv.cpp b/WiFi/utility/wifi_drv.cpp index 0e3c2813c..180633852 100644 --- a/WiFi/utility/wifi_drv.cpp +++ b/WiFi/utility/wifi_drv.cpp @@ -430,12 +430,12 @@ int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult) { while(!getHostByName(aResult) && --retry > 0) { - delay(500); + delay(1000); } }else{ return 0; } - return 1; + return (retry>0); } WiFiDrv wiFiDrv; From b6490ad458a522a6d1237e52a9a8bb23f210b0fa Mon Sep 17 00:00:00 2001 From: Mimmo La Fauci Date: Thu, 24 May 2012 08:57:46 +0200 Subject: [PATCH 77/98] 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 78/98] 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 79/98] 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 80/98] 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 81/98] 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 82/98] 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 83/98] 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 84/98] 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 85/98] 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 86/98] 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 87/98] 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 88/98] 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 89/98] 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 90/98] 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 91/98] 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 92/98] 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 93/98] 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 94/98] 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 95/98] 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 96/98] 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; + } +} + From 0b0566054ebdd71258d1a9aac9f85f71a9d2bdd1 Mon Sep 17 00:00:00 2001 From: Scott Date: Sat, 8 Sep 2012 15:49:37 -0400 Subject: [PATCH 97/98] Renamed Cosm examples to Pachube --- .../WifiPachubeClient.ino} | 18 ++++++++++-------- .../WifiPachubeClientString.ino} | 16 +++++++++------- 2 files changed, 19 insertions(+), 15 deletions(-) rename WiFi/examples/{WifiCosmClient/WifiCosmClient.ino => WifiPachubeClient/WifiPachubeClient.ino} (91%) rename WiFi/examples/{WifiCosmClientString/WifiCosmClientString.ino => WifiPachubeClientString/WifiPachubeClientString.ino} (91%) diff --git a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino similarity index 91% rename from WiFi/examples/WifiCosmClient/WifiCosmClient.ino rename to WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino index bd43d59fa..bf590c998 100644 --- a/WiFi/examples/WifiCosmClient/WifiCosmClient.ino +++ b/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino @@ -1,13 +1,13 @@ /* - Wifi Cosm sensor client + Wifi Pachube sensor client - This sketch connects an analog sensor to Cosm (http://www.cosm.com) + This sketch connects an analog sensor to Pachube (http://www.pachube.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. + This example has been updated to use version 2.0 of the Pachube 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. @@ -18,6 +18,8 @@ created 13 Mar 2012 modified 14 May 2012 by Tom Igoe + modified 8 Sept 2012 + by Scott Fitzgerald This code is in the public domain. @@ -25,7 +27,7 @@ #include #include -#define APIKEY "YOUR API KEY GOES HERE" // replace your cosm api key here +#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 @@ -38,12 +40,12 @@ int status = WL_IDLE_STATUS; 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 +IPAddress server(216,52,233,121); // numeric IP for api.pachube.com +//char server[] = "api.pachube.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 +const unsigned long postingInterval = 10*1000; //delay between updates to pachube.com void setup() { // start serial port: @@ -101,7 +103,7 @@ void sendData(int thisData) { client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); - client.println("Host: api.cosm.com"); + client.println("Host: api.pachube.com"); client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); diff --git a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino similarity index 91% rename from WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino rename to WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino index ec3da3174..0a939d417 100644 --- a/WiFi/examples/WifiCosmClientString/WifiCosmClientString.ino +++ b/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino @@ -1,13 +1,13 @@ /* - Wifi Cosm sensor client with Strings + Wifi Pachube sensor client with Strings - This sketch connects an analog sensor to Cosm (http://www.cosm.com) + This sketch connects an analog sensor to Pachube (http://www.pachube.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. + This example has been updated to use version 2.0 of the pachube.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. @@ -21,6 +21,8 @@ created 16 Mar 2012 modified 14 May 2012 by Tom Igoe + modified 8 Sept 2012 + by Scott Fitzgerald This code is in the public domain. @@ -43,12 +45,12 @@ 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 +//IPAddress server(216,52,233,121); // numeric IP for api.pachube.com +char server[] = "api.pachube.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 +const unsigned long postingInterval = 10*1000; //delay between updates to pachube.com void setup() { // start serial port: @@ -115,7 +117,7 @@ void sendData(String thisData) { client.print("PUT /v2/feeds/"); client.print(FEEDID); client.println(".csv HTTP/1.1"); - client.println("Host: api.cosm.com"); + client.println("Host: api.pachube.com"); client.print("X-ApiKey: "); client.println(APIKEY); client.print("User-Agent: "); From ed511819a8ea162a740448081affaa01fa4d8e4e Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Thu, 13 Sep 2012 10:20:48 -0400 Subject: [PATCH 98/98] Moving back into libraries folder. --- {WiFi => libraries/WiFi}/WiFi.cpp | 0 {WiFi => libraries/WiFi}/WiFi.h | 0 {WiFi => libraries/WiFi}/WiFiClient.cpp | 0 {WiFi => libraries/WiFi}/WiFiClient.h | 0 {WiFi => libraries/WiFi}/WiFiServer.cpp | 0 {WiFi => libraries/WiFi}/WiFiServer.h | 0 .../WiFi}/examples/ConnectNoEncryption/ConnectNoEncryption.ino | 0 .../WiFi}/examples/ConnectWithWEP/ConnectWithWEP.ino | 0 .../WiFi}/examples/ConnectWithWPA/ConnectWithWPA.ino | 0 {WiFi => libraries/WiFi}/examples/ScanNetworks/ScanNetworks.ino | 0 .../WiFi}/examples/WifiChatServer/WifiChatServer.ino | 0 .../WiFi}/examples/WifiPachubeClient/WifiPachubeClient.ino | 0 .../examples/WifiPachubeClientString/WifiPachubeClientString.ino | 0 .../WiFi}/examples/WifiTwitterClient/WifiTwitterClient.ino | 0 {WiFi => libraries/WiFi}/examples/WifiWebClient/WifiWebClient.ino | 0 .../examples/WifiWebClientRepeating/WifiWebClientRepeating.ino | 0 {WiFi => libraries/WiFi}/examples/WifiWebServer/WifiWebServer.ino | 0 {WiFi => libraries/WiFi}/keywords.txt | 0 {WiFi => libraries/WiFi}/utility/debug.h | 0 {WiFi => libraries/WiFi}/utility/server_drv.cpp | 0 {WiFi => libraries/WiFi}/utility/server_drv.h | 0 {WiFi => libraries/WiFi}/utility/socket.c | 0 {WiFi => libraries/WiFi}/utility/socket.h | 0 {WiFi => libraries/WiFi}/utility/spi_drv.cpp | 0 {WiFi => libraries/WiFi}/utility/spi_drv.h | 0 {WiFi => libraries/WiFi}/utility/wifi_drv.cpp | 0 {WiFi => libraries/WiFi}/utility/wifi_drv.h | 0 {WiFi => libraries/WiFi}/utility/wifi_spi.h | 0 {WiFi => libraries/WiFi}/utility/wl_definitions.h | 0 {WiFi => libraries/WiFi}/utility/wl_types.h | 0 30 files changed, 0 insertions(+), 0 deletions(-) rename {WiFi => libraries/WiFi}/WiFi.cpp (100%) rename {WiFi => libraries/WiFi}/WiFi.h (100%) rename {WiFi => libraries/WiFi}/WiFiClient.cpp (100%) rename {WiFi => libraries/WiFi}/WiFiClient.h (100%) rename {WiFi => libraries/WiFi}/WiFiServer.cpp (100%) rename {WiFi => libraries/WiFi}/WiFiServer.h (100%) rename {WiFi => libraries/WiFi}/examples/ConnectNoEncryption/ConnectNoEncryption.ino (100%) rename {WiFi => libraries/WiFi}/examples/ConnectWithWEP/ConnectWithWEP.ino (100%) rename {WiFi => libraries/WiFi}/examples/ConnectWithWPA/ConnectWithWPA.ino (100%) rename {WiFi => libraries/WiFi}/examples/ScanNetworks/ScanNetworks.ino (100%) rename {WiFi => libraries/WiFi}/examples/WifiChatServer/WifiChatServer.ino (100%) rename {WiFi => libraries/WiFi}/examples/WifiPachubeClient/WifiPachubeClient.ino (100%) rename {WiFi => libraries/WiFi}/examples/WifiPachubeClientString/WifiPachubeClientString.ino (100%) rename {WiFi => libraries/WiFi}/examples/WifiTwitterClient/WifiTwitterClient.ino (100%) rename {WiFi => libraries/WiFi}/examples/WifiWebClient/WifiWebClient.ino (100%) rename {WiFi => libraries/WiFi}/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino (100%) rename {WiFi => libraries/WiFi}/examples/WifiWebServer/WifiWebServer.ino (100%) rename {WiFi => libraries/WiFi}/keywords.txt (100%) rename {WiFi => libraries/WiFi}/utility/debug.h (100%) rename {WiFi => libraries/WiFi}/utility/server_drv.cpp (100%) rename {WiFi => libraries/WiFi}/utility/server_drv.h (100%) rename {WiFi => libraries/WiFi}/utility/socket.c (100%) rename {WiFi => libraries/WiFi}/utility/socket.h (100%) rename {WiFi => libraries/WiFi}/utility/spi_drv.cpp (100%) rename {WiFi => libraries/WiFi}/utility/spi_drv.h (100%) rename {WiFi => libraries/WiFi}/utility/wifi_drv.cpp (100%) rename {WiFi => libraries/WiFi}/utility/wifi_drv.h (100%) rename {WiFi => libraries/WiFi}/utility/wifi_spi.h (100%) rename {WiFi => libraries/WiFi}/utility/wl_definitions.h (100%) rename {WiFi => libraries/WiFi}/utility/wl_types.h (100%) diff --git a/WiFi/WiFi.cpp b/libraries/WiFi/WiFi.cpp similarity index 100% rename from WiFi/WiFi.cpp rename to libraries/WiFi/WiFi.cpp diff --git a/WiFi/WiFi.h b/libraries/WiFi/WiFi.h similarity index 100% rename from WiFi/WiFi.h rename to libraries/WiFi/WiFi.h diff --git a/WiFi/WiFiClient.cpp b/libraries/WiFi/WiFiClient.cpp similarity index 100% rename from WiFi/WiFiClient.cpp rename to libraries/WiFi/WiFiClient.cpp diff --git a/WiFi/WiFiClient.h b/libraries/WiFi/WiFiClient.h similarity index 100% rename from WiFi/WiFiClient.h rename to libraries/WiFi/WiFiClient.h diff --git a/WiFi/WiFiServer.cpp b/libraries/WiFi/WiFiServer.cpp similarity index 100% rename from WiFi/WiFiServer.cpp rename to libraries/WiFi/WiFiServer.cpp diff --git a/WiFi/WiFiServer.h b/libraries/WiFi/WiFiServer.h similarity index 100% rename from WiFi/WiFiServer.h rename to libraries/WiFi/WiFiServer.h diff --git a/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino b/libraries/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino similarity index 100% rename from WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino rename to libraries/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino diff --git a/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino b/libraries/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino similarity index 100% rename from WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino rename to libraries/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino diff --git a/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino b/libraries/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino similarity index 100% rename from WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino rename to libraries/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino diff --git a/WiFi/examples/ScanNetworks/ScanNetworks.ino b/libraries/WiFi/examples/ScanNetworks/ScanNetworks.ino similarity index 100% rename from WiFi/examples/ScanNetworks/ScanNetworks.ino rename to libraries/WiFi/examples/ScanNetworks/ScanNetworks.ino diff --git a/WiFi/examples/WifiChatServer/WifiChatServer.ino b/libraries/WiFi/examples/WifiChatServer/WifiChatServer.ino similarity index 100% rename from WiFi/examples/WifiChatServer/WifiChatServer.ino rename to libraries/WiFi/examples/WifiChatServer/WifiChatServer.ino diff --git a/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino b/libraries/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino similarity index 100% rename from WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino rename to libraries/WiFi/examples/WifiPachubeClient/WifiPachubeClient.ino diff --git a/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino b/libraries/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino similarity index 100% rename from WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino rename to libraries/WiFi/examples/WifiPachubeClientString/WifiPachubeClientString.ino diff --git a/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino b/libraries/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino similarity index 100% rename from WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino rename to libraries/WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino diff --git a/WiFi/examples/WifiWebClient/WifiWebClient.ino b/libraries/WiFi/examples/WifiWebClient/WifiWebClient.ino similarity index 100% rename from WiFi/examples/WifiWebClient/WifiWebClient.ino rename to libraries/WiFi/examples/WifiWebClient/WifiWebClient.ino diff --git a/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino b/libraries/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino similarity index 100% rename from WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino rename to libraries/WiFi/examples/WifiWebClientRepeating/WifiWebClientRepeating.ino diff --git a/WiFi/examples/WifiWebServer/WifiWebServer.ino b/libraries/WiFi/examples/WifiWebServer/WifiWebServer.ino similarity index 100% rename from WiFi/examples/WifiWebServer/WifiWebServer.ino rename to libraries/WiFi/examples/WifiWebServer/WifiWebServer.ino diff --git a/WiFi/keywords.txt b/libraries/WiFi/keywords.txt similarity index 100% rename from WiFi/keywords.txt rename to libraries/WiFi/keywords.txt diff --git a/WiFi/utility/debug.h b/libraries/WiFi/utility/debug.h similarity index 100% rename from WiFi/utility/debug.h rename to libraries/WiFi/utility/debug.h diff --git a/WiFi/utility/server_drv.cpp b/libraries/WiFi/utility/server_drv.cpp similarity index 100% rename from WiFi/utility/server_drv.cpp rename to libraries/WiFi/utility/server_drv.cpp diff --git a/WiFi/utility/server_drv.h b/libraries/WiFi/utility/server_drv.h similarity index 100% rename from WiFi/utility/server_drv.h rename to libraries/WiFi/utility/server_drv.h diff --git a/WiFi/utility/socket.c b/libraries/WiFi/utility/socket.c similarity index 100% rename from WiFi/utility/socket.c rename to libraries/WiFi/utility/socket.c diff --git a/WiFi/utility/socket.h b/libraries/WiFi/utility/socket.h similarity index 100% rename from WiFi/utility/socket.h rename to libraries/WiFi/utility/socket.h diff --git a/WiFi/utility/spi_drv.cpp b/libraries/WiFi/utility/spi_drv.cpp similarity index 100% rename from WiFi/utility/spi_drv.cpp rename to libraries/WiFi/utility/spi_drv.cpp diff --git a/WiFi/utility/spi_drv.h b/libraries/WiFi/utility/spi_drv.h similarity index 100% rename from WiFi/utility/spi_drv.h rename to libraries/WiFi/utility/spi_drv.h diff --git a/WiFi/utility/wifi_drv.cpp b/libraries/WiFi/utility/wifi_drv.cpp similarity index 100% rename from WiFi/utility/wifi_drv.cpp rename to libraries/WiFi/utility/wifi_drv.cpp diff --git a/WiFi/utility/wifi_drv.h b/libraries/WiFi/utility/wifi_drv.h similarity index 100% rename from WiFi/utility/wifi_drv.h rename to libraries/WiFi/utility/wifi_drv.h diff --git a/WiFi/utility/wifi_spi.h b/libraries/WiFi/utility/wifi_spi.h similarity index 100% rename from WiFi/utility/wifi_spi.h rename to libraries/WiFi/utility/wifi_spi.h diff --git a/WiFi/utility/wl_definitions.h b/libraries/WiFi/utility/wl_definitions.h similarity index 100% rename from WiFi/utility/wl_definitions.h rename to libraries/WiFi/utility/wl_definitions.h diff --git a/WiFi/utility/wl_types.h b/libraries/WiFi/utility/wl_types.h similarity index 100% rename from WiFi/utility/wl_types.h rename to libraries/WiFi/utility/wl_types.h