Converting char array to string and then publishing it

I’m having a problem publishing a String using IFTTT. I believe the problem lies in converting the char array to a string but I’m not sure (am I doing this correctly?). The “Error” publish sends me an email with the String in the 2nd parameter. The “Data” Publish fills a row to a google spreadsheet with the String in the 2nd parameter. UnsentData.txt is a file that lines of data were written to previously when they failed to send (in case of a Wi-fi outage). I can to send lines of data to the spreadsheet with wi-fi by just having the second parameter be the same String I would write to the SD if it didn’t send. I don’t get any emails past “Some data still needs to be sent…” and nothing is saved onto the spreadsheet.

File dataToBeSent ="UnsentData.txt", FILE_WRITE);
                    Particle.publish("Error","Some data still needs to be sent...");
                    while(dataToBeSent.peek()!=-1) //continue until .peak() returns -1 (end of file)
                        if (val[i-1]=='\n')
                            if (Particle.publish("Error", v))
                                Particle.publish("Error", "The Error call worked for the unsent data");
                            if(Particle.publish("Data", v)) //Send the unsent value to google spreadsheet
                                Particle.publish("Error", "Unsent data sent");
                                //SD.remove("UnsentData.txt"); //Destroy the "to be sent" file if the data sent
                            char val[40];

I don’t know SD library at all, but don’t you accidentally remove whole file’s content when opening it for writing?

This is some funny way of coding :confused:
Why on earth do you declare char val[40] near the end of the function while you already use it further up?
Where do you declare v?

1 Like

I’m declaring char val[40] again so it is reset to an empty array. That way I can fill it with information from the next line of data. With v and i I am resetting them by setting their values to “” and 0 respectively.

I don’t believe so; the information is on the SD card when pull it up on my computer after the code executes

I would try to open it in read mode anyway.

Opening in read mode fixes everything, thanks!

That’s a rather unorthodox way of doing that.
You’d rather use memset(val, 0, sizeof(val)) but the way you are building up the string you wouldn’t even need to reset it at all.

What is the benefit of using memset rather than redeclaring it?

I’d go beyond “unorthodox way of doing that” and state “I don’t think that does what you want”.


Why do you believe redeclaring it does anything ?

Say when I declare an int array I get an empty array full of zeros. I would assume that by declaring it again I would once again get an empty array full of zeros, effectively resetting it.

You assume wrong on two counts:

  1. Automatic variables are uninitialized. Reference.
  2. Redeclaring something does nothing to the original storage (I expect most compilers to at least warn about the duplicate declaration, maybe that warning is hidden by the particle tools.)
1 Like

Well, you can’t “redeclare” a variable this way. What actually happens is you declare a new variable with the same name, which hides whatever previous variable it was. In your case, this new variable lasts just one line of code (delay(1000)) and disappears with closing bracket of if() block containing the declaration. Compiler even may discard the declaration as you don’t use the variable in its scope at all.

See here for variable scope (your case is third example on the page).