Pubsub callback advice

Hi all,

I have used Pubsub for a while now with simple payloads, but am not sure how to handle this.

I have the below sketch with values for RED, GREEN and BLUE (0 to 1023 accepted). I want to send these to the Arduino sketch from node red via mqtt as a single payload, and somehow split the payload into three int variables which can be passed to the "RGB_colour" function.

I 'think' it can be done by using the payload and length, and reading each part separately. Maybe strlen?
But I get mixed up with the length of bits/bytes involved in 0000 to 1023..

Callback components:
topic const char[] - the topic the message arrived on
payload byte[] - the message payload
length unsigned int - the length of the message payload

Any pointers would be appreciated (no pun intended)! :slight_smile:

Sketch:

// 14-02-2021 by 877dev, based on https://create.arduino.cc/projecthub/muhammad-aqib/arduino-rgb-led-tutorial-fc003e
// Wemos D1 mini and 5V +GRB LED strip, controlled via PWM.
//Wiring (Wemos - LED strip): 5V  -  5V, D1  -  R, D2  -  G, D3  -  B
//Note pull pin low to turn that channel on
//i.e. R=0, G=1023, B=1023 for RED colour.

//v001 basic test with correct colours, just scrolls through 

int red_light_pin = D1;
int green_light_pin = D2;
int blue_light_pin = D3;
int interval = 2000;

void setup() {
  Serial.begin(115200);
  pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
}

void loop() {
  Serial.print("Red: ");
  RGB_color(0, 1023, 1023); // Red
  delay(interval);

  Serial.print("Green: ");
  RGB_color(1023, 0, 1023); // Green
  delay(interval);

  Serial.print("Blue: ");
  RGB_color(1023, 1023, 0); // Blue
  delay(interval);
}

void RGB_colour(int red_light_value, int green_light_value, int blue_light_value)
{
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);
}

Well i would simply first find out what the payload actually looks like. Just do a simple passthrough first. These values must be sent using separators... or not. etc.

Put the data into a struct and send that

Deva_Rishi:
Well i would simply first find out what the payload actually looks like. Just do a simple passthrough first. These values must be sent using separators... or not. etc.

Hi, I can make the payload anything I want, as I am sending it from node red. The issue is splitting the payload into three individual variables. What doe you mean by separators?

UKHeliBob:
Put the data into a struct and send that

Thanks, I don't know how to do that, I'll start googling...

Thanks, I don't know how to do that, I'll start googling...

To be honest, I don't know if you can send a struct, but you can build a comma separated string, send that and parse it when received

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