Now that my firmware codes are getting bigger and more complex, memory optimization is proving to be critical.
My understanding is that character arrays are preferred and can be written just fine without casting to publish and subscribe commands. I have written below a subscription handler written with and without Duino string objects.
Suggestions or corrections welcome. I haven’t tried this method yet, but it does compile in the IDE.
Note that I am also using a response template.
The Arduino String way:
void getWeatherHandler(const char * event, const char * data){ //The cloud sends back weather data
String weatherReturn = String(data);
char weatherBuffer[250] = "";
weatherReturn.toCharArray(weatherBuffer, 250);
String condition = strtok(weatherBuffer, "\"~");
String updatetime2 = strtok(NULL, "~");
String epochtime = strtok(NULL, "~");
icon = atoi(strtok(NULL, "~")); //get the weather icon number
String useless = strtok(NULL, "/"); //getting through some useless data here
String useless2 = strtok(NULL, "/"); //probably there is a better way?
String useless3 = strtok(NULL, "/");
String country = strtok(NULL, "/");
String city = strtok(NULL, "/");
float age = ( Time.local() - atoi(epochtime) - zone*3600)/60;
//send to Google
Particle.publish("WeatherGoogle", "{\"City\":\"" + city + "\",\"Country\":\"" + country + "\", \
\"Condition\": \"" + condition + "\", \"Update_Time\": \"" + updatetime2 + "\", \"Age\":\"" + String::format("%.1f",age) + "\",\
\"Icon\":\"" + (String)icon + "\", \"DeviceID\": \"" + Particle.deviceID() + "\"}", 60, PRIVATE);
}
Now with CStrings!
void getWeatherHandler(const char * event, const char * data){ //The cloud sends back weather data
char dataCopy[250] ="";
strcpy (dataCopy, data);
char condition[50] = "";
strcpy (condition, strtok(dataCopy, "\"~"));
char updatetime2[50] = "";
strcpy (updatetime2, strtok(NULL, "~"));
char epochtime[50] = "";
strcpy (epochtime, strtok(NULL, "~"));
icon = atoi(strtok(NULL, "~")); //get the weather icon number
char useless[50] = "";
strcpy (useless, strtok(NULL, "/")); //just writing some tokens to the same buffer
strcpy (useless, strtok(NULL, "/")); //to get through some useless sections of data
strcpy (useless, strtok(NULL, "/")); //perhaps there is a better way?
char country[50] = "";
strcpy (country, strtok(NULL, "/"));
char city[50] = "";
strcpy (city, strtok(NULL, "/"));
float age = ( Time.local() - atoi(epochtime) - zone*3600)/60;
//send to Google
char pubString[300] = "";
strcpy (pubString, "{\"City\":\"");
strcat (pubString, city);
strcat (pubString, "\",\"Country\":\"");
strcat (pubString, country);
strcat (pubString, "\",\"Condition\": \"");
strcat (pubString, condition);
strcat (pubString, "\", \"Update_Time\": \"");
strcat (pubString, updatetime2);
char age1[16];
snprintf(age1, sizeof(age1), "%.2f", age);
strcat (pubString, "\", \"Age\": \"");
strcat (pubString, age1);
char icon1[8];
snprintf(icon1, sizeof(icon1), "%i", icon);
strcat (pubString, "\", \"DeviceID\": \"");
strcat (pubString, Particle.deviceID());
strcat (pubString, "\"}");
Particle.publish("WeatherGoogle", pubString, 60, PRIVATE);
}