From cefeaa3b370f0dced4a6e91c58daecd2c849a5cb Mon Sep 17 00:00:00 2001 From: Jens B Date: Sun, 13 Mar 2016 12:15:28 +0100 Subject: [PATCH 01/10] pin functions based on board depended defaults to support different ESP8266 boad layouts, use board depended constants from "...\esp8266\hardware\esp8266\2.1.0\variants\...\pins_arduino.h" --- Boards.h | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/Boards.h b/Boards.h index 855b6ebe..1ac96965 100644 --- a/Boards.h +++ b/Boards.h @@ -665,22 +665,29 @@ 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 +// hardware: TX0=1, TX1=2, RX0=3, MISO=12, MOSI=13, SCLK=14, CS=15, A0=17 +// board depended defaults: LED_BUILTIN, SDA, SCL +// 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 1 +#define TOTAL_PINS 18 // 11 digital + 1 analog + 6 inaccessible +#define VERSION_BLINK_PIN LED_BUILTIN +#define PIN_SERIAL_RX 3 +#define PIN_SERIAL_TX 1 +#define PIN_SERIAL1_TX 2 +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) <= 16)) +#define IS_PIN_ANALOG(p) ((p) == A0) +#define IS_PIN_PWM(p) IS_PIN_DIGITAL(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) >= 12 && (p) <= 15) +#define IS_PIN_INTERRUPT(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) <= 15)) +#define IS_PIN_SERIAL(p) ((p) == 1 || (p) == 3) #define PIN_TO_DIGITAL(p) (p) -#define PIN_TO_ANALOG(p) ((p) - 17) +#define PIN_TO_ANALOG(p) (p) #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) -#define PIN_TO_SERVO(p) p +#define PIN_TO_SERVO(p) (p) // anything else From fb243ce06faa66f5c694f4209bcb0f3bf89bcf3b Mon Sep 17 00:00:00 2001 From: Jens B Date: Sun, 13 Mar 2016 18:52:49 +0100 Subject: [PATCH 02/10] analog pin mapping fixed requires Arduino core for ESP8266 V2.2 --- Boards.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Boards.h b/Boards.h index 1ac96965..0c83315b 100644 --- a/Boards.h +++ b/Boards.h @@ -685,7 +685,7 @@ writePort(port, value, bitmask): Write an 8 bit port. #define IS_PIN_INTERRUPT(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) <= 15)) #define IS_PIN_SERIAL(p) ((p) == 1 || (p) == 3) #define PIN_TO_DIGITAL(p) (p) -#define PIN_TO_ANALOG(p) (p) +#define PIN_TO_ANALOG(p) ((p) - 17) #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) #define PIN_TO_SERVO(p) (p) From a375d3a614aa858e2ed0e592cd9340b22255f225 Mon Sep 17 00:00:00 2001 From: Jens B Date: Sun, 13 Mar 2016 21:23:56 +0100 Subject: [PATCH 03/10] VERSION_BLINK_PIN and PIN_SERIAL1_TX removed --- Boards.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/Boards.h b/Boards.h index 0c83315b..43e7e15d 100644 --- a/Boards.h +++ b/Boards.h @@ -672,10 +672,8 @@ writePort(port, value, bitmask): Write an 8 bit port. #elif defined(ESP8266) #define TOTAL_ANALOG_PINS 1 #define TOTAL_PINS 18 // 11 digital + 1 analog + 6 inaccessible -#define VERSION_BLINK_PIN LED_BUILTIN #define PIN_SERIAL_RX 3 #define PIN_SERIAL_TX 1 -#define PIN_SERIAL1_TX 2 #define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) <= 16)) #define IS_PIN_ANALOG(p) ((p) == A0) #define IS_PIN_PWM(p) IS_PIN_DIGITAL(p) From e6a09e58fa5edb98fe843a902d31a3a33cc300b9 Mon Sep 17 00:00:00 2001 From: Jens B Date: Fri, 18 Mar 2016 20:43:15 +0100 Subject: [PATCH 04/10] most remaining absolte pin numbers/counts replaced by defines/constants from pins_arduino.h --- Boards.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Boards.h b/Boards.h index 43e7e15d..e8b2cb65 100644 --- a/Boards.h +++ b/Boards.h @@ -666,24 +666,23 @@ writePort(port, value, bitmask): Write an 8 bit port. #define PIN_TO_SERVO(p) ((p) - 2) // ESP8266 -// hardware: TX0=1, TX1=2, RX0=3, MISO=12, MOSI=13, SCLK=14, CS=15, A0=17 -// board depended defaults: LED_BUILTIN, SDA, SCL -// note: boot mode GPIOs 0, 2 and 15 can be used as outputs (GPIOs 6-11 are in use for flash IO) +// 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 1 -#define TOTAL_PINS 18 // 11 digital + 1 analog + 6 inaccessible +#define TOTAL_ANALOG_PINS NUM_ANALOG_INPUTS +#define TOTAL_PINS (NUM_DIGITAL_PINS > A0 + NUM_ANALOG_INPUTS ? NUM_DIGITAL_PINS : A0 + NUM_ANALOG_INPUTS) +#define VERSION_BLINK_PIN LED_BUILTIN #define PIN_SERIAL_RX 3 #define PIN_SERIAL_TX 1 -#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) <= 16)) -#define IS_PIN_ANALOG(p) ((p) == A0) +#define IS_PIN_DIGITAL(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) < (NUM_DIGITAL_PINS > A0 + NUM_ANALOG_INPUTS ? NUM_DIGITAL_PINS : A0))) +#define IS_PIN_ANALOG(p) ((p) >= A0 && (p) < A0 + NUM_ANALOG_INPUTS) #define IS_PIN_PWM(p) IS_PIN_DIGITAL(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) >= 12 && (p) <= 15) -#define IS_PIN_INTERRUPT(p) (((p) >= 0 && (p) <= 5) || ((p) >= 12 && (p) <= 15)) -#define IS_PIN_SERIAL(p) ((p) == 1 || (p) == 3) +#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK) +#define IS_PIN_INTERRUPT(p) digitalPinToInterrupt(p) +#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) From 71af575c85422a180d643a5fe471098a0697c6d3 Mon Sep 17 00:00:00 2001 From: Jens B Date: Sun, 20 Mar 2016 20:31:32 +0100 Subject: [PATCH 05/10] config optimizations - Firmata 2.5.1 or higher required - esp8266/Adruino needs to fix macros digitalPinHasPWM and digitalPinToInterrupt - no wifio board support --- Boards.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Boards.h b/Boards.h index e8b2cb65..52bc789b 100644 --- a/Boards.h +++ b/Boards.h @@ -669,13 +669,12 @@ writePort(port, value, bitmask): Write an 8 bit port. // 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 NUM_ANALOG_INPUTS -#define TOTAL_PINS (NUM_DIGITAL_PINS > A0 + NUM_ANALOG_INPUTS ? NUM_DIGITAL_PINS : A0 + NUM_ANALOG_INPUTS) -#define VERSION_BLINK_PIN LED_BUILTIN +#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) < (NUM_DIGITAL_PINS > A0 + NUM_ANALOG_INPUTS ? NUM_DIGITAL_PINS : A0))) +#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) IS_PIN_DIGITAL(p) +#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) From 1f3527ad5a763843581b15b797d184a8aba19028 Mon Sep 17 00:00:00 2001 From: Jens B Date: Tue, 29 Mar 2016 12:28:09 +0200 Subject: [PATCH 06/10] fixed macro IS_PIN_INTERRUPT --- Boards.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Boards.h b/Boards.h index 52bc789b..8f4a3fb0 100644 --- a/Boards.h +++ b/Boards.h @@ -678,7 +678,7 @@ writePort(port, value, bitmask): Write an 8 bit port. #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) +#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) - A0) From ec29b12ec0a09a04a1a3915ab31a563302c38db5 Mon Sep 17 00:00:00 2001 From: Jens B Date: Sun, 3 Apr 2016 17:08:34 +0200 Subject: [PATCH 07/10] new define DEFAULT_PWM_RESOLUTION - default to 8-bit for all architectures and board - ESP8266 default is 10-bit --- Boards.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Boards.h b/Boards.h index 8f4a3fb0..b7c7b054 100644 --- a/Boards.h +++ b/Boards.h @@ -684,6 +684,7 @@ writePort(port, value, bitmask): Write an 8 bit port. #define PIN_TO_ANALOG(p) ((p) - A0) #define PIN_TO_PWM(p) PIN_TO_DIGITAL(p) #define PIN_TO_SERVO(p) (p) +#define DEFAULT_PWM_RESOLUTION 10 // anything else @@ -700,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 From 625cff9ebac5f3b5e095b5ce97687ae2b3cc1ed2 Mon Sep 17 00:00:00 2001 From: Jens B Date: Sun, 3 Apr 2016 17:15:24 +0200 Subject: [PATCH 08/10] examples updated to use define DEFAULT_PWM_RESOLUTION --- examples/StandardFirmata/StandardFirmata.ino | 2 +- examples/StandardFirmataChipKIT/StandardFirmataChipKIT.ino | 2 +- examples/StandardFirmataEthernet/StandardFirmataEthernet.ino | 2 +- .../StandardFirmataEthernetPlus/StandardFirmataEthernetPlus.ino | 2 +- examples/StandardFirmataPlus/StandardFirmataPlus.ino | 2 +- examples/StandardFirmataWiFi/StandardFirmataWiFi.ino | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) 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..067facd8 100644 --- a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino +++ b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino @@ -692,7 +692,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); From 47993810aa7b50e22c1ee810e2ad5f36bfaa9a74 Mon Sep 17 00:00:00 2001 From: Jens B Date: Sat, 9 Apr 2016 20:31:33 +0200 Subject: [PATCH 09/10] WiFiStream variant integration --- .../StandardFirmataWiFi.ino | 32 +++++++--- examples/StandardFirmataWiFi/wifiConfig.h | 61 ++++++++++++------- utility/WiFiStream.h | 33 +++++++++- 3 files changed, 94 insertions(+), 32 deletions(-) diff --git a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino index 067facd8..de7ecf5a 100644 --- a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino +++ b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino @@ -125,6 +125,16 @@ SerialFirmata serialFeature; #ifdef STATIC_IP_ADDRESS IPAddress local_ip(STATIC_IP_ADDRESS); #endif +#ifdef SUBNET_MASK +IPAddress subnet(SUBNET_MASK); +#else +IPAddress subnet(0,0,0,0); +#endif +#ifdef GATEWAY_IP_ADDRESS +IPAddress gateway(GATEWAY_IP_ADDRESS); +#else +IPAddress gateway(0,0,0,0); +#endif int wifiConnectionAttemptCounter = 0; int wifiStatus = WL_IDLE_STATUS; @@ -822,37 +832,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 +883,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 @@ -886,7 +898,11 @@ void setup() 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 stream.config( local_ip ); +#endif #else DEBUG_PRINTLN( "IP will be requested from DHCP ..." ); #endif diff --git a/examples/StandardFirmataWiFi/wifiConfig.h b/examples/StandardFirmataWiFi/wifiConfig.h index 6fe9b304..266e7841 100644 --- a/examples/StandardFirmataWiFi/wifiConfig.h +++ b/examples/StandardFirmataWiFi/wifiConfig.h @@ -23,8 +23,9 @@ */ #define ARDUINO_WIFI_SHIELD -//do not modify these next 4 lines +//do not modify these next 5 lines #ifdef ARDUINO_WIFI_SHIELD +#include #include "utility/WiFiStream.h" WiFiStream stream; #endif @@ -44,37 +45,46 @@ WiFiStream stream; */ //#define WIFI_101 -//do not modify these next 4 lines +//do not modify these next 5 lines #ifdef WIFI_101 +#include #include "utility/WiFi101Stream.h" -WiFi101Stream stream; +WiFiStream stream; #endif /* - * OPTION C: Configure for HUZZAH + * OPTION C: Configure for ESP8266 + * + * 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 ESP8266_WIFI below and verify the #define values under + * options A and B are commented out. * - * HUZZAH is not yet supported, this will be added in a later revision to StandardFirmataWiFi + * IMPORTANT: You must have the esp8266 board support installed. To easily install this board, open the board manager via: + * Arduino IDE Menus: Tools > Board > Manage Boards > filter search for "esp8266" > Select the result and click 'install' */ +//#define ESP8266_WIFI -//------------------------------ -// TODO -//------------------------------ -//#define HUZZAH_WIFI +//do not modify these next 5 lines +#ifdef ESP8266_WIFI +#include +#include "utility/WiFiStream.h" +WiFiStream stream; +#endif /* - * OPTION D: Configure for ESP6288 + * OPTION D: Configure for HUZZAH * - * ESP6288 is supported through its Arduino Core at - * https://github.com/esp8266/Arduino/ + * HUZZAH with CC3000 is not yet supported, this will be added in a later revision to StandardFirmataWiFi. + * For HUZZAH with ESP8266 use ESP8266_WIFI. */ -//#define ESP_WIFI +//------------------------------ +// TODO +//------------------------------ +//#define HUZZAH_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 @@ -82,8 +92,10 @@ 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 the first 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 +#define SUBNET_MASK 255,255,255,0 // REQUIRED for ESP8266_WIFI, ignored for others +#define GATEWAY_IP_ADDRESS 0,0,0,0 // REQUIRED for ESP8266_WIFI, ignored 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 @@ -107,6 +119,7 @@ char ssid[] = "your_network_name"; char wpa_passphrase[] = "your_wpa_passphrase"; #endif //WIFI_WPA_SECURITY + /* * OPTION B: WEP * @@ -140,11 +153,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))) +#if ((defined(ARDUINO_WIFI_SHIELD) && (defined(WIFI_101) || defined(HUZZAH_WIFI))) || (defined(WIFI_101) && defined(HUZZAH_WIFI)) || (defined(WIFI_101) && defined(ESP8266_WIFI)) || (defined(ESP8266_WIFI) && defined(HUZZAH_WIFI)) || (defined(ESP8266_WIFI) && defined(ARDUINO_WIFI_SHIELD))) #error "you may not define more than one wifi device type in wifiConfig.h." #endif //WIFI device type check -#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI) || defined(ESP_WIFI)) +#if !(defined(ARDUINO_WIFI_SHIELD) || defined(WIFI_101) || defined(HUZZAH_WIFI) || defined(ESP8266_WIFI)) #error "you must define a wifi device type in wifiConfig.h." #endif @@ -156,6 +169,10 @@ 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) *============================================================================*/ diff --git a/utility/WiFiStream.h b/utility/WiFiStream.h index fdcb483a..8c76848a 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. @@ -19,7 +19,6 @@ #include #include -#include class WiFiStream : public Stream { @@ -29,6 +28,8 @@ 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 @@ -59,12 +60,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; +#ifdef ESP8266 + WiFi.config( local_ip, gateway, subnet ); +#else + WiFi.config( local_ip, IPAddress(0, 0, 0, 0), gateway, subnet ); +#endif + } // get DCHP IP inline IPAddress localIP() @@ -72,6 +88,9 @@ class WiFiStream : public Stream return WiFi.localIP(); } + /** + * @return true if connected + */ inline bool maintain() { if( connect_client() ) return true; @@ -82,17 +101,23 @@ class WiFiStream : public Stream { if( _local_ip ) { +#ifdef ESP8266 + WiFi.config( _local_ip, _gateway, _subnet ); +#else WiFi.config( _local_ip ); +#endif } if( _passphrase ) { result = WiFi.begin( _ssid, _passphrase); } +#ifndef ESP8266 else if( _key_idx && _key ) { result = WiFi.begin( _ssid, _key_idx, _key ); } +#endif else { result = WiFi.begin( _ssid ); @@ -124,6 +149,7 @@ 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) { @@ -141,6 +167,7 @@ class WiFiStream : public Stream _server.begin(); return result; } +#endif //WPA-encrypted networks inline int begin(char *ssid, const char *passphrase, uint16_t port) @@ -163,6 +190,7 @@ class WiFiStream : public Stream * Connection functions without DHCP ******************************************************************************/ +#ifndef ESP8266 //OPEN networks with static IP inline int begin(char *ssid, IPAddress local_ip, uint16_t port) { @@ -219,6 +247,7 @@ class WiFiStream : public Stream _server.begin(); return result; } +#endif /****************************************************************************** * Stream implementations From 7ecba4ec7acac9264f5d072f48a57d2a980dcb39 Mon Sep 17 00:00:00 2001 From: Jeff Hoefs Date: Sun, 10 Apr 2016 17:21:49 -0700 Subject: [PATCH 10/10] wifi updates - Consolidate WiFi stream classes into single class - Remove unused WiFi stream classes - Fix ESP8266 serial output (needed flush) - Fix ESP8266 connection status issue - Automatically include WiFi lib for MKR1000 and ESP8266 - Simplify config error checking - Update instructions in wifiConfig.h - Do not ignore MKR1000 pins --- .../StandardFirmataWiFi.ino | 29 +- examples/StandardFirmataWiFi/wifiConfig.h | 133 +++++---- utility/ESPWiFiStream.cpp | 4 - utility/ESPWiFiStream.h | 263 ------------------ utility/WiFi101Stream.cpp | 4 - utility/WiFi101Stream.h | 259 ----------------- utility/WiFiStream.h | 56 +++- utility/firmataDebug.h | 2 +- 8 files changed, 140 insertions(+), 610 deletions(-) delete mode 100644 utility/ESPWiFiStream.cpp delete mode 100644 utility/ESPWiFiStream.h delete mode 100644 utility/WiFi101Stream.cpp delete mode 100644 utility/WiFi101Stream.h diff --git a/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino b/examples/StandardFirmataWiFi/StandardFirmataWiFi.ino index de7ecf5a..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 @@ -127,13 +125,9 @@ IPAddress local_ip(STATIC_IP_ADDRESS); #endif #ifdef SUBNET_MASK IPAddress subnet(SUBNET_MASK); -#else -IPAddress subnet(0,0,0,0); #endif #ifdef GATEWAY_IP_ADDRESS IPAddress gateway(GATEWAY_IP_ADDRESS); -#else -IPAddress gateway(0,0,0,0); #endif int wifiConnectionAttemptCounter = 0; @@ -896,13 +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 +#ifdef ESP8266_WIFI stream.config( local_ip , gateway, subnet ); -#else +#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 +#endif #else DEBUG_PRINTLN( "IP will be requested from DHCP ..." ); #endif @@ -968,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 266e7841..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,44 +12,58 @@ // 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 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. + * 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 configure StandardFirmataWiFi to use the Arduino WiFi shield - * leave the #define below uncommented. + * 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 ARDUINO_WIFI_SHIELD +//#define WIFI_101 -//do not modify these next 5 lines -#ifdef ARDUINO_WIFI_SHIELD -#include +//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 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 - * with 802.11 B/G/N networks. + * OPTION B: Configure for legacy Arduino WiFi shield * - * To enable, uncomment the #define WIFI_101 below and verify the #define values under - * options A and C are commented out. + * 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. * - * 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' + * 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 WIFI_101 +//#define ARDUINO_WIFI_SHIELD -//do not modify these next 5 lines -#ifdef WIFI_101 -#include -#include "utility/WiFi101Stream.h" +//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 /* @@ -58,25 +72,33 @@ WiFiStream stream; * 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 ESP8266_WIFI below and verify the #define values under - * options A and B 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 esp8266 board support installed. To easily install this board, open the board manager via: - * Arduino IDE Menus: Tools > Board > Manage Boards > filter search for "esp8266" > 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 ESP8266_WIFI - -//do not modify these next 5 lines +//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 D: Configure for HUZZAH * - * HUZZAH with CC3000 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. */ @@ -90,28 +112,32 @@ WiFiStream stream; // 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 the first 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 -#define SUBNET_MASK 255,255,255,0 // REQUIRED for ESP8266_WIFI, ignored for others -#define GATEWAY_IP_ADDRESS 0,0,0,0 // REQUIRED for ESP8266_WIFI, ignored for others +// 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 @@ -123,11 +149,11 @@ char wpa_passphrase[] = "your_wpa_passphrase"; /* * 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 @@ -142,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. @@ -153,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(ESP8266_WIFI)) || (defined(ESP8266_WIFI) && defined(HUZZAH_WIFI)) || (defined(ESP8266_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(ESP8266_WIFI)) +#ifndef WIFI_LIB_INCLUDED #error "you must define a wifi device type in wifiConfig.h." #endif @@ -171,7 +198,7 @@ char wep_key[] = "your_wep_key"; #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 +#endif /*============================================================================== * PIN IGNORE MACROS (don't change anything here) @@ -179,7 +206,7 @@ char wep_key[] = "your_wep_key"; // 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 8c76848a..4f21c109 100644 --- a/utility/WiFiStream.h +++ b/utility/WiFiStream.h @@ -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 @@ -35,6 +37,7 @@ class WiFiStream : public Stream const char *_key = nullptr; //WEP const char *_passphrase = nullptr; //WPA char *_ssid = nullptr; + bool _new_connection = false; inline int connect_client() { @@ -51,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(); @@ -75,10 +88,10 @@ class WiFiStream : public Stream _local_ip = local_ip; _gateway = gateway; _subnet = subnet; -#ifdef ESP8266 - WiFi.config( local_ip, gateway, subnet ); -#else +#ifndef ESP8266 WiFi.config( local_ip, IPAddress(0, 0, 0, 0), gateway, subnet ); +#else + WiFi.config( local_ip, gateway, subnet ); #endif } @@ -101,16 +114,21 @@ class WiFiStream : public Stream { if( _local_ip ) { -#ifdef ESP8266 - WiFi.config( _local_ip, _gateway, _subnet ); -#else +#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 ) @@ -120,7 +138,12 @@ class WiFiStream : public Stream #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; @@ -137,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 ); @@ -153,6 +179,7 @@ class WiFiStream : public Stream //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; @@ -172,13 +199,20 @@ class WiFiStream : public Stream //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 ); @@ -190,10 +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; @@ -212,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; @@ -232,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; 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)