Help creating STRING variables

Dear forum,

I have the 4.5 firmware in my photos .When I made the update I noticed that the coding have been changed so my previous program was not working and having errors.

I correct these errors and found out that the JSON method I use to read a value is now returning an error that does not finds the variable . My code is below : Can someone help me ASAP?

////////////////READING VARIABLE FROM MY PC THROUGH CURL////////////////
ioannis@ioannis-Inspiron-5547:~$ curl ""
  "ok": false,
  "error": "Variable not found"

/////////// PHOTON CODE ////////////////////////////////////

// This #include statement was automatically added by the Particle IDE.
#include "SparkFunMAX17043/SparkFunMAX17043.h"

// This #include statement was automatically added by the Spark IDE.
#include "HTU21D/HTU21D.h"

// -----------------
// Read temperature
// -----------------

// Create a variable that will store the temperature value
//float temperature = 0.0;
//float humitidy = 0.0;

HTU21D htu ;
char *resultstr;
char *resultstrhum;
char *resultvolt;
char *resultstrsoc;
char *resultstrmoisture;
int analogPin = A0;
float moisture = 0;
int led1 = D2;
void setup()
  pinMode(led1, OUTPUT);

  // Set up the MAX17043 LiPo fuel gauge:
  lipo.begin(); // Initialize the MAX17043 LiPo fuel gauge

  // Quick start restarts the MAX17043 in hopes of getting a more accurate
  // guess for the SOC.

  // Register a Spark variable here
  Spark.variable("temperature", resultstr, STRING);
  Spark.variable("humidity", resultstrhum, STRING);
  Spark.variable("voltage", resultvolt, STRING);
  Spark.variable("soc", resultstrsoc, STRING);
  Spark.variable("moisture", resultstrmoisture, STRING);
  Spark.function("led", ledToggle);

  digitalWrite(led1, LOW);
  // Spark.variable("humitidy", &resultstr, STRING);
  // Connect the temperature sensor to A7 and configure it
  // to be an input
  //pinMode(A7, INPUT);

void loop()

  moisture = analogRead(analogPin);
  moisture = moisture / 40.95;
  // lipo.getVoltage() returns a voltage value (e.g. 3.93)
  float voltage = lipo.getVoltage();
  // lipo.getSOC() returns the estimated state of charge (e.g. 79%)
  float soc = lipo.getSOC();

  // Keep reading the temperature so when we make an API
  // call to read its value, we have the latest one
  float humd = htu.readHumidity();
  float temp = htu.readTemperature();  // *9/5+32  FORMULA FOR  fahrenheit
  sprintf(resultstrhum, "%.2f", humd);
  sprintf(resultstr, "%.2f", temp);
  sprintf(resultvolt, "%.2f", voltage);
  sprintf(resultstrsoc, "%.2f", soc);
  sprintf(resultstrmoisture, "%.2f", moisture);


  //temperature = analogRead(A7);

int ledToggle(String command) {
  /* Spark.functions always take a string as an argument and return an integer.
  Since we can pass a string, it means that we can give the program commands on how the function should be used.
  In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
  Then, the function returns a value to us to let us know what happened.
  In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
  and -1 if we received a totally bogus command that didn't do anything to the LEDs.

  if (command == "on") {
    digitalWrite(led1, HIGH);
    return 1;
  else if (command == "off") {
    digitalWrite(led1, LOW);
    return 0;
  else {
    return -1;
char *resultstr;

This needs reworking to be a buffer (especially as you’re passing it to sprintf).

So change to

char resultstr[30];

Make the number as large as is needed to hold the result of the variable.

1 Like

String cloud variables cause quite a lot of confusion. I’ve filed an issue with steps that we can take to make things simpler.