Go Down

Topic: hex or rgb from a MQTT message - Conversion or a better way (Read 4612 times) previous topic - next topic

eeze

Right now i'm facing watch dog errors as I try to use a for loop to pass the r,g,b values to my 300 LED's.

Code: [Select]

void fnc_ColorChange() {
//fill_solid(leds, NUM_LEDS /*number of leds*/, 0xFF44DD); 
    Serial.print("Inside fnc_ColorChange attempted to change color");
    // Get rid of '#' and convert it to integer
    long number = strtol( &payload_buff[1], NULL, 16);

// Split them up into r, g, b values
    long r = number >> 16;
    long g = number >> 8 & 0xFF;
    long b = number & 0xFF;
    // String below causes issues
    // String value = String((char*)payload_buff);
    // split string at every "," and store in proper variable
    // convert final result to integer
    /*
    valueR = value.substring(0,value.indexOf(',')).toInt();
    valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    valueB = value.substring(value.lastIndexOf(',')+1).toInt();
    */
   
    Serial.println("The colors received are");
    Serial.print(r);
    Serial.print(g);
    Serial.print(b);
    Serial.println("");
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( r, g, b);
      FastLED.show();
    } 
   
   
   
}


Here is the serial output

Code: [Select]

Entering loop()
Checking for new messages
CallBack Function Trigged
Message arrived for topic [/home/1-ESP8266-OutDoor/Color] with a length of 12
The char* version of the payload is rgb(0,0,255)
End of Callback

payload_buff is rgb(0,0,255) before fnc_processMQTTmessage inside loop()
ranCallback is true, processing message
Topic_buff is/home/1-ESP8266-OutDoor/Color
payload_buff is rgb(0,0,255)
Matched topic Color, calling function
Inside fnc_ColorChange attempted to change colorThe colors received are
000

Clearing message buffers
-------------End of Loop---------------
Entering loop()
Checking for new messages

payload_buff is  before fnc_processMQTTmessage inside loop()
Clearing message buffers
-------------End of Loop---------------

 ets Jan  8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld


I've got all the other code going and would like to ask for some help

Option #1
Send the code via hex as a char
I tried this sending to the fill_solid function of fastLED.
Given error as couldn't convert a char* to uint8

Option #2
I tried to present the rgb values directly to the code and use a for i loop (leds.setRGB( r, g, b);)
but that is causing the watchdog reset.


I see the final stretch of my project and major headwinds.

Thanks for the look

PaulRB

Read the forum rules. Following them helps us to help you. Yes, you have broken several already.

eeze

Thanks PaulRB

Had to go through the list of forum points to check what I may have done.
For those that may stumble here - Forum Recommendations
http://forum.arduino.cc/index.php/topic,148850.0.html

I thought by posting the snippet of code I'm working on would make it easier as the rest works fine.
But to point #16  my code is below

For point #15 - 
Rather than posting what I think are the options,  I'll state my issue as
I'm sending in either a char as "0xff00ff" or as "rgb (50,100,150)" to my board using MQTT and the fill_solid in MQTT compaints about a unint8 or CRGB. I've tried to send it the rgb codes and using a loop to put the r,g,b values there.  It's creating a watchdog soft reset. If I disable the soft watchdog, it triggers a hard reset.

Point #13 - Forum Etiquette - This question is specific to the fastLED function so I created a seperate topic in the LED section as my previous question was related to MQTT.
"

Point #6 - I think my topic is descriptive

Point #7 - Yep, used the code/code

Point #10 - My spelling may be poor at times, but I try to follow sentence structure.

Point #11 - Equipment is a ESP8266,  WS2812B,  5V adapter, an SMD level-shifter.
"However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem "


I'll be sure to follow these closer where applicable.


Code: [Select]



// Version 0.1 Setup
// Version 0.2 Adding MQTT
// Version 0.3 Turn LED a color by MQTT message - Currently not working

/////////////////////
// Inclusions      //
/////////////////////
#include <FastLED.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>

/////////////////////
//    Definitions  //
/////////////////////

#define NUM_LEDS 300
#define LED_TYPE WS2812B
#define DATA_PIN 4
CRGB leds[NUM_LEDS];

//////////////////////
// WiFi Definitions //
//////////////////////

const char* ssid     = "|$n0w|";
const char* pass = "$n0wb@llz";
char callbackString;

