Go Down

Topic: How to change size of readstring() in client.read() on Arduino Ethernet? (Read 3731 times) previous topic - next topic

SurferTim

I use the w5100 and SD card together a lot. That is how I get my files into my SD card. I use a FTP program to transfer them to and from a FTP server. That program may be a bit too memory heavy to use on an Uno or Arduino Ethernet tho. That is why I have a Mega.
http://playground.arduino.cc/Code/FTP
That sketch uses two sockets on the w5100 (one command and one data) and the SD together. Smooth!!  :)

nathanas

Is there a way to use this example with TCP packets?
Because I want to login a user of a specific IP.
I dont think UDP packets can be sent through html... I didn;t find anything on google..
Code: [Select]
#include <SPI.h>       
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD };
IPAddress ip(192, 168, 10, 110);

unsigned int localPort = 8870;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() {

  int packetSize = Udp.parsePacket();
  Serial.print("1 ");
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From IP : ");

    IPAddress remote = Udp.remoteIP();
    //print out the remote connection's IP address
    Serial.print(remote);

    Serial.print(" on port : ");
    //print out the remote connection's port
    Serial.println(Udp.remotePort());
  }

}

nathanas

How can I copy the "clientIP" value to another array? because I need to compare them afterwards..
I managed to receive the remote IP address but I don't know how to save it.

I declared "char loggedip[];"
and then I try to copy it like that... loggedip[0]=clientIP; but I get other values.
I read that IPAddress value is a unit32_t byte and when I try to strcpy it says I cannot convert it to char.

Can anyone help?

SurferTim

Here is how to use it.
Code: [Select]
IPAddress clientIP = Ethernet.localIP();
Serial.println(clientIP);
Serial.print(clientIP[0]);
Serial.print(".");
Serial.print(clientIP[1]);
Serial.print(".");
Serial.print(clientIP[2]);
Serial.print(".");
Serial.println(clientIP[3]);


nathanas

What I do:  
Code: [Select]
Serial.println(clientIP);
All I need is to store the IP to an array and then compare it again with the "clientIP" value
I dont care about the format as I can see it using serial.print

Something like that: "loggedip[]= clientIP[];"

PaulS

Quote
How can I copy the "clientIP" value to another array? because I need to compare them afterwards..
I managed to receive the remote IP address but I don't know how to save it.

Start with showing this code.

If you get the IP address as a string, that is not the same type as an IPAddress.

If you have the IP address as 4 bytes, you can simply declare two IPAddress variables, using the same 4 bytes.

If you want to copy the 4 byte array, use a for loop, and copy each element of the array.

nathanas

Thinking on going to something like this board!
But unfortunately it doesn't support 5V.. :(
http://grobotronics.com/arduino-due-el.html#.UMSDu4N9Dh4

As for the IP serial print code here it is below...
Code: [Select]
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
 0x00, 0x00, 0xAA, 0xBB, 0xCC, 0xDD };
IPAddress ip(192, 168, 10, 110);

// local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP

EthernetServer server(8070);
void setup() {
 Serial.begin(9600);
 // start the Ethernet and UDP:
 Ethernet.begin(mac,ip);

 server.begin();
}

void loop() {

 EthernetClient client = server.available();
 Serial.println("Waiting for client... ");
 if(client) {
   Serial.println("Client request!!!! ");
   while (client.connected()) {
     while(client.available()) {
       char c = client.read();
       if (c == '\n' ) {
         client.stop();
       }
       Serial.print("Received packet ...    ");

       Serial.print("From IP : ");

       IPAddress clientIP = client.remoteIP();
       //print out the remote connection's IP address
       Serial.println(clientIP);



     }
   }
 }
}



I've made some changes to the Ethernet.cpp/.h & EthernetClient.cpp/.h files so the "  IPAddress clientIP = client.remoteIP();" works.. The changes where made according to SurferTim's link I found from arduino forum.

SurferTim

This is how. Change testIP to another ip. It will not match.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

IPAddress clientIP(192,168,2,2);
IPAddress testIP(192,168,2,2);

void setup() {
 Serial.begin(9600);

 if(clientIP == testIP) Serial.println("match");
 else Serial.println("no match");

 if(clientIP[0] == testIP[0] && clientIP[1] == testIP[1] && clientIP[2] == testIP[2]) {
   Serial.println("Same subnet");
 }
 else Serial.println("different subnets");
}

void loop() {
}


Here is a firewall-type code using the array of IPAddress types. See reply #1.
http://arduino.cc/forum/index.php/topic,135082.0.html

edit: Here is the way I have this planned. One ip changes, the subnet views, and all others are rejected.
Code: [Select]
if(clientIP == testIP) {
 Serial.println("match");
 // get input from the GET, process, and send page with form inputs here
}
else if(clientIP[0] == testIP[0] && clientIP[1] == testIP[1] && clientIP[2] == testIP[2]) {
 Serial.println("Same subnet");
 // do not get input. send page with data, but no form
}
else {
 Serial.println("different subnets");
 // send 401 Unauthorized page
}


nathanas

I just bought my Arduino Mega 2560 today and an ethrnet shield. But unfortunately when I tried to compile the same sketch on the mega says:
Code: [Select]
c:/program files/arduino-1.0.2/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o: In function `__vector_default':
(.vectors+0x8): relocation truncated to fit: R_AVR_13_PCREL against symbol `__vector_2' defined in .text section in c:/program files/arduino-1.0.2/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o

SurferTim

What same sketch? Didn't I compile that for you to check memory? It did ok here. All my tests are on a Mega2560/ethernet shield with a uSD card and IDE v1.0.2. However, I use Ubuntu 11.10 and 12.04, not Windows.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy