diff --git a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino index 7f8478b6..f931d909 100644 --- a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino +++ b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino @@ -27,6 +27,9 @@ /* README + StandardFirmataWiFi is a WiFi server application. You will need a Firmata client library with + a network transport in order to establish a connection with StandardFirmataWiFi. + To use StandardFirmataWiFi you will need to have one of the following boards or shields: @@ -38,6 +41,11 @@ Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to configure your particular hardware. + Dependencies: + - WiFi Shield 101 requires version 0.7.0 or higher of the WiFi101 library (available in Arduino + 1.6.8 or higher, or update the library via the Arduino Library Manager or clone from source: + https://github.com/arduino-libraries/WiFi101) + In order to use the WiFi Shield 101 with Firmata you will need a board with at least 35k of Flash memory. This means you cannot use the WiFi Shield 101 with an Arduino Uno or any other ATmega328p-based microcontroller or with an Arduino Leonardo or other @@ -99,7 +107,7 @@ *============================================================================*/ #ifdef STATIC_IP_ADDRESS - IPAddress local_ip(STATIC_IP_ADDRESS); +IPAddress local_ip(STATIC_IP_ADDRESS); #endif int wifiConnectionAttemptCounter = 0; @@ -782,7 +790,7 @@ void systemResetCallback() void printWifiStatus() { #if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) - if( WiFi.status() != WL_CONNECTED ) + if ( WiFi.status() != WL_CONNECTED ) { DEBUG_PRINT( "WiFi connection failed. Status value: " ); DEBUG_PRINTLN( WiFi.status() ); @@ -797,7 +805,7 @@ void printWifiStatus() { DEBUG_PRINTLN( WiFi.SSID() ); #endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) - // print your WiFi shield's IP address: + // print your WiFi shield's IP address: DEBUG_PRINT( "IP Address: " ); #if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) @@ -805,7 +813,7 @@ void printWifiStatus() { DEBUG_PRINTLN( ip ); #endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) - // print the received signal strength: + // print the received signal strength: DEBUG_PRINT( "signal strength (RSSI): " ); #if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) @@ -823,7 +831,7 @@ void setup() * WIFI SETUP */ DEBUG_BEGIN(9600); - + /* * This statement will clarify how a connection is being made */ @@ -834,7 +842,7 @@ void setup() DEBUG_PRINTLN( "using the legacy WiFi library." ); #elif defined(HUZZAH_WIFI) DEBUG_PRINTLN( "using the HUZZAH WiFi library." ); -//else should never happen here as error-checking in wifiConfig.h will catch this + //else should never happen here as error-checking in wifiConfig.h will catch this #endif //defined(WIFI_101) /* @@ -854,8 +862,8 @@ void setup() * Configure WiFi security */ #if defined(WIFI_WEP_SECURITY) - while(wifiStatus != WL_CONNECTED) { - DEBUG_PRINT("Attempting to connect to WEP SSID: "); + while (wifiStatus != WL_CONNECTED) { + DEBUG_PRINT( "Attempting to connect to WEP SSID: " ); DEBUG_PRINTLN(ssid); wifiStatus = stream.begin( ssid, wep_index, wep_key, SERVER_PORT ); delay(5000); // TODO - determine minimum delay @@ -863,8 +871,8 @@ void setup() } #elif defined(WIFI_WPA_SECURITY) - while(wifiStatus != WL_CONNECTED) { - DEBUG_PRINT("Attempting to connect to WPA SSID: "); + while (wifiStatus != WL_CONNECTED) { + DEBUG_PRINT( "Attempting to connect to WPA SSID: " ); DEBUG_PRINTLN(ssid); wifiStatus = stream.begin(ssid, wpa_passphrase, SERVER_PORT); delay(5000); // TODO - determine minimum delay @@ -872,9 +880,10 @@ void setup() } #else //OPEN network - DEBUG_PRINTLN( "Connecting to an open network ..." ); - while(wifiStatus != WL_CONNECTED) { - wifiStatus = stream.begin( ssid, SERVER_PORT ); + while (wifiStatus != WL_CONNECTED) { + DEBUG_PRINTLN( "Attempting to connect to open SSID: " ); + DEBUG_PRINTLN(ssid); + wifiStatus = stream.begin(ssid, SERVER_PORT); delay(5000); // TODO - determine minimum delay if (++wifiConnectionAttemptCounter > WIFI_MAX_CONN_ATTEMPTS) break; } @@ -909,7 +918,7 @@ void setup() || 24 == i // On Leonardo, pin 24 maps to D4 and pin 28 maps to D10 || 28 == i #endif //defined(__AVR_ATmega32U4__) - ) { + ) { #elif defined (WIFI_101) if (IS_IGNORE_WIFI101_SHIELD(i)) { #elif defined (HUZZAH_WIFI) @@ -922,7 +931,7 @@ void setup() } } -//Set up controls for the Arduino WiFi Shield SS for the SD Card + //Set up controls for the Arduino WiFi Shield SS for the SD Card #ifdef ARDUINO_WIFI_SHIELD // Arduino WiFi, Arduino WiFi Shield and Arduino Yun all have SD SS wired to D4 pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata diff --git a/examples/StandardFirmataWiFi/wifiConfig.h b/examples/StandardFirmataWiFi/wifiConfig.h index 24790e16..3cd4b5cd 100644 --- a/examples/StandardFirmataWiFi/wifiConfig.h +++ b/examples/StandardFirmataWiFi/wifiConfig.h @@ -2,6 +2,9 @@ * WIFI CONFIGURATION * * You must configure your particular hardware. Follow the steps below. + * + * Currently StandardFirmataWiFi is configured as a server. An option to + * configure as a client may be added in the future. *============================================================================*/ // STEP 1 [REQUIRED] @@ -10,12 +13,12 @@ /* * OPTION A: Configure for Arduino WiFi shield - * + * * This will configure StandardFirmataWiFi to use the original WiFi library (deprecated) provided * with the Arduino IDE. It is supported by the Arduino WiFi shield (a discontinued product) and * is compatible with 802.11 B/G networks. * - * To configure StandardFirmataWiFi to use the Arduino WiFi shield + * To configure StandardFirmataWiFi to use the Arduino WiFi shield * leave the #define below uncommented. */ #define ARDUINO_WIFI_SHIELD @@ -28,9 +31,9 @@ WiFiStream stream; /* * OPTION B: Configure for WiFi 101 - * + * * This will configure StandardFirmataWiFi to use the WiFi101 library, which works with the Arduino WiFi101 - * shield and devices that have the WiFi101 chip built in (such as the MKR1000). It is compatible + * shield and devices that have the WiFi101 chip built in (such as the MKR1000). It is compatible * with 802.11 B/G/N networks. * * To enable, uncomment the #define WIFI_101 below and verify the #define values under @@ -49,7 +52,7 @@ WiFi101Stream stream; /* * OPTION C: Configure for HUZZAH - * + * * HUZZAH is not yet supported, this will be added in a later revision to StandardFirmataWiFi */ @@ -78,9 +81,9 @@ char ssid[] = "your_network_name"; /* * OPTION A: WPA / WPA2 - * + * * WPA is the most common network security type. A passphrase is required to connect to this type. - * + * * To enable, leave #define WIFI_WPA_SECURITY uncommented below, set your wpa_passphrase value appropriately, * and do not uncomment the #define values under options B and C */ @@ -95,7 +98,7 @@ char wpa_passphrase[] = "your_wpa_passphrase"; * * WEP is a less common (and regarded as less safe) security type. A WEP key and its associated index are required * to connect to this type. - * + * * To enable, Uncomment the #define below, set your wep_index and wep_key values appropriately, and verify * the #define values under options A and C are commented out. */ @@ -111,7 +114,7 @@ char wep_key[] = "your_wep_key"; /* * OPTION C: Open network (no security) - * + * * Open networks have no security, can be connected to by any device that knows the ssid, and are unsafe. * * To enable, uncomment #define WIFI_NO_SECURITY below and verify the #define values @@ -150,4 +153,3 @@ char wep_key[] = "your_wep_key"; // ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) #define IS_IGNORE_WIFI_SHIELD(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10) - diff --git a/utility/WiFi101Stream.cpp b/utility/WiFi101Stream.cpp index 0c7945de..3beaf40e 100644 --- a/utility/WiFi101Stream.cpp +++ b/utility/WiFi101Stream.cpp @@ -1,4 +1,4 @@ /* * Implementation is in WiFi101Stream.h to avoid linker issues. Legacy WiFi and modern WiFi101 both define WiFiClass which * will cause linker errors whenever Firmata.h is included. - */ \ No newline at end of file + */ diff --git a/utility/WiFi101Stream.h b/utility/WiFi101Stream.h index da3ffbf4..6bfb413b 100644 --- a/utility/WiFi101Stream.h +++ b/utility/WiFi101Stream.h @@ -11,7 +11,7 @@ class WiFi101Stream : public Stream private: WiFiServer _server = WiFiServer(23); WiFiClient _client; - + //configuration members IPAddress _local_ip; uint16_t _port = 0; @@ -19,7 +19,7 @@ class WiFi101Stream : public Stream const char *_key = nullptr; //WEP const char *_passphrase = nullptr; //WPA char *_ssid = nullptr; - + inline int connect_client() { if( !( _client && _client.connected() ) ) @@ -29,34 +29,34 @@ class WiFi101Stream : public Stream { return 0; } - + _client = newClient; } return 1; } - + inline bool is_ready() { uint8_t status = WiFi.status(); return !( status == WL_NO_SHIELD || status == WL_CONNECTED ); } -public: +public: WiFi101Stream() {}; - + // allows another way to configure a static IP before begin is called inline void config(IPAddress local_ip) { _local_ip = local_ip; WiFi.config( local_ip ); } - + // get DCHP IP inline IPAddress localIP() { return WiFi.localIP(); } - + inline bool maintain() { if( connect_client() ) return true; @@ -69,7 +69,7 @@ class WiFi101Stream : public Stream { WiFi.config( _local_ip ); } - + if( _passphrase ) { result = WiFi.begin( _ssid, _passphrase); @@ -84,66 +84,66 @@ class WiFi101Stream : public Stream } } if( result == 0 ) return false; - + _server = WiFiServer( _port ); _server.begin(); return result; } - + /****************************************************************************** * Connection functions with DHCP ******************************************************************************/ - + //OPEN networks inline int begin(char *ssid, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; int result = WiFi.begin( ssid ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; } - + //WEP-encrypted networks inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _key_idx = key_idx; _key = key; - + int result = WiFi.begin( ssid, key_idx, key ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; } - + //WPA-encrypted networks inline int begin(char *ssid, const char *passphrase, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _passphrase = passphrase; - + int result = WiFi.begin( ssid, passphrase); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; } - + /****************************************************************************** * Connection functions without DHCP ******************************************************************************/ @@ -152,15 +152,15 @@ class WiFi101Stream : public Stream inline int begin(char *ssid, IPAddress local_ip, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _local_ip = local_ip; - + WiFi.config( local_ip ); int result = WiFi.begin( ssid ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -170,17 +170,17 @@ class WiFi101Stream : public Stream inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _local_ip = local_ip; _key_idx = key_idx; _key = key; - + WiFi.config( local_ip ); int result = WiFi.begin( ssid, key_idx, key ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -190,16 +190,16 @@ class WiFi101Stream : public Stream inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _local_ip = local_ip; _passphrase = passphrase; - + WiFi.config( local_ip ); int result = WiFi.begin( ssid, passphrase); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -237,7 +237,7 @@ class WiFi101Stream : public Stream inline size_t write(uint8_t byte) { if( connect_client() ) _client.write( byte ); - } + } }; #endif //WIFI101_STREAM_H diff --git a/utility/WiFiStream.cpp b/utility/WiFiStream.cpp index f12f4c32..9b54a5ac 100644 --- a/utility/WiFiStream.cpp +++ b/utility/WiFiStream.cpp @@ -1,4 +1,4 @@ /* * Implementation is in WiFiStream.h to avoid linker issues. Legacy WiFi and modern WiFi101 both define WiFiClass which * will cause linker errors whenever Firmata.h is included. - */ \ No newline at end of file + */ diff --git a/utility/WiFiStream.h b/utility/WiFiStream.h index 303dc1b8..a576e39f 100644 --- a/utility/WiFiStream.h +++ b/utility/WiFiStream.h @@ -28,7 +28,7 @@ class WiFiStream : public Stream { return 0; } - + _client = newClient; } return 1; @@ -42,7 +42,7 @@ class WiFiStream : public Stream public: WiFiStream() {}; - + // allows another way to configure a static IP before begin is called inline void config(IPAddress local_ip) { @@ -59,7 +59,7 @@ class WiFiStream : public Stream inline bool maintain() { if( connect_client() ) return true; - + stop(); int result = 0; if( WiFi.status() != WL_CONNECTED ) @@ -68,7 +68,7 @@ class WiFiStream : public Stream { WiFi.config( _local_ip ); } - + if( _passphrase ) { result = WiFi.begin( _ssid, _passphrase); @@ -83,12 +83,12 @@ class WiFiStream : public Stream } } if( result == 0 ) return false; - + _server = WiFiServer( _port ); _server.begin(); return result; } - + /****************************************************************************** * Connection functions with DHCP ******************************************************************************/ @@ -97,12 +97,12 @@ class WiFiStream : public Stream inline int begin(char *ssid, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; int result = WiFi.begin( ssid ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -112,15 +112,15 @@ class WiFiStream : public Stream inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _key_idx = key_idx; _key = key; - + int result = WiFi.begin( ssid, key_idx, key ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -130,19 +130,19 @@ class WiFiStream : public Stream inline int begin(char *ssid, const char *passphrase, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _passphrase = passphrase; - + int result = WiFi.begin( ssid, passphrase); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; } - + /****************************************************************************** * Connection functions without DHCP ******************************************************************************/ @@ -151,15 +151,15 @@ class WiFiStream : public Stream inline int begin(char *ssid, IPAddress local_ip, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _local_ip = local_ip; - + WiFi.config( local_ip ); int result = WiFi.begin( ssid ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -169,17 +169,17 @@ class WiFiStream : public Stream inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _local_ip = local_ip; _key_idx = key_idx; _key = key; - + WiFi.config( local_ip ); int result = WiFi.begin( ssid, key_idx, key ); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -189,16 +189,16 @@ class WiFiStream : public Stream inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port) { if( !is_ready() ) return 0; - + _ssid = ssid; _port = port; _local_ip = local_ip; _passphrase = passphrase; - + WiFi.config( local_ip ); int result = WiFi.begin( ssid, passphrase); if( result == 0 ) return 0; - + _server = WiFiServer( port ); _server.begin(); return result; @@ -236,7 +236,7 @@ class WiFiStream : public Stream inline size_t write(uint8_t byte) { if( connect_client() ) _client.write( byte ); - } + } }; #endif //WIFI_STREAM_H