/////////////////////
//     Variables   //
//////////////////////
char payload_buff[100];
char topic_buff[50];
int strcomparison;
bool ranCallback;
 
/////////////////////
// MQTT Definitions //
//////////////////////
#define mqtt_server "cloudmqtt.com"
#define mqtt_user "mysensorname"
#define mqtt_password "sensorpassword
#define TopicOnOff "/home/1-ESP8266-OutDoor/OnOff"
#define TopicColor "/home/1-ESP8266-OutDoor/Color"

///////////////////////
// Other Definitions //
///////////////////////
WiFiClient espClient;
PubSubClient client(espClient);


// This is the MQTT Message Arrival Function which puts messages into topic_buff and payload_buff for action
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("CallBack Function Trigged");
  Serial.print("Message arrived for topic [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.print("with a length of ");
  Serial.print(length);
  Serial.println();

  byte len = _min(strlen(topic), sizeof(topic_buff) - 1);
  memcpy(topic_buff, topic, len);
  topic_buff[len] = 0;

  len = _min(length, sizeof(payload_buff) - 1);
  memcpy(payload_buff, payload, len);
  payload_buff[len] = 0;
 
  Serial.print("The char* version of the payload is ");
  Serial.print(payload_buff);
  ranCallback = true;

// Set this so I can process the message
  Serial.println("");
  Serial.println("End of Callback ");
 
// trying to move the char* to a string for easy comparison and use outside of this function 
//  topic_buff = topic;   
 
  }  // End of Callback

 

void fnc_ColorChange() {
//fill_solid(leds, NUM_LEDS /*number of leds*/, 0xFF44DD); 
    Serial.print("Inside fnc_ColorChange attempted to change color");
    // Get rid of '#' and convert it to integer
    long number = strtol( &payload_buff[1], NULL, 16);

// Split them up into r, g, b values
    long r = number >> 16;
    long g = number >> 8 & 0xFF;
    long b = number & 0xFF;

    // String below causes issues
    // String value = String((char*)payload_buff);
    // split string at every "," and store in proper variable
    // convert final result to integer
    /*
    valueR = value.substring(0,value.indexOf(',')).toInt();
    valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    valueB = value.substring(value.lastIndexOf(',')+1).toInt();
    */
   
    Serial.println("The colors received are");
    Serial.print(r);
    Serial.print(g);
    Serial.print(b);
    Serial.println("");
//  This currently causes a watchdog soft reset.   Disable watchdog causes a hard reset.  Time to execute?
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( r, g, b);
      FastLED.show();
    } 
   
   
   
}

void fnc_OnOff() {
 
}

// This is called by main loop when ranCallback is true
void fnc_processMQTTmessage() {
    Serial.print("Topic_buff is");
    Serial.print(topic_buff);
    Serial.println();
    Serial.print("payload_buff is ");
    Serial.print(payload_buff);
    Serial.println();
    // Will execute if the topic is OnOff
    strcomparison = strcmp(topic_buff, TopicOnOff);
    if (strcomparison == 0) { //   Work to be done here
    Serial.println("Matched topic OnoFF, calling function");
     fnc_OnOff();
    } 
// Will execute if the topic is Color
    strcomparison = strcmp(topic_buff, TopicColor);
    if (strcomparison == 0) { //   Work to be done here
    Serial.println("Matched topic Color, calling function");
    fnc_ColorChange();
    } 
}





void setup() {
  // Initial Array
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

  // Start WiFi Connection
  Serial.begin(115200);
  Serial.println("");
  pinMode(D1, OUTPUT);
  WiFi.begin(ssid, pass);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  //  END WiFi Connection

    //MQTT Connection
   client.setServer(mqtt_server, 15557);  // 15557 is the port given my cloudmqtt
   client.setCallback(callback);
}

