Hello,
I have a esp32 (lolin D32) recieveing an RGB message from node-red on a raspbery pi.
this RGB message will be used to set a string of leds to this colour using fast.led
Im not sure how to get the numbers from this message and hold them as a byte value to then send to the LED string
the message i recieve from the serial monitor is:
Message arrived on topic: Garage/Colour. Message: rgb(128, 128, 128)
the only variables in the actual message, "rgb(xxx, xxx, xxx)" will be the numbers
I would then store the numbers as a byte and send them to the leds
Here is what i have so far....
serial.print output for debugging purposes. i know the topic and messageTemp part is correct im just struggling to interpret the message.
if(switch6toggle == HIGH){
if(topic=="Garage/Colour"){
//grab the rgb data from node red then set the output colour
if(messageTemp == ("rgb, (RED, GREEN, BLUE)")){
Serial.print("SET LEDS TO....");
Serial.print(RED);
Serial.print(GREEN);
Serial.print(BLUE);
setAll(RED, GREEN, BLUE);
}
}
char message[] = {"rgb(126, 127, 128)"};
char * ptr;
void setup()
{
Serial.begin(115200);
ptr = strtok(message, "("); //move past (
ptr = strtok(NULL, ","); //next comma is red
byte red = atoi(ptr);
Serial.println(red);
byte green = atoi(strtok(NULL, ",")); //this works too but may not be so easy to understand
Serial.println(green);
ptr = strtok(NULL, ","); //next comma is blue
byte blue = atoi(ptr);
Serial.println(blue);
}
void loop()
{
}
Thank you for the reply,
i have been doing a lot of playing around but im still stuck.
this part works great:
char message[] = {"rgb(126, 127, 128)"};
char * ptr;
ptr = strtok(message, "("); //move past (
ptr = strtok(NULL, ","); //next comma is red
byte RED = atoi(ptr);
Serial.print("SET LEDS TO....");
Serial.println(RED);
ptr = strtok(NULL, ","); //next comma is green
byte GREEN = atoi(ptr);
Serial.println(GREEN);
ptr = strtok(NULL, ","); //next comma is blue
byte BLUE = atoi(ptr);
Serial.println(BLUE);
as expected it outputs 126 127 and 128
I cant figure out how to use the incoming String of data.
i think i have converted it to a charArray so it can be used, but using that converted data to use causes either invalid conversions during a compile or crashes when the data is converted/used.
messageTemp is the incoming data from MQTT.
messageTemp.toCharArray(charbuf, 20);
char message[] = {"charbuf"};
char * ptr;
ptr = strtok(message, "("); //move past (
ptr = strtok(NULL, ","); //next comma is red
byte RED = atoi(ptr);
Serial.print("SET LEDS TO....");
Serial.println(RED);
ptr = strtok(NULL, ","); //next comma is green
byte GREEN = atoi(ptr);
Serial.println(GREEN);
ptr = strtok(NULL, ","); //next comma is blue
byte BLUE = atoi(ptr);
Serial.println(BLUE);
I think im heading in the right direction??
Thanks.
"I cant figure out how to use the incoming String of data."
If you capture the data as a string, then just use the String functions to parse out the data. Otherwise you might use something like below to convert the String variable into a character array.
If you capture the data as a string, then just use the String functions to parse out the data.
This is exactly the sort of sloppy use of language that causes confusion
A string in C++ is not the same as a String
If the message is in a string (a zero terminated array of chars) then use strtok() to split it
If the message is in a String (an object of the String library) then use the String functions to split it
Thanks for the help, I have managed to do what i needed to using the String functions.
its not the best way but it works and im happy with that.
if(switch6toggle == HIGH){
if(topic=="Garage/Colour"){
int posred = messageTemp.indexOf('('); //index the first position
String red = messageTemp.substring(posred +1, posred +4); //substring of red using refrence position
int posgrn = messageTemp.indexOf(','); //index the next position
String green = messageTemp.substring(posgrn +1, posgrn +5); //substring of green using refrence position
int posblu = messageTemp.indexOf(')'); //index the last position
String blue = messageTemp.substring(posblu -3, posblu); //substring of blue using refrence positions
RED = red.toInt(); //convert the substrings to an Int so it can be used
GREEN = green.toInt();
BLUE = blue.toInt();
Serial.print("Red: ");
Serial.println(RED);
Serial.print("Green: ");
Serial.println(GREEN);
Serial.print("Blue: ");
Serial.println(BLUE);
As long as it works for you then OK, but be aware that Strings (uppercase S) have a reputation for fragmenting memory which is undesirable in the small memory footprint in most microcontrollers