Accessing & updating IP add on DYNDNS

arduino4fun
Site Admin
Posts: 37
Joined: Mon Jun 06, 2016 1:16 pm

Accessing & updating IP add on DYNDNS

Postby arduino4fun » Tue Jun 07, 2016 10:09 am

Code: Select all

#include "DYNDNS.h"
#include "base64.h"

extern "C"
{
  #include "user_interface.h"
}

DYNDNS_c::DYNDNS_c( )
{
   ipcheck = DDNS_CHECK_IP;
  checkPort = DDNS_CHECK_PORT;
}

void DYNDNS_c::init( char* pSSId, char* pPswd, char* pUser, char* pPwd, char* pHost )
{
  pssid = pSSId;
  ppswd = pPswd;
 
   pusername = pUser;
   ppassword = pPwd;
   phostname = pHost;

  Serial.begin(9600);
  Serial.setDebugOutput(true);
   
  delay(1000);
  Serial.println(F("***********************************************"));
  Serial.println(F("DYNDNS on WeMos D1 Board by www.arduino4fun.com"));
  Serial.print(F("Build ")); Serial.print(build); Serial.print(F(".")); Serial.println(revision);
  Serial.print(F("Heap Size: \t"));    Serial.print(system_get_free_heap_size()/1024); Serial.println(F("KB"));
  Serial.print(F("Boot Version: \t")); Serial.println(system_get_boot_version());
  Serial.print(F("CPU Speed: \t"));    Serial.print(system_get_cpu_freq()); Serial.println(F("MHz"));
  Serial.print(F("SDK: ")); Serial.println(system_get_sdk_version());
  Serial.print(F("Chip ID: ")); Serial.println(system_get_chip_id());
  Serial.print(F("Flash ID: ")); Serial.println(spi_flash_get_id());
  //Serial.print(F("Vcc: ")); Serial.println(readvdd33()); //See more at: http://www.esp8266.com/viewtopic.php?p=24930#sthash.ssoRTZyQ.dpuf
  // mac ---------------------------------------------------------------------------------------------
  // See more at: http://www.esp8266.com/viewtopic.php?f=29&t=3587#sthash.E9Tlf3eG.dpuf
  Serial.println();

  //WiFi.macAddress(MAC_array);
  //for (int i = 0; i < sizeof(MAC_array); ++i)
  //{
  //  sprintf(MAC_char,"%s%02x:",MAC_char,MAC_array[i]);
  //}
 
  //Serial.println(MAC_char);
  Serial.println(F("***********************************************"));
 
  Serial.printf( "Connecting to %s ", pssid );
  WiFi.begin(pssid, ppswd);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.print(" with IP address of ");
  Serial.println(WiFi.localIP());
  Serial.println();   
}

// deleteHttpHeader(): remove all of the html header
void DYNDNS_c::deleteHttpHeader()
{
  if(result.endsWith("Content-Type: text/html"))
    result="";
}

