Go Down

Topic: Problem with Arduino+EthernetShield and NAT (Read 2831 times) previous topic - next topic

mapiram


I thought that script worked for you. It did not?


The WebClient example works.


Using a PC like a router? How do you have that set up? Are you using internet connection sharing? And that works ok? But the Arduno does not receive if connected directly to the ADSL modem. Correct?


Yes. Perfect. It is.


Add: My cablemodem does not like new devices. If I use a device with a different mac address using the same ip as a previous device, I must power down my cablemodem for several seconds, and then power it up again to get it to work. It has to do with the arp register in the cablemodem. Maybe your ADSL modem is the same.


It is a new thing to try. I'll done. Thanks!

SurferTim

#16
Oct 12, 2011, 11:55 am Last Edit: Oct 12, 2011, 12:09 pm by SurferTim Reason: 1
The WebClient sketch works connected directly to the ADSL modem?

I forgot to mention one thing. If the ISP issues ips by dhcp, you might have a problem. If the device does not get its ip by dhcp, it may not be able to communicate with the ADSL modem.

Does your computer (the one you use for the connection sharing) get its public ip by dhcp? Or is it a static assignment?

mapiram


The WebClient sketch works connected directly to the ADSL modem?

Yes, it works. Make it more complex, it doesn't work. For example if you try to move the requests in the loop.


I forgot to mention one thing. If the ISP issues ips by dhcp, you might have a problem. If the device does not get its ip by dhcp, it may not be able to communicate with the ADSL modem.

Does your computer (the one you use for the connection sharing) get its public ip by dhcp? Or is it a static assignment?


That computer doesn't get a public IP, it gets the IP by DHCP from the same ADSL router raising the Ethernet problem (it is also a WiFi router)

SurferTim

#18
Oct 12, 2011, 03:57 pm Last Edit: Oct 12, 2011, 05:42 pm by SurferTim Reason: 1
This is a little more complex. It does not load the webpage on startup. You press 'r' and enter key to load.
You can load multiple times by pressing 'r' again. I would pick a webpage that is not so large tho. That Google download is a big one!
Code: [Select]

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0,5 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };

byte server[] = { 74,125,65,99 }; // Google

Client client(server, 80);

void setup() {
 Ethernet.begin(mac, ip, gateway, subnet);
 Serial.begin(9600);
 delay(2000);
 Serial.println("Ready. Press 'r' and enter to load page.");
}

void loop()
{
 if(Serial.available())
 {
   int inByte = Serial.read();
   if( inByte == 'r') getPage();
 }  
}

void getPage()
{
  int inChar;

 Serial.print("connecting...");

 if (client.connect())
  {
   Serial.println("connected");
   client.write("GET / HTTP/1.0\r\n\r\n");
 }
 else
 {
   Serial.println("failed");
   return;
 }

 while(client.connected())
 {
   while (client.available())
    {
     inChar = client.read();
     Serial.write(inChar);
   }
 }

 Serial.println();
 Serial.println("disconnecting.");
 client.stop();
}


Edit: This new code should be a little more patient about delays.

mapiram

#19
Oct 17, 2011, 11:15 am Last Edit: Oct 17, 2011, 11:57 am by mapiram Reason: 1
SurferTim, thanks.
Your sketch and an advice by the Italian forum drove me on the solution. It doesn't works but with a little change it was fine:
waiting a response for 5 seconds or until a data is available.
I don't understand because such a problem was done only in case of direct connection on adsl router and not other cases (the pc used like router should be a worse situation ...)
Anyway, it's fixed and energies will be for the next problem  :)

Code: [Select]

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

byte mac[6] = { 0x90, 0xA2, 0xDA, 0x00, 0x36, 0x01 };
byte ip[4] = { 192, 168, 0, 4 };
const char url[]="/";


byte server[4] = { 74,125,65,99 };
Client client(server, 80);
int i;
int waiting;
char c;

void setup() {
 for(i = 2; i < 10; i++){
   pinMode(i, OUTPUT);
   digitalWrite(i, LOW);
 }
 for(i = 14; i < 20; i++){
   pinMode(i, OUTPUT);
   digitalWrite(i, LOW);
 }
 Serial.begin(9600);
 Ethernet.begin(mac, ip);
 delay(1000);
}

void loop() {
 if (client.connect()) {
   client.print("GET ");
   client.print(url);
   client.println(" HTTP/1.1");
   client.println();
   Serial.println("           ");
   Serial.println("reading ...");

   waiting = 0;
   while (!client.available() && waiting < 5000){
      delay(1);
       waiting++;
   }

   while (client.available()){
       c = client.read();
       Serial.print(c);      
    }
 }
 client.stop();
 delay(1000);
 Ethernet.begin(mac, ip);
}



Go Up