Arduino MEGA & Ethernet Shield ENC28J60

Hello everybody, I am a new user of Arduino from Spain.

I have an Arduino MEGA2560 and a Ethernet Shied type ENC28J60, I have connected them & every is OK with this library:

http://iteadstudio.com/application-note/ethernet-shield-go-with-arduino-libraries/

I am able to do ping, like a server and like a client. But I need some suggestions. Let me explain my project:

I need to extract some information from the Microsoft FSX Simulator through the TCP protocol using the IOCP software. I have checked it usig LabView environment and it works fine. But I need not to use a PC, and use only the Arduino connected to a router.

It is easy, my Arduino needs to write to the server (192.2.1.2 port 8090) the next command: "Arn.Inicio:Var419"
After that, I must to read the incommnig messages "Arn.Var419:xxxxxx"

Must I connect the Arduino like a clent? I have tried (see bellow) this code but It doesn't work, what do you think?
Thanks you very much & My best regards.
Pedro

#include <Ethernet.h>

byte mac[] = { 0x00, 0x1B, 0xB9, 0x84, 0xCB, 0x60 };
byte ip[] = { 192, 2, 1, 4 };
byte server[] = { 192, 2, 1, 2 };

Client client(server, 8090);

void setup() {
Serial.begin(9600);
Serial.println("Iniciando conexion FSX");
delay(100);
Ethernet.begin(mac, ip);
delay(5000);
Serial.println("conextando a ip: 192.2.1.2.??????...");

if (client.connect()) {
Serial.println("FSX Conectado----->");
client.println("Arn.Inicio:Var419");
//client.println();
delay(10000);
}
else {
Serial.println("FSX NO conectado *********");
}
}

void loop() {
cnx:
delay (1000);
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("Reintentando");
goto cnx;

}
}

If there client is no client connected, goto the start of a function that will be called again, in an infinite loop anyway. Why?

I have tried (see bellow) this code but It doesn't work, what do you think?

I think you need to lean a little to the left. You are blocking your screen. We can't see what the program actually does, and you haven't told us.

A little farther. Still can't see.

A little more...

Thanks Paul,

I have simplified the code, still not working:

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 2, 1, 4 }; //my ip
byte server[] = { 192, 2, 1, 2 }; //FSX ip

Client client(server, 8090); //port must be 8090

void setup() {
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("Begin FSX connection");
}

void loop() {

client.println("Arn.Inicio:Var419"); //command from Arduino to FSX
client.println();
delay (10000);
rdx: // scan for responses from FSX
delay (100);
if (client.available()) {
char c = client.read();
Serial.print(c); //link TCP messages to serial (for debugging)
goto rdx;
}

}

void loop() {

  client.println("Arn.Inicio:Var419");       //command from Arduino to FSX
  client.println();
  delay (10000);
rdx:                                                 // scan for responses from FSX
  delay (100);
  if (client.available()) {
    char c = client.read();
    Serial.print(c);                 //link TCP messages to serial (for debugging)
    goto rdx;
  }
 
}

The client.available() function returns the number of characters available to read. Read them in a while loop, rather than inventing your own. The goto statement has very little going for it here.

I have simplified the code, still not working:

You have some expectation of what the code should do. There is something that the code does. All we can tell from the information you have provided is that those two are not the same thing. We have a pretty good idea of what you expect it to do. What it actually does is a mystery.

You need to clear up that mystery if you expect help.

hadnt come across that library. Looks very simple and similar to the original ethernet shield.
Are you using the ENC28J60 shield with it??
Are you using a gateway?
Ive tried this and does not connect, maybe you can give me a hint.,

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 88 };
byte server[] = { 74, 125, 224, 72 };
byte gateway[] = {192,168,1,1};

Client client(server, 80);

void setup() {
 // Ethernet.begin(mac, ip);
  Ethernet.begin(mac, ip, gateway);            //Setup ethernet client
  Serial.begin(9600);
  
  delay(1000);
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  }
  else {
    Serial.println("connection failed");
  }
}

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

All the examples included in the ibrary works fine. I have tried all of them with my ip's.

Tomorrow I will try your code, and i will tell you about it.

Regards.

My code is just the WebClient code that comes with the library you posted, but with a fixed gateway and changed server IP to call google.

These parts...

byte server[] = { 74, 125, 224, 72 };
byte gateway[] = {192,168,1,1};
 Ethernet.begin(mac, ip, gateway);            //Setup ethernet client

please let me know if this code works for you cause i dont understand what is happening.
this is the one i used with an Arduino UNO.
http://www.ebay.com/itm/170696120342?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649

The code you posted looks like code for the wiznet 5100 chip. Below is what seems to be the ENC28J60 type coding. If you got ping to work, then maybe not.

#include "etherShield.h"

static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24}; 
static uint8_t myip[4] = {192,168,1,15};
static char baseurl[]="http://192.168.1.15/";
static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)

zoomkat:
The code you posted looks like code for the wiznet 5100 chip. Below is what seems to be the ENC28J60 type coding. If you got ping to work, then maybe not.

With the library indicated at the beginning of the topic, is valid for ENC28J60.

Regards

Sergegsx:
My code is just the WebClient code that comes with the library you posted, but with a fixed gateway and changed server IP to call google.

These parts...

byte server[] = { 74, 125, 224, 72 };

byte gateway[] = {192,168,1,1};
Ethernet.begin(mac, ip, gateway);            //Setup ethernet client



please let me know if this code works for you cause i dont understand what is happening.
this is the one i used with an Arduino UNO.

I suppose that if you want to go out to the internet, without a router, through your PC, you need a crossover ethernet cable, take a look for this:

Regards

Perico2005:

Sergegsx:
My code is just the WebClient code that comes with the library you posted, but with a fixed gateway and changed server IP to call google.

These parts...

byte server[] = { 74, 125, 224, 72 };

byte gateway[] = {192,168,1,1};
Ethernet.begin(mac, ip, gateway);            //Setup ethernet client



please let me know if this code works for you cause i dont understand what is happening.
this is the one i used with an Arduino UNO.

I suppose that if you want to go out to the internet, without a router, through your PC, you need a crossover ethernet cable, take a look for this:
kiranjose.com

Regards

no, i want to go out to the internet but i am using a router that has ip 192.168.1.1 thats why i need the gateway cause if not it does not find the router (same happens in my computers) i need to specify the routers ip.