Hello,
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() {
Serial.begin(9600);
}
void loop() {
int a =random(10);
int b = random(5);
Serial.println(a + sep +b);
delay(1500);
}
ESP side:
#define RXD2 16
#define TXD2 17
void setup() {
// Serial2.begin(baud-rate,protocol,RX pin, TX pin);
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Data received:");
Serial.println(Serial2.readString());
delay(200);
}
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.
Thanks,
Snor
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
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
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
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.
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
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.