Skip to content

refactor setup function #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 81 additions & 75 deletions examples/StandardFirmataWiFi/StandardFirmataWiFi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

See file LICENSE.txt for further informations on licensing terms.

Last updated by Jeff Hoefs: April 17th, 2016
Last updated by Jeff Hoefs: April 24th, 2016
*/

/*
Expand All @@ -36,7 +36,7 @@

- Arduino WiFi Shield (or clone)
- Arduino WiFi Shield 101
- Arduino MKR1000 board (built-in WiFi 101)
- Arduino MKR1000 board
- ESP8266 WiFi board compatible with ESP8266 Arduino core

Follow the instructions in the wifiConfig.h file (wifiConfig.h tab in Arduino IDE) to
Expand All @@ -46,7 +46,7 @@
- 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:
- ESP8266 requires the Arduino ESP8266 core v2.1.0 or higher 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
Expand Down Expand Up @@ -308,7 +308,7 @@ void checkDigitalInputs(void)
}

// -----------------------------------------------------------------------------
// function forward declarations
// function forward declarations for xtensa compiler (ESP8266)
void enableI2CPins();
void disableI2CPins();
void reportAnalogCallback(byte analogPin, int value);
Expand Down Expand Up @@ -830,6 +830,28 @@ void systemResetCallback()
isResetting = false;
}

/*
* Called when a TCP connection is either connected or disconnected.
* TODO:
* - report connected or reconnected state to host (to be added to protocol)
* - report current state to host (to be added to protocol)
*/
void hostConnectionCallback(byte state)
{
switch (state) {
case HOST_CONNECTION_CONNECTED:
DEBUG_PRINTLN( "TCP connection established" );
Copy link
Member Author

@soundanalogous soundanalogous Apr 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO:

break;
case HOST_CONNECTION_DISCONNECTED:
DEBUG_PRINTLN( "TCP connection disconnected" );
break;
}
}

