how do i set analog value from a http string?

Thanks to zoomkat for the help getting me this far

So far i have an "on" and "off" button that controlls an led on pin 4
and a status showing the led status

Then i have a text box with a submit button, when i enter a value say 554 and press submit i get
http://10.1.1.177/?form=554

How do i then assign the value 554 to "int analog"

heres my code

//*******************************

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

int analog ;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 10,10,1,145 }; // ip in lan
byte gateway[] = { 123,197,43,36 }; // the IP of the router or acsesspoint
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask (i dont think this is neccesary
Server server(80); // server port (change this if you are having a local webserver else than the arduino)

int ledPin = 4; // LED pin

int sensorPin = A0; // analog in 0 for testing
int sensorValue = 0; // integer for the analog sensor

String readString = String(30); // string for fetching data from address

boolean LEDON = false; // LED status flag

void setup()
{
Serial.begin (9600);
Ethernet.begin(mac, ip, gateway, subnet); //start Ethernet

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

analog = 100;
}

void loop(){

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

char c = client.read();

if (readString.length() < 30) { //read char by char HTTP request
readString += c; } //store characters to string

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

int stringthing = readString.indexOf("="); //here is a key component of where the status is being read by the arduino
Serial.println (readString);

if (stringthing > 1){

if (readString.indexOf ("on")>0) { //led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on

LEDON = true;
}

if (readString.indexOf( "off") >0) {
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF

LEDON = false;
}

}

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

client.print("LED . ");

//client.println (""); //submit button
client.print("LED status: ");

if (LEDON == true) {
client.println("ON");

}
else {
client.println("OFF");

}

client.print ("

");
client.print ("<form name=input action= method= get >");

client.print ("

");

client.print ( analog );

client.println("");

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

client.stop(); //stopping client

}}}}}

Any help or direction will be much apreciated, cheers, corey

Hi,

If the value is always the rightmost element in the returned string, since you're using the String type, a function like this will do the trick:

int valueFromString(String test) { // pass it the String from your result
  int result = 0; // accumulator for value
  int power = 1; // self-explantatory (I hope!)

  // find the substring after the = which contains our value
  test = test.substring(test.lastIndexOf('='));

  // iterate through the substring after the = tallying up the values as we go
  for(int i = test.length()-1; i >= 0; i--) {
    result += (test.charAt(i)-'0') * power;
    power *= 10; 
  }
  return result;
}

Of course this is untested, so normal caveats about warranties apply...but pretty sure unless I've made a typo it should do the trick for you.

Cheers, Geoff

strykeroz:
Of course this is untested, so normal caveats about warranties apply...but pretty sure unless I've made a typo it should do the trick for you.

Cheers, Geoff

Thanks geoff ,but i cant get it to work, could you please show me how to incorporate it into my code above.
That would be AWESOME .
I have spent Weeks and Weeks doing all the automation, lcd, ethernet, sd, rct code etc. and this is the last piece of the puzzle and its so frustrating ive been stuck on it for days.

Thanks heaps for your time, corey

Hi Corey

I don't have a lot of time to look at your code, but from what I see you don't differentiate whether the value being returned is from the buttons in the form that turn the LED on/off or the string. So presuming your example of what returns above is correct, the options are the URL will look like one of these depending on what is done on the form...
http://10.1.1.177/?form=554 (or any other digit) or
http://10.1.1.177/?form=on or
http://10.1.1.177/?form=off

If that's the case, copy the function in my other post into your code, at the end is fine. Then make this little modification to your existing testing of the string (just one way to do it, there are always many)

          boolean allDoneNow = false;
          if (stringthing > 1) {
          
            if (readString.indexOf ("on")>0) {  //led has to be turned ON
                digitalWrite(ledPin, HIGH);      // set the LED on
                allDoneNow = true;
                LEDON = true;
            }  

            if (readString.indexOf( "off") >0) {
            //led has to be turned OFF
              digitalWrite(ledPin, LOW); // set the LED OFF
              allDoneNow = true;
              LEDON = false;
            }

            if(!allDoneNow)  // it wasn't on or off, but a digit that was returned
              analog = valueFromString(readString);

        } // end if (stringthing > 1)

All I'm doing there with the allDoneNow flag is ensuring that you don't try to populate the analog variable if "on" or "off" was returned as my function isn't written to handle the possibility of being sent a readString that isn't just digits following the =. If you trust your users never to input anything but digits that's good as it is, but you might want to add checks in valueFromString() to ensure that's all you're getting if it's going to be used by others aside from yourself.

Remember to declare analog as an integer somewhere, because I haven't anywhere here.

Cheers, Geoff

Thanks geoff , i could not get it to work but got this to

if (readString.substring(6,12)== "analog") // checks to make sure analog field submitted
{
newString =(readString.substring(13,17)); // gets number from string
analog = newString.toInt(); //convert newString into an int analog
}

Thanks, corey

Kewl, you learn something new every day :slight_smile: