Hi to everyone :),
I've just started my adventure with Arduino and I'm new in this Forum and I'm having some little problem with the EthernetServer.
What I'd like to do is to update the NO-IP address and then display my external and internal address on a web page.
At the moment, seems the connection to NO-IP is working but I'm not sure it is the wright way to do it but the webpage part doesn't.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC ADD OF ETHERNET SHEILD
byte ip[] = { 192, 168, 1, 168 }; // SET AN IP
byte gateway[] = { 192, 168, 1, 254 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; // subnet mask
char server1[] = "checkip.dyndns.org"; // THIS WOULD GIVE US THE EXTERNAL IP ITS NEEDED SO WE CAN UPDATE THE EXTERNAL IP ONLY IF THERE IS A CHANGE IN IP
char server2[] = "dynupdate.no-ip.com"; // NO-IP SERVER
int i = 0;
int j = 0;
String readString;
String rawip = "\0";
String ipnew;
String ipold = "111.255.155.27"; // THIS IS JUST FOR COMPARISON PURPOSE (WITH IPNEW AND IPOLD)
float hum; // Stores humidity value
float temp; // Stores temperature valu
EthernetServer server(84); // server port
/*Clients declaration*/
EthernetClient client, client1, client2 ;
void setup() {
Serial.begin(9600); // enable serial data print
while (!Serial){
;
}
/* start the Ethernet connection:*/
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
/* give the Ethernet shield a second to initialize: */
delay(1000);
Serial.println("connecting...");
getip(); // THIS FUNCTION GET THE HTML FROM THE CHECKIP.DYNDNS.ORG
server.begin();
}
void loop(){
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
rawip="\0";
j = 0;
getip(); // THIS FUNCTION GET THE HTML FROM THE CHECKIP.DYNDNS.ORG
delay(3000);
/*================================================================================================
CLIENT
================================================================================================*/
while (client.available()) { //THIS WHILE LOOP REMOVES THE UNNECESSARY CHARACTERS FROM THE CHAR C
char c = client.read();
if ( c =='<'){
j++;
}
if ( j >= 1){
rawip = rawip + c; //GETTING ONLY THE <HTML> TAGS AND IP
}
Serial.print(c); // PRINTS THE WHOLE THING FROM THE LIKE SERVER AND STUFF
}
Serial.println("html:");
Serial.println(rawip);
ipnew ="\0";
ipnew =dec_rawip(rawip); // dec_rawip() function removes all other texts and tags and gets only the ip address from rawip
Serial.println(" New ip");
Serial.println(ipnew);
Serial.println("Old ip:");
Serial.println(ipold);
/* IF THE OLD IP IS NOT SAME AS THE NEW IP THE NEW IP HAS TO BE UPDATED TO NO-IP SERVER.. */
if (ipold != ipnew){
ipold = "\0";
ipold = ipnew;
Serial.println(" Changing old IP to:");
Serial.println(ipold);
update_ddns(); // THIS FUNCTION UPDATES THE NO-IP SERVER TO YOUR ACTUAL IP
while (client2.available()) { // THIS IS NOT WORKING I DONT KNOW WHYYYYYYYYYYYYYY >< BUT IT IS UPDATING THE IP..
char c = client.read();
Serial.print(c);
}
delay(5000);
} else{
Serial.println("Nothing to be changed"); // IF IP HASNT CHANGED THE NO-IP SERVER DOESNT NEED TO BE UPDATED
}
// if the server's disconnected, stop the client:X
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
delay(10000); //WAIT FOR SOME TIME
}
webpage();
}
/*===================================================================================
FUNCTIONS
=====================================================================================*/
void getip(){
/* if you get a connection, report back via serial:*/
if (client.connect(server1, 80)) {
Serial.println("connected");
/* Make a HTTP request:*/
client.println("GET / HTTP/1.0");
client.println("Host: www.checkip.dyndns.org");
client.println("Connection: close");
client.println();
}
}
/* GETTING THE IP FROM THE HTML STRING */
String dec_rawip( String ip){
int y = ip.length();
char myStr[y];
int i;
char c[20];
String str;
i = ip.length();
ip.toCharArray(myStr, i -1);
str ="\0";
for (i=0;i < sizeof(myStr) - 1; i++){
if (myStr[i] == '1' || myStr[i] == '2' ||myStr[i] == '3' ||myStr[i] == '4' ||myStr[i] == '5' ||myStr[i] == '6'||myStr[i] == '7'||myStr[i] == '8'||myStr[i] == '9'||myStr[i] == '0' || myStr[i] =='.' ){
c[i] = myStr[i];
str = str + c[i];
}
}
return(str);
}
/* UPDATES YOUR IP TO NO-IP */
void update_ddns(){
if (client2.connect(server2, 80)){
Serial.println("connected to no-ip");
client2.println("GET /nic/update?hostname=your_user_address.no-ip.org HTTP/1.0"); // replace 'your_user_address.no-ip.org' by your hostname e.g jimmyhendricks.no-ip.org
client2.println("Host: dynupdate.no-ip.com");
client2.println("Authorization: Basic aaaaaaaaaaaaaaaaaaaaaaa=="); // USERNAME AND PASSWORD BY ENCODING IT FIRST TO BASE64. GO TO https://www.base64encode.org/ AND TYPE YOUR USERNAME AND PASSWORD IN THE FORMAT username:password AND REPLACE 'dXNlcm5hbWU6cGFzc3dvcmQNCg==' BY THE ENCODED STRING
client2.println("User-Agent: Arduino_update_client/1.0 YOUREMAILADDRESS@gmail.com"); // REPLACE YOUREMAILADDRESS BY YOUR EMAIL ADDRESS :|
client2.println();
Serial.println("connected to no-ip ended");
}else {
Serial.println("connection failed");
}
client2.stop();
}
/* WEBPAGE */
void webpage(){
Serial.println("webpage");
if (client1.available()) {
Serial.println("+++ CLIENT 1 +++");
// Create a client connection
while (client1.connected()) {
if (client1.available()) {
Serial.println("+++ CLIENT 1 +++");
char c1 = client1.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c1;
}
//if HTTP request has ended
if (c1 == '\n') {
Serial.println("+++ CLIENT 1 +++ if HTTP request has ended");
//now output HTML data header
client1.println("HTTP/1.1 200 OK");
client1.println("Content-Type: text/html");
client1.println();
client1.println("<HTML>");
client1.println("<HEAD>");
client1.println("<TITLE>Shroom GET test page</TITLE>");
client1.println("</HEAD>");
client1.println("<BODY>");
client1.println("<H1>SHROOM</H1>");
client1.println("</BODY>");
client1.println("</HTML>");
client1.println("<H1>IP : </H1>");
Serial.println("+++ CLIENT 1 +++ HTTP ended");
delay(1000);
//stopping client1
client1.stop();
}
}
}
}
client1.stop();
}