Reputation: 515
So i've managed to wire up an ESP-01 module to my arduino, now im attempting to parse the +IPD responses the chip gives over a serial connection. Im not really all that handy with C++ but this is what i've managed to come up with after researching a bunch online:
#include <SoftwareSerial.h>
SoftwareSerial ESP8266(2, 3); // RX | TX
int baud = 9600;
void setup() {
ESP8266.begin(baud);
Serial.begin(baud);
Serial.println("--- Start ---");
}
void loop() {
if (ESP8266.available()) // check if the esp is sending a message
{
Serial.println("Something received");
delay(500);
if (ESP8266.find("%<"))
{
Serial.println("--------------- DEBUG ----------------A");
char temp = {char(ESP8266.read())};
while ((temp != '%') && (ESP8266.available())) {
Serial.print(temp);
temp = char(ESP8266.read());
}
Serial.println("\n--------------- END DEBUG ----------------");
}
}
}
The standard response the chip gives when receiving a message is as follows:
+IPD,<len>:<Message>
+IPD,0,14:%<255,128,0%
The data im trying to send - random RGB values (using '%<' and '%' as flags/markers):
%<255,128,0%
From here, i've managed to write the above code, which will print out the following over serial:
So i've managed to get it to print the values i need over Serial, but i cant seem to store them in an array of some sort to do things with the data.
Things i've tried:
Ideally, i'd like a function that reads the +IPD value, extracts the RGB data and then splits it into a 3 index array, like so:
rgbArray = {124, 234, 96};
Any and all help is super appreciated!
Upvotes: 0
Views: 1620
Reputation: 515
Ended up going with a different way of doing things:
Managed to read about readStringUntil(''); in some dark corner of the web. So i came up with a super dirty implementation - but it works:
Assume your input string is:
+IPD,0,14:%<255,128,0%
Then do:
if (ESP8266.available()) // check if the esp is sending a message
{
delay(500);
if (ESP8266.find("%<"))
{
r = ESP8266.readStringUntil(',');
g = ESP8266.readStringUntil(',');
b = ESP8266.readStringUntil('%');
}
}
Upvotes: 0
Reputation: 75062
To store the input to an array, just allocate an array and store the data there.
Serial.println("--------------- DEBUG ----------------A");
int receivedLength = 0;
char data[16];
char temp = ESP8266.available();
while ((temp != '%') && (ESP8266.available())) {
Serial.print(temp);
if (receivedLength < 16) data[receivedLength++] = temp;
}
for (int i = 0; i < receivedLength; i++) Serial.print(data[i]);
Serial.println("\n--------------- END DEBUG ----------------");
Alternatively, you can do conversion to integers while reading like this:
Serial.println("--------------- DEBUG ----------------A");
int rgbSize = 0;
int rgbArray[3];
int currentValue = 0;
char temp = ESP8266.available();
while ((temp != '%') && (ESP8266.available())) {
Serial.print(temp);
if (temp == ',') {
if (rgbSize < 3) rgbArray[rgbSize++] = currentValue;
currentValue = 0;
} else {
currentValue = currentValue * 10 + (temp - '0');
}
}
if (rgbSize < 3) rgbArray[rgbSize++] = currentValue;
for (int i = 0; i < rgbSize; i++) {
if (i > 0) Serial.print(',');
Serial.print(rgbArray[i]);
}
Serial.println("\n--------------- END DEBUG ----------------");
Upvotes: 1