first of all, sorry for my bad english. I'm new to the arduino development, so I got a simple question. I got to control some LEDs with the Arduino Board, and it works fine, now i want to set the Color from an extern source so I need to Convert a String like "FFFFFF" (which i get from a client as a String) to a long hex number (0xFFFFFF). Its a simple problem, but i just cant solve it. I looked for solutions with atoi() but it doesnt work.
long color = 0;
void loop()
{
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
String clientMsg ="";
String colorString = "";
String positionString = "";
while (client.connected()) {
if (client.available()) {
client.println("Hi there!");
char c = client.read();
clientMsg+=c;//store the recieved chracters in a string
//if the character is an "\" the whole message is recieved
if (c == '\\') {
colorString = colorString + clientMsg.charAt(1) + clientMsg.charAt(2) + clientMsg.charAt(3) + clientMsg.charAt(4) + clientMsg.charAt(5) + clientMsg.charAt(6); // create the hex color String
color = stringToNumber(colorString);
....
int stringToNumber(String thisString) {
int i, value = 0, length;
length = thisString.length();
for(i=0; i<length; i++) {
value = (10*value) + thisString.charAt(i)-(int) '0';;
}
return value;
}
the stringToNumber doesnt work, i found it on another forum. I need a replacement for this function. I tried some other solutions but i also didnt work. I also tried the strtol, but the long is still 0.
Why are you using Strings? You are going to have problems until you stop.
You're accessing different sets of characters in the two methods - 1 to 6 in one case and 0 to n in the other. You need to make up your mind which set is important.
Finally, the code you posted does something. You want it to do something. All you have said about those two somethings is that the second one is not what you want. What that is, or what the code actually does is a mystery.
PaulS:
You're accessing different sets of characters in the two methods - 1 to 6 in one case and 0 to n in the other. You need to make up your mind which set is important.
where? clientMsg is the same set or i missunderstand something
what i want exactly is to convert my for Example "FFFFFF" string into a 0xFFFFFF long... somehow
where? clientMsg is the same set or i missunderstand something
Never mind, I was reading it wrong. I still don't understand why you skip the first character in clientMsg, though.
I still don't know what is in client message, nor do I know what you get when you call strtol().
The code in stringToNumber is clearly designed to handle base 10 strings, only. You have to multiply by 16 each time, if you know the string is a string of hex characters, and 'F' - '0' is not 15.
I get a String with several Settings für a LED-strip, the String contains in the first Character what scenario it is (blinking LED, running LED etc.) in the 1-6 Characters I get the Color for the RGB-LED-strip, thats why i need to pull apart the String. The color of every LED is stored in a long-array. Every long is a color of 1 LED of the strip.