Receive String from Ethernet Shield/Server
hello all, have arduino ethernet hooked serial lcd screen spark fun. screen 20x4. have screen working arduino , can statically print strings on screen. however, want send packet arduino via ethernet contains strings print on screen. best way implement this? thinking packet send arduino have newline character tell me when break string next line , on. can't seem wrap head around how this. can setup ethernet server no issue, stuck on how receive/parse string. know much:
code: [select]
ethernetclient client = server.available();
if (client == true) {
while (client.connected()){
if (client.available() ) {
parsestring(client); //would method call handle right or should parse right here? , how parse each line out assuming use newline seperate? should globally declare 4 strings put each in?
}
}
}
basic server code captures sent client in readstring variable.
code: [select]
//zoomkat 12-8-11
//simple button iframe code
//for use ide 1.0
//open serial monitor see arduino receives
//use \ slash escape " in html (or use ')
//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
ethernetserver server(84); //server port
string readstring;
//////////////////////
void setup(){
pinmode(5, output); //pin selected control
//start ethernet
ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
//enable serial data print
serial.begin(9600);
serial.println("server led test 1.0"); // can keep track of loaded
}
void loop(){
// create client connection
ethernetclient 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=\"/?on1\" 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("on1") >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="";
}
}
}
}
}
Arduino Forum > Using Arduino > Programming Questions > Receive String from Ethernet Shield/Server
arduino
Comments
Post a Comment