Skip to content

Commit d12da78

Browse files
Merge pull request #295 from firmata/ethernet-updates
Ethernet updates
2 parents 5d93938 + bcabc47 commit d12da78

File tree

7 files changed

+187
-1601
lines changed

7 files changed

+187
-1601
lines changed

examples/StandardFirmataEthernet/StandardFirmataEthernet.ino

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
2121
See file LICENSE.txt for further informations on licensing terms.
2222
23-
Last updated by Jeff Hoefs: January 10th, 2016
23+
Last updated by Jeff Hoefs: June 18th, 2016
2424
*/
2525

2626
/*
2727
README
2828
29-
StandardFirmataEthernet is a client implementation. You will need a Firmata client library with
30-
a network transport that can act as a server in order to establish a connection between
29+
StandardFirmataEthernet is a TCP client implementation. You will need a Firmata client library
30+
with a network transport that can act as a TCP server in order to establish a connection between
3131
StandardFirmataEthernet and the Firmata client application.
3232
3333
To use StandardFirmataEthernet you will need to have one of the following
@@ -58,9 +58,9 @@
5858
#include <Firmata.h>
5959

6060
/*
61-
* Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your connection
62-
* that may help in the event of connection issues. If defined, some boards may not begin executing this sketch
63-
* until the Serial console is opened.
61+
* Uncomment the #define SERIAL_DEBUG line below to receive serial output messages relating to your
62+
* connection that may help in the event of connection issues. If defined, some boards may not begin
63+
* executing this sketch until the Serial console is opened.
6464
*/
6565
//#define SERIAL_DEBUG
6666
#include "utility/firmataDebug.h"
@@ -69,6 +69,17 @@
6969
#include "ethernetConfig.h"
7070
#include "utility/EthernetClientStream.h"
7171

72+
/*
73+
* Uncomment the following include to enable interfacing with Serial devices via hardware or
74+
* software serial.
75+
*
76+
* DO NOT uncomment if you are running StandardFirmataEthernet on an Arduino Leonardo,
77+
* Arduino Micro or other ATMega32u4-based board or you will not have enough Flash and RAM
78+
* remaining to reliably run Firmata. Arduino Yun is okay because it doesn't import the Ethernet
79+
* libraries.
80+
*/
81+
//#include "utility/SerialFirmata.h"
82+
7283
#define I2C_WRITE B00000000
7384
#define I2C_READ B00001000
7485
#define I2C_READ_CONTINUOUSLY B00010000
@@ -88,7 +99,6 @@
8899
* GLOBAL VARIABLES
89100
*============================================================================*/
90101

