How do I split a string/int in Arduino to three parts?

I am working on a code that basically receives coordinates from a Pi over the i2c bus. Everything at the moment works fine as I can transmit information to and from both devices with ease. Before you ask "But wait how do you catch these?" That is not an issue as I made a code to catch and store coordinates:

void convertToInt() {
  int n = received_str.toInt();
  if (n > 0) {
    Serial.println(received_str);
    received_str = "";
  } else if (received_str.substring(0) == "(") {
    Serial.println(received_str);
    received_str = "";
  } else {
    received_str = "";
  }
}

However, I want to split incoming coordinates into three separate variables, e.g.

Coordinate = (20,90,22)

To become:

x = 20
y = 90
z = 22

I have looked at functions such as strtok and readStringUntil but the former kept throwing conversion errors even when it was the same type, whilst the latter just said that it was not part of the String class. Did I just select the wrong methods in doing this? How do I achieve this?

Are always the coordinates in the form (xx,yy,zz) or can they have a variable number of digits or a negative sign?

How did Strings get involved in this process?

You are transmitting strings over the i2c bus?

Is that bad? In case if it is, what should be done to compensate?
I don't usually send strings over i2c and keep it to numbers, these numbers get translated on each device as they have a function of status codes translating each number, but for coordinates I had no choice; just using a normal substring function would split the number at the wrong places whenever the number would get bigger, so to me atleast in my train of thought, I did not want the e.g. coordinate 908833 to come out as 908, 8, 33

You have an Arduino and a RPI. Which is the master and which is the slave?

Typically the slave would be putting the data into a struct or an array and sending as binary in a Wire.onRequest() to the master Wire.requestFrom(). Alternatively, the master could be pushing data to the slave which uses a Wire.onReceive() to deal with what the master sent it. What arrangement are you using?

What arduino are you using? There may be an issue with endianess, integer sizes and packing between the rpi and the arduino?

If you really want to send text between the pi and the Arduino, then the data is best structured with separators and parsed with strtok(). Since this is i2c and a response to a Wire.requestFrom()
you will need to have a fixed number of bytes and may need to send dummy placeholders up to the max number size like "zzzz1,zzz12, zz123"

Can you show us what didn't work using this approach?

Thanks for your response.
The Raspberry Pi is the Master here, it is a Raspberry Pi Model 3b+. The Arduino is the slave, which is an Arduino Nano(received it from college so I'm not sure what exact model it is, but definitely not BLE Sense).

I did mine a little bit differently, and that wasn't out of preference but out of what I could conclude on the little findings I could find on I2C(most comms code between Pi and Arduino is just SerialUSB).
The slave(Arduino) does not put data to be sent as a struct or an array, I just send normal integers through onRequest. A function within the parameter of onRequest handles the data writing which gets sent to the Pi.

void requestEvent() {
  Wire.write(toSend);
}

The Pi then receives this number, goes through a conditional statement and outputs it with ease on the shell:
image
As API Status calls usually use numbers, I followed the same concept here.

The master only pushes the necessary data to the Arduino such as coordinates or commands, it does this using Python to split the strings into separate bytes, then writing said bytes to the Arduino to be basically recombined and stored as a string there. I never even knew of requestFrom, only of:

  Wire.onReceive();
  Wire.onRequest();

I also found a solution to the split problem being sscanf, might you know of any proper documentation that shows why it works? Or should I avoid that altogether?

sscanf is a fine way to parse your text. There are plenty of online C and C++ references which Mr. Google will show you.

Thanks but for some reason I could not find any documentations about sscanf as ridiculous as that sounds, only requests for help about sscanf not working.

sscanf() is a C library function. There are literally thousands of reference sites for the C/C++ libraries.

https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm

Thanks for the link, very weird I could not find any sites at all about it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.