Arduino Mega 2560 + Ethernet Shield (W5100) + Teleduino - problem

Hi, first I'm sorry for my English. I want make "Smart home" using Arduino Mega 2560 and Ethernet Shield (W5100) + Teleduino.

The problem is here; when I have shield connected to my router and I power up arduino, the control led of Teleduino will flash 3 times (that means it's trying to connect to server) and after about 2 minutes it starts blinking till I power of arduino.

In the arduino I have uploaded "Teleduino2560EthernetClientProxy" , mac adress changed and key changed to my key from teleduino. I have Dhcp and Dns on true.

Please help me. :frowning:

Which ethernet shield are you using? Can you post a link to it?

It would be worth you posting the sketch you are using (remove any personal information such as SSID & password, and your teleduino key. When posting a sketch remember to use code tags- the first icon on the quick reply window.

Have you tried using the Ethernet shield with any simple test programs before trying teleduino?

countrypaul:
Which ethernet shield are you using? Can you post a link to it?

Link to Aliexpress

countrypaul:
It would be worth you posting the sketch you are using (remove any personal information such as SSID & password, and your teleduino key. When posting a sketch remember to use code tags- the first icon on the quick reply window.

#include <EEPROM.h>
#include <Servo.h>
#include <Wire.h>
#include <Teleduino2560.h>

#include <SPI.h>
#include <Ethernet.h>

// User configurable variables
byte useDhcp = true;
byte useDns = true;
byte mac[] = { 0x53, 0x4E, 0x6B, 0x2C, 0x37, 0x6C };
IPAddress deviceIp(192, 168, 1, 100); // Only if useDhcp is false
IPAddress gatewayIp(192, 168, 1, 1); // Only if useDhcp is false
IPAddress dnsIp(192, 168, 1, 1); // Only if useDhcp is false
IPAddress subnet(255, 255, 255, 0); // Only if useDhcp is false
IPAddress serverIp(173, 230, 152, 173); // Only if useDns is false
char serverName[] = "us01.proxy.teleduino.org"; // Only if useDns is true
unsigned int serverPort = 5353; // Can be set to either 53 or 5353
byte statusLedPin = 8;

// User configurable key, this is used to authenticate with the proxy server
// This is checked against the EEPROM on boot and written if necessary
// The proxy server retreives the key from the EEPROM
byte key[] = { 0x00 };

// Other required variables
byte data[257];
byte dataLength;
byte hexStage;
unsigned long lastInstruction = 0;
unsigned long lastRefresh = 0;
byte stage = 0;

// Declare client object
EthernetClient Client;

void setup()
{
  // This is here purely to add a bit of padding to the sketch to fix a compiling bug
  // http://arduino.cc/forum/index.php/topic,60649.0.html
  EEPROM.read(0);
  
  // Load presets
  Teleduino2560.loadPresets();
  
  // Set status LED pin
  Teleduino2560.setStatusLedPin(statusLedPin);

  Teleduino2560.setStatusLed(1); // Initialisation
  // Check the EEPROM header and check to see if the key is correct
  // This is to ensure the key is not cleared from the EEPROM
  if(EEPROM.read(0) != '#')
  {
    EEPROM.write(0, '#');
  }
  if(EEPROM.read(1) != 1)
  {
    EEPROM.write(1, 1);
  }
  if(EEPROM.read(2) != '#')
  {
    EEPROM.write(2, '#');
  }
  if(EEPROM.read(395) != '#')
  {
    EEPROM.write(395, '#');
  }
  for(byte i = 0; i < 16; i++)
  {
    if(EEPROM.read(396 + i) != key[i])
    {
      EEPROM.write(396 + i, key[i]);
    }
  }
  if(EEPROM.read(412) != '#')
  {
    EEPROM.write(412, '#');
  }

  // Start network and attempt to connect to proxy server
  Teleduino2560.setStatusLed(2); // Network configuration
  if(useDhcp)
  {
    if(!Ethernet.begin(mac))
    {
      Teleduino2560.setStatusLed(2, false, 10000);
      Teleduino2560.reset();
    }
  }
  else
  {
    Ethernet.begin(mac, deviceIp, dnsIp, gatewayIp, subnet);
  }
  delay(1000);

  Teleduino2560.setStatusLed(3); // Connect to server
  if((useDns && !Client.connect(serverName, serverPort)) || (!useDns && !Client.connect(serverIp, serverPort)))
  {
    Teleduino2560.setStatusLed(3, false, 10000);
    Teleduino2560.reset();
  }
  lastInstruction = millis();
}

void loop()
{
  if(Client.connected())
  {
    // What we need to do depends on which 'stage' we are at
    switch(stage)
    {
      case 0: // Wait for start byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage++;
          }
        }
        break;
      case 1: // Reset variables
        dataLength = 0;
        hexStage = 0;
        stage++;
        break;
      case 2: // Instruction byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            stage = 0;
            break;
          }
          if(!hexStage)
          {
            data[0] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[0] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            stage++;
          }
        }
        break;
      case 3: // Data length byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            stage = 0;
            break;
          }
          if(!hexStage)
          {
            data[1] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[1] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            stage++;
          }
        }
        break;
      case 4: // Data
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            if(dataLength == data[1])
            {
              stage++;
              break;
            }
            else
            {
              stage = 0;
              break;
            }
          }
          if(!hexStage)
          {
            data[2 + dataLength] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[2 + dataLength] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            dataLength++;
          }
        }
        break;
      case 5: // Execute instruction and return result
        Teleduino2560.instruction(data);
        Client.write('!');
        for(int i = 0; i < data[1] + 2; i++)
        {
          Client.write(Teleduino2560.hexEncode(data[i] / 16));
          Client.write(Teleduino2560.hexEncode(data[i] % 16));
        }
        Client.write('\n');
        lastInstruction = millis();
        stage = 0;
        break;
    }
  }
  else
  {
    Teleduino2560.setStatusLed(10);
    Teleduino2560.reset();
  }

  // Has the instruction timeout been reached?
  if(millis() - lastInstruction > 30000)
  {
    Client.flush();
    Client.stop();
    Teleduino2560.setStatusLed(9);
    Teleduino2560.reset();
  }

  // Process refreshes every 50ms
  if(millis() - lastRefresh >= 50)
  {
    Teleduino2560.pinTimers();
    Teleduino2560.shiftRegisterTimers();
    Teleduino2560.shiftRegisters();
    lastRefresh = millis();
  }

  // Check to see if reset has been requested
  Teleduino2560.checkReset();
}

