0

I'm getting a few errors when merging strings with constants.

Here's the pertinent code...

#define STATION_ID ABC123
#define STATION_PASS XYZ987

void loop(){
    String path = "/weatherstation/updateweatherstation.php?ID="+STATION_ID+"&PASSWORD="+STATION_PASS+"&dateutc=now&tempf="+celsiusToFahrenheit(temperature)+"&humidity="+String(humidity)+"&action=updateraw";
}

And then the errors...

weather.cpp:15:20: error: 'ABC123' was not declared in this scope
weather.cpp:64:66: note: in expansion of macro 'STATION_ID'

weather.cpp:16:22: error: 'XYZ987' was not declared in this scope
weather.cpp:64:90: note: in expansion of macro 'STATION_PASS'

Not sure where I'm going wrong on this.

1

Maybe? Not sure, but give this a try.

#define STATION_ID "ABC123"
#define STATION_PASS "XYZ987"
1

Further optimization is:

#define STATION_ID "ABC123"
#define STATION_PASS "XYZ987"

void loop(){
    String path = "/weatherstation/updateweatherstation.php?ID=" STATION_ID "&PASSWORD=" STATION_PASS "&dateutc=now&tempf="+celsiusToFahrenheit(temperature)+"&humidity="+String(humidity)+"&action=updateraw";
}

This will remove some intermediate String variables in the expression, etc.

1

That is just so wrong on so many levels.

Firstly, you should really not use String, especially not for constants.

Secondly, you do not need to manually concatenate string constants at all.

Thirdly, there is very rarely ever any need to concatenate things together into a single string before using them. Instead you can in general send individual parts separately:

#define STATION_ID "ABC123"
#define STATION_PASS "XYZ987"

client.print(F("/weatherstation/updateweatherstation.php?ID=" 
               STATION_ID 
               "&PASSWORD=" 
               STATION_PASS 
               "&dateutc=now&tempf="));
client.print(celsiusToFahrenheit(temperature));
client.print(F("&humidity="));
client.print(humidity);
client.print(F("&action=updateraw\r\n"));

By having multiple string constants next to each other (such as "string one " "string two") the compiler will automatically join them together (into "string one string two") for you. No plus, no comma, no nothing - just place them next to each other. And always use F() for string constants.

  • Calm down @Majenko, did you see this? Here's the pertinent code... I think he just gave an example. I think this: That is just so wrong on so many levels. is a little rude – Dat Ha Nov 22 '16 at 20:06
  • You may think it's rude, but that doesn't stop it being completely true. It is so very very very wrong. On soooo many levels. – Majenko Nov 22 '16 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.