Sending HTTP: get response to a server, code for dnsExit.com IP dynamic DNS upda

Here is working code for sending an update response to the dnsExit.com server. If others have useful modifications please post, I could use examples of other applications. This is a code fragment.

/*
Web client
based on:
using an Arduino Wiznet Ethernet shield.

Modified to connect to dnsExit.com no-ip service
Provides template for sending http:get and http: requests to a server from Arduino Ethernet
Out of respect to dnsexit.com and their users it is important to limit the frequency of requests.
Once the additional code is included this will check every 8 hours
Modified 9/15/2012
By: Tom Northen
*/

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0xBB, 0xAA };
char dnsexitServer[] = "67.214.161.141";
//char dnsexitServer[] ="http://update.dnsexit.com";

EthernetClient client;

void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}

//Give time to initilize:
delay(2000);

if (client.connect(dnsexitServer, 80)) {
Serial.println("connected");
Serial.println(" Making an HTTP request:");

delay(4000);

client.println("GET /RemoteUpdate.sv?login=yourUserID&password=yourPassword&host=yourDomainName.com&myip= HTTP/1.1"); //ip not required
//login, password, URL, and ip are not in quotes
//their literature showing host.domain.com seems incorrect
client.println("Host: update.dnsexit.com");
client.println("User-Agent: User-Agent: Arduino Sketch/1.0 user@host.com");

client.println("Accept: text/htm"); //other parameters may be included but not necessary
client.println("Accept-Language: en-us,en;q=0.5");
client.println("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
client.println("Connection: close"); // keep-alive not required as noted by following post
client.println("Referer: http://update.dnsexit.com/RemoteUpdate.sv?login=yourUserID&password=yourPassword&host=yourDomainName.com&myip= ");
client.println("Accept-Encoding: gzip, deflate");

delay(1000);

client.println("");
Serial.println("finished sending http://update string");
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
for(;:wink:
;
}
}

I don't have code ready but it may be a good idea to read the server's response and act in the case of an error, don't you think?

Thanks for thinking about the problem for me, and yes at the least it should go to an alternate site or shorten the time before retrying. I'm working on that and may need more help.

and yes at the least it should go to an alternate site or shorten the time before retrying.

Retrying? Your sketch runs exactly once and will never retry, even not in the case of an error.

Another problem (from the view of the service provider): You're opening a keep-alive connection but you're not closing it but wait for a timeout on the server side. This way you're wasting resources. For what you're doing an HTTP/1.0 connection is enough and there the keep-alive option should be ignored anyway but nevertheless you should not send it.

Where do you have the IP from? If it's fixed you wouldn't need the service so it must be dynamically assigned but you wrote it directly into a constant string.

You should store the constant strings into program memory and use the F() macro for all print() and println() calls. This saves you RAM that you may need otherwise.

Thanks for suggesting eliminating the keep-alive. It is what the dnsexit.com form uses but thanks to you I am now doing it better. I had quite a bit of trouble getting their server to recognize my request. After going down many roads this finally worked.

According to dnsexit literature my transmitted ip is ignored, I should have posted the version with it blank, sorry, corrected now.

The posted code is for anyone who has had the same connection issues (this is a very different world for some of us). I hope to have the arduino monitor the environment and be able to serve me back a simple page with some very basic data. All that is yet to be written.

You suggested saving space using a print function. The next piece for me is to install the SD memory chip on the ethernet card. Is it not possible to use this as program space? If you have a good reference for me that would be super :slight_smile:

Is it not possible to use this as program space?

No, the program must reside in the flash on the microcontroller. But you can store any other information there, as strings, values, any kind of data.

You suggested saving space using a print function.

No, you are currently using the print() method. But if you use the F() macro for constant string parameters for that method, you're saving RAM. Example:

Serial.println("connected");

gets

Serial.println(F("connected"));

Nothing more to do and this change saved you 10 bytes of RAM. Not that much for a PC but a microcontroller with only 2048 bytes of RAM this is almost half a percent. Guess the savings if you change all constant strings this way.

thanks, you are helping make it a better system for everyone.