Can't get webhooks example to compile


#1

I am trying to use the webhook example for reading the weather XML

https://docs.particle.io/tutorials/integrations/webhooks/#what-39-s-the-weather-like-

I’m using the exact code from the example, but I get the following error when I try to compile:

weather.ino:74:16: converting to ‘String’ from initializer list would use explicit constructor ‘String::String(int, unsigned char)’

This error is caused by the last set of lines in the code where NULL is returned.

Any help is greatly appreciated. Here is the code:

// called once on startup
void setup() {
    // Begin serial communication
    Serial.begin(115200);

    // Listen for the webhook response, and call gotWeatherData()
    Particle.subscribe("hook-response/get_weather", gotWeatherData, MY_DEVICES);

    // Give ourselves 10 seconds before we actually start the
    // program.  This will open the serial monitor before
    // the program sends the request
    for(int i=0;i<10;i++) {
        Serial.println("waiting " + String(10-i) + " seconds before we publish");
        delay(1000);
    }
}

void loop() {

    // Let's request the weather, but no more than once every 60
    // seconds.
    Serial.println("Requesting Weather!");

    // publish the event that will trigger our Webhook
    Particle.publish("get_weather");

    // and wait at least 60 seconds before doing it again
    delay(60000);
}

// This function will get called when weather data comes in
void gotWeatherData(const char *name, const char *data) {
    String str = String(data);
    String locationStr = tryExtractString(str, "<location>",
"</location>");
    String weatherStr = tryExtractString(str, "<weather>",
"</weather>");
    String tempStr = tryExtractString(str, "<temp_f>", "</temp_f>");
    String windStr = tryExtractString(str, "<wind_string>",
"</wind_string>");

    if (locationStr != NULL) {
        Serial.println("At location: " + locationStr);
    }

    if (weatherStr != NULL) {
        Serial.println("The weather is: " + weatherStr);
    }

    if (tempStr != NULL) {
        Serial.println("The temp is: " + tempStr + String(" *F"));
    }

    if (windStr != NULL) {
        Serial.println("The wind is: " + windStr);
    }
}

// Returns any text found between a start and end string inside 'str'
// example: startfooend  -> returns foo
String tryExtractString(String str, const char* start, const char* end)
{
    if (str == NULL) {
        return NULL;
    }

    int idx = str.indexOf(start);
    if (idx < 0) {
        return NULL;
    }

    int endIdx = str.indexOf(end);
    if (endIdx < 0) {
        return NULL;
    }

    return str.substring(idx + strlen(start), endIdx);
}

#2

the tryExtractString() function returns a String… but this return statement doesn’t match the String type, just returning a zero:

return NULL;

try

return String(NULL); 

but that is a strangely written function…