countrypaul:
Have you tried using the Ethernet shield with any simple test programs before trying teleduino?

No, I’m total beginner with ethernet and arduino.

Try putting in some PrintLn statements to show progress.For example:

void setup()
{
  // This is here purely to add a bit of padding to the sketch to fix a compiling bug
  // http://arduino.cc/forum/index.php/topic,60649.0.html
  EEPROM.read(0);
 
  // Load presets
  Teleduino2560.loadPresets();
 
PrintLn ("Loaded Presets");

  // Set status LED pin
  Teleduino2560.setStatusLedPin(statusLedPin);

PrintLn ("Set status LED Pin");

  Teleduino2560.setStatusLed(1); // Initialisation
  // Check the EEPROM header and check to see if the key is correct
  // This is to ensure the key is not cleared from the EEPROM
  if(EEPROM.read(0) != '#')
  {
    EEPROM.write(0, '#');
  }
  if(EEPROM.read(1) != 1)
  {
    EEPROM.write(1, 1);
  }
  if(EEPROM.read(2) != '#')
  {
    EEPROM.write(2, '#');
  }
  if(EEPROM.read(395) != '#')
  {
    EEPROM.write(395, '#');
  }
  for(byte i = 0; i < 16; i++)
  {
    if(EEPROM.read(396 + i) != key[i])
    {
      EEPROM.write(396 + i, key[i]);
    }
  }
  if(EEPROM.read(412) != '#')
  {
    EEPROM.write(412, '#');
  }

  // Start network and attempt to connect to proxy server
  Teleduino2560.setStatusLed(2); // Network configuration

PrintLn ("Set status LED 2");


  if(useDhcp)
  {
    if(!Ethernet.begin(mac))
    {
      Teleduino2560.setStatusLed(2, false, 10000);
      Teleduino2560.reset();
    }
  }
  else
  {
    Ethernet.begin(mac, deviceIp, dnsIp, gatewayIp, subnet);
  }
  delay(1000);

PrintLn ("Ethernet begin");


  Teleduino2560.setStatusLed(3); // Connect to server

PrintLn ("Set status LED 3");


  if((useDns && !Client.connect(serverName, serverPort)) || (!useDns && !Client.connect(serverIp, serverPort)))
  {
    Teleduino2560.setStatusLed(3, false, 10000);
    Teleduino2560.reset();
  }
  lastInstruction = millis();
}