void reconnect() {      // This is for the MQTT connection
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    //  //client.connect("clientID", "mqtt_username", "mqtt_password");
    // The clientID must be unique otherwise disconnections will happen!
    if (client.connect("account_name", mqtt_user, mqtt_password)) {
      Serial.println("connected");
      client.subscribe(TopicOnOff);
      client.subscribe(TopicColor);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void clearMQTT() {   //  Clear the MQTT Payload
  memset(payload_buff, 0, sizeof(payload_buff));
  memset(topic_buff, 0, sizeof(topic_buff));
}  // End of clearPaylod()


void loop() {
  Serial.println("Entering loop()");
// First make sure MQTT is connected
  {
  if (!client.connected()) {
    reconnect();           }
  }  //Close If
 
// Serial.println("Attempting a client.publish to " TopicOnOff);
// client.publish(TopicTemperature, temperatureString);

// Start MQTT Message Function  //
    Serial.println("Checking for new messages"); 
    client.loop(); // This will check the callback function to see if there is a message"
// Taking action on MQTT Messages if ranCallback is set by client.loop()
    Serial.println();
// This is here to confirm what the payload_buff looks like in loop() before processing - currently not working...
    Serial.print("payload_buff is ");
    Serial.print(payload_buff);
    Serial.print(" before fnc_processMQTTmessage inside loop()");
    if (ranCallback == true) {
    Serial.println();
    Serial.println("ranCallback is true, processing message");
    fnc_processMQTTmessage ();
    }
// Setting ranCallback to False to prevent rerun
    ranCallback = false;
// Clearing Array
   Serial.println();
   Serial.println("Clearing message buffers");
   clearMQTT();
   Serial.println("-------------End of Loop---------------");
   delay(3000);

// End of Loop
}





PaulRB

Thanks.

Have you tried
Code: [Select]
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( r, g, b);
    } 
    FastLED.show();
   

eeze

I hadn't tried this but after I did, no more watchdogs.  Thanks PaulRB

Very silly mistake I did. Embarrassing actually. 

Thanks.

Have you tried
Code: [Select]
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( r, g, b);
    } 
    FastLED.show();
   

However, it seems my original issue is still here.

In a normal sketch i can use where the hex is the color and it works a treat.
fill_solid(leds, NUM_LEDS /*number of leds*/, 0xFF44DD);

However, in my sketch I don't know how to pass that hex value that I can send in from MQTT. It's a byte (payload) at first, which I convert to a char array.

But I get an error that it should unsigned uint8

If I try to use the r,g,b method as last posted, i get 0,0,0 as seen from serial output

Code: [Select]

-------------End of Loop---------------
Entering loop()
Checking for new messages
CallBack Function Trigged
Message arrived for topic [/home/1-ESP8266-OutDoor/Color] with a length of 15
The char* version of the payload is rgb(0,255,127)
End of Callback

payload_buff is rgb(0,255,127)  before fnc_processMQTTmessage inside loop()
ranCallback is true, processing message
Topic_buff is/home/1-ESP8266-OutDoor/Color
payload_buff is rgb(0,255,127)
Matched topic Color, calling function
Inside fnc_ColorChange attempted to change colorThe colors received are
000
About to attempt FastLED.show()


May I ask which method is better? Seems I have a lot of learning to do about conversions and data types.

Update:  After putting in some more serial.print's to show the output, it seems the issue is with my conversion.



eeze

I originally wanted to use fill_solid(leds, NUM_LEDS /*number of leds*/, 0xFF44DD);  but it seems maybe the for loop is better? Is there a way to pass the hex value directly or the rgb ?

But it seems that fnc_ColorChange that the colors are 000

but I'm too amateur to know why.

Code: [Select]


// Version 0.1 T.S  Setup
// Version 0.2 T.S  Adding MQTT

/////////////////////
// Inclusions      //
/////////////////////
#include <FastLED.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>

/////////////////////
//    Definitions  //
/////////////////////

#define NUM_LEDS 300
#define LED_TYPE WS2812B
#define DATA_PIN 4
CRGB leds[NUM_LEDS];

//////////////////////
// WiFi Definitions //
//////////////////////

const char* ssid     = "";
const char* pass = "";
char callbackString;

/////////////////////
//     Variables   //
//////////////////////
char payload_buff[100];
char topic_buff[50];
int strcomparison;
bool ranCallback;
 
/////////////////////
// MQTT Definitions //
//////////////////////
#define mqtt_server ""
#define mqtt_user ""
#define mqtt_password ""
#define TopicOnOff "/home/1-ESP8266-OutDoor/OnOff"
#define TopicColor "/home/1-ESP8266-OutDoor/Color"

///////////////////////
// Other Definitions //
///////////////////////
WiFiClient espClient;
PubSubClient client(espClient);


