Hi there i have been looking through this site for a while now ..very informative yet i cant seem to find an answer to my predicament.I am using a php file to obtain the forecast temp for singapore for tomorrow. Which works fine. it displays on my localhost webpage as eg. <30>
If i use the following sketch my arduino can read it.
But if i use that sketch in my main sketch it doesnt want to connect or will say its connected and then not show the data or say there is no data. Ive tried various ways as making the "dataget" sketch a seperate function and calling it and i have place it within the loop and setup as it is originally made. Whichever way i cant seem to get it to work. I am quite new at this and plod along albiet by trial and error. But any help would be appreciated in informing me how i would go about this. I am not sure whether you would require both sketches. Ill post the dataget one and if needed ill post the other which is much longer. Thankyou
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte server[] = { 192,168,0,2 }; //ip Address of the server you will connect to
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/index1.php HTTP/1.0";
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
////////////////////////////////////////////////////////////////////////
EthernetClient client;
int convertedtemp;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
String responseString;
void setup(){
delay( 100 );
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(5000); //wait 5 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;
//Serial.print(inString);
//convertedtemp = atoi(inString).c_str());
int val = atoi(inString);
//int convertedhumid = atoi(getValuesFromKey(responseString, "relative_humidity").c_str());
Serial.println(val);
client.stop();
client.flush();
Serial.println("disconnecting.");
//return inString;
//return ;
}
}
}
}
}