PrintLn ("Client connect");

If you can watch the serial output when trying to run the code hopefully it will show how far it has got and make it easier to narrow down the location of the problem.

Sorry for late reply, do you think Serial.println?

If not how to use PrintLn

Hi, first I’m sorry for my English. I want make “Smart home” using Arduino Mega 2560 and Ethernet Shield (W5100) [Aliexpress (where I bought it)] + Teleduino.

The problem is here; when I have shield connected to my router and I power up arduino, the control led of Teleduino will flash 3 times (that means it’s trying to connect to server) and after about 2 minutes it starts blinking till I power of arduino.

In the arduino I have uploaded “Teleduino2560EthernetClientProxy” , mac adress changed and key changed to my key from teleduino. I have Dhcp and Dns on true.

This is sketch I’m using:

#include <EEPROM.h>
#include <Servo.h>
#include <Wire.h>
#include <Teleduino2560.h>

#include <SPI.h>
#include <Ethernet.h>

// User configurable variables
byte useDhcp = true;
byte useDns = true;
byte mac[] = { 0x53, 0x4E, 0x6B, 0x2C, 0x37, 0x6C };
IPAddress deviceIp(192, 168, 1, 100); // Only if useDhcp is false
IPAddress gatewayIp(192, 168, 1, 1); // Only if useDhcp is false
IPAddress dnsIp(192, 168, 1, 1); // Only if useDhcp is false
IPAddress subnet(255, 255, 255, 0); // Only if useDhcp is false
IPAddress serverIp(173, 230, 152, 173); // Only if useDns is false
char serverName[] = "us01.proxy.teleduino.org"; // Only if useDns is true
unsigned int serverPort = 5353; // Can be set to either 53 or 5353
byte statusLedPin = 8;

// User configurable key, this is used to authenticate with the proxy server
// This is checked against the EEPROM on boot and written if necessary
// The proxy server retreives the key from the EEPROM
byte key[] = { censured };

// Other required variables
byte data[257];
byte dataLength;
byte hexStage;
unsigned long lastInstruction = 0;
unsigned long lastRefresh = 0;
byte stage = 0;

// Declare client object
EthernetClient Client;

void setup()
{
  // This is here purely to add a bit of padding to the sketch to fix a compiling bug
  // http://arduino.cc/forum/index.php/topic,60649.0.html
  EEPROM.read(0);
 
  // Load presets
  Teleduino2560.loadPresets();
 
  // Set status LED pin
  Teleduino2560.setStatusLedPin(statusLedPin);

  Teleduino2560.setStatusLed(1); // Initialisation
  // Check the EEPROM header and check to see if the key is correct
  // This is to ensure the key is not cleared from the EEPROM
  if(EEPROM.read(0) != '#')
  {
    EEPROM.write(0, '#');
  }
  if(EEPROM.read(1) != 1)
  {
    EEPROM.write(1, 1);
  }
  if(EEPROM.read(2) != '#')
  {
    EEPROM.write(2, '#');
  }
  if(EEPROM.read(395) != '#')
  {
    EEPROM.write(395, '#');
  }
  for(byte i = 0; i < 16; i++)
  {
    if(EEPROM.read(396 + i) != key[i])
    {
      EEPROM.write(396 + i, key[i]);
    }
  }
  if(EEPROM.read(412) != '#')
  {
    EEPROM.write(412, '#');
  }

  // Start network and attempt to connect to proxy server
  Teleduino2560.setStatusLed(2); // Network configuration
  if(useDhcp)
  {
    if(!Ethernet.begin(mac))
    {
      Teleduino2560.setStatusLed(2, false, 10000);
      Teleduino2560.reset();
    }
  }
  else
  {
    Ethernet.begin(mac, deviceIp, dnsIp, gatewayIp, subnet);
  }
  delay(1000);

  Teleduino2560.setStatusLed(3); // Connect to server
  if((useDns && !Client.connect(serverName, serverPort)) || (!useDns && !Client.connect(serverIp, serverPort)))
  {
    Teleduino2560.setStatusLed(3, false, 10000);
    Teleduino2560.reset();
  }
  lastInstruction = millis();
}