void DYNDNS_c::update_dyndns()
{
  // https://help.dyn.com/remote-access-api/perform-update/
  // http://username:password@members.dyndns.org/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
  int timeout=0;
  String  connstring;
 
  Serial.print("connecting to : ");
  Serial.println(DDNS_CHECK_IP);

  Serial.print("Public IP : ");
  get_external_ip();
  Serial.println( externalIP );

  //if( matchfailIP==true )
  //  return;

  //if (client.connect(ddns, 80))
  if (client.connect(DDNS_CHECK_IP, 8245))
  {
    Serial.println("connected - connection string : ");

    //connstring = "GET /nic/update?hostname=" + phostname; // + "&myip=" + ipadsl + " HTTP/1.1 " + "Host: " + ddns + "Authorization: Basic " + puserpwdb64 + "User-Agent: arduino.com";
   
    client.print("GET /nic/update?hostname=");  Serial.print("GET /nic/update?hostname=");
    client.print(phostname);                    Serial.print(phostname);
    client.print("&myip=");                     Serial.print("&myip=");
    client.print(externalIP);                   Serial.print(externalIP);
    client.println(" HTTP/1.1 ");               Serial.println(" HTTP/1.1 ");
    client.print("Host: ");                     Serial.print("Host: ");
    client.println(ddns);                       Serial.println(ddns);

    client.print("Authorization: Basic ");      Serial.print("Authorization: Basic ");

    int le = strlen(pusername);
    le++; // ":"
    le += strlen(ppassword);

    char input[le];
    strcpy( input, pusername);
    strcat( input, ":" );
    strcat( input, ppassword );

    int inputLen = sizeof(input);
 
    int encodedLen = base64_enc_len(inputLen);
    char encoded64[encodedLen];
 
    //Serial.println( "##########################" );
    //Serial.print(input); Serial.print(" = ");
 
    // note input is consumed in this step: it will be empty afterwards
    base64_encode(encoded64, input, inputLen);
 
    //Serial.println(encoded);
    //Serial.println( "##########################" );
   
    client.println(encoded64);                    Serial.println(encoded64);

    client.println("User-Agent: DFRobot - domi at arduino4fun.com"); Serial.println("User-Agent: DFRobot - domi at arduino4fun.com");
    client.println();                             Serial.println();
  }
  else
  {
    Serial.println("connection failed");
  }

  while (!client.available() && timeout<50)
  {
    timeout++;
    //Serial.print("Time out");
    //Serial.println(timeout);
    //delay(100);
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  while (client.available())
  {
    if (client.available())
    {
      char c = client.read();
      Serial.print(c);
    }
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
}

void DYNDNS_c::get_external_ip()
{
  char c = ' ';
  //frequency to check ext IP
  //String delayvalue_str = "7200000"; // 2 hours
  //int delayvalue = 30000;
  char serverName[] = IP_ADD_WEB;

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

  // if you get a connection, report back via serial:
  if (client.connect(serverName, 80))
  {
    Serial.println("connected");
    //make HTTP request
    client.println("GET /");
    client.println();
    delay(1000);
  }//end if
  else
  { // you didn't get a connection to the server:
    Serial.println("connection failed");
  }//end else

  /* while (client.available()) {
    char c = client.read();
    Serial.print(c); */
  if (client.available())
  {
    TextFinder finder( client);
   
    if(finder.find("IP Address: "))
    {
      for (char k = 0; k < 17; k++)
      {
        c = client.read();
        if(c != '<')
        {
          Serial.print(c);
          externalIP[k] = c;
        } //endif
        else
          break;
      }//end for
    }//end if
  }//end if
  client.flush();
  client.stop();
 
  Serial.println();
  for (char k = 0; k < 17; k++)
  {
    if (lastExternalIP[k] != externalIP[k])
    {
      matchfailIP = true;
      break;
    }//end if
    else
      matchfailIP = false;
  } //end for
 
  if (matchfailIP == true)
  {
    Serial.println("DOES NOT MATCH");
//    sendemail();
  }
  else
  {
    Serial.println("MATHCHES");
  }
  Serial.println();
//  show_extip_lcd(); // display ext IP on LCD

  if( matchfailIP==true )
  {
    for(char j = 0; j < 17; j++)
    { //Serial.print(externalIP[j]);   
      lastExternalIP[j] = externalIP[j]; //assign current extIP to last extIP
    }// end for
  }
 
  Serial.println();
  Serial.println("disconnecting.");
  client.stop();

  //Serial.println("1#########################################");
  //Serial.println( externalIP );
  //Serial.println("#########################################");

//  delay(delayvalue);
}

void DYNDNS_c::show_external_ip()
{
  Serial.println("2#########################################");
  Serial.println( externalIP );
  Serial.println("#########################################");
}

Return to “WeMos D1 R2 WiFi ESP8266 Development Board”

Who is online

Users browsing this forum: No registered users and 1 guest