Functions: Converting string to integers

I’ve searched the web over for an answer to this but somehow it’s escaping me. If anyone can help me, I would be most appreciative.

I’m sending an integer to my particle.function. Obviously it gets sent as a string, but I cannot for the life of me get it to an integer. Here’s what I got:

particle.function("tempCurSet", tempCurSet);    

int tempCurSet(String setTemp) {
    char * params = new char[setTemp.length() + 1];
    strcpy(params, setTemp.c_str());
    int setTemp1 = atoi(params);
    Serial.println(setTemp1);
    return 1;
}

I’m sending the function say, 60. What prints out is 0. If I just print out setTemp (or do a setTemp.toInt()), it will spit out [object HTMLFormElement].

I found the above here and looked here and lots of other places. What am I missing? Thanks in advance!

Have you tried using toInt()?

The String class differs from classic C-style strings.

This is what I mean:

int tempCurSet(String setTemp) {
    int setTemp1 = setTemp.toInt();
    Serial.println(setTemp1);
    return 1;
}

I have indeed. I send the function a number, say 60, and it prints out a 0.

Just as a rule of thumb, instead of just returning a constant 1, why not return the actually converted integer?
Another problem we often see is a redeclaration issue with local variables hiding the global version resulting in the inner value never to reach the outside world.
A third thought may be to double check what exactly are you sending in? First print the incoming, raw string (preferably with some surrounding makers to give indication whether or not you’re also receiving leading/trailing characters that might impair the result), then print out any intermediate variable and finally the data you are interested in. The development/evolution of your data over the course of your program logic may reveal the cause.

3 Likes

@griffgj,

I am doing something very similar to you, except I’m passing in a float instead of an integer. Here is my code:

int setHighTH(String cmd)
{
    float temp;
    
    if ((temp = cmd.toFloat()) == 0)
    {
        DEBUGln(F("setHighTH(): Invalid temperature threshold = " + cmd));
        return 0;
    }
    else
    {
        DEBUGln(F("setHighTH(): Setting high temperature threshold to " + String(temp, 1)));
        HIGHTEMPTHRESHOLD = temp;
        return 1;
    }
} 

DEBUGln is just a macro that I use to debug my scripts. HIGHTEMPTHRESHOLD is a global float that use in multiple places in the program. I have also used the toInt() function many times in similar situations. I use Postman to access all of my Particle functions.

Well, I found my problem. The web page I created to send the data to the function was sending to it this [object HTMLFormElement]. I went back to use just the simple html form from the led tutorial and modified it to send numbers and it works like a charm. So my ineptitude at javascript has been revealed. Sorry to bother you guys and thank you for your help!

Here’s the code I went with for future reference:

int tempCurSet(String setTemp) {
    int setTemp1 = setTemp.toInt();
    Serial.println(setTemp1);
    return 1;
}
2 Likes

@griffgj,

Glad you got it fixed. If you’re paranoid like me, and want to add a small layer of protection, you could check the return code from the toInt() function. If you pass in a string that doesn’t start with a number the function will return a 0. In school they always pounded it into our heads to always check return codes when possible to make sure that input data is valid.