void loop()
{
  if(Client.connected())
  {
    // What we need to do depends on which 'stage' we are at
    switch(stage)
    {
      case 0: // Wait for start byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage++;
          }
        }
        break;
      case 1: // Reset variables
        dataLength = 0;
        hexStage = 0;
        stage++;
        break;
      case 2: // Instruction byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            stage = 0;
            break;
          }
          if(!hexStage)
          {
            data[0] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[0] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            stage++;
          }
        }
        break;
      case 3: // Data length byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            stage = 0;
            break;
          }
          if(!hexStage)
          {
            data[1] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[1] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            stage++;
          }
        }
        break;
      case 4: // Data
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            if(dataLength == data[1])
            {
              stage++;
              break;
            }
            else
            {
              stage = 0;
              break;
            }
          }
          if(!hexStage)
          {
            data[2 + dataLength] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[2 + dataLength] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            dataLength++;
          }
        }
        break;
      case 5: // Execute instruction and return result
        Teleduino2560.instruction(data);
        Client.write('!');
        for(int i = 0; i < data[1] + 2; i++)
        {
          Client.write(Teleduino2560.hexEncode(data[i] / 16));
          Client.write(Teleduino2560.hexEncode(data[i] % 16));
        }
        Client.write('\n');
        lastInstruction = millis();
        stage = 0;
        break;
    }
  }
  else
  {
    Teleduino2560.setStatusLed(10);
    Teleduino2560.reset();
  }

  // Has the instruction timeout been reached?
  if(millis() - lastInstruction > 30000)
  {
    Client.flush();
    Client.stop();
    Teleduino2560.setStatusLed(9);
    Teleduino2560.reset();
  }

  // Process refreshes every 50ms
  if(millis() - lastRefresh >= 50)
  {
    Teleduino2560.pinTimers();
    Teleduino2560.shiftRegisterTimers();
    Teleduino2560.shiftRegisters();
    lastRefresh = millis();
  }

  // Check to see if reset has been requested
  Teleduino2560.checkReset();
}

Im totaly beginner so,
please help me. :frowning:

Yes, sorry it should be Serial.println.

Threads merged. @zige, please do not cross-post.

countrypaul:
Yes, sorry it should be Serial.println.

Sorry for soooooo long pause between reply. (i had school problems)

I think, I cant use Serial communication because, it's in use by that ethernet shield.

The Ethernet shield should not normally interefere with the Serial communications. Have you got anything else connected to the Mega?

I notice you have several other libraries included that are not used in the sketch, I suggest you remove or comment these out to simplify the code we are looking at.

countrypaul:
Have you got anything else connected to the Mega?

No I havent. (just 4 leds+ 1 control led)

countrypaul:
I notice you have several other libraries included that are not used in the sketch, I suggest you remove or comment these out to simplify the code we are looking at.

I'm using just example sketch just with different mac address and key.
So which one u think?

I'm speculating if the teleduino servers are still online.

What are the 4 leds connected to, the control led is on pin8 I assume.

The teleduino server can certainly be pinged so appears to still be there.

Can you ping the teleduino server from your PC?

I know this sounds obvious, but can you check the IP address of your router make sure it is the sam as your code.

countrypaul:
What are the 4 leds connected to, the control led is on pin8 I assume.

The teleduino server can certainly be pinged so appears to still be there.

Can you ping the teleduino server from your PC?

I know this sounds obvious, but can you check the IP address of your router make sure it is the sam as your code.

4 leds just for test, but i can disconnect them.

I dont know what do u think as the ping but I tried this in cmd:

C:\Users\honza>ping us01.proxy.teleduino.org

Pinging us01.proxy.teleduino.org [173.230.152.173] with 32 bytes of data:
Reply from 173.230.152.173: bytes=32 time=179ms TTL=52
Reply from 173.230.152.173: bytes=32 time=178ms TTL=52
Reply from 173.230.152.173: bytes=32 time=178ms TTL=52
Reply from 173.230.152.173: bytes=32 time=178ms TTL=52

