Switching between ethernet client & server
hi,
this simple 1 when know doing or can dig library code in ethernet files having difficulty in figuring out how switch between ethernet board being server switching client , again.
i have embedded application allows me switch lights on , off & display temperature of solar hot water system. web side of runs on ethermega , serves pages, embedded tokens filled in real-world data (via rs485 & rfm12 links). works ok.
now time-to-time, send of temperature data pachube cant figure out how stop board being server , switch client allow pachube stuff, server again.
any appreciated.
i not 'c' programmer bit painful bit i'm getting less worse. write embedded stuff in assembly arduino seemed way go web-interface side, i'm trying there....
this simple 1 when know doing or can dig library code in ethernet files having difficulty in figuring out how switch between ethernet board being server switching client , again.
i have embedded application allows me switch lights on , off & display temperature of solar hot water system. web side of runs on ethermega , serves pages, embedded tokens filled in real-world data (via rs485 & rfm12 links). works ok.
now time-to-time, send of temperature data pachube cant figure out how stop board being server , switch client allow pachube stuff, server again.
any appreciated.
i not 'c' programmer bit painful bit i'm getting less worse. write embedded stuff in assembly arduino seemed way go web-interface side, i'm trying there....
combined client/server code worked 0022 ide. need date use 1.0 ide.
code: [select]
//zoomkat 11-25-11
//simple button iframe code
//for use ide 0021
//open serial monitor , send e test client and
//open serial monitor see arduino receives
//use \ slash escape " in html
//address http://192.168.1.102:84/ when submited
//for use w5100 based ethernet shields
#include <spi.h>
#include <ethernet.h>
byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page
server server(84); //server port
client client(myserver, 80);
string readstring;
//////////////////////
void setup(){
pinmode(5, output); //pin selected control
ethernet.begin(mac, ip, gateway, subnet);
server.begin();
serial.begin(9600);
serial.println("server/client test 11/25/11"); // can keep track of loaded
}
void loop(){
// check serial input
if (serial.available() > 0)
{
byte inchar;
inchar = serial.read();
if(inchar == 'e')
{
sendget(); // call sendget function
}
}
client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char char http request
if (readstring.length() < 100) {
//store characters string
readstring += c;
//serial.print(c);
}
//if http request has ended
if (c == '\n') {
///////////////
serial.println(readstring); //print serial monitor debuging
//now output html data header
if(readstring.indexof('?') >=0) { //don't send new page
client.println("http/1.1 204 zoomkat");
client.println();
client.println();
}
else {
client.println("http/1.1 200 ok"); //send new page
client.println("content-type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<title>arduino test page</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>zoomkat's simple arduino button</h1>");
client.println("<a href=\"/?on\" target=\"inlineframe\">on</a>");
client.println("<a href=\"/?off\" target=\"inlineframe\">off</a>");
//client.println("<iframe name=inlineframe src=\"res://d:/windows/dnserror.htm\" width=1 height=1\">");
client.println("<iframe name=inlineframe style=\"display:none\" >");
client.println("</iframe>");
client.println("</body>");
client.println("</html>");
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readstring.indexof("on") >0)//checks on
{
digitalwrite(5, high); // set pin 4 high
serial.println("led on");
}
if(readstring.indexof("off") >0)//checks off
{
digitalwrite(5, low); // set pin 4 low
serial.println("led off");
}
//clearing string next read
readstring="";
}
}
}
}
}
//////////////////////////
void sendget() //client function send/receie request data.
{
if (client.connect()) {
serial.println("connected");
client.println("get /~shb/arduino.txt http/1.0");
client.println();
}
else {
serial.println("connection failed");
serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
serial.print(c);
}
serial.println();
serial.println("disconnecting.");
serial.println("==================");
serial.println();
client.stop();
}
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Switching between ethernet client & server
arduino
Comments
Post a Comment