This section covers the methods and techniques for splitting a string containing comma-separated values into individual components. Both Arduino and MicroPython platforms offer distinct ways to parse and manipulate strings, making it essential to understand the differences and similarities. This knowledge is crucial for effectively managing data received from sensors, user inputs, or communication interfaces in your ESP32 projects.
What We Will Learn in This Section
In this section, you will learn how to split a string containing comma-separated values into individual parts using both Arduino and MicroPython.
Why Is This Lesson Important to You?
Understanding how to parse and manipulate comma-separated data is crucial for handling sensor readings, configuration settings, and data packets in IoT applications. This skill is fundamental for processing data efficiently on microcontroller platforms like the ESP32.
Components List
Arduino and MicroPython for ESP32" is your go-to guide for mastering ESP32 projects with clear examples and practical code. For free reading, visit Mechatronics Lab and grab your copy here
Code Arduino
// Arduino code to split a comma-separated string
String text = "Sarful,Lima,Rowsoni"; // an example string
String message = text; // holds text not yet split
int commaPosition; // the position of the next comma in the string
void setup()
{
Serial.begin(9600);
while(!Serial); // Wait for serial port (Leonardo, 32-bit boards)
Serial.println(message); // show the source string
do
{
commaPosition = message.indexOf(',');
if(commaPosition != -1)
{
Serial.println( message.substring(0,commaPosition));
message = message.substring(commaPosition+1, message.length());
}
else
{ // here after the last comma is found
if(message.length() > 0)
Serial.println(message); // if there is text after the last comma,
// print it
}
}
while(commaPosition >=0);
}
void loop()
{
// Empty loop, no functionality needed here
}
Here's an explanation of the code:
1. Variable Declarations
String text = "Sarful, Lima, Rowsoni"; // an example string
String message = text; // holds text not yet split
int commaPosition; // the position of the next comma in the string
String text = "Sarful, Lima, Rowsoni";: Defines a string variable text that contains a comma-separated list of names.
String message = text;: Copies the text string into message. This message variable will be manipulated to extract and print individual names.
int commaPosition;: Declares an integer variable commaPosition to store the position of the next comma found in the message string.
2. Setup Function
void setup()
String text = "Sarful, Lima, Rowsoni"; // an example string
String message = text; // holds text not yet split
int commaPosition; // the position of the next comma in the string
void setup(): Runs once when the Arduino starts.
Serial.begin(9600);: Initializes serial communication at a baud rate of 9600.
while(!Serial);: Waits until the serial port is ready, which is important for boards like the Arduino Leonardo or other 32-bit boards.
3. Displaying the Original String
Serial.println(message); // show the source string
Serial.println(message);: Prints the original message string to the Serial Monitor.
4. Splitting the String
do
{
commaPosition = message.indexOf(', ');
if(commaPosition != -1)
{
Serial.println( message.substring(0, commaPosition));
message = message.substring(commaPosition+1, message.length());
}
else
{
if(message.length() > 0)
Serial.println(message);
}
}
while(commaPosition >=0);
do {... } while(commaPosition >=0);: This loop continues until all commas have been processed.
commaPosition = message.indexOf(', ');: Finds the position of the first comma in the message string. If no comma is found, indexOf returns -1.
if(commaPosition != -1): If a comma is found, the following code executes:
Serial.println(message.substring(0, commaPosition));: Extracts and prints the substring from the start of message up to the comma's position.
message = message.substring(commaPosition+1, message.length());: Updates message to remove the part that was just printed, effectively moving on to the next segment of the string.
else {... }: If no more commas are found (i.e., commaPosition is -1):
if(message.length() > 0): Checks if there's any text left in message. If so, it prints the remaining part.
5. Loop Function
void loop()
{
// Empty loop, no functionality needed here
}
void loop(): This function is empty and does not perform any actions. It is required in an Arduino sketch, but no repeated actions are needed here since the string-splitting logic is entirely handled in setup().
Code MicroPython
# MicroPython code to split a comma-separated string
text = "Sarful,Lima,Rowsoni" # an example string
message = text # holds text not yet split
while ',' in message:
part, _, message = message.partition(',')
print(part)
if message: # print the last part if any
print(message)
Here's an explanation of the MicroPython code:
1. Variable Declarations
text = "Sarful, Lima, Rowsoni" # an example string
message = text # holds text not yet split
text = "Sarful, Lima, Rowsoni": Defines a string variable text containing a comma-separated list of names.
message = text: Copies the text string into message. This message variable will be manipulated to extract and print individual names.
2. Splitting the String with a While Loop
while ', ' in message:
part, _, message = message.partition(', ')
print(part)
while ', ' in message:: This loop continues as long as there is a comma in the message string.
part, _, message = message.partition(', '):
partition(', '): Splits the string message into three parts:
part: The part before the comma.
_: The comma itself (not used here).
message: The part after the comma.
print(part): Prints the part before the comma.
3. Handling the Last Part After the Loop
if message: # print the last part if any
print(message)
if message:: After all commas have been processed, there might be a final part of the string that doesn’t have a comma after it. This checks if there's any text left in message.
print(message): If there is any remaining text, it prints the last part of the string.
Output
Summary
Mastering the skill of splitting comma-separated text into individual parts is essential for handling various data parsing tasks in IoT applications. Whether you choose to implement this in Arduino using String functions or in MicroPython with built-in string methods like partition(), understanding these techniques ensures efficient data management and manipulation. As you delve deeper into ESP32 development, these foundational skills will prove invaluable in designing robust and scalable IoT solutions.
Comments
Please log in or sign up to comment.