// This is the MQTT Message Arrival Function which puts messages into topic_buff and payload_buff for action
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("CallBack Function Trigged");
  Serial.print("Message arrived for topic [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.print("with a length of ");
  Serial.print(length);
  Serial.println();
  // char *callbackString = (char *) payload;
  // callbackString[length] = '\0';    // Adds a terminator to end of string based on length of current payload
  // char *payload_buff = (char *) payload;
 //  payload_buff[length] = '\0';

  byte len = _min(strlen(topic), sizeof(topic_buff) - 1);
  memcpy(topic_buff, topic, len);
  topic_buff[len] = 0;

  len = _min(length, sizeof(payload_buff) - 1);
  memcpy(payload_buff, payload, len);
  payload_buff[len] = 0;
  
  Serial.print("The char* version of the payload is ");
  Serial.print(payload_buff);
  ranCallback = true;
// Set this so I can process the message
  Serial.println("");
  Serial.println("End of Callback ");
  
// trying to move the char* to a string for easy comparison and use outside of this function  
//  topic_buff = topic;  
  
  }  // End of Callback

  
  /*  //Old Method of String Comparison
  int strcomparison = strcmp(topic, TopicOnOff);
  if (strcomparison == 0) { //   Work to be done here
    Serial.println("Matched OnOff");
    if (length == 2) {
      digitalWrite(D1, HIGH);
      Serial.println("Sending PIN High as message was On");
    }
    if (length == 3) {
      digitalWrite(D1, LOW);
      Serial.println("Sending PIN Low as message was Off");
    }
                         } //end If
                        
  strcomparison = strcmp(topic, TopicColor); //   Work to be done here
  if (strcomparison == 0) {
    Serial.println("Matched Color");
                             } //end If
  */


void fnc_ColorChange() {
//fill_solid(leds, NUM_LEDS /*number of leds*/, 0xFF44DD);  
    Serial.print("Inside fnc_ColorChange attempted to change color");
    Serial.print("");
    Serial.print("payload_buff is ");
    Serial.print(payload_buff);
    Serial.print("before attempted conversion");
    // Get rid of '#' and convert it to integer
    long number = strtol( &payload_buff[1], NULL, 16);

// Split them up into r, g, b values
    long r = number >> 16;
    long g = number >> 8 & 0xFF;
    long b = number & 0xFF;
    // String below causes issues
    // String value = String((char*)payload_buff);
    // split string at every "," and store in proper variable
    // convert final result to integer
    /*
    valueR = value.substring(0,value.indexOf(',')).toInt();
    valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    valueB = value.substring(value.lastIndexOf(',')+1).toInt();
    */
    
    Serial.println("After conversion the colors are");
    Serial.print(r);
    Serial.print(g);
    Serial.print(b);
    Serial.println("");
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( r, g, b);
    }  
    Serial.println("About to attempt FastLED.show()");
    FastLED.show();
    
    
}

void fnc_OnOff() {
  
}

// This is called by main loop when ranCallback is true
void fnc_processMQTTmessage() {
    Serial.println("Now inside fnc_processMQTTmessage()");
    Serial.print("Topic_buff is");
    Serial.print(topic_buff);
    Serial.println();
    Serial.print("payload_buff is ");
    Serial.print(payload_buff);
    Serial.println();
    // Will execute if the topic is OnOff
    strcomparison = strcmp(topic_buff, TopicOnOff);
    if (strcomparison == 0) { //   Work to be done here
    Serial.println("Matched topic OnoFF, calling function");
     fnc_OnOff();
    }  
// Will execute if the topic is Color
    strcomparison = strcmp(topic_buff, TopicColor);
    if (strcomparison == 0) { //   Work to be done here
    Serial.println("Matched topic Color, calling function");
    fnc_ColorChange();
    }  
}





void setup() {
  // Initial Array
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

  // Start WiFi Connection
  Serial.begin(115200);
  Serial.println("");
  pinMode(D1, OUTPUT);
  WiFi.begin(ssid, pass);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  //  END WiFi Connection

    //MQTT Connection
   client.setServer(mqtt_server, 15557);  // 15557 is the port given my cloudmqtt
   client.setCallback(callback);
}

