Hello to all!
I'm a happy new owner of an arduino + ethershield.
My goal is to create a little webserver that gives me the ability to control lights or read sensors. Nothing new I think
Meanwhile it's better to start with simple examples.
I was able to turn on and off a led using a single char as controller. Why single char? Because I've understood the 'client.read();' function can read one char per time and so in an IF statement I cannot search for a 'c' = 'ON', but only 'c' = '1'.
Hope I've descrived it as good enough
My question is... is there a way to parse the URL asked to the arduino WEBSERVER to then save the argumento passed as variables?
Supposing arduino answer on 192.168.1.100
opening http://192.168.1.100/led01=on
I would like to create a if statement like
if led01 = 'on' then
OR
if url like 'led01' then .... if url like 'led01=on' then turn it on.
Based on examples found on site this is my code:
#include <SPI.h>
#include <Ethernet.h>
#include <LED.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 177 };
byte gateway[] = { 192,168,1,1 };
byte subnet[] = { 255, 255, 255, 0 };
LED led = LED(9); // the pin that the LED is attached to
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
}
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
client.print(c);
if ( c == 'Y' ) {
led.on();
client.println("LED IS ON");
}
if ( c == 'X' ) {
client.println("LED IS OFF");
led.off();
}
if ( c == 'Z' ) {
client.println("LED IS BLINKING");
for (int x = 0; x = 10; x++) {
led.blink(100);
}
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
// client.println("HTTP/1.1 200 OK");
// client.println("Content-Type: text/html");
// client.println();
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
INSTEAD of using c=X (or Y or Z) is there a way to parse every line passed as single variable and then use string functions to parse the lines and create variables to analyse?
This is the first step to then create structured GET and control more than one led and query different sensors.
Below is some test code I made that captures a "GET" request, parces data from that request, and then sends that data out the serial port to an SSC-32 servo controller (and also to the serial monitor).
//zoomkat 5-24-10
// http://www.arduino.cc/en/Tutorial/TextString for WString.h
#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
String readString = String(100); //string for fetching data from address
///////////////////////
String teststring = String(100);
String finalstring = String(100);
String flag = String(2);
int ind1 = 0;
int ind2 = 0;
int pos = 0;
//////////////////////
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//enable serial data 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);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
//Serial.println(readString);
//readString looks like "GET /?-0p1555-1p500t1000 HTTP/1.1"
if(readString.contains("-")) { //test for servo control sring
readString.replace('-', '#');
pos = readString.length(); //capture string length
//find start of servo command string (#)
ind1 = readString.indexOf('#');
//capture front part of command string
teststring = readString.substring(ind1, pos);
//locate the end of the command string
ind2 = teststring.indexOf(' ');
//capturing the servo command string from readString
finalstring = readString.substring(ind1, ind2+ind1);
//print "finalstring" to com port;
Serial.println(finalstring); //print string with CR
}
////////////////////////
//GET /?Slidervalue0=1800&Submit=Sub+0 HTTP/1.1
if(readString.contains("Slidervalue")) {
ind1 = readString.indexOf('u');
ind2 = readString.indexOf('&');
finalstring = readString.substring(ind1+1, ind2);
finalstring.replace('e', '#');
finalstring.replace('=', 'p');
Serial.println(finalstring);
}
///////////////////
//now output HTML data header
client.println("HTTP/1.1 204 Zoomkat");
client.println();
client.println();
delay(1);
//stopping client
client.stop();
/////////////////////
//clearing string for next read
readString="";
teststring="";
finalstring="";
}}}}}