Sketch: #2

What Is My Ip


Fetch remote ip
post remote ip info to website
sleep aproximatly 13 minutes to prevent spam.
repeat

The next version will implement authentication.

I need to work on the formatting of the code block, but the base is there for now.

                ---- BEGIN ----

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


int current_count = 9000;   //set counter high to force an initial run.
int max_count = 60 * 11;    //sleeping for about 11 minutes

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// initialize the library instance:
EthernetClient client;

bool request_to_die = false;
char get_ip_from_this_server[] = "checkip.dyndns.com"; // test web page server
char server[] = "138.197.230.242";  // also change the Host line in httpRequest()
unsigned long lastConnectionTime = 0;           // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10 * 1000;  // delay between updates, in milliseconds

void setup() {
  //Ethernet.init(10);  // Most Arduino shields

  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
    Serial.print("My IP address: ");
    Serial.println(Ethernet.localIP());
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }
  
  if (millis() - lastConnectionTime > postingInterval) {
    if (current_count < max_count){
      current_count += 1;
      delay(1000);
      Serial.print("[");
      Serial.print(current_count);
      Serial.print("/");
      Serial.print(max_count);
      Serial.print("] ");
      Serial.println("Sleeping for a moment.");
    }else{
      Serial.println("Prepping to post!");
      current_count = 0; //reset current count
      httpPost();
    }
  }
}

// this method makes a HTTP connection to the server:
void httpPost() {
  client.stop();
  String appartment_ip = get_external_ip();
  String data;
  data+="sensor_model=Arduino Uno (With Ethernet Shield)";
  data+="&sensor_location=Apartment - Bedroom";
  data+="&message="+ appartment_ip; // Submitting data

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("POST /api/motion_readings/ HTTP/1.1");
    client.println("Host: 138.197.230.242");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

String get_external_ip(){
  request_to_die = true;
  String byte_string = "";
  
  if (client.connect(get_ip_from_this_server, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    //Serial.print(c); //prints byte to serial monitor 

    byte_string.concat(String(c));
  }

  String remote_ip = split_string(byte_string);
  
  //client.stop(); //stop client
  delay(1000);

  return remote_ip;
}

String split_string(String input_string){
  String result = "127.9.9.1";

  int starting_index = input_string.indexOf("Address: ");
  int ending_index = input_string.indexOf("</body></html>");

  //shift the substring
  result = input_string.substring(starting_index + 9, ending_index);

  return result;
}