void reconnect() {      // This is for the MQTT connection
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    //  //client.connect("clientID", "mqtt_username", "mqtt_password");
    // The clientID must be unique otherwise disconnections will happen!
    if (client.connect("tisnow_1313136575", mqtt_user, mqtt_password)) {
      Serial.println("connected");
      client.subscribe(TopicOnOff);
      client.subscribe(TopicColor);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void clearMQTT() {   //  Clear the MQTT Payload
  memset(payload_buff, 0, sizeof(payload_buff));
  memset(topic_buff, 0, sizeof(topic_buff));
}  // End of clearPaylod()


void loop() {
  Serial.println("Entering loop()");
// First make sure MQTT is connected
  {
  if (!client.connected()) {
    reconnect();           }
  }  //Close If
  
// Serial.println("Attempting a client.publish to " TopicOnOff);
// client.publish(TopicTemperature, temperatureString);

// Start MQTT Message Function  //
    Serial.println("Checking for new messages");  
    client.loop(); // This will check the callback function to see if there is a message"
// Taking action on MQTT Messages if ranCallback is set by client.loop()
    Serial.println();
// This is here to confirm what the payload_buff looks like in loop() before processing - currently not working...
    Serial.print("payload_buff is ");
    Serial.print(payload_buff);
    Serial.print(" before fnc_processMQTTmessage inside loop()");
    if (ranCallback == true) {
    Serial.println();
    Serial.println("ranCallback is true, processing message");
    fnc_processMQTTmessage ();
    }
// Setting ranCallback to False to prevent rerun
    ranCallback = false;
// Clearing Array
   Serial.println();
   Serial.println("Clearing message buffers");
   clearMQTT();
   Serial.println("-------------End of Loop---------------");
   delay(3000);

// End of Loop
}






eeze

Based on previous message

here is the outputs.  The message limit was reached so here is the 2nd part

Here is when I attempt to send 0xff00ff

Code: [Select]

Entering loop()
Checking for new messages
CallBack Function Trigged
Message arrived for topic [/home/1-ESP8266-OutDoor/Color] with a length of 2
The char* version of the payload is gb
End of Callback

payload_buff is gb before fnc_processMQTTmessage inside loop()
ranCallback is true, processing message
Now inside fnc_processMQTTmessage()
Topic_buff is/home/1-ESP8266-OutDoor/Color
payload_buff is gb
Matched topic Color, calling function
Inside fnc_ColorChange attempted to change colorpayload_buff is gbbefore attempted conversionAfter conversion the colors are
0011
About to attempt FastLED.show()

Clearing message buffers


Here is when I send RGB of (50,150,250)
Code: [Select]

Entering loop()
Checking for new messages
CallBack Function Trigged
Message arrived for topic [/home/1-ESP8266-OutDoor/Color] with a length of 15
The char* version of the payload is rgb(50,150,250)
End of Callback

payload_buff is rgb(50,150,250) before fnc_processMQTTmessage inside loop()
ranCallback is true, processing message
Now inside fnc_processMQTTmessage()
Topic_buff is/home/1-ESP8266-OutDoor/Color
payload_buff is rgb(50,150,250)
Matched topic Color, calling function
Inside fnc_ColorChange attempted to change colorpayload_buff is rgb(50,150,250)before attempted conversionAfter conversion the colors are
000
About to attempt FastLED.show()

eeze

Does anyone have some examples on how I can convert these variable types?

I'm stills struggling with this.


PaulRB

Code: [Select]
The char* version of the payload is rgb(50,150,250)
The above is the literal interpretation of the message being sent/received. That's why its length is 15. 15 characters.

This code, which is commented out in your sketch:
Code: [Select]
    /*
    valueR = value.substring(0,value.indexOf(',')).toInt();
    valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    valueB = value.substring(value.lastIndexOf(',')+1).toInt();
    */

is closer to being able to interpret that message, compared to this code:
Code: [Select]
    long number = strtol( &payload_buff[1], NULL, 16);

// Split them up into r, g, b values
    long r = number >> 16;
    long g = number >> 8 & 0xFF;
    long b = number & 0xFF;

but even the first code is not quite correct. The first number, the red value, does not begin at the start of the message, it begins after the "(". Also, the last number, the blue value, ends just before the ")".

Put in other words, the message is in text form, not packed into bytes, as you seem to think it is.

Does this give you a bit more of a clue?

eeze

Code: [Select]
The char* version of the payload is rgb(50,150,250)
The above is the literal interpretation of the message being sent/received. That's why its length is 15. 15 characters.

This code, which is commented out in your sketch:
Code: [Select]
    /*
    valueR = value.substring(0,value.indexOf(',')).toInt();
    valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    valueB = value.substring(value.lastIndexOf(',')+1).toInt();
    */

is closer to being able to interpret that message, compared to this code:
Code: [Select]
    long number = strtol( &payload_buff[1], NULL, 16);

// Split them up into r, g, b values
    long r = number >> 16;
    long g = number >> 8 & 0xFF;
    long b = number & 0xFF;

but even the first code is not quite correct. The first number, the red value, does not begin at the start of the message, it begins after the "(". Also, the last number, the blue value, ends just before the ")".

Put in other words, the message is in text form, not packed into bytes, as you seem to think it is.

Does this give you a bit more of a clue?
Absolutely it does. Thanks PaulRB.

The two items you quoted, were attempts at conversion.

The first was taking the RGB, the second was the hex. Both I failed it.

I do see how the my attempts were flawed.  Will give it a go when I get home tomorrow and see.

Thanks!

eeze

Indeed it worked when I sent
(50,150,250) as the message.

I can see that starting to read in at char 0 meant the ( which wasn't what I wanted.

However, I have just uncovered something else.  I receive the color mqtt message as rgb (x,y,z) and during my test was always (x,y,z)

If I pass (50,150,250) into below it works perfectly

Code: [Select]

  int valueR = value.substring(1,value.indexOf(',')).toInt();
  int valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
  int valueB = value.substring(value.lastIndexOf(',')+1).toInt();


I'm struggling to find the way to use IndexOf to ignore anything before the first ( and read all the numbers before the first , into valueR

That way if I sent (50,150,250) it'll start start at the (+1 and read the numbers to the first ,
Same with rgb (50,150,250)  so as to ignore the rgb (  and just read the numbers.

Could you give me a pointer on that?


While posting, i'm wondering if I could do this to create a counter to the first time I see ( and then start my previous attempt at bringing in the values

Code: [Select]

int ignoreXindex = value.substring(0,value.indexOf('(')).toInt();
    Serial.println("");
    Serial.print("The value of ignoreXindex is ");
    Serial.print(ignoreXindex);
    Serial.println("");
    int valueR = value.substring(1,value.indexOf(',')).toInt();
    int valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    int valueB = value.substring(value.lastIndexOf(',')+1).toInt();

but this didn't work.

int ignoreXindex was going to the be the position of (
so that i can use it instead of 1 in this
int valueR = value.substring(1,value.indexOf(',')).toInt();


Update:  I was able to get the correct spot of ( using
int ignoreXindex = value.indexOf('(');

So i'll now try to use that starting reference to look for the numbers.

PaulRB

Code: [Select]
    int valueR = value.substring(value.indexOf('(')+1,value.indexOf(',')).toInt();
    int valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    int valueB = value.substring(value.lastIndexOf(',')+1).toInt();

maybe?

eeze

Code: [Select]
    int valueR = value.substring(value.indexOf('(')+1,value.indexOf(',')).toInt();
    int valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    int valueB = value.substring(value.lastIndexOf(',')+1).toInt();

maybe?
Thanks PaulRB.  That indeed did work. Much appreciated.

Question for you,   earlier I tried to set the value of the starting point, the first ( I saw similar to what you did above but it didn't work. 

int ignoreXindex = value.indexOf('(');
int valueR = value.substring(ignoreXindex,value.indexOf(',')).toInt();

This above was to look for the (an insert it into the value.substring) and then +1 as you did.

Was my thinking wrong or execution wrong :)


PaulRB

Initially, your thinking was wrong because you did not understand how .substring() and .toInt() work. But then you figured that out I think. In the end you must have been almost there. You said you added the +1, but I can't see where you did that.

eeze

Initially, your thinking was wrong because you did not understand how .substring() and .toInt() work. But then you figured that out I think. In the end you must have been almost there. You said you added the +1, but I can't see where you did that.
Just tried with my old code and I saw the error, added the +1 and it worked.  However, your last code (below) is much cleaner.

I wish I could find some tutorials online to walk through all of these in a lab situation.  I tend to stumble upon these and create topics.

With that, Thanks Paul RB for all of your help.  Karma max!

int valueR = value.substring(value.indexOf('(')+1,value.indexOf(',')).toInt();

Just to confirm this line says
create an integer called valueR and in that integer take the the data between the first ( plus one character up to the first ,  and run toInt on it storing that integer into the variable of valueR

Seems correct?  I'm still wrapping my head around being able to stack things like +1 and toInt()

Go Up