Hi, I'm having a project that can send 3 data to from Arduino to ESP32 and send these data to google sheets using substrings.
This is the data that the Arduino send to the ESP32:
1,3,1
And then ESP32 will send these data to Google Sheets and in Google Sheets will look like this:
I'm really not entirely sure what it is you're asking, as your text doesn't match up with your sketch. Why, for example, is your sketch looking for "///" when you don't mention that? And why do you have what appears to be an optional fourth field that you also don't mention?
Just going on your text, to split up three numbers separated by commas using substring, here's an example of how it's done. Let me also say that I don't recommend doing it this way! String in the Arduino world will cause you grief sooner or later. I'd suggest using character arrays and maybe even learning the ins and outs of strtok. Having said that, here's an example of how to do it with the String class and substring and indexOf.
The main problem is this one, let's make your example is 1,2,3. The output must be 1.00, 2.00, 3.00 right? But for some reason my output shows 1.00, 2.00, 1.00. Let's take another example, the input is 4, 0, 1 but with your code for some reason the output is 4.00, 0.00, 4.00. You get my point right? Anyways I think we're getting closer to my expectation!
Oh wait, clumsy me! I found out what caused the problem. I forgot to change a dot to a comma, like the input must be 1, 2, 3 not 1, 2. 3!!
Anyways it works flawlessly, thank you so much!!
there are multiple way of parsing "1,2,3" @van_der_decken mentioned using strtok, e.g.
// strtok example - parse tokens into float values
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char str[] ="1,2,3"; // text to tokenise
char * pch; // pointer to tokens
Serial.print("Splitting string ");
Serial.print(str);
Serial.println(" into tokens:");
pch = strtok (str,","); // get first token
while (pch != NULL)
{
Serial.print("string found ");
Serial.println(pch); // print it
float x=atof(pch); // convert to float
Serial.print(" float ");
Serial.println(x); // print
pch = strtok (NULL, ","); // get next token
}
}
void loop() {}
gives
Splitting string 1,2,3 into tokens:
string found 1
float 1.00
string found 2
float 2.00
string found 3
float 3.00
worth noting that the strtok() code will work on a UNO, nano, etc which the code of post 2 won't as sscanf() float conversions do not work on such low power microcontrollers