Hi,
Given this web address : http://www.realip.info/api/p/realip.php
I would like to store the page output in a string.
How can i achieve it?
Thanks.
Hi,
Given this web address : http://www.realip.info/api/p/realip.php
I would like to store the page output in a string.
How can i achieve it?
Thanks.
You'll have to be a bit more specific . . .
I gather you want your Arduino to receive an IP address from that website. What hardware are you using? Do you have a shield for internet/networking? Or are you using a PC and serial port?
Yes , i have an arduino Uno Rev3 with ethernet shield.
All i want to do is retrieve the ip address output from the page
it's outputted on the webpage in this way : {"IP":"214.123.165.236"}
I just want to read the whole string or even better only what is between :" and "}
Can you help please?
I would like to store the page output in a string.
You can capture the data in a String like below.
//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString
#include <SPI.h>
#include <Ethernet.h>
String readString;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "www.realip.info"; // myIP server test web page server
EthernetClient client;
//////////////////////
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("client readString test 11/04/13"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
Serial.println();
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /api/p/realip.php HTTP/1.1"); //download text
client.println("Host: www.realip.info");
client.println("Connection: close"); //close 1.1 persistent connection
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
readString += c; //places captured byte in readString
}
//Serial.println();
client.stop(); //stop client
Serial.println("client disconnected.");
Serial.println("Data from server captured in readString:");
Serial.println();
Serial.print(readString); //prints readString to serial monitor
Serial.println();
Serial.println();
Serial.println("End of readString");
Serial.println("==================");
Serial.println();
readString=""; //clear readString variable
}
Ok, thanks for the example, i managed to workout the output of the webpage that gives the IP Address, i modified it at my needs and here is what came out and that may be usefull to someone else:
// reads IP address output from the www.realip.info website
// Thanks to ZoomKat for his precious help
#include <SPI.h>
#include <Ethernet.h>
String readString;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
const unsigned long requestInterval = 120000; //delay between requests 300.000 = 5 min / 60.000 = 1 minuto / 120.000 = 2 minuti
unsigned long lastAttemptTime = 0 ; //var for lastattemptTime for requesting new ip reading
int lastStringLength = readString.length(); //var for reading length of ipaddress string output from website
char serverName[] = "www.realip.info"; //myIP server test web page server
EthernetClient client;
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
sendGET(); //asks for the ip address
}
void loop()
{
if (millis() - lastAttemptTime > requestInterval) // se sono passati almeno x minuti dall'ultimo tweet d'allarme allora invia di nuovo se necessario
{
sendGET();
lastAttemptTime = millis(); // note the time of this connect attempt:
}
//occorre attendere prima di poter eseguire un nuovo controllo dell'ip
}
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /api/p/realip.php HTTP/1.1"); //download text
client.println("Host: www.realip.info");
client.println("Connection: close"); //close 1.1 persistent connection
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
readString += c; //places captured byte in readString
}
client.stop(); //stop client
Serial.print("IP = ");
int primaparentesigraffa = readString.indexOf('{'); //inserisce in variabile la posizione della prima parentesi graffa all'interno della quale c'è l'IP
lastStringLength = readString.length(); //assegna alla variabile lastringlength la lunghezza della stringa letta contenente l'IP
readString.remove(0, primaparentesigraffa+7); //rimuove tutto quello che c'è dall'inizio della stringa fino alla prima parentesi graffa + 7 caratteri per arrivare all'ip
lastStringLength = readString.length(); //rilegge la lunghezza della stringa ora che abbiamo rimosso parte delle cose inutili
readString.remove(lastStringLength-2, lastStringLength); //rimuove tutto il resto partendo dalla fine -2 verso la fine in modo da ottenere solo l'IP
Serial.print(readString); //stampa sul monitor seriale l'IP
readString=""; //svuota la variabile readString
}
Interesting that String.remove() seems to have been added to the String functions recently (not in 1.0.5 IDE version).