Skip to content

Commit 562e060

Browse files
Merge pull request #290 from soundanalogous/wifi-client
refactor setup function
2 parents c553c3b + 7573a58 commit 562e060

File tree

2 files changed

+96
-79
lines changed

2 files changed

+96
-79
lines changed

examples/StandardFirmataWiFi/StandardFirmataWiFi.ino

Lines changed: 81 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
2323
See file LICENSE.txt for further informations on licensing terms.
2424
25-
Last updated by Jeff Hoefs: April 17th, 2016
25+
Last updated by Jeff Hoefs: April 24th, 2016
2626
*/
2727

2828
/*
@@ -36,7 +36,7 @@
3636
3737
- Arduino WiFi Shield (or clone)
3838
- Arduino WiFi Shield 101
39-
- Arduino MKR1000 board (built-in WiFi 101)
39+
- Arduino MKR1000 board
4040
- ESP8266 WiFi board compatible with ESP8266 Arduino core
4141
4242
Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to
@@ -46,7 +46,7 @@
4646
- WiFi Shield 101 requires version 0.7.0 or higher of the WiFi101 library (available in Arduino
4747
1.6.8 or higher, or update the library via the Arduino Library Manager or clone from source:
4848
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:
5050
https://github.com/esp8266/Arduino
5151
5252
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)
308308
}
309309

310310
// -----------------------------------------------------------------------------
311-
// function forward declarations
311+
// function forward declarations for xtensa compiler (ESP8266)
312312
void enableI2CPins();
313313
void disableI2CPins();
314314
void reportAnalogCallback(byte analogPin, int value);
@@ -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:
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+
*/
833855
void printWifiStatus() {
834856
if ( WiFi.status() != WL_CONNECTED )
835857
{
@@ -855,16 +877,38 @@ 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 ignorePins()
859887
{
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
864895

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
868912
DEBUG_PRINT( "StandardFirmataWiFi will attempt a WiFi connection " );
869913
#if defined(WIFI_101)
870914
DEBUG_PRINTLN( "using the WiFi 101 library." );
@@ -877,9 +921,7 @@ void setup()
877921
//else should never happen here as error-checking in wifiConfig.h will catch this
878922
#endif //defined(WIFI_101)
879923

880-
/*
881-
* Configure WiFi IP Address
882-
*/
924+
// Configure WiFi IP Address
883925
#ifdef STATIC_IP_ADDRESS
884926
DEBUG_PRINT( "Using static IP: " );
885927
DEBUG_PRINTLN( local_ip );
@@ -894,44 +936,35 @@ void setup()
894936
DEBUG_PRINTLN( "IP will be requested from DHCP ..." );
895937
#endif
896938

897-
/*
898-
* Configure WiFi security and initiate WiFi connection
899-
*/
939+
stream.attach(hostConnectionCallback);
940+
941+
// Configure WiFi security and initiate WiFi connection
900942
#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);
903945
stream.begin(ssid, wep_index, wep_key);
904946
#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);
907949
stream.begin(ssid, wpa_passphrase);
908950
#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);
911953
stream.begin(ssid);
912954
#endif //defined(WIFI_WEP_SECURITY)
913955
DEBUG_PRINTLN( "WiFi setup done" );
914956

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) {
919959
delay(500);
920960
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" );
927961
}
928962
printWifiStatus();
963+
}
929964

930-
/*
931-
* FIRMATA SETUP
932-
*/
965+
void initFirmata()
966+
{
933967
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
934-
935968
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
936969
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
937970
Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
@@ -941,47 +974,20 @@ void setup()
941974
Firmata.attach(START_SYSEX, sysexCallback);
942975
Firmata.attach(SYSTEM_RESET, systemResetCallback);
943976

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();
948978

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+
}
975983

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);
979987

980-
#endif //ARDUINO_WIFI_SHIELD
988+
initTransport();
981989

982-
// start up Network Firmata:
983-
Firmata.begin(stream);
984-
systemResetCallback(); // reset to default config
990+
initFirmata();
985991
}
986992

987993
/*==============================================================================

examples/StandardFirmataWiFi/wifiConfig.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,21 @@ char wep_key[] = "your_wep_key";
220220
* PIN IGNORE MACROS (don't change anything here)
221221
*============================================================================*/
222222

223+
#if defined(WIFI_101) && !defined(ARDUINO_SAMD_MKR1000)
223224
// ignore SPI pins, pin 5 (reset WiFi101 shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS)
224-
// also don't ignore SS pin if it's not pin 10
225-
// Not needed for Arduino MKR1000.
226-
#define IS_IGNORE_WIFI101_SHIELD(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7)
225+
// also don't ignore SS pin if it's not pin 10. Not needed for Arduino MKR1000.
226+
#define IS_IGNORE_PIN(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7)
227227

228+
#elif defined(ARDUINO_WIFI_SHIELD) && defined(__AVR_ATmega32U4__)
228229
// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS)
229-
#define IS_IGNORE_WIFI_SHIELD(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10)
230+
// On Leonardo, pin 24 maps to D4 and pin 28 maps to D10
231+
#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10 || (p) == 24 || (p) == 28)
232+
233+
#elif defined(ARDUINO_WIFI_SHIELD)
234+
// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS)
235+
#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10)
236+
237+
#elif defined(ESP8266_WIFI) && defined(SERIAL_DEBUG)
238+
#define IS_IGNORE_PIN(p) ((p) == 1)
239+
240+
#endif

0 commit comments

Comments
 (0)