Take URL information and put in the arduino program

Hi evryone!

Sorry for my bad english, i'm french student :slight_smile:

So, i wanna collect URL information to put it on the arduino'code

Example: web URL: www.test.fr/value=1.42 I want that value (1.42) replaces the previous value present in AnalogWrite

I just want to change tension of led with webserver, for the moment, i'v create text area , when you write something on, this value go dirrectely on the URL of the website.

thank you in advance

cordially Aedriar

Extract of my actually arduino'code:

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,119,26,195);
String readString;

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);

float Envoi ;
//float v1= 0 ;

void setup() {

  pinMode(5, OUTPUT); //pin selected to control
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
  char c = client.read();
  //Serial.print("readString =");
  //Serial.println(readString);
 
  
     if (readString.length() < 100) {

      // Serial.println(readString.length());
          //store characters to string 
          readString += c; 
          }
          /*
          Serial.print("readString_length()=");
           Serial.println(readString.length());
          Serial.print("c=");
          Serial.println(c);
          Serial.print("ReadString=");
          Serial.println(readString);
         */
      
      
        //int indextension = readString.indexOf("Tension1=");
        //Serial.println(indextension);
        
      
         
        
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {

    Serial.println("new client");

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
       // Serial.print("c=");
        Serial.write(c);

        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        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("Connection: close");  // the connection will be closed after completion of the response
	 
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          


client.println("<form name=\"mon-formulaire1\" action=\"1\" method=\"get\">");

client.println("<textarea name=\"Tension1\" rows=\"2\" cols=\"4\"></textarea>");


client.println("

");


client.println("<input type=\"submit\" value=\"Envoyer\" />");
client.println("<input type=\"reset\" value=\"Annuler\" />");
client.println("</form>");


client.println("

");

          client.println("</html>");
          break;
        }
        //Serial.print(c);
        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();
   Serial.println("affichage de la tension");
   
   
     if(readString.indexOf("Tension1") >0) {
       
       Serial.println("Tension 1 lue");
       int pos = readString.indexOf("Tension1");
       Serial.println(pos);
       
       //String v1=" ";
       
       
       
       //for( int i=indextension+15;i<indextension+18;++i)
       
      // v1 = v1+readString[i];
       
            //Serial.println(v1);
           
            //analogWrite(6, 1023);
         
          }
    Serial.println("client disonnected");
  }
      }
    }
  }
}

Sorry for my bad english, i'm french student

So why not post in the French section? Just curious.

Example: web URL: www.test.fr/value=1.42

That is NOT what the URL would look like. The client (browser) might be pointed to www.test.fr, which might (I have my doubts) cause your page to be displayed, but the GET request that arrives at the Arduino would look like "GET /". After a submit action on the form, the GET request might look like "GET /?Tension1=1.42".

There should be EXACTLY one

  EthernetClient client = server.available();
  if (client) {

block of code in loop(), NOT 2 as in your case.

Use Tools + Auto Format to properly indent that mess.

Remove any commented out code before posting, unless the purpose of the post is to discuss the commented out code.

You'll need to fix the problems with your code before we can offer any more advice. Get rid of:

  EthernetClient client = server.available();
  if (client)
  {
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (readString.length() < 100)
        {
          readString += c; 
        }

at the top of loop, and the corresponding close braces at the end of loop.

Hello

I have post in french section but to maximize the chances of response I post on English/American server too :slight_smile:

My exactly URL is :" http://10.119.26.195/1?Tension1=22"

I just cleen my Arduino code and it's giving this :

#include <SPI.h>
#include <Ethernet.h>


byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,119,26,195);
String readString;

EthernetServer server(80);

float Envoi ;


void setup() {

  pinMode(5, OUTPUT); 

  Serial.begin(9600);
  while (!Serial) {
    ; 
  }



  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();


        if (readString.length() < 100) {


          readString += c; 
        }

        Serial.println("new client");


        boolean currentLineIsBlank = true;
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();

            Serial.write(c);

            if (c == '\n' && currentLineIsBlank) {

              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println("Connection: close");  // the connection will be closed after completion of the response

              client.println();
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");



              client.println("<form name=\"mon-formulaire1\" action=\"1\" method=\"get\">");

              client.println("<textarea name=\"Tension1\" rows=\"2\" cols=\"4\"></textarea>");


              client.println("

");


              client.println("<input type=\"submit\" value=\"Envoyer\" />");
              client.println("<input type=\"reset\" value=\"Annuler\" />");
              client.println("</form>");


              client.println("

");

              client.println("</html>");
              break;
            }

            if (c == '\n') {

              currentLineIsBlank = true;
            } 
            else if (c != '\r') {

              currentLineIsBlank = false;
            }
          }
        }

        delay(1);

        client.stop();
        Serial.println("affichage de la tension");


        if(readString.indexOf("Tension1") >0) {

          Serial.println("Tension 1 lue");
          int pos = readString.indexOf("Tension1");
          Serial.println(pos);



        }
        Serial.println("client disonnected");
      }
    }
  }
}

I hope is better and sorry for my knowledge of starting :slight_smile:

Cordially Aedriar

I hope is better

A little. The only argument for putting the { on the same line as the statement that it goes with that holds any water is that it saves a line. Following that up with two blank lines is not a good idea.

Get rid of the excessive blank lines.

You still have a while(client.connected()) block inside a while(client.connected()) block. ONLY have one!

Get rid of the first three of these lines:

        while (client.connected()) {
          if (client.available()) {
            char c = client.read();

            Serial.write(c);

(and the matching }s at the bottom).

            if (c == '\n' && currentLineIsBlank) {

This indicates that reading of the client's request is complete. At this point, you should do something based on what the client asked for. "GET /" and "GET /?Torsion1=22" may need to result in different output. Or, they may not. You need to decide that.

        if(readString.indexOf("Tension1") >0)
        {
          Serial.println("Tension 1 lue");
          int pos = readString.indexOf("Tension1");
          Serial.println(pos);
        }

Move the int pos statement before the if statement. Change the if statement to if(pos > 0).

The substring of interest is the part after the single =, so locate the equal sign.

int equalPos = readString.indexOf("=");

Then, create a substring from the next character to the end of the string:

String crap = readString.substring(equalPos);

Then, extract the string that is wrapped in the String:

char goodStuff[10];
crap.toCharArray(goodStuff, 10);

Then, convert the good stuff to an int or a float, as appropriate, using atoi(goodStuff) or atof(goodStuff).

Simple web server code that captures the GET request from the client browser in the form of a String, evaluates the String contents, then performs an action based on the GET request contents.

//zoomkat 10-6-13
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes 
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino 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(4, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient 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 += c; 
          //Serial.print(c);
        } 

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

          ///////////////
          Serial.println(readString); //print to serial monitor for 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 GET 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 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 for on
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}