@@ -830,6 +830,28 @@ void systemResetCallback()
830830 isResetting = false ;
831831}
832832
833+ /*
834+ * Called when a TCP connection is either connected or disconnected.
835+ * TODO - figure out why the callback is not being called when using ESP8266 as a TCP server and
836+ * why only connect is called when using ESP8266 as a TCP client. In both cases the actual
837+ * connection is working but not reported via the callback.
838+ */
839+ void hostConnectionCallback (byte state)
840+ {
841+ switch (state) {
842+ case HOST_CONNECTION_CONNECTED:
843+ DEBUG_PRINTLN ( " TCP connection established" );
844+ break ;
845+ case HOST_CONNECTION_DISCONNECTED:
846+ DEBUG_PRINTLN ( " TCP connection disconnected" );
847+ break ;
848+ }
849+ }
850+
851+ /*
852+ * Print the status of the WiFi connection. This is the connection to the access point rather
853+ * than the TCP connection.
854+ */
833855void printWifiStatus () {
834856 if ( WiFi.status () != WL_CONNECTED )
835857 {
@@ -855,16 +877,51 @@ void printWifiStatus() {
855877 }
856878}
857879
858- void setup ()
880+ /*
881+ * StandardFirmataWiFi communicates with WiFi shields over SPI. Therefore all
882+ * SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication.
883+ * Additional pins may also need to be ignored depending on the particular board or
884+ * shield in use.
885+ */
886+ void ignoreWiFiPins ()
859887{
860- /*
861- * WIFI SETUP
862- */
863- DEBUG_BEGIN (9600 );
888+ for (byte i = 0 ; i < TOTAL_PINS; i++) {
889+ #if defined(ARDUINO_WIFI_SHIELD)
890+ if (IS_IGNORE_WIFI_SHIELD (i)
891+ #if defined(__AVR_ATmega32U4__)
892+ || 24 == i // On Leonardo, pin 24 maps to D4 and pin 28 maps to D10
893+ || 28 == i
894+ #endif // defined(__AVR_ATmega32U4__)
895+ ) {
896+ // don't ignore pins when using Wi-Fi 101 library with the MKR1000
897+ #elif defined (WIFI_101) && !defined(ARDUINO_SAMD_MKR1000)
898+ if (IS_IGNORE_WIFI101_SHIELD (i)) {
899+ #elif defined (HUZZAH_WIFI)
900+ // TODO
901+ if (false ) {
902+ #else
903+ if (false ) {
904+ #endif
905+ Firmata.setPinMode (i, PIN_MODE_IGNORE);
906+ }
907+ }
864908
865- /*
866- * This statement will clarify how a connection is being made
867- */
909+ // Set up controls for the Arduino WiFi Shield SS for the SD Card
910+ #ifdef ARDUINO_WIFI_SHIELD
911+ // Arduino WiFi, Arduino WiFi Shield and Arduino Yun all have SD SS wired to D4
912+ pinMode (PIN_TO_DIGITAL (4 ), OUTPUT); // switch off SD card bypassing Firmata
913+ digitalWrite (PIN_TO_DIGITAL (4 ), HIGH); // SS is active low;
914+
915+ #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
916+ pinMode (PIN_TO_DIGITAL (53 ), OUTPUT); // configure hardware SS as output on MEGA
917+ #endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
918+
919+ #endif // ARDUINO_WIFI_SHIELD
920+ }
921+
922+ void initWiFi ()
923+ {
924+ // This statement will clarify how a connection is being made
868925 DEBUG_PRINT ( " StandardFirmataWiFi will attempt a WiFi connection " );
869926#if defined(WIFI_101)
870927 DEBUG_PRINTLN ( " using the WiFi 101 library." );
@@ -877,9 +934,7 @@ void setup()
877934 // else should never happen here as error-checking in wifiConfig.h will catch this
878935#endif // defined(WIFI_101)
879936
880- /*
881- * Configure WiFi IP Address
882- */
937+ // Configure WiFi IP Address
883938#ifdef STATIC_IP_ADDRESS
884939 DEBUG_PRINT ( " Using static IP: " );
885940 DEBUG_PRINTLN ( local_ip );
@@ -894,44 +949,39 @@ void setup()
894949 DEBUG_PRINTLN ( " IP will be requested from DHCP ..." );
895950#endif
896951
897- /*
898- * Configure WiFi security and initiate WiFi connection
899- */
952+ stream. attach (hostConnectionCallback);
953+
954+ // Configure WiFi security and initiate WiFi connection
900955#if defined(WIFI_WEP_SECURITY)
901- DEBUG_PRINT ( " Attempting to connect to WEP SSID: " );
902- DEBUG_PRINTLN (ssid);
956+ DEBUG_PRINT ( " Attempting to connect to WEP SSID: " );
957+ DEBUG_PRINTLN (ssid);
903958 stream.begin (ssid, wep_index, wep_key);
904959#elif defined(WIFI_WPA_SECURITY)
905- DEBUG_PRINT ( " Attempting to connect to WPA SSID: " );
906- DEBUG_PRINTLN (ssid);
960+ DEBUG_PRINT ( " Attempting to connect to WPA SSID: " );
961+ DEBUG_PRINTLN (ssid);
907962 stream.begin (ssid, wpa_passphrase);
908963#else // OPEN network
909- DEBUG_PRINTLN ( " Attempting to connect to open SSID: " );
910- DEBUG_PRINTLN (ssid);
964+ DEBUG_PRINTLN ( " Attempting to connect to open SSID: " );
965+ DEBUG_PRINTLN (ssid);
911966 stream.begin (ssid);
912967#endif // defined(WIFI_WEP_SECURITY)
913968 DEBUG_PRINTLN ( " WiFi setup done" );
914969
915- /*
916- * Wait for TCP connection to be established
917- */
918- while (!streamConnected && ++connectionAttempts <= MAX_CONN_ATTEMPTS) {
970+ // Wait for connection to access point to be established. This is necessary for ESP8266
971+ // or we won't have a connection state once printWiFiStatus() is called and the state
972+ // will be reported as disconnected. We don't want to wait until the TCP connection is
973+ // established before calling printWiFiStatus() because printing the IP address upon
974+ // connection with the access point is useful when using DHCP
975+ while (WiFi.status () != WL_CONNECTED && ++connectionAttempts <= MAX_CONN_ATTEMPTS) {
919976 delay (500 );
920977 DEBUG_PRINT (" ." );
921- streamConnected = stream.maintain ();
922- }
923- if (streamConnected) {
924- DEBUG_PRINTLN ( " TCP connection established" );
925- } else {
926- DEBUG_PRINTLN ( " failed to establish TCP connection" );
927978 }
928979 printWifiStatus ();
980+ }
929981
930- /*
931- * FIRMATA SETUP
932- */
982+ void initFirmata ()
983+ {
933984 Firmata.setFirmwareVersion (FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
934-
935985 Firmata.attach (ANALOG_MESSAGE, analogWriteCallback);
936986 Firmata.attach (DIGITAL_MESSAGE, digitalWriteCallback);
937987 Firmata.attach (REPORT_ANALOG, reportAnalogCallback);
@@ -941,47 +991,20 @@ void setup()
941991 Firmata.attach (START_SYSEX, sysexCallback);
942992 Firmata.attach (SYSTEM_RESET, systemResetCallback);
943993
944- // StandardFirmataWiFi communicates with WiFi shields over SPI. Therefore all
945- // SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication.
946- // Additional pins may also need to be ignored depending on the particular board or
947- // shield in use.
994+ ignoreWiFiPins ();
948995
949- for (byte i = 0 ; i < TOTAL_PINS; i++) {
950- #if defined(ARDUINO_WIFI_SHIELD)
951- if (IS_IGNORE_WIFI_SHIELD (i)
952- #if defined(__AVR_ATmega32U4__)
953- || 24 == i // On Leonardo, pin 24 maps to D4 and pin 28 maps to D10
954- || 28 == i
955- #endif // defined(__AVR_ATmega32U4__)
956- ) {
957- // don't ignore pins when using Wi-Fi 101 library with the MKR1000
958- #elif defined (WIFI_101) && !defined(ARDUINO_SAMD_MKR1000)
959- if (IS_IGNORE_WIFI101_SHIELD (i)) {
960- #elif defined (HUZZAH_WIFI)
961- // TODO
962- if (false ) {
963- #else
964- if (false ) {
965- #endif
966- Firmata.setPinMode (i, PIN_MODE_IGNORE);
967- }
968- }
969-
970- // Set up controls for the Arduino WiFi Shield SS for the SD Card
971- #ifdef ARDUINO_WIFI_SHIELD
972- // Arduino WiFi, Arduino WiFi Shield and Arduino Yun all have SD SS wired to D4
973- pinMode (PIN_TO_DIGITAL (4 ), OUTPUT); // switch off SD card bypassing Firmata
974- digitalWrite (PIN_TO_DIGITAL (4 ), HIGH); // SS is active low;
996+ // Initialize Firmata to use the WiFi stream object as the transport.
997+ Firmata.begin (stream);
998+ systemResetCallback (); // reset to default config
999+ }
9751000
976- # if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__ )
977- pinMode ( PIN_TO_DIGITAL ( 53 ), OUTPUT); // configure hardware SS as output on MEGA
978- # endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
1001+ void setup ( )
1002+ {
1003+ DEBUG_BEGIN ( 9600 );
9791004
980- # endif // ARDUINO_WIFI_SHIELD
1005+ initWiFi ();
9811006
982- // start up Network Firmata:
983- Firmata.begin (stream);
984- systemResetCallback (); // reset to default config
1007+ initFirmata ();
9851008}
9861009
9871010/* ==============================================================================
0 commit comments