Ethernet GW WITHOUT radio on Arduino Mega

  • Fellow board members,

    I've got a Arduino Mega 2560 in my shed which I would like to function as a GW by Ethernet.
    I've got the code right for MYS 2.0 so that is not the problem I think.

    I've got an W5100 Ethernet module connected to the ports 50-53 and 5+ and GND on my Arduino Mega. Fixed IP and valid MAC address. When I debug my Mega it says IP: but when I connect the same module with the same code to a Nano on the ports as advised than debug It says IP: as programmed.

    On what pins do I need to connect my W5100 on my Mega?
    Is it on the middle PIN's? or just the 50-53 pins?
    Do I need some changes in the code? For example SoftSPI enable?

    Again: Nano connected with same code and same module: OK!
    Mega connected with same code and same module: IP

    Can somebody point me in the right direction?

  • This is the code used for both devices:

    #include <SPI.h>
    #define MY_DEBUG
    #define MY_GATEWAY_W5100
    // W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
    //#define MY_W5100_SPI_EN 4
    //Nano  Mega
    //10 SS 53
    //11 MO 51
    //12 MI 50
    //13 SCK  52
    #define MY_IP_ADDRESS 192,168,1,80  // If this is disabled, DHCP is used to retrieve address
    // The port to keep open on node server mode / or port to contact in client mode
    #define MY_PORT 5003
    #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDD
    #if defined(MY_USE_UDP)
    #include <EthernetUdp.h>
    // Relay 1 Perk en Gras
    // Relay 2 Fontein
    // Relay 3 Relay_3_MS
    // Relay 4 Relay_4_MS
    // PIR   5 PIR Schuur
    // Dista 6 Auto geparkeerd
    // Door  7 Poort
    // Door  8 Schuurdeur
    //Adding modules
    #include <Ethernet.h>
    #include <MySensors.h>
    #include <NewPing.h>
    #include <Bounce2.h>
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    // PIR
    #define CHILD_ID_PIR 5   // Id of the sensor child
    #define DIGITAL_INPUT_SENSOR_PIR 21   // PIR
    #define INTERRUPT DIGITAL_INPUT_SENSOR_PIR // Usually the interrupt = pin -2 (on uno/nano anyway)
    int oldValueTripped=-1;
    // Distance sensor
    #define CHILD_ID_DIST 6
    #define TRIGGER_PIN 12 // Afstandsmeter Trigger
    #define ECHO_PIN 13 // Afstandsmeter Echo
    #define MAX_DISTANCE 300 // Max distance we want to start indicating green (in cm)
    #define PARKED_DISTANCE 130// Distance when "parked signal" should be sent to controller (in cm)
    unsigned long sendIntervalDIST = 15000;  // Send park status at maximum every 5 second.
    unsigned long lastSend;
    int oldParkedStatus=-1;
    int skipZero=0;
    // Doors
    #define CHILD_ID_GATE 7
    #define CHILD_ID_SD 8
    #define Sheddoor 8 // Schuurdeur
    #define Gate 7 // Poort
    Bounce debouncerGate = Bounce();
    Bounce debouncerSD = Bounce(); 
    int GateoldValue=-1;
    int SDoldValue=-1;
    MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
    MyMessage msgGate(CHILD_ID_GATE,V_TRIPPED);
    MyMessage msgSD(CHILD_ID_SD,V_TRIPPED);
    void before()
       for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Then set relay pins in output mode
            pinMode(pin, OUTPUT);
            // Set relay to last known state (using eeprom storage)
            digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    void setup()
      // PIR
      // Gate
      // Sheddoor
    void presentation() {
      // PIR
      present(CHILD_ID_PIR, S_MOTION);
      // Distance
      present(CHILD_ID_DIST, S_DOOR);
      // Gate
      present(CHILD_ID_GATE, S_DOOR);
      // Shed
      present(CHILD_ID_SD, S_DOOR);
      // Relays
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Register all sensors to gw (they will be created as child devices)
            present(sensor, S_BINARY);
    void loop() 
      // PIR Get the update value
      int tripped = digitalRead(DIGITAL_INPUT_SENSOR_PIR) == HIGH;
       if (tripped != oldValueTripped) {
         // Send in the new value
         send(msgPIR.set(tripped==HIGH ? 1 : 0 ));
         oldValueTripped = tripped;
      // Distance
      unsigned long now = millis();
      unsigned int fullDist = (sonar.ping_median() / US_ROUNDTRIP_CM);
      int displayDist = min(fullDist, MAX_DISTANCE);
      if (displayDist == 0 && skipZero<10) {
        // Try to filter zero readings
        // Update parked status
        int parked = displayDist != 0 && displayDist<PARKED_DISTANCE;
        if (parked != oldParkedStatus && now-lastSend > sendIntervalDIST) {
          if (parked){
          else {
          oldParkedStatus = parked;
          lastSend = now;
      // Gate
      // Get the update value
      int Gatevalue =;
      if (Gatevalue != GateoldValue) {
         // Send in the new value
         send(msgGate.set(Gatevalue==HIGH ? 1 : 0));
         GateoldValue = Gatevalue;
      // Sheddoor
      // Get the update value
      int SDvalue =;
      if (SDvalue != SDoldValue) {
         // Send in the new value
         send(msgSD.set(SDvalue==HIGH ? 1 : 0));
         SDoldValue = SDvalue;
    void incomingMessage(const MyMessage &message) {
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(message.sensor, message.getBool());

  • Mod

    Soft spi I think it's used when you are using w5100 and a radio module on the gateway. I can't help you more because I used the w5100 shield.

  • Hero Member

    @gohan softspi is used for the connection to the radio not to the ethernet shield.

    Have you tried an example ethernet sketch or the stock ethernet gateway sketch with radio disabled? To ensure the wiring of the ethernet gateway with the mega is correct?

  • Mod

    That's what I meant, I didn't explain it well

  • I also believe it has to be something with the wireing. I will check this tonight when I'm going to try it on the ICSP header.
    And to test it with the default sketch!

  • Example sketch is also not working...
    Ditto for the ICSP connection header
    It must be the wires!

  • So here is the deal!
    You need to set Pin 53 to Output in your sketch and wire from 50-53 instead of 10-13.
    It's working now

  • Mod

    that was clearly stated in the code and also in the guide

    //Nano  Mega
    //10 SS 53
    //11 MO 51
    //12 MI 50
    //13 SCK  52

  • @gohan Yes I know but the pinout 53, output not. That was all that was needed eventually .

Log in to reply

Suggested Topics