Different IP for client and Server?
hello,
i'm trying make arduino work both client , server @ same time, different ips. want client weather information yahoo api, , want use server chose city. when give them both same ip works when change it doesn't. router gives ips subnet 255.255.255.0, it's ip 192.168.0.1, , want arduino server have 169.254.x.x ip, client won't connect internet unless has 192.168.0.x ip. thats why i'm trying work 2 ips @ same time.
my code follows:
server code w.a. smith
client code webmeister
combined using zoomkat's mixed client/server test code.
any appreciated.
if matters, i'm using arduino uno ethernet shield , sd card, , ide 1.0.5-r2.
i'm trying make arduino work both client , server @ same time, different ips. want client weather information yahoo api, , want use server chose city. when give them both same ip works when change it doesn't. router gives ips subnet 255.255.255.0, it's ip 192.168.0.1, , want arduino server have 169.254.x.x ip, client won't connect internet unless has 192.168.0.x ip. thats why i'm trying work 2 ips @ same time.
my code follows:
code: [select]
#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 server @ port 80
ipaddress yahooserver(87,248,122,181);
ethernetclient client;
textfinder finder( client );
file webfile;
string http_req; // stores 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 listen clients
serial.begin(9600); // 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 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 client
if (client)
{ // got client?
serial.println(f("client"));
endoffirstline = false;
boolean currentlineisblank = true;
while (client.connected())
{
if (client.available())
{ // client data available read
char c = client.read(); // read 1 byte (character) client
if (c != '\n' && endoffirstline == false)
{
http_req += c; // save http request 1 char @ time
}
else
{
endoffirstline = true;
}
// last line of client request blank , ends \n
// respond client after last line received
if (c == '\n' && currentlineisblank)
{
// send 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 client
}
webfile.close();
}
break;
}
// every line of text received client ends \r\n
if (c == '\n')
{
// last character on line of received text
// starting new line next character read
currentlineisblank = true;
}
else if (c != '\r')
{
// text character received client
currentlineisblank = false;
}
} // end if (client.available())
} // end while (client.connected())
//serial.println(f("end of while!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
delay(1); // give web browser time receive data
client.stop(); // close connection
} // end if (client)
http_req = "";
if ( millis() - timer >= 5000)
{
timer = millis();
yahoodata();
}
//delay(1000);
}
void yahoodata() //client function receie weather data.
{
ethernet.begin(mac, ipclient);
serial.println(ethernet.localip());
if (client.connect(yahooserver, 80)) {
// call wetter-api
// w: id city
// http://weather.yahooapis.com/forecastrss?w=12893459&u=c
///
serial.println("connect 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 ", " ",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 w.a. smith
client code webmeister
combined using zoomkat's mixed client/server test code.
any appreciated.
if matters, i'm using arduino uno ethernet shield , sd card, , ide 1.0.5-r2.
you should need 1 ip address. use 192 series one. act public facing server, need configure router direct incoming traffic on port 80 arduino.
Arduino Forum > Topics > Home Automation and Networked Objects > Different IP for client and Server?
arduino
Comments
Post a Comment