Hello,
I'm trying to make my arduino work as both a client and a server at the same time, but with different IPs. I want it as a client to get weather information from Yahoo API, and I want to use it as a server to chose the city. When I give them both the same IP it works but when I change it it doesn't. My router gives IPs with the subnet 255.255.255.0, it's IP is 192.168.0.1, and I want my arduino server to have a 169.254.x.x IP, but the client won't connect to the internet unless it has a 192.168.0.x IP. Thats why I'm trying to get it work with 2 IPs at the same time.
my code is as follows:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <TextFinder.h>
byte mac[] = { 0xDE, 0xAD, 0xDE, 0xEF, 0xFE, 0xED }; // MAC address
IPAddress ipClient(192, 168, 0, 205); // IP address
IPAddress ipServer(169, 254, 183, 205); // IP address
EthernetServer server(80); // create a server at port 80
IPAddress yahooServer(87,248,122,181);
EthernetClient client;
TextFinder finder( client );
File webFile;
String HTTP_req; // stores the first line of HTTP request
boolean endOfFirstLine = false;
boolean pageFound;
char place[50];
char hum[30];
byte timer = 0;
void setup()
{
HTTP_req.reserve(50);
Ethernet.begin(mac, ipServer); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for debugging
Serial.println("Setup...");
Serial.println(Ethernet.localIP());
// initialize SD card
Serial.println(F("Initializing SD card..."));
if (!SD.begin(4))
{
Serial.println(F("ERROR - SD card initialization failed!"));
return; // init failed
}
Serial.println(F("SUCCESS - SD card initialized."));
// check for web page files
if (!SD.exists("home.htm"))
{
Serial.println(F("ERROR - Can't find home.htm file!"));
return; // can't find index file
}
else
{
Serial.println(F("SUCCESS - Found home.htm file."));
}
if (!SD.exists("City.htm"))
{
Serial.println(F("ERROR - Can't find City.htm file!"));
return; // can't find index file
}
else
{
Serial.println(F("SUCCESS - Found City.htm file."));
}
if (!SD.exists("Data.htm"))
{
Serial.println(F("ERROR - Can't find Data.htm file!"));
return; // can't find index file
}
else
{
Serial.println(F("SUCCESS - Found Data.htm file."));
}
if (!SD.exists("error.htm"))
{
Serial.println(F("ERROR - Can't find error.htm file!"));
return; // can't find index file
}
else
{
Serial.println(F("SUCCESS - Found error.htm file."));
}
}
void loop()
{
Ethernet.begin(mac, ipServer);
Serial.println(Ethernet.localIP());
EthernetClient client = server.available(); // try to get client
if (client)
{ // got client?
Serial.println(F("client"));
endOfFirstLine = false;
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{ // client data available to read
char c = client.read(); // read 1 byte (character) from client
if (c != '\n' && endOfFirstLine == false)
{
HTTP_req += c; // save the HTTP request 1 char at a time
}
else
{
endOfFirstLine = true;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close"));
client.println();
Serial.println(HTTP_req);
pageFound = 0;
// choose web page
if (HTTP_req[6] == 'H' && HTTP_req[7] == 'T' && HTTP_req[8] == 'T' && HTTP_req[9] == 'P') // home page
{
pageFound = 1;
Serial.println(F("home"));
webFile = SD.open("home.htm");
}
if ((HTTP_req[5] == 'C' || HTTP_req[5] == 'c') && (HTTP_req[6] == 'i' || HTTP_req[6] == 'I') && (HTTP_req[7] == 't' || HTTP_req[7] == 't') && (HTTP_req[8] == 'y' || HTTP_req[8] == 'Y')) // city page
{
pageFound = 1;
// show City page
Serial.println(F("city"));
webFile = SD.open("City.htm");
}
if ((HTTP_req[5] == 'v' || HTTP_req[5] == 'V') && (HTTP_req[6] == 'i' || HTTP_req[6] == 'I') && (HTTP_req[7] == 'e' || HTTP_req[7] == 'E') && (HTTP_req[8] == 'w' || HTTP_req[8] == 'W')) // data page
{
pageFound = 1;
// show Data page
Serial.println(F("data"));
webFile = SD.open("Data.htm");
}
if (HTTP_req[5] == 'f' && HTTP_req[6] == 'a' && HTTP_req[7] == 'v' && HTTP_req[8] == 'i') // ignored request
{
pageFound = 1;
}
if (pageFound == 0)
{
Serial.println(F("ERROR 404"));
webFile = SD.open("error.htm");
}
// send choosen web page
if (webFile) {
while(webFile.available())
{
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n')
{
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r')
{
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
//Serial.println(F("end of while!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
HTTP_req = "";
if ( millis() - timer >= 5000)
{
timer = millis();
yahooData();
}
//delay(1000);
}
void yahooData() //client function to receie weather data.
{
Ethernet.begin(mac, ipClient);
Serial.println(Ethernet.localIP());
if (client.connect(yahooServer, 80)) {
// Call Wetter-API
// w: ID from your City
// http://weather.yahooapis.com/forecastrss?w=12893459&u=c
///
Serial.println("Connect to Yahoo Weather...");
client.println("GET /forecastrss?w=12893459&u=c HTTP/1.0");
client.println("HOST:weather.yahooapis.com\n\n");
client.println();
Serial.println("Connected...");
}
else
{
Serial.println("connection failed");
Serial.println();
}
if (client.connected())
{
// Humidity
if ( (finder.getString("<yweather:atmosphere humidity=\"", "\"",hum,4)!=0) )
{
Serial.print("Humidity: ");
Serial.println(hum);
}
else
{
Serial.print("No Humidity Data");
}
// Place/City
if ( (finder.getString("<title>Conditions for ", " ",place,50)!=0) )
{
Serial.print("City: ");
Serial.println(place);
}
// Temperature
if(finder.find("temp=") )
{
int temperature = finder.getValue();
Serial.print("Temp C: ");
Serial.println(temperature);
}
else
{
Serial.print("No Temperature Data");
}
// END XML
}
else
{
Serial.println("Disconnected");
}
client.stop();
client.flush();
}
Server code from W.A. Smith
Client code from Webmeister
combined using Zoomkat's Mixed client/server test code.
any help is appreciated.
If it matters, I'm using Arduino UNO with Ethernet Shield and SD card, and IDE 1.0.5-r2.