@@ -830,6 +830,28 @@ void systemResetCallback()
830
830
isResetting = false ;
831
831
}
832
832
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
+ */
833
855
void printWifiStatus () {
834
856
if ( WiFi.status () != WL_CONNECTED )
835
857
{
@@ -855,16 +877,51 @@ void printWifiStatus() {
855
877
}
856
878
}
857
879
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 ()
859
887
{
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
+ }
864
908
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
868
925
DEBUG_PRINT ( " StandardFirmataWiFi will attempt a WiFi connection " );
869
926
#if defined(WIFI_101)
870
927
DEBUG_PRINTLN ( " using the WiFi 101 library." );
@@ -877,9 +934,7 @@ void setup()
877
934
// else should never happen here as error-checking in wifiConfig.h will catch this
878
935
#endif // defined(WIFI_101)
879
936
880
- /*
881
- * Configure WiFi IP Address
882
- */
937
+ // Configure WiFi IP Address
883
938
#ifdef STATIC_IP_ADDRESS
884
939
DEBUG_PRINT ( " Using static IP: " );
885
940
DEBUG_PRINTLN ( local_ip );
@@ -894,44 +949,39 @@ void setup()
894
949
DEBUG_PRINTLN ( " IP will be requested from DHCP ..." );
895
950
#endif
896
951
897
- /*
898
- * Configure WiFi security and initiate WiFi connection
899
- */
952
+ stream. attach (hostConnectionCallback);
953
+
954
+ // Configure WiFi security and initiate WiFi connection
900
955
#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);
903
958
stream.begin (ssid, wep_index, wep_key);
904
959
#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);
907
962
stream.begin (ssid, wpa_passphrase);
908
963
#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);
911
966
stream.begin (ssid);
912
967
#endif // defined(WIFI_WEP_SECURITY)
913
968
DEBUG_PRINTLN ( " WiFi setup done" );
914
969
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) {
919
976
delay (500 );
920
977
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" );
927
978
}
928
979
printWifiStatus ();
980
+ }
929
981
930
- /*
931
- * FIRMATA SETUP
932
- */
982
+ void initFirmata ()
983
+ {
933
984
Firmata.setFirmwareVersion (FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
934
-
935
985
Firmata.attach (ANALOG_MESSAGE, analogWriteCallback);
936
986
Firmata.attach (DIGITAL_MESSAGE, digitalWriteCallback);
937
987
Firmata.attach (REPORT_ANALOG, reportAnalogCallback);
@@ -941,47 +991,20 @@ void setup()
941
991
Firmata.attach (START_SYSEX, sysexCallback);
942
992
Firmata.attach (SYSTEM_RESET, systemResetCallback);
943
993
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 ();
948
995
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
+ }
975
1000
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 );
979
1004
980
- # endif // ARDUINO_WIFI_SHIELD
1005
+ initWiFi ();
981
1006
982
- // start up Network Firmata:
983
- Firmata.begin (stream);
984
- systemResetCallback (); // reset to default config
1007
+ initFirmata ();
985
1008
}
986
1009
987
1010
/* ==============================================================================
0 commit comments