/*
* Print the status of the WiFi connection. This is the connection to the access point rather
* than the TCP connection.
*/
void printWifiStatus() {
if ( WiFi.status() != WL_CONNECTED )
{
Expand All @@ -855,16 +877,38 @@ void printWifiStatus() {
}
}

void setup()
/*
* StandardFirmataWiFi communicates with WiFi shields over SPI. Therefore all
* SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication.
* Additional pins may also need to be ignored depending on the particular board or
* shield in use.
*/
void ignorePins()
{
/*
* WIFI SETUP
*/
DEBUG_BEGIN(9600);
#ifdef IS_IGNORE_PIN
for (byte i = 0; i < TOTAL_PINS; i++) {
if (IS_IGNORE_PIN(i)) {
Firmata.setPinMode(i, PIN_MODE_IGNORE);
}
}
#endif

/*
* This statement will clarify how a connection is being made
*/
//Set up controls for the Arduino WiFi Shield SS for the SD Card
#ifdef ARDUINO_WIFI_SHIELD
// Arduino WiFi Shield has SD SS wired to D4
pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata
digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low;

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA
#endif //defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

#endif //ARDUINO_WIFI_SHIELD
}

void initTransport()
{
// This statement will clarify how a connection is being made
DEBUG_PRINT( "StandardFirmataWiFi will attempt a WiFi connection " );
#if defined(WIFI_101)
DEBUG_PRINTLN( "using the WiFi 101 library." );
Expand All @@ -877,9 +921,7 @@ void setup()
//else should never happen here as error-checking in wifiConfig.h will catch this
#endif //defined(WIFI_101)

/*
* Configure WiFi IP Address
*/
// Configure WiFi IP Address
#ifdef STATIC_IP_ADDRESS
DEBUG_PRINT( "Using static IP: " );
DEBUG_PRINTLN( local_ip );
Expand All @@ -894,44 +936,35 @@ void setup()
DEBUG_PRINTLN( "IP will be requested from DHCP ..." );
#endif

/*
* Configure WiFi security and initiate WiFi connection
*/
stream.attach(hostConnectionCallback);

// Configure WiFi security and initiate WiFi connection
#if defined(WIFI_WEP_SECURITY)
DEBUG_PRINT( "Attempting to connect to WEP SSID: " );
DEBUG_PRINTLN(ssid);
DEBUG_PRINT( "Attempting to connect to WEP SSID: " );
DEBUG_PRINTLN(ssid);
stream.begin(ssid, wep_index, wep_key);
#elif defined(WIFI_WPA_SECURITY)
DEBUG_PRINT( "Attempting to connect to WPA SSID: " );
DEBUG_PRINTLN(ssid);
DEBUG_PRINT( "Attempting to connect to WPA SSID: " );
DEBUG_PRINTLN(ssid);
stream.begin(ssid, wpa_passphrase);
#else //OPEN network
DEBUG_PRINTLN( "Attempting to connect to open SSID: " );
DEBUG_PRINTLN(ssid);
DEBUG_PRINTLN( "Attempting to connect to open SSID: " );
DEBUG_PRINTLN(ssid);
stream.begin(ssid);
#endif //defined(WIFI_WEP_SECURITY)
DEBUG_PRINTLN( "WiFi setup done" );

/*
* Wait for TCP connection to be established
*/
while (!streamConnected && ++connectionAttempts <= MAX_CONN_ATTEMPTS) {
// Wait for connection to access point to be established.
while (WiFi.status() != WL_CONNECTED && ++connectionAttempts <= MAX_CONN_ATTEMPTS) {
delay(500);
DEBUG_PRINT(".");
streamConnected = stream.maintain();
}
if (streamConnected) {
DEBUG_PRINTLN( "TCP connection established" );
} else {
DEBUG_PRINTLN( "failed to establish TCP connection" );
}
printWifiStatus();
}

/*
* FIRMATA SETUP
*/
void initFirmata()
{
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);

Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
Expand All @@ -941,47 +974,20 @@ void setup()
Firmata.attach(START_SYSEX, sysexCallback);
Firmata.attach(SYSTEM_RESET, systemResetCallback);

// StandardFirmataWiFi communicates with WiFi shields over SPI. Therefore all
// SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication.
// Additional pins may also need to be ignored depending on the particular board or
// shield in use.
ignorePins();

for (byte i = 0; i < TOTAL_PINS; i++) {
#if defined(ARDUINO_WIFI_SHIELD)
if (IS_IGNORE_WIFI_SHIELD(i)
#if defined(__AVR_ATmega32U4__)
|| 24 == i // On Leonardo, pin 24 maps to D4 and pin 28 maps to D10
|| 28 == i
#endif //defined(__AVR_ATmega32U4__)
) {
// 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
if (false) {
#else
if (false) {
#endif
Firmata.setPinMode(i, PIN_MODE_IGNORE);
}
}

//Set up controls for the Arduino WiFi Shield SS for the SD Card
#ifdef ARDUINO_WIFI_SHIELD
// Arduino WiFi, Arduino WiFi Shield and Arduino Yun all have SD SS wired to D4
pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata
digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low;
// Initialize Firmata to use the WiFi stream object as the transport.
Firmata.begin(stream);
systemResetCallback(); // reset to default config
}

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA
#endif //defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
void setup()
{
DEBUG_BEGIN(9600);

#endif //ARDUINO_WIFI_SHIELD
initTransport();

// start up Network Firmata:
Firmata.begin(stream);
systemResetCallback(); // reset to default config
initFirmata();
}

/*==============================================================================
Expand Down
19 changes: 15 additions & 4 deletions examples/StandardFirmataWiFi/wifiConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,21 @@ char wep_key[] = "your_wep_key";
* PIN IGNORE MACROS (don't change anything here)
*============================================================================*/

#if defined(WIFI_101) && !defined(ARDUINO_SAMD_MKR1000)
// 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
// Not needed for Arduino MKR1000.
#define IS_IGNORE_WIFI101_SHIELD(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7)
// also don't ignore SS pin if it's not pin 10. Not needed for Arduino MKR1000.
#define IS_IGNORE_PIN(p) ((p) == 10 || (IS_PIN_SPI(p) && (p) != SS) || (p) == 5 || (p) == 7)

#elif defined(ARDUINO_WIFI_SHIELD) && defined(__AVR_ATmega32U4__)
// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS)
#define IS_IGNORE_WIFI_SHIELD(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10)
// On Leonardo, pin 24 maps to D4 and pin 28 maps to D10
#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10 || (p) == 24 || (p) == 28)

#elif defined(ARDUINO_WIFI_SHIELD)
// ignore SPI pins, pin 4 (SS for SD-Card on WiFi-shield), pin 7 (WiFi handshake) and pin 10 (WiFi SS)
#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 7 || (p) == 10)

#elif defined(ESP8266_WIFI) && defined(SERIAL_DEBUG)
#define IS_IGNORE_PIN(p) ((p) == 1)

#endif