Splitting string to int with ESP serial communication

I'm trying to use serial communication between my arduino and esp.
I send some data from my arduino to my esp, which are integers seperated by a ;
I receive those strings, but now I want to split those to their original integer values, so I can later send them to a database.
I'm using following code to test it (it's without the database yet).
Arduino side:

String sep = ";";
void setup() {


void loop() {
  int a =random(10);
  int b = random(5);
  Serial.println(a + sep +b);


ESP side:

#define RXD2 16
#define TXD2 17

void setup() {
  // Serial2.begin(baud-rate,protocol,RX pin, TX pin);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);


void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Data received:");


I receive following on the ESP side:
Received data: 4;2 or 5;2 etc
But now I want to split it like the following:
number1 = 'the first number'
number 2 = 'second'
Anyone who has an idea how I can do this? I tried multiple things and it didn't work.
I use an ESP32 and arduino uno.

Yea, a code snippet, hope it helps.

      int commaIndex = sTmp.indexOf(',');
      int ScanPoints =  sTmp.substring(0, commaIndex).toInt() ;
      sTmp.remove( 0, (commaIndex + 1) ); // chop off begining of message
      commaIndex = sTmp.indexOf(',');
      int ScanDirection = sTmp.substring(0, commaIndex).toInt() ; // left to right?
      sTmp.remove( 0, (commaIndex + 1) ); // chop off begining of message
      commaIndex = sTmp.indexOf(',');
      for (CellCount; CellCount <= ScanPoints; CellCount++)
        commaIndex = sTmp.indexOf(',');
        int iValue = sTmp.substring(0, commaIndex).toInt() ;
        sTmp.remove( 0, (commaIndex + 1) ); // chop off begining of message

As you are using a String to hold the received data you can use the String indexOf() function to find the semicolons then substring() to split the String up followed by toInt() to convert to an int

There are other methods available if you use strings instead of Strings

What is the difference between strings and Strings?
So I can do the following:

To find where there is a ; and then use substring to split it?

  • string - an array of chars terminated by a zero
  • String - an object created by the String library

There is much discussion here and elsewhere about the merits/demerits of using Strings in the small memory footprint of the average Arduino as they can cause memory fragmentation. This is not so much of a problem with larger memory models as used by the ESP chips

1 Like

Ah ok thanks.
Does splitting the string in subs etc make the code run slower? (and with slower I mean much slower)

Calling a function to do something will always incur some time penalty but the advantages of using functions far outweighs the disadvantages. In any case, the compiler will do its best to optimise the code for you

Ok, thanks I will try this tomorrow to send data to my database.
Thanks for the help everyone!

For my database there's a variable, which represents a duty cycle of a PWM wave, so it's a float.
Anyone who has an idea how I can convert their string to a float?
I don't think toFloat() works for arduino and I don't find a working alternative.

Why do you think that? Did you try?

Yes I did but the problem is it didn't became orange so I guess it doesn't work.
Here's a screenshot to show that it isn't orange
Well I tried it in my test code, and it works. But I don't understand how it can work without changing to orange like with toInt :stuck_out_tongue:

Keyword highlighting is not to be relied on.

See this discussion of highlighting , in particular post#2 from @pert.

If you are using Strings on the ESP check out my tutorial Taming Arduino Strings
It has guideline for avoiding memory fragmentation in your code and how to detect it when it happens.

On the ESP32, the web support uses Strings extensively so you can not avoid them.
The tutorial has suggestions for how to cope ESP32 memory leaks and String fragmentation in the web support library.

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