I am having a hard time with getting an int value parsed out of a string. I have searched endlessly with several examples but cannot seem to get it to work. A simple explanation of my project: Control the arduino from a web browser, turn on/off outputs, select 3 int values for RGB leds.
An example of my string string looks like this: ipaddress:port/?RED=255
I have gotten all the other commands to work using indexOf to find the "?" and enabling outputs but can't figure out how to get the 255 at the end to be transfered to my REDval int to use as analogWrite for my led's
Any help would be greatly appreciated. I tried to post my code but it says its too long with the copy for forum method from the IDE.
can you make it simpler?
ipaddress:port/?RED=255
instead
ipaddress:port/?R255G0B0
then parse the integers after the three markers, R, G and B something like this:
if (Serial.available())
{
char myChar = Serial.read();
if (myChar == 'R')
{
red = Serial.parseInt();
}
else if (myChar == 'G')
{
green = Serial.parseInt();
}
else if (myChar == 'B')
{
blue = Serial.parseInt(); //edit- whoops forgot semicolon
}
}
I suppose I could, the only issue is the HTML in my code makes a web page with three text boxes. one for each color with a set button that send the string to the Arduino over Ethernet. I can't really get away from that format because I am using an app on my Iphone that uses sliders to send the values individually. the actual string is RED and the =255 is added by the apps slider position or the text entered in web page text box.
I'd like to try your method but I'm not sure how to get rid of the = in the html sent from the browser... Any ideas on that? If i can get rid of it I think your way will work perfectly.
assuming they are strictly RED, GREEN and BLUE:
if (Serial.available())
{
char myChar = Serial.read();
if (myChar == 'D')
{
red = Serial.parseInt();
}
else if (myChar == 'N')
{
green = Serial.parseInt();
}
else if (myChar == 'E')
{
blue = Serial.parseInt(); //edit- whoops forgot semicolon
}
}
I agree with that but the = is there every time in the html code. Can I use something other than a single char or something like Mychar = "D="
there are only single byte chars, sorry.
try it...
It will ignore the '=' as it is looking for the next thing that looks like an int
I'll try it out. Thanks a bunch.
The below test code uses the serial port, but the capture/parsing would be the same for a GET query string received. You could substitute the ? in the get request for the * in the below code and capture every thing after the ? as a String. Then use the String functions to extract the desired data, then use a function like data.toInt(); to convert the numeric characters into an integer for use in controlling your LEDs. You can work out the capture/parsing code ising the serial monitor, then use it in the server code.
//zoomkat 11-12-13 String capture and parsing
//from serial port input (via serial monitor)
//and print result out serial port
//copy test strings and use ctrl/v to paste in
//serial monitor if desired
// * is used as the data string delimiter
// , is used to delimit individual data
String readString; //main captured String
String angle; //data String
String fuel;
String speed1;
String altidude;
int ind1; // , locations
int ind2;
int ind3;
int ind4;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 11-12-13"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like 90,low,15.6,125*
//or 130,hi,7.2,389*
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == '*') {
//do stuff
Serial.println();
Serial.print("captured String is : ");
Serial.println(readString); //prints string to serial port out
ind1 = readString.indexOf(','); //finds location of first ,
angle = readString.substring(0, ind1); //captures first data String
ind2 = readString.indexOf(',', ind1+1 ); //finds location of second ,
fuel = readString.substring(ind1+1, ind2+1); //captures second data String
ind3 = readString.indexOf(',', ind2+1 );
speed1 = readString.substring(ind2+1, ind3+1);
ind4 = readString.indexOf(',', ind3+1 );
altidude = readString.substring(ind3+1); //captures remain part of data after last ,
Serial.print("angle = ");
Serial.println(angle);
Serial.print("fuel = ");
Serial.println(fuel);
Serial.print("speed = ");
Serial.println(speed1);
Serial.print("altidude = ");
Serial.println(altidude);
Serial.println();
Serial.println();
readString=""; //clears variable for new input
angle="";
fuel="";
speed1="";
altidude="";
}
else {
readString += c; //makes the string readString
}
}
}