Ping statistics for 173.230.152.173:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 178ms, Maximum = 179ms, Average = 178ms

I have Dhcp on true so it connect it automaticaly.

And now the news; I connected arduino to my computer and watched serial com. and this is the result:

Loaded Presets
Set status LED Pin
Set status LED 2
  • nothing more

I can't see where those lines on the serial output come from having looked through the teleduino example fles and library files.

Also why Set status LED 2 when your code has: byte statusLedPin = 8;
[Edit] Just realised it does not mean pin 2 - sorry.

The ping looks fine to me.

What are the 4 LEDs connected to - please do not change them for the moment.

I am wondering if you have more than one instance of some files on your PC and it is not using the ones you expect - seems unlikely though.

countrypaul:
I can’t see where those lines on the serial output come from having looked through the teleduino example fles and library files.

Some time ago u sent me this (so I used that):

void setup()
{
  // This is here purely to add a bit of padding to the sketch to fix a compiling bug
  // http://arduino.cc/forum/index.php/topic,60649.0.html
  EEPROM.read(0);
 
  // Load presets
  Teleduino2560.loadPresets();
 
PrintLn ("Loaded Presets");

  // Set status LED pin
  Teleduino2560.setStatusLedPin(statusLedPin);

PrintLn ("Set status LED Pin");

  Teleduino2560.setStatusLed(1); // Initialisation
  // Check the EEPROM header and check to see if the key is correct
  // This is to ensure the key is not cleared from the EEPROM
  if(EEPROM.read(0) != '#')
  {
    EEPROM.write(0, '#');
  }
  if(EEPROM.read(1) != 1)
  {
    EEPROM.write(1, 1);
  }
  if(EEPROM.read(2) != '#')
  {
    EEPROM.write(2, '#');
  }
  if(EEPROM.read(395) != '#')
  {
    EEPROM.write(395, '#');
  }
  for(byte i = 0; i < 16; i++)
  {
    if(EEPROM.read(396 + i) != key[i])
    {
      EEPROM.write(396 + i, key[i]);
    }
  }
  if(EEPROM.read(412) != '#')
  {
    EEPROM.write(412, '#');
  }

  // Start network and attempt to connect to proxy server
  Teleduino2560.setStatusLed(2); // Network configuration

PrintLn ("Set status LED 2");


  if(useDhcp)
  {
    if(!Ethernet.begin(mac))
    {
      Teleduino2560.setStatusLed(2, false, 10000);
      Teleduino2560.reset();
    }
  }
  else
  {
    Ethernet.begin(mac, deviceIp, dnsIp, gatewayIp, subnet);
  }
  delay(1000);

PrintLn ("Ethernet begin");


  Teleduino2560.setStatusLed(3); // Connect to server

PrintLn ("Set status LED 3");


  if((useDns && !Client.connect(serverName, serverPort)) || (!useDns && !Client.connect(serverIp, serverPort)))
  {
    Teleduino2560.setStatusLed(3, false, 10000);
    Teleduino2560.reset();
  }
  lastInstruction = millis();

PrintLn ("Client connect");
}

