22
22
23
23
See file LICENSE.txt for further informations on licensing terms.
24
24
25
- Last updated by Jeff Hoefs: April 17th , 2016
25
+ Last updated by Jeff Hoefs: April 24th , 2016
26
26
*/
27
27
28
28
/*
36
36
37
37
- Arduino WiFi Shield (or clone)
38
38
- Arduino WiFi Shield 101
39
- - Arduino MKR1000 board (built-in WiFi 101)
39
+ - Arduino MKR1000 board
40
40
- ESP8266 WiFi board compatible with ESP8266 Arduino core
41
41
42
42
Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to
46
46
- WiFi Shield 101 requires version 0.7.0 or higher of the WiFi101 library (available in Arduino
47
47
1.6.8 or higher, or update the library via the Arduino Library Manager or clone from source:
48
48
https://github.com/arduino-libraries/WiFi101)
49
- - ESP8266 requires the Arduino ESP8266 core which can be obtained here:
49
+ - ESP8266 requires the Arduino ESP8266 core v2.1.0 or higher which can be obtained here:
50
50
https://github.com/esp8266/Arduino
51
51
52
52
In order to use the WiFi Shield 101 with Firmata you will need a board with at least 35k of Flash
@@ -308,7 +308,7 @@ void checkDigitalInputs(void)
308
308
}
309
309
310
310
// -----------------------------------------------------------------------------
311
- // function forward declarations
311
+ // function forward declarations for xtensa compiler (ESP8266)
312
312
void enableI2CPins ();
313
313
void disableI2CPins ();
314
314
void reportAnalogCallback (byte analogPin, int value);
@@ -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:
836
+ * - report connected or reconnected state to host (to be added to protocol)
837
+ * - report current state to host (to be added to protocol)
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,38 @@ 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 ignorePins ()
859
887
{
860
- /*
861
- * WIFI SETUP
862
- */
863
- DEBUG_BEGIN (9600 );
888
+ #ifdef IS_IGNORE_PIN
889
+ for (byte i = 0 ; i < TOTAL_PINS; i++) {
890
+ if (IS_IGNORE_PIN (i)) {
891
+ Firmata.setPinMode (i, PIN_MODE_IGNORE);
892
+ }
893
+ }
894
+ #endif
864
895
865
- /*
866
- * This statement will clarify how a connection is being made
867
- */
896
+ // Set up controls for the Arduino WiFi Shield SS for the SD Card
897
+ #ifdef ARDUINO_WIFI_SHIELD
898
+ // Arduino WiFi Shield has SD SS wired to D4
899
+ pinMode (PIN_TO_DIGITAL (4 ), OUTPUT); // switch off SD card bypassing Firmata
900
+ digitalWrite (PIN_TO_DIGITAL (4 ), HIGH); // SS is active low;
901
+
902
+ #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
903
+ pinMode (PIN_TO_DIGITAL (53 ), OUTPUT); // configure hardware SS as output on MEGA
904
+ #endif // defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
905
+
906
+ #endif // ARDUINO_WIFI_SHIELD
907
+ }
908
+
909
+ void initTransport ()
910
+ {
911
+ // This statement will clarify how a connection is being made
868
912
DEBUG_PRINT ( " StandardFirmataWiFi will attempt a WiFi connection " );
869
913
#if defined(WIFI_101)
870
914
DEBUG_PRINTLN ( " using the WiFi 101 library." );
@@ -877,9 +921,7 @@ void setup()
877
921
// else should never happen here as error-checking in wifiConfig.h will catch this
878
922
#endif // defined(WIFI_101)
879
923
880
- /*
881
- * Configure WiFi IP Address
882
- */
924
+ // Configure WiFi IP Address
883
925
#ifdef STATIC_IP_ADDRESS
884
926
DEBUG_PRINT ( " Using static IP: " );
885
927
DEBUG_PRINTLN ( local_ip );
@@ -894,44 +936,35 @@ void setup()
894
936
DEBUG_PRINTLN ( " IP will be requested from DHCP ..." );
895
937
#endif
896
938
897
- /*
898
- * Configure WiFi security and initiate WiFi connection
899
- */
939
+ stream. attach (hostConnectionCallback);
940
+
941
+ // Configure WiFi security and initiate WiFi connection
900
942
#if defined(WIFI_WEP_SECURITY)
901
- DEBUG_PRINT ( " Attempting to connect to WEP SSID: " );
902
- DEBUG_PRINTLN (ssid);
943
+ DEBUG_PRINT ( " Attempting to connect to WEP SSID: " );
944
+ DEBUG_PRINTLN (ssid);
903
945
stream.begin (ssid, wep_index, wep_key);
904
946
#elif defined(WIFI_WPA_SECURITY)
905
- DEBUG_PRINT ( " Attempting to connect to WPA SSID: " );
906
- DEBUG_PRINTLN (ssid);
947
+ DEBUG_PRINT ( " Attempting to connect to WPA SSID: " );
948
+ DEBUG_PRINTLN (ssid);
907
949
stream.begin (ssid, wpa_passphrase);
908
950
#else // OPEN network
909
- DEBUG_PRINTLN ( " Attempting to connect to open SSID: " );
910
- DEBUG_PRINTLN (ssid);
951
+ DEBUG_PRINTLN ( " Attempting to connect to open SSID: " );
952
+ DEBUG_PRINTLN (ssid);
911
953
stream.begin (ssid);
912
954
#endif // defined(WIFI_WEP_SECURITY)
913
955
DEBUG_PRINTLN ( " WiFi setup done" );
914
956
915
- /*
916
- * Wait for TCP connection to be established
917
- */
918
- while (!streamConnected && ++connectionAttempts <= MAX_CONN_ATTEMPTS) {
957
+ // Wait for connection to access point to be established.
958
+ while (WiFi.status () != WL_CONNECTED && ++connectionAttempts <= MAX_CONN_ATTEMPTS) {
919
959
delay (500 );
920
960
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
961
}
928
962
printWifiStatus ();
963
+ }
929
964
930
- /*
931
- * FIRMATA SETUP
932
- */
965
+ void initFirmata ()
966
+ {
933
967
Firmata.setFirmwareVersion (FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
934
-
935
968
Firmata.attach (ANALOG_MESSAGE, analogWriteCallback);
936
969
Firmata.attach (DIGITAL_MESSAGE, digitalWriteCallback);
937
970
Firmata.attach (REPORT_ANALOG, reportAnalogCallback);
@@ -941,47 +974,20 @@ void setup()
941
974
Firmata.attach (START_SYSEX, sysexCallback);
942
975
Firmata.attach (SYSTEM_RESET, systemResetCallback);
943
976
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.
977
+ ignorePins ();
948
978
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;
979
+ // Initialize Firmata to use the WiFi stream object as the transport.
980
+ Firmata.begin (stream);
981
+ systemResetCallback (); // reset to default config
982
+ }
975
983
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__)
984
+ void setup ( )
985
+ {
986
+ DEBUG_BEGIN ( 9600 );
979
987
980
- # endif // ARDUINO_WIFI_SHIELD
988
+ initTransport ();
981
989
982
- // start up Network Firmata:
983
- Firmata.begin (stream);
984
- systemResetCallback (); // reset to default config
990
+ initFirmata ();
985
991
}
986
992
987
993
/* ==============================================================================
0 commit comments