91-
/* network */
92102
#if defined remote_ip && !defined remote_host
93103
#ifdef local_ip
94104
EthernetClientStream stream(client, local_ip, remote_ip, NULL, remote_port);
@@ -304,7 +314,8 @@ void setPinModeCallback(byte pin, int mode)
304314
}
305315
}
306316
if (IS_PIN_ANALOG(pin)) {
307-
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == PIN_MODE_ANALOG ? 1 : 0); // turn on/off reporting
317+
// turn on/off reporting
318+
reportAnalogCallback(PIN_TO_ANALOG(pin), mode == PIN_MODE_ANALOG ? 1 : 0);
308319
}
309320
if (IS_PIN_DIGITAL(pin)) {
310321
if (mode == INPUT || mode == PIN_MODE_PULLUP) {
@@ -801,10 +812,36 @@ void systemResetCallback()
801812
isResetting = false;
802813
}
803814

804-
void setup()
815+
/*
816+
* StandardFirmataEthernet communicates with Ethernet shields over SPI. Therefore all
817+
* SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication.
818+
* Additional pins may also need to be ignored depending on the particular board or
819+
* shield in use.
820+
*/
821+
void ignorePins()
805822
{
806-
DEBUG_BEGIN(9600);
823+
#ifdef IS_IGNORE_PIN
824+
for (byte i = 0; i < TOTAL_PINS; i++) {
825+
if (IS_IGNORE_PIN(i)) {
826+
Firmata.setPinMode(i, PIN_MODE_IGNORE);
827+
}
828+
}
829+
#endif
807830

831+
#ifdef WIZ5100_ETHERNET
832+
// Arduino Ethernet and Arduino EthernetShield have SD SS wired to D4
833+
pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata
834+
digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low;
835+
836+
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
837+
pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA
838+
#endif
839+
840+
#endif // WIZ5100_ETHERNET
841+
}
842+
843+
void initTransport()
844+
{
808845
#ifdef YUN_ETHERNET
809846
Bridge.begin();
810847
#else
@@ -816,9 +853,11 @@ void setup()
816853
#endif
817854

818855
DEBUG_PRINTLN("connecting...");
856+
}
819857

858+
void initFirmata()
859+
{
820860
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
821-
822861
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
823862
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
824863
Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
@@ -828,36 +867,22 @@ void setup()
828867
Firmata.attach(START_SYSEX, sysexCallback);
829868
Firmata.attach(SYSTEM_RESET, systemResetCallback);
830869

831-
#ifdef WIZ5100_ETHERNET
832-
// StandardFirmataEthernet communicates with Ethernet shields over SPI. Therefore all
833-
// SPI pins must be set to IGNORE. Otherwise Firmata would break SPI communication.
834-
// add Pin 10 and configure pin 53 as output if using a MEGA with an Ethernet shield.
835-
836-
for (byte i = 0; i < TOTAL_PINS; i++) {
837-
if (IS_IGNORE_ETHERNET_SHIELD(i)
838-
#if defined(__AVR_ATmega32U4__)
839-
|| 24 == i // On Leonardo, pin 24 maps to D4 and pin 28 maps to D10
840-
|| 28 == i
841-
#endif
842-
) {
843-
Firmata.setPinMode(i, PIN_MODE_IGNORE);
844-
}
845-
}
846-
847-
// Arduino Ethernet and Arduino EthernetShield have SD SS wired to D4
848-
pinMode(PIN_TO_DIGITAL(4), OUTPUT); // switch off SD card bypassing Firmata
849-
digitalWrite(PIN_TO_DIGITAL(4), HIGH); // SS is active low;
850-
#endif // WIZ5100_ETHERNET
851-
852-
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
853-
pinMode(PIN_TO_DIGITAL(53), OUTPUT); // configure hardware SS as output on MEGA
854-
#endif
870+
ignorePins();
855871

856872
// start up Network Firmata:
857873
Firmata.begin(stream);
858874
systemResetCallback(); // reset to default config
859875
}
860876

877+
void setup()
878+
{
879+
DEBUG_BEGIN(9600);
880+
881+
initTransport();
882+
883+
initFirmata();
884+
}
885+
861886
/*==============================================================================
862887
* LOOP()
863888
*============================================================================*/

examples/StandardFirmataEthernet/ethernetConfig.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* You must configure your particular hardware. Follow the steps below.
55
*
6-
* Currently StandardFirmataEthernet is configured as a client. An option to
7-
* configure as a server may be added in the future.
6+
* Currently StandardFirmataEthernet is configured as a TCP client. An
7+
* option to configure as a server may be added in the future.
88
*============================================================================*/
99

1010
// STEP 1 [REQUIRED]
@@ -35,6 +35,8 @@ EthernetClient client;
3535
*
3636
* On Yun there's no need to configure local_ip and mac address as this is automatically
3737
* configured on the linux-side of Yun.
38+
*
39+
* Note that it may take several seconds to establish a connection with the Yun.
3840
*/
3941
//#define YUN_ETHERNET
4042

@@ -44,24 +46,23 @@ EthernetClient client;
4446
YunClient client;
4547
#endif
4648

47-
48-
// STEP 2 [REQUIRED for all boards and shields]
49+
// STEP 2[REQUIRED for all boards and shields]
4950
// replace with IP of the server you want to connect to, comment out if using 'remote_host'
5051
#define remote_ip IPAddress(10, 0, 0, 3)
5152
// *** REMOTE HOST IS NOT YET WORKING ***
5253
// replace with hostname of server you want to connect to, comment out if using 'remote_ip'
5354
// #define remote_host "server.local"
5455

55-
// STEP 3 [REQUIRED unless using Arduin Yun]
56+
// STEP 3 [REQUIRED]
5657
// Replace with the port that your server is listening on
5758
#define remote_port 3030
5859

59-
// STEP 4 [REQUIRED unless using Arduino Yun OR if not using DHCP]
60+
// STEP 4 [REQUIRED unless using DHCP]
6061
// Replace with your board or ethernet shield's IP address
6162
// Comment out if you want to use DHCP
6263
#define local_ip IPAddress(10, 0, 0, 15)
6364

64-
// STEP 5 [REQUIRED unless using Arduino Yun]
65+
// STEP 5 [REQUIRED]
6566
// replace with ethernet shield mac. Must be unique for your network
6667
const byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x53, 0xE5};
6768

@@ -81,5 +82,9 @@ const byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x53, 0xE5};
8182
* PIN IGNORE MACROS (don't change anything here)
8283
*============================================================================*/
8384

85+
#if defined(WIZ5100_ETHERNET)
86+
8487
// ignore SPI pins, pin 10 (Ethernet SS) and pin 4 (SS for SD-Card on Ethernet shield)
85-
#define IS_IGNORE_ETHERNET_SHIELD(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 10)
88+
#define IS_IGNORE_PIN(p) ((IS_PIN_SPI(p) || (p) == 4) || (p) == 10)
89+
90+
#endif

0 commit comments

Comments
 (0)