But from the serial communication i realized the problem is in the Ethernet.begin(mac);
(edit) I think that returns false so it will not continue and reset (so it will try connect again, again, again, again,…

countrypaul:
The ping looks fine to me.

Ok.

countrypaul:
What are the 4 LEDs connected to - please do not change them for the moment.

I had connected just 4 led to test the teleduino control of light in my house, but then i found out the control LED and the connect problem. So they are just connected to the pins with some resistors. I can disconnect them for a while, then i fix the connection problem. Just to test. They aren’t the problem why the connection does not work.

OK, I was assuming the last sketch you posted was what generated those messages probabl becasue they seemed familiar - obviously in hindsight my mistake.

I think you should add some more println statements to determine exactly where the path is and which ethernet.begin is being called rather than assuming. I can't see a loop there that would cause the ethernet.begin to be called multiple times so I wonder if it getting stuck in either the ethernet.begin or one of the calls to a teleduino function? The Println statements should give us a large clue.

I had connected just 4 led to test the teleduino control of light in my house, but then i found out the control LED and the connect problem. So they are just connected to the pins with some resistors. I can disconnect them for a while, then i fix the connection problem. Just to test. They aren't the problem why the connection does not work.

Which pins are they connected to? If they turn out to be ones used by the Ethernet that could explain the probem for example.

countrypaul:
I think you should add some more println statements to determine exactly where the path is and which ethernet.begin is being called rather than assuming.

I I’ve already done this and found out the exact location of the error.
This is my code with the error indicated:

#include <EEPROM.h>
#include <Servo.h>
#include <Wire.h>
#include <Teleduino2560.h>

#include <SPI.h>
#include <Ethernet.h>

// User configurable variables
byte useDhcp = true;
byte useDns = true;
byte mac[] = { 0x53, 0x4E, 0x6B, 0x2C, 0x37, 0x6C };
IPAddress deviceIp(192, 168, 1, 100); // Only if useDhcp is false
IPAddress gatewayIp(192, 168, 100, 1); // Only if useDhcp is false
IPAddress dnsIp(208, 67, 222, 222); // Only if useDhcp is false
IPAddress subnet(255, 255, 255, 0); // Only if useDhcp is false
IPAddress serverIp(173, 230, 152, 173); // Only if useDns is false
char serverName[] = "us01.proxy.teleduino.org"; // Only if useDns is true
unsigned int serverPort = 5353; // Can be set to either 53 or 5353
byte statusLedPin = 8;

// User configurable key, this is used to authenticate with the proxy server
// This is checked against the EEPROM on boot and written if necessary
// The proxy server retreives the key from the EEPROM
byte key[] = { 0xEE, 0x57, 0xE8, 0x33,
               0x0B, 0x6A, 0xF8, 0xB9,
               0x08, 0xDC, 0xB9, 0x04,
               0x31, 0x62, 0x69, 0xBB };

// Other required variables
byte data[257];
byte dataLength;
byte hexStage;
unsigned long lastInstruction = 0;
unsigned long lastRefresh = 0;
byte stage = 0;

// Declare client object
EthernetClient Client;

void setup()
{
  Serial.begin(9600);
  // This is here purely to add a bit of padding to the sketch to fix a compiling bug
  // http://arduino.cc/forum/index.php/topic,60649.0.html
  EEPROM.read(0);
 
  // Load presets
  Teleduino2560.loadPresets();
 
Serial.println ("Loaded Presets");

  // Set status LED pin
  Teleduino2560.setStatusLedPin(statusLedPin);

Serial.println ("Set status LED Pin");

  Teleduino2560.setStatusLed(1); // Initialisation
  // Check the EEPROM header and check to see if the key is correct
  // This is to ensure the key is not cleared from the EEPROM
  if(EEPROM.read(0) != '#')
  {
    EEPROM.write(0, '#');
  }
  if(EEPROM.read(1) != 1)
  {
    EEPROM.write(1, 1);
  }
  if(EEPROM.read(2) != '#')
  {
    EEPROM.write(2, '#');
  }
  if(EEPROM.read(395) != '#')
  {
    EEPROM.write(395, '#');
  }
  for(byte i = 0; i < 16; i++)
  {
    if(EEPROM.read(396 + i) != key[i])
    {
      EEPROM.write(396 + i, key[i]);
    }
  }
  if(EEPROM.read(412) != '#')
  {
    EEPROM.write(412, '#');
  }

  // Start network and attempt to connect to proxy server
  Teleduino2560.setStatusLed(2); // Network configuration

Serial.println ("Set status LED 2");

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  if(useDhcp)
  {
    if(!Ethernet.begin(mac))  //////////////////////////// - this command returns false so it do the code in { }
    {
      Teleduino2560.setStatusLed(2, false, 10000);
      Teleduino2560.reset();  //////////////////////////// - and this command reset all and reapeat all the code, this is the last command, where the code will get. It won't do anything further.
    }
  }
  else
  {
    Ethernet.begin(mac, deviceIp, dnsIp, gatewayIp, subnet);
  }
  delay(1000);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Serial.println ("Ethernet begin");


  Teleduino2560.setStatusLed(3); // Connect to server

Serial.println ("Set status LED 3");


  if((useDns && !Client.connect(serverName, serverPort)) || (!useDns && !Client.connect(serverIp, serverPort)))
  {
    Teleduino2560.setStatusLed(3, false, 10000);
    Teleduino2560.reset();
  }
  lastInstruction = millis();
  
Serial.println ("Client connect");
}
void loop()
{
  if(Client.connected())
  {
    // What we need to do depends on which 'stage' we are at
    switch(stage)
    {
      case 0: // Wait for start byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage++;
          }
        }
        break;
      case 1: // Reset variables
        dataLength = 0;
        hexStage = 0;
        stage++;
        break;
      case 2: // Instruction byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            stage = 0;
            break;
          }
          if(!hexStage)
          {
            data[0] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[0] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            stage++;
          }
        }
        break;
      case 3: // Data length byte
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            stage = 0;
            break;
          }
          if(!hexStage)
          {
            data[1] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[1] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            stage++;
          }
        }
        break;
      case 4: // Data
        if(Client.available())
        {
          char c = Client.read();
          if(c == '?')
          {
            stage = 1;
            break;
          }
          else if(c == '\r' || c == '\n' || c == '.')
          {
            if(dataLength == data[1])
            {
              stage++;
              break;
            }
            else
            {
              stage = 0;
              break;
            }
          }
          if(!hexStage)
          {
            data[2 + dataLength] = Teleduino2560.hexDecode(c) * 16;
          }
          else
          {
            data[2 + dataLength] += Teleduino2560.hexDecode(c);
          }
          hexStage = !hexStage;
          if(!hexStage)
          {
            dataLength++;
          }
        }
        break;
      case 5: // Execute instruction and return result
        Teleduino2560.instruction(data);
        Client.write('!');
        for(int i = 0; i < data[1] + 2; i++)
        {
          Client.write(Teleduino2560.hexEncode(data[i] / 16));
          Client.write(Teleduino2560.hexEncode(data[i] % 16));
        }
        Client.write('\n');
        lastInstruction = millis();
        stage = 0;
        break;
    }
  }
  else
  {
    Teleduino2560.setStatusLed(10);
    Teleduino2560.reset();
  }

  // Has the instruction timeout been reached?
  if(millis() - lastInstruction > 30000)
  {
    Client.flush();
    Client.stop();
    Teleduino2560.setStatusLed(9);
    Teleduino2560.reset();
  }

  // Process refreshes every 50ms
  if(millis() - lastRefresh >= 50)
  {
    Teleduino2560.pinTimers();
    Teleduino2560.shiftRegisterTimers();
    Teleduino2560.shiftRegisters();
    lastRefresh = millis();
  }

  // Check to see if reset has been requested
  Teleduino2560.checkReset();
}

