#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // Direccion MAC
byte ip[] = { 192, 168, 0, 150 }; // Direccion IP del Arduino
byte server[] = { 192,168,0,101 }; // ip del Hosting
IPAddress dnServer(192,168,0,101);
// the router's gateway address:
IPAddress gateway(192,168,0,1);
// the subnet:
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
String location = "http://192.168.0.100/javascript/index.php HTTP/1.0";
//EthernetClient client;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
void setup(){
Ethernet.begin(mac);
Serial.begin(9600);
}
void loop(){
String pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
delay(3000); //wait 3 seconds before connecting again
}
String connectAndRead(){
//connect to the server
Serial.println("connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
Serial.println("connected");
client.print("GET ");
client.println(location);
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return "connection failed";
}
}
String readPage(){
//read the page, and capture & return everything between '<' and '>'
stringPos = 0;
memset( &inString, 0, 32 ); //clear inString memory
while(true){
if (client.available()) {
char c = client.read();
if (c == '<' ) { //'<' is our begining character
startRead = true; //Ready to start reading the part
}else if(startRead){
if(c != '>'){ //'>' is our ending character
inString[stringPos] = c;
stringPos ++;
}else{
//got what we need here! We can disconnect now
startRead = false;
client.stop();
client.flush();
Serial.println("disconnecting.");
return inString;
}
}
}
}
}