diff --git a/Boards.h b/Boards.h index 855b6ebe..b7c7b054 100644 --- a/Boards.h +++ b/Boards.h @@ -665,22 +665,26 @@ writePort(port, value, bitmask): Write an 8 bit port. #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) #define PIN_TO_SERVO(p) ((p) - 2) -// ESP8266 generic +// ESP8266 +// note: boot mode GPIOs 0, 2 and 15 can be used as outputs, GPIOs 6-11 are in use for flash IO #elif defined(ESP8266) -#define TOTAL_ANALOG_PINS 0 -#define TOTAL_PINS 17 -#define VERSION_BLINK_PIN 4 -// #define IS_PIN_DIGITAL(p) ((p) == 0 || (p) == 1 || (p) == 2 || (p) == 3 || (p) == 4 || (p) == 5 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15 || (p) == 16) //for wifi dont protect serial pins because these things only have 2 pins otherwise -#define IS_PIN_DIGITAL(p) ((p) == 0 || (p) == 2 || (p) == 4 || (p) == 5 || (p) == 12 || (p) == 13 || (p) == 14 || (p) == 15 || (p) == 16) -#define IS_PIN_ANALOG(p) (false) -#define IS_PIN_PWM(p) (false) -#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) -#define IS_PIN_I2C(p) (false) -#define IS_PIN_SPI(p) (false) +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS A0 + NUM_ANALOG_INPUTS +#define PIN_SERIAL_RX 3 +#define PIN_SERIAL_TX 1 +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < A0)) +#define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS) +#define IS_PIN_PWM(p) digitalPinHasPWM(p) +#define IS_PIN_SERVO(p) (IS_PIN_DIGITAL(p) && (p) < MAX_SERVOS) +#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_INTERRUPT(p) (digitalPinToInterrupt(p) > NOT_AN_INTERRUPT) +#define IS_PIN_SERIAL(p) ((p) == PIN_SERIAL_RX || (p) == PIN_SERIAL_TX) #define PIN_TO_DIGITAL(p) (p) -#define PIN_TO_ANALOG(p) ((p) - 17) +#define PIN_TO_ANALOG(p) ((p) - A0) #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) -#define PIN_TO_SERVO(p) p +#define PIN_TO_SERVO(p) (p) +#define DEFAULT_PWM_RESOLUTION 10 // anything else @@ -697,6 +701,9 @@ writePort(port, value, bitmask): Write an 8 bit port. #define IS_PIN_SERIAL(p) 0 #endif +#ifndef DEFAULT_PWM_RESOLUTION +#define DEFAULT_PWM_RESOLUTION 8 +#endif /*============================================================================== * readPort() - Read an 8 bit port diff --git a/examples/StandardFirmata/StandardFirmata.ino b/examples/StandardFirmata/StandardFirmata.ino index e969e5a8..eadee278 100755 --- a/examples/StandardFirmata/StandardFirmata.ino +++ b/examples/StandardFirmata/StandardFirmata.ino @@ -615,7 +615,7 @@ void sysexCallback(byte command, byte argc, byte *argv) } if (IS_PIN_PWM(pin)) { Firmata.write(PIN_MODE_PWM); - Firmata.write(8); // 8 = 8-bit resolution + Firmata.write(DEFAULT_PWM_RESOLUTION); } if (IS_PIN_DIGITAL(pin)) { Firmata.write(PIN_MODE_SERVO); diff --git a/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino b/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino index 11437a65..c5f53f21 100644 --- a/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino +++ b/examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino @@ -616,7 +616,7 @@ void sysexCallback(byte command, byte argc, byte *argv) } if (IS_PIN_PWM(pin)) { Firmata.write(PIN_MODE_PWM); - Firmata.write(8); // 8 = 8-bit resolution + Firmata.write(DEFAULT_PWM_RESOLUTION); } if (IS_PIN_DIGITAL(pin)) { Firmata.write(PIN_MODE_SERVO); diff --git a/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino b/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino index d0f1e585..2d724107 100644 --- a/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino +++ b/examples/StandardFirmataEthernet/StandardFirmataEthernet.ino @@ -672,7 +672,7 @@ void sysexCallback(byte command, byte argc, byte *argv) } if (IS_PIN_PWM(pin)) { Firmata.write(PIN_MODE_PWM); - Firmata.write(8); // 8 = 8-bit resolution + Firmata.write(DEFAULT_PWM_RESOLUTION); } if (IS_PIN_DIGITAL(pin)) { Firmata.write(PIN_MODE_SERVO); diff --git a/examples/StandardFirmataEthernetPlus/StandardFirmataEthernetPlus.ino b/examples/StandardFirmataEthernetPlus/StandardFirmataEthernetPlus.ino index a1d13aba..eb8b856e 100644 --- a/examples/StandardFirmataEthernetPlus/StandardFirmataEthernetPlus.ino +++ b/examples/StandardFirmataEthernetPlus/StandardFirmataEthernetPlus.ino @@ -680,7 +680,7 @@ void sysexCallback(byte command, byte argc, byte *argv) } if (IS_PIN_PWM(pin)) { Firmata.write(PIN_MODE_PWM); - Firmata.write(8); // 8 = 8-bit resolution + Firmata.write(DEFAULT_PWM_RESOLUTION); } if (IS_PIN_DIGITAL(pin)) { Firmata.write(PIN_MODE_SERVO); diff --git a/examples/StandardFirmataPlus/StandardFirmataPlus.ino b/examples/StandardFirmataPlus/StandardFirmataPlus.ino index 657a9da1..828719c1 100644 --- a/examples/StandardFirmataPlus/StandardFirmataPlus.ino +++ b/examples/StandardFirmataPlus/StandardFirmataPlus.ino @@ -635,7 +635,7 @@ void sysexCallback(byte command, byte argc, byte *argv) } if (IS_PIN_PWM(pin)) { Firmata.write(PIN_MODE_PWM); - Firmata.write(8); // 8 = 8-bit resolution + Firmata.write(DEFAULT_PWM_RESOLUTION); } if (IS_PIN_DIGITAL(pin)) { Firmata.write(PIN_MODE_SERVO); diff --git a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino index e206cbc3..2971051b 100644 --- a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino +++ b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino @@ -13,6 +13,7 @@ Copyright (C) 2009 Shigeru Kobayashi. All rights reserved. Copyright (C) 2009-2016 Jeff Hoefs. All rights reserved. Copyright (C) 2015-2016 Jesse Frush. All rights reserved. + Copyright (C) 2016 Jens B. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -21,7 +22,7 @@ See file LICENSE.txt for further informations on licensing terms. - Last updated by Jeff Hoefs: January 10th, 2016 + Last updated by Jeff Hoefs: April 10th, 2016 */ /* @@ -36,7 +37,7 @@ - Arduino WiFi Shield (or clone) - Arduino WiFi Shield 101 - Arduino MKR1000 board (built-in WiFi 101) - - Adafruit HUZZAH CC3000 WiFi Shield (support coming soon) + - ESP8266 WiFi board compatible with ESP8266 Arduino core Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to configure your particular hardware. @@ -45,6 +46,8 @@ - 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) + - ESP8266 requires the Arduino ESP8266 core which can be obtained here: + https://github.com/esp8266/Arduino 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 @@ -74,11 +77,6 @@ #include #include -// I dont understand either -void disableI2CPins(); -void enableI2CPins(); -void reportAnalogCallback(byte analogPin, int value); - /* * Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your * connection that may help in the event of connection issues. If defined, some boards may not begin @@ -125,6 +123,12 @@ SerialFirmata serialFeature; #ifdef STATIC_IP_ADDRESS IPAddress local_ip(STATIC_IP_ADDRESS); #endif +#ifdef SUBNET_MASK +IPAddress subnet(SUBNET_MASK); +#endif +#ifdef GATEWAY_IP_ADDRESS +IPAddress gateway(GATEWAY_IP_ADDRESS); +#endif int wifiConnectionAttemptCounter = 0; int wifiStatus = WL_IDLE_STATUS; @@ -692,7 +696,7 @@ void sysexCallback(byte command, byte argc, byte *argv) } if (IS_PIN_PWM(pin)) { Firmata.write(PIN_MODE_PWM); - Firmata.write(8); // 8 = 8-bit resolution + Firmata.write(DEFAULT_PWM_RESOLUTION); } if (IS_PIN_DIGITAL(pin)) { Firmata.write(PIN_MODE_SERVO); @@ -822,37 +826,37 @@ void systemResetCallback() } void printWifiStatus() { -#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) if ( WiFi.status() != WL_CONNECTED ) { DEBUG_PRINT( "WiFi connection failed. Status value: " ); DEBUG_PRINTLN( WiFi.status() ); } else -#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) { // print the SSID of the network you're attached to: DEBUG_PRINT( "SSID: " ); -#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) DEBUG_PRINTLN( WiFi.SSID() ); -#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) // print your WiFi shield's IP address: DEBUG_PRINT( "IP Address: " ); -#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) IPAddress ip = WiFi.localIP(); DEBUG_PRINTLN( ip ); -#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) // print the received signal strength: DEBUG_PRINT( "signal strength (RSSI): " ); -#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#if defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) long rssi = WiFi.RSSI(); DEBUG_PRINT( rssi ); -#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) +#endif //defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(ESP8266_WIFI) DEBUG_PRINTLN( " dBm" ); } @@ -873,6 +877,8 @@ void setup() DEBUG_PRINTLN( "using the WiFi 101 library." ); #elif defined(ARDUINO_WIFI_SHIELD) DEBUG_PRINTLN( "using the legacy WiFi library." ); +#elif defined(ESP8266_WIFI) + DEBUG_PRINTLN( "using the ESP8266 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 @@ -884,9 +890,13 @@ void setup() #ifdef STATIC_IP_ADDRESS DEBUG_PRINT( "Using static IP: " ); DEBUG_PRINTLN( local_ip ); - //you can also provide a static IP in the begin() functions, but this simplifies - //ifdef logic in this sketch due to support for all different encryption types. +#ifdef ESP8266_WIFI + stream.config( local_ip , gateway, subnet ); +#else + // you can also provide a static IP in the begin() functions, but this simplifies + // ifdef logic in this sketch due to support for all different encryption types. stream.config( local_ip ); +#endif #else DEBUG_PRINTLN( "IP will be requested from DHCP ..." ); #endif @@ -952,7 +962,8 @@ void setup() || 28 == i #endif //defined(__AVR_ATmega32U4__) ) { -#elif defined (WIFI_101) +// don't ignore pins when using Wi-Fi 101 library with the MKR1000 +#elif defined (WIFI_101) && !defined(ARDUINO_SAMD_MKR1000) if (IS_IGNORE_WIFI101_SHIELD(i)) { #elif defined (HUZZAH_WIFI) // TODO diff --git a/examples/StandardFirmataWiFi/wifiConfig.h b/examples/StandardFirmataWiFi/wifiConfig.h index 6fe9b304..84b3acd0 100644 --- a/examples/StandardFirmataWiFi/wifiConfig.h +++ b/examples/StandardFirmataWiFi/wifiConfig.h @@ -3,8 +3,8 @@ * * 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. + * Currently StandardFirmataWiFi is configured as a Wi-Fi server. An option to + * configure as a Wi-Fi client will be added in the future. *============================================================================*/ // STEP 1 [REQUIRED] @@ -12,48 +12,94 @@ // Option A is enabled by default. /* - * OPTION A: Configure for Arduino WiFi shield + * OPTION A: Configure for Arduino MKR1000 or Arduino WiFi Shield 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 with 802.11 B/G/N networks. + * + * If you are using the MKR1000 board, continue on to STEP 2. If you are using the WiFi 101 shield, + * follow the instructions below. + * + * To enable for the WiFi 101 shield, uncomment the #define WIFI_101 below and verify the + * #define ARDUINO_WIFI_SHIELD is commented out for OPTION B. + * + * IMPORTANT: You must have the WiFI 101 library installed. To easily install this library, open + * the library manager via: Arduino IDE Menus: Sketch > Include Library > Manage Libraries > filter + * search for "WiFi101" > Select the result and click 'install' + */ +//#define WIFI_101 + +//do not modify the following 10 lines +#if defined(ARDUINO_SAMD_MKR1000) && !defined(WIFI_101) +// automatically include if compiling for MRK1000 +#define WIFI_101 +#endif +#ifdef WIFI_101 +#include +#include "utility/WiFiStream.h" +WiFiStream stream; +#define WIFI_LIB_INCLUDED +#endif + +/* + * OPTION B: Configure for legacy 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 - * leave the #define below uncommented. + * To configure StandardFirmataWiFi to use the legacy Arduino WiFi shield + * leave the #define below uncommented and ensure #define WIFI_101 is commented out for OPTION A. */ -#define ARDUINO_WIFI_SHIELD +//#define ARDUINO_WIFI_SHIELD -//do not modify these next 4 lines +//do not modify the following 10 lines #ifdef ARDUINO_WIFI_SHIELD +#include #include "utility/WiFiStream.h" WiFiStream stream; + #ifdef WIFI_LIB_INCLUDED + #define MULTIPLE_WIFI_LIB_INCLUDES + #else + #define WIFI_LIB_INCLUDED + #endif #endif /* - * OPTION B: Configure for WiFi 101 + * OPTION C: Configure for ESP8266 * - * 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 - * with 802.11 B/G/N networks. + * This will configure StandardFirmataWiFi to use the ESP8266WiFi library for boards + * with an ESP8266 chip. It is compatible with 802.11 B/G/N networks. * - * To enable, uncomment the #define WIFI_101 below and verify the #define values under - * options A and C are commented out. + * The appropriate libraries are included automatically when compiling for the ESP8266 so + * continue on to STEP 2. * - * IMPORTANT: You must have the WiFI 101 library installed. To easily install this library, opent the library manager via: - * Arduino IDE Menus: Sketch > Include Library > Manage Libraries > filter search for "WiFi101" > Select the result and click 'install' + * IMPORTANT: You must have the esp8266 board support installed. To easily install this board, open + * see the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager. */ -//#define WIFI_101 - -//do not modify these next 4 lines -#ifdef WIFI_101 -#include "utility/WiFi101Stream.h" -WiFi101Stream stream; +//do not modify the following 14 lines +#ifdef ESP8266 +// automatically include if compiling for ESP8266 +#define ESP8266_WIFI +#endif +#ifdef ESP8266_WIFI +#include +#include "utility/WiFiStream.h" +WiFiStream stream; + #ifdef WIFI_LIB_INCLUDED + #define MULTIPLE_WIFI_LIB_INCLUDES + #else + #define WIFI_LIB_INCLUDED + #endif #endif /* - * OPTION C: Configure for HUZZAH + * OPTION D: Configure for HUZZAH * - * HUZZAH is not yet supported, this will be added in a later revision to StandardFirmataWiFi + * HUZZAH with CC3000 is not yet supported, this will be added in a later revision to + * StandardFirmataWiFi. + * For HUZZAH with ESP8266 use ESP8266_WIFI. */ //------------------------------ @@ -61,45 +107,37 @@ WiFi101Stream stream; //------------------------------ //#define HUZZAH_WIFI -/* - * OPTION D: Configure for ESP6288 - * - * ESP6288 is supported through its Arduino Core at - * https://github.com/esp8266/Arduino/ - */ - -//#define ESP_WIFI - -//do not modify these next 4 lines -#ifdef ESP_WIFI -#include "utility/ESPWiFiStream.h" -WiFiStream stream; -#endif // STEP 2 [REQUIRED for all boards and shields] // replace this with your wireless network SSID char ssid[] = "your_network_name"; + // STEP 3 [OPTIONAL for all boards and shields] -// if you want to use a static IP (v4) address, uncomment the line below. You can also change the IP. -// if this line is commented out, the WiFi shield will attempt to get an IP from the DHCP server -// #define STATIC_IP_ADDRESS 192,168,1,113 +// If you want to use a static IP (v4) address, uncomment the line below. You can also change the IP. +// If the first line is commented out, the WiFi shield will attempt to get an IP from the DHCP server. +// If you are using a static IP with the ESP8266 then you must also uncomment the SUBNET and GATEWAY. +//#define STATIC_IP_ADDRESS 192,168,1,113 +//#define SUBNET_MASK 255,255,255,0 // REQUIRED for ESP8266_WIFI, optional for others +//#define GATEWAY_IP_ADDRESS 0,0,0,0 // REQUIRED for ESP8266_WIFI, optional for others + // STEP 4 [REQUIRED for all boards and shields] // define your port number here, you will need this to open a TCP connection to your Arduino #define SERVER_PORT 3030 -// STEP 5 [REQUIRED for all boards and shields] -// determine your network security type (OPTION A, B, or C). Option A is the most common, and the default. +// STEP 5 [REQUIRED for all boards and shields] +// determine your network security type (OPTION A, B, or C). Option A is the most common, and the +// default. /* * 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 + * 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 */ #define WIFI_WPA_SECURITY @@ -107,14 +145,15 @@ char ssid[] = "your_network_name"; char wpa_passphrase[] = "your_wpa_passphrase"; #endif //WIFI_WPA_SECURITY + /* * OPTION B: WEP * - * 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. + * 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. + * 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. */ //#define WIFI_WEP_SECURITY @@ -129,7 +168,8 @@ 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. + * 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 * under options A and B are commented out. @@ -140,11 +180,11 @@ char wep_key[] = "your_wep_key"; * CONFIGURATION ERROR CHECK (don't change anything here) *============================================================================*/ -#if ((defined(ARDUINO_WIFI_SHIELD) && (defined(WIFI_101) || defined(HUZZAH_WIFI))) || (defined(WIFI_101) && defined(HUZZAH_WIFI)) || (defined(WIFI_101) && defined(ESP_WIFI)) || (defined(ESP_WIFI) && defined(HUZZAH_WIFI)) || (defined(ESP_WIFI) && defined(ARDUINO_WIFI_SHIELD))) +#ifdef MULTIPLE_WIFI_LIB_INCLUDES #error "you may not define more than one wifi device type in wifiConfig.h." -#endif //WIFI device type check +#endif -#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI) || defined(ESP_WIFI)) +#ifndef WIFI_LIB_INCLUDED #error "you must define a wifi device type in wifiConfig.h." #endif @@ -156,13 +196,17 @@ char wep_key[] = "your_wep_key"; #error "you must define a wifi security type in wifiConfig.h." #endif //WIFI_* security define check +#if (defined(ESP8266_WIFI) && !(defined(WIFI_NO_SECURITY) || (defined(WIFI_WPA_SECURITY)))) +#error "you must choose between WIFI_NO_SECURITY and WIFI_WPA_SECURITY" +#endif + /*============================================================================== * PIN IGNORE MACROS (don't change anything here) *============================================================================*/ // ignore SPI pins, pin 5 (reset WiFi101 shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) // also don't ignore SS pin if it's not pin 10 -// TODO - need to differentiate between Arduino WiFi1 101 Shield and Arduino MKR1000 +// Not needed for Arduino MKR1000. #define IS_IGNORE_WIFI101_SHIELD(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7) // ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS) diff --git a/utility/ESPWiFiStream.cpp b/utility/ESPWiFiStream.cpp deleted file mode 100644 index 9b54a5ac..00000000 --- a/utility/ESPWiFiStream.cpp +++ /dev/null @@ -1,4 +0,0 @@ -/* - * 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. - */ diff --git a/utility/ESPWiFiStream.h b/utility/ESPWiFiStream.h deleted file mode 100644 index 005faf09..00000000 --- a/utility/ESPWiFiStream.h +++ /dev/null @@ -1,263 +0,0 @@ -/* - WiFiStream.h - An Arduino Stream that wraps an instance of a WiFi server. For use - with legacy Arduino WiFi shield and other boards and sheilds that - are compatible with the Arduino WiFi library. - - Copyright (C) 2015-2016 Jesse Frush. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - See file LICENSE.txt for further informations on licensing terms. - */ - -#ifndef ESPWIFI_STREAM_H -#define ESPWIFI_STREAM_H - -#include -#include -#include - -class WiFiStream : public Stream -{ -private: - WiFiServer _server = WiFiServer(23); - WiFiClient _client; - - //configuration members - IPAddress _local_ip; - uint16_t _port = 0; - uint8_t _key_idx = 0; //WEP - const char *_key = nullptr; //WEP - const char *_passphrase = nullptr; //WPA - char *_ssid = nullptr; - - inline int connect_client() - { - if( !( _client && _client.connected() ) ) - { - WiFiClient newClient = _server.available(); - if( !newClient ) - { - return 0; - } - - _client = newClient; - } - return 1; - } - - inline bool is_ready() - { - uint8_t status = WiFi.status(); - return !( status == WL_NO_SHIELD || status == WL_CONNECTED ); - } - -public: - WiFiStream() {}; - - // 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; - - stop(); - int result = 0; - if( WiFi.status() != WL_CONNECTED ) - { - // if( _local_ip ) - // { - // WiFi.config( _local_ip ); - // } - - if( _passphrase ) - { - result = WiFi.begin( _ssid, _passphrase); - } - // else if( _key_idx && _key ) - // { - // result = WiFi.begin( _ssid, _key_idx, _key ); - // } - // else - // { - // result = WiFi.begin( _ssid ); - // } - } - 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; - return 0; - } - - //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; - return 0; - } - - //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 - ******************************************************************************/ - - //OPEN networks with static IP - 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; - return 0; - } - - //WEP-encrypted networks with static IP - 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; - return 0; - } - - //WPA-encrypted networks with static IP - 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; - return 0; - } - -/****************************************************************************** - * Stream implementations - ******************************************************************************/ - - inline int available() - { - return connect_client() ? _client.available() : 0; - } - - inline void flush() - { - if( _client ) _client.flush(); - } - - inline int peek() - { - return connect_client() ? _client.peek(): 0; - } - - inline int read() - { - return connect_client() ? _client.read() : -1; - } - - inline void stop() - { - _client.stop(); - } - - inline size_t write(uint8_t byte) - { - if( connect_client() ) _client.write( byte ); - } -}; - -#endif //ESPWIFI_STREAM_H diff --git a/utility/WiFi101Stream.cpp b/utility/WiFi101Stream.cpp deleted file mode 100644 index 3beaf40e..00000000 --- a/utility/WiFi101Stream.cpp +++ /dev/null @@ -1,4 +0,0 @@ -/* - * 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. - */ diff --git a/utility/WiFi101Stream.h b/utility/WiFi101Stream.h deleted file mode 100644 index eb95cf51..00000000 --- a/utility/WiFi101Stream.h +++ /dev/null @@ -1,259 +0,0 @@ -/* - WiFi101Stream.h - An Arduino Stream that wraps an instance of a WiFi101 server. For use - with Arduino WiFi 101 shield, Arduino MKR1000 and other boards and - shields that are compatible with the Arduino WiFi101 library. - - Copyright (C) 2015-2016 Jesse Frush. All rights reserved. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - See file LICENSE.txt for further informations on licensing terms. - */ - -#ifndef WIFI101_STREAM_H -#define WIFI101_STREAM_H - -#include -#include -#include - - -class WiFi101Stream : public Stream -{ -private: - WiFiServer _server = WiFiServer(23); - WiFiClient _client; - - //configuration members - IPAddress _local_ip; - uint16_t _port = 0; - uint8_t _key_idx = 0; //WEP - const char *_key = nullptr; //WEP - const char *_passphrase = nullptr; //WPA - char *_ssid = nullptr; - - inline int connect_client() - { - if( !( _client && _client.connected() ) ) - { - WiFiClient newClient = _server.available(); - if( !newClient ) - { - return 0; - } - - _client = newClient; - } - return 1; - } - - inline bool is_ready() - { - uint8_t status = WiFi.status(); - return !( status == WL_NO_SHIELD || status == WL_CONNECTED ); - } - -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; - - stop(); - int result = 0; - if( WiFi.status() != WL_CONNECTED ) - { - if( _local_ip ) - { - WiFi.config( _local_ip ); - } - - if( _passphrase ) - { - result = WiFi.begin( _ssid, _passphrase); - } - else if( _key_idx && _key ) - { - result = WiFi.begin( _ssid, _key_idx, _key ); - } - else - { - result = WiFi.begin( _ssid ); - } - } - 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 - ******************************************************************************/ - - //OPEN networks with static IP - 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; - } - - //WEP-encrypted networks with static IP - 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; - } - - //WPA-encrypted networks with static IP - 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; - } - -/****************************************************************************** - * Stream implementations - ******************************************************************************/ - - inline int available() - { - return connect_client() ? _client.available() : 0; - } - - inline void flush() - { - if( _client ) _client.flush(); - } - - inline int peek() - { - return connect_client() ? _client.peek(): 0; - } - - inline int read() - { - return connect_client() ? _client.read() : -1; - } - - inline void stop() - { - _client.stop(); - } - - inline size_t write(uint8_t byte) - { - if( connect_client() ) _client.write( byte ); - } -}; - -#endif //WIFI101_STREAM_H diff --git a/utility/WiFiStream.h b/utility/WiFiStream.h index fdcb483a..4f21c109 100644 --- a/utility/WiFiStream.h +++ b/utility/WiFiStream.h @@ -1,7 +1,7 @@ /* WiFiStream.h An Arduino Stream that wraps an instance of a WiFi server. For use - with legacy Arduino WiFi shield and other boards and sheilds that + with legacy Arduino WiFi shield and other boards and shields that are compatible with the Arduino WiFi library. Copyright (C) 2015-2016 Jesse Frush. All rights reserved. @@ -12,6 +12,8 @@ version 2.1 of the License, or (at your option) any later version. See file LICENSE.txt for further informations on licensing terms. + + Last updated April 10th, 2016 */ #ifndef WIFI_STREAM_H @@ -19,7 +21,6 @@ #include #include -#include class WiFiStream : public Stream { @@ -29,11 +30,14 @@ class WiFiStream : public Stream //configuration members IPAddress _local_ip; + IPAddress _gateway; + IPAddress _subnet; uint16_t _port = 0; uint8_t _key_idx = 0; //WEP const char *_key = nullptr; //WEP const char *_passphrase = nullptr; //WPA char *_ssid = nullptr; + bool _new_connection = false; inline int connect_client() { @@ -50,6 +54,16 @@ class WiFiStream : public Stream return 1; } + inline bool is_new_connection() + { + if (_new_connection && WiFi.status() == WL_CONNECTED) { + _new_connection = false; + return true; + } + _new_connection = true; + return false; + } + inline bool is_ready() { uint8_t status = WiFi.status(); @@ -59,12 +73,27 @@ class WiFiStream : public Stream public: WiFiStream() {}; +#ifndef ESP8266 // 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 ); } +#endif + + // allows another way to configure a static IP before begin is called + inline void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) + { + _local_ip = local_ip; + _gateway = gateway; + _subnet = subnet; +#ifndef ESP8266 + WiFi.config( local_ip, IPAddress(0, 0, 0, 0), gateway, subnet ); +#else + WiFi.config( local_ip, gateway, subnet ); +#endif + } // get DCHP IP inline IPAddress localIP() @@ -72,6 +101,9 @@ class WiFiStream : public Stream return WiFi.localIP(); } + /** + * @return true if connected + */ inline bool maintain() { if( connect_client() ) return true; @@ -82,20 +114,36 @@ class WiFiStream : public Stream { if( _local_ip ) { +#ifndef ESP8266 WiFi.config( _local_ip ); +#else + WiFi.config( _local_ip, _gateway, _subnet ); +#endif } if( _passphrase ) { +#ifndef ESP8266 result = WiFi.begin( _ssid, _passphrase); +#else + WiFi.begin( _ssid, _passphrase); + result = WiFi.status(); +#endif } +#ifndef ESP8266 else if( _key_idx && _key ) { result = WiFi.begin( _ssid, _key_idx, _key ); } +#endif else { - result = WiFi.begin( _ssid ); +#ifndef ESP8266 + result = WiFi.begin( _ssid); +#else + WiFi.begin( _ssid); + result = WiFi.status(); +#endif } } if( result == 0 ) return false; @@ -112,11 +160,14 @@ class WiFiStream : public Stream //OPEN networks inline int begin(char *ssid, uint16_t port) { + if( is_new_connection() ) return WL_CONNECTED; if( !is_ready() ) return 0; _ssid = ssid; _port = port; - int result = WiFi.begin( ssid ); + + int result = WiFi.begin(ssid); + // will always return 0 for ESP8266 if( result == 0 ) return 0; _server = WiFiServer( port ); @@ -124,9 +175,11 @@ class WiFiStream : public Stream return result; } +#ifndef ESP8266 //WEP-encrypted networks inline int begin(char *ssid, uint8_t key_idx, const char *key, uint16_t port) { + if( is_new_connection() ) return WL_CONNECTED; if( !is_ready() ) return 0; _ssid = ssid; @@ -141,17 +194,25 @@ class WiFiStream : public Stream _server.begin(); return result; } +#endif //WPA-encrypted networks inline int begin(char *ssid, const char *passphrase, uint16_t port) { + // TODO - figure out a cleaner way to handle this. The issue is that with the ESP8266 + // WiFi.begin does not wait so the connect state is is therefore not updated until the + // next time begin is called. The call to !is_ready() below however returns 0 if + // WL_CONNECTED is true. This is to allow a new connection with different parameters than + // the original connection. is_new_connection is a temporary solution. + if( is_new_connection() ) return WL_CONNECTED; if( !is_ready() ) return 0; _ssid = ssid; _port = port; _passphrase = passphrase; - int result = WiFi.begin( ssid, passphrase); + int result = WiFi.begin(ssid, passphrase); + // will always return 0 for ESP8266 if( result == 0 ) return 0; _server = WiFiServer( port ); @@ -163,9 +224,12 @@ class WiFiStream : public Stream * Connection functions without DHCP ******************************************************************************/ +// ESP8266 requires gateway and subnet so the following functions are not compatible +#ifndef ESP8266 //OPEN networks with static IP inline int begin(char *ssid, IPAddress local_ip, uint16_t port) { + if( is_new_connection() ) return WL_CONNECTED; if( !is_ready() ) return 0; _ssid = ssid; @@ -184,6 +248,7 @@ class WiFiStream : public Stream //WEP-encrypted networks with static IP inline int begin(char *ssid, IPAddress local_ip, uint8_t key_idx, const char *key, uint16_t port) { + if( is_new_connection() ) return WL_CONNECTED; if( !is_ready() ) return 0; _ssid = ssid; @@ -204,6 +269,7 @@ class WiFiStream : public Stream //WPA-encrypted networks with static IP inline int begin(char *ssid, IPAddress local_ip, const char *passphrase, uint16_t port) { + if( is_new_connection() ) return WL_CONNECTED; if( !is_ready() ) return 0; _ssid = ssid; @@ -219,6 +285,7 @@ class WiFiStream : public Stream _server.begin(); return result; } +#endif /****************************************************************************** * Stream implementations diff --git a/utility/firmataDebug.h b/utility/firmataDebug.h index 6e364b0c..dce0f801 100644 --- a/utility/firmataDebug.h +++ b/utility/firmataDebug.h @@ -3,7 +3,7 @@ #ifdef SERIAL_DEBUG #define DEBUG_BEGIN(baud) Serial.begin(baud); while(!Serial) {;} - #define DEBUG_PRINTLN(x) Serial.println (x) + #define DEBUG_PRINTLN(x) Serial.println (x); Serial.flush() #define DEBUG_PRINT(x) Serial.print (x) #else #define DEBUG_BEGIN(baud)