It just won’t get beyond if(!Ethernet.begin(mac)) {}.

countrypaul:
Which pins are they connected to? If they turn out to be ones used by the Ethernet that could explain the probem for example.

They are connected to pin from 1 to 4. But when I unpinned them, nothing else will happend.

I think the problem is in Ethernet.begin(mac); because it is not normal when it return false. Normally it returns true, I think.

I suppose the obvious thing to try is setting useDhcp to False.

Also check the return status from the Ethernet.begin and issue a println to see if that too fails.

Failing that, I think it would be worth trying a simple Ethernet example such as

Arduino>libraries>Ethernet>examples>UdpNtpClient

This will test the Ethernet connection and also the router connection so we know not just whether it will work on you ethernet but also if it will connect to the internet.

countrypaul:
I suppose the obvious thing to try is setting useDhcp to False.

I tried, but I dont know what to set as DnsIP and Device IP.

countrypaul:
Also check the return status from the Ethernet.begin and issue a println to see if that too fails.

I used this: println(Ethernet.begin(mac));
And the value was 0.

countrypaul:
Failing that, I think it would be worth trying a simple Ethernet example such as

Arduino>libraries>Ethernet>examples>UdpNtpClient

This will test the Ethernet connection and also the router connection so we know not just whether it will work on you ethernet but also if it will connect to the internet.

I tried it and it works. This is result:
Seconds since Jan 1 1900 = 3810292707
Unix time = 1601303907
The UTC time is 14:38:27

and every cca 10 sec it will update