Pages: [1]   Go Down
Author Topic: Capture string in ethernet web control sketch?  (Read 6458 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm tinkering with the below ethernet sketch and desire to capture the "get" request string for modifying and sending out the serial port. The desired string is output to the serial port in the "Serial.print(c);" action, but it seems the string is generated in a loop. The commented out second attempt to print to the serial port causes strange output to the serial port, with the string components printed individually seperate lines. need to capture the get individual characters in a single line string for manipulation. Any ideas?  

Code:
#include <WString.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
Server server(84); //server port
int ledPin =  4; // LED pin
String readString = String(100); //string for fetching data from address
boolean LEDON = false; //LED status flag

void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//Set pin 4 to output
pinMode(ledPin, OUTPUT);

//enable serial datada print
Serial.begin(9600); }

void loop(){

// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString.append(c);
}

//output chars to serial port
Serial.print(c);

//Serial.print("\n");
//Serial.print(c);

//if HTTP request has ended
if (c == '\n') {

//lets check if LED should be lighted
if(readString.contains("L=1")) {

//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{

//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false; }

// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

client.print("<html>");
client.print("<body>");
client.println("<h1>HTTP test routines</h1>");
client.println("<br>");//some space between lines
client.println("LED control");
client.println("<br>");
client.println("<br>");
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED<br><input type=submit value=submit></form>");
client.println("<br>");
client.print("<font size='5'>LED status: ");
if (LEDON)
client.println("<font color='green' size='5'>ON");
else
client.println("<font color='grey' size='5'>OFF");
client.println("</body></html>");

//clearing string for next read
readString="";

//stopping client
client.stop();
}}}}}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
Serial.print(c);
prints the last character read on the current line.

That's what you are seeing.

This:
Code:
//Serial.print("\n");
//Serial.print(c);
moves the cursor to the start of a new line, and then prints the last character read.

That's what you were seeing before you commented this code out. Nothing strange about it at all.

This:
Code:
readString.append(c);
is adding the last character read to the end of the String stored in readString.

Quote
The desired string is output to the serial port in the "Serial.print(c);" action
The "string" is output as a series of characters. The string is stored in readString, as a string.

Quote
I'm tinkering with the below ethernet sketch and desire to capture the "get" request string
You are doing that, in readString.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Doing more checking, "readString" does contain the get request string. One thing that is confusing to me is that "GET /?L=1 HTTP/1.1" is output to the serial port yet "Serial.print(c);" appears to be the only action sending to the port. A string is produced instead of a single character. Obviously there is looping activity to capture the characters to readString. If the below commands are used, the appending sequence of the string is printed to the serial port. Anyhow I'm moving my string parcing out of that area into the section of code where the pin setting and web page generation is performed and see how that works.


Code:
//output chars to serial port
Serial.print(c);
Serial.print("\n");
Serial.print(readString);
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Making progress. The below code will toggle pin 4 on/off, and will send the servo control string to the serial port when a control string is detected. Now to see if the hardware part works.

Code:
#include <WString.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
Server server(84); //server port
int ledPin =  4; // LED pin
String readString = String(100); //string for fetching data from address
boolean LEDON = false; //LED status flag

///////////////////////
 String url = String(100);
 String teststring = String(100);
 String finalstring = String(100);
 
 int ind1 = 0;
 int ind2 = 0;
 int pos = 0;
 //////////////////////

void setup(){

//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//Set pin 4 to output
pinMode(ledPin, OUTPUT);

//enable serial datada print
Serial.begin(9600); }

void loop(){

// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString.append(c);
}

//output chars to serial port
Serial.print(c);

//if HTTP request has ended
if (c == '\n') {

//lets check if LED should be lighted
if(readString.contains("L=1")) {

//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{

//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false; }

///////////////
//readString looks like "GET /?-0p1555-1p500t1000 HTTP/1.1"

  if(readString.contains("-")) { //test for servo control sring
  readString.replace('-', '#');
  Serial.print("readString after replace: ");
  Serial.print(readString);
  Serial.print("\n");

  //Serial.print("readString length: ");
  Serial.print("length of readString: ");
  Serial.print(readString.length());
  Serial.print("\n");
  
  pos = readString.length(); //capture string length
  
  //find start of servo command string (#)
  ind1 = readString.indexOf('#');
  Serial.print("location of first #: ");
  Serial.print(ind1);
  Serial.print("\n");
  
  //capture front part of command string
  teststring = readString.substring(ind1, pos);
  Serial.print("intermediate teststring: ");
  Serial.print("\n");
  Serial.print(teststring);
  Serial.print("\n");
  
  //locate the end of the command string
  ind2 = teststring.indexOf(' ');
  Serial.print("location of space: ");
  Serial.print(ind2);
  Serial.print("\n");
 
 //capturing the servo command string from readString
  finalstring = readString.substring(ind1, ind2+ind1);

  Serial.print("finalstring: ");
  Serial.print(finalstring);
  Serial.print("\n");
  }
/////////////////////


// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();

client.print("<html>");
client.print("<body>");
client.println("<h1>HTTP test routines</h1>");
client.println("<br>");//some space between lines
client.println("LED control");
client.println("<br>");
client.println("<br>");
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED<br><input type=submit value=submit></form>");
client.println("<br>");
client.print("<font size='5'>LED status: ");
if (LEDON)
client.println("<font color='green' size='5'>ON");
else
client.println("<font color='grey' size='5'>OFF");
client.println("</body></html>");

//clearing string for next read
readString="";

//stopping client
client.stop();
}}}}}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Making more good progress. I've connected my ssc-32 to the arduino serial port so the arduino can send it servo commands received from a web brouser. For testing I modified my webcam page so it sends servo commands to the arduino webserver, which modifies the command and then sends it to the ssc-32. The test flowpath is PC > wifi > netgear wireless router > cat5 > arduino > rs232 > ssc-32. This demonstrates that a wireless router bot can be made fairly easily.  Geeks has a referbished netgear router like I'm using for $20 (supports bridging). I haven't tested the arduino controlling servos yet. Just wanting to see if a simple and fairly inexpensive wifi bot setup could be made that didn't need a pc on the bot side.

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: [1]   Go Up
Jump to:  

Powered by SMF 1.1.19 | SMF © 2013, Simple Machines