0

I am doing this IoT based project on displaying data to connected display( I've used the MAX7219 module, in this case) with the help of nodeMCU. The idea here is that the string which is stored in my firebase database is to be display on the led display.

I've had no trouble in getting the value from the database to my nodeMCU but there is this little problem with converting that string to char array since the code i am using( Max72xx_Message_serial, which was available as an example with the max72xx library) has used char array but i can only fetch the stored data in string format. I've modified that code so as to connect with firebase but the main issue is to convert the string fetched from the database to char array.

I tried toCharArray() but it still shows conversion error.

void readfromfirebase(void)
{
  static uint8_t  putIndex = 0;
  int n=1;
  while (Firebase.available())
   {
    newMessage[putIndex] = (char)Firebase.getString("Submit Message"); // this line produces the error
    if ((newMessage[putIndex] == '\n') || (putIndex >= BUF_SIZE-3)) // end of message character or full buffer
    {
      // put in a message separator and end the string
      newMessage[putIndex++] = ' ';
      newMessage[putIndex] = '\0';
      // restart the index for next filling spree and flag we have a message waiting
      putIndex = 0;
      newMessageAvailable = true;
    }
    else if (newMessage[putIndex] != '\r')
      // Just save the next char in next location
      {putIndex++;}
      n++;
  }
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • Why are you casting a `String` object to a single `char`? – gre_gor Jan 24 '19 at 19:41
  • https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/c_str/ – gre_gor Jan 24 '19 at 19:41
  • gre_gor the value returned from my firebase db is of string type and the function of the code deals with char array, that's why. Is there a work around? Would this c_str function work? – Dhrumin Patel Jan 25 '19 at 01:23

1 Answers1

1

I think you are confusing the types

getString returns a String object wich can be converted to a char[] using the methods of the String class.

I assume your newMessage is of type char[] or char*. Then I would advise you to go for the String.c_str() method, because it returns a C style null-terminated string, meaning a char*. See https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/c_str/ for reference.

It also sets the last character of the string to 0. So methods like strlen, strcmp etc will work.

! be carefull not to modify the array returned by c_str(), if you want to modify it you chould copy the char[] or use string.toCharArray(buf, len).

Your Code might then look like the following.

    String msg = Firebase.getString("Submit Message");
    newMessage = msg.c_str();
   // rest of your code

If newMessage is a buffer storing multiple messages, meaning char* newMessage[3].

    String msg = Firebase.getString("Submit Message");
    newMessage[putIndex] = msg.c_str();
   // rest of your code

Be careful, because you are storing multiple characters in an array, so use strcmp to compare these arrays!

If you are new to C I would recommend reading.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Flo
  • 161
  • 1
  • 6