Go Down

Topic: Converting String to const char*  (Read 13457 times) previous topic - next topic

nonlinearmind

I'm trying to send a string from a Blend micro to an iOS app. I've been trying to modify the chat example but keep running into the error:


SimpleChat2.ino: In function 'void transmit(String)':
SimpleChat2:128: error: invalid cast from type 'String' to type 'unsigned char*'
SimpleChat2:128: error: cannot convert 'String' to 'const char*' for argument '1' to 'size_t strlen(const char*)'


 I'm trying to replace the line from the example:

  ble_write( Serial.read() );

with:

 String testString = "test content";
       
ble_write( testString );


How would I go about converting my string into something that will work?

Code: [Select]

#include <SPI.h>
#include <boards.h>
#include <RBL_nRF8001.h>

void setup()

  // Default pins set to 9 and 8 for REQN and RDYN
  // Set your REQN and RDYN here before ble_begin() if you need
  //ble_set_pins(3, 2);
 
  // Set your BLE Shield name here, max. length 10
  //ble_set_name("My Name");
 
  // Init. and start BLE library.
  ble_begin();
 
  // Enable serial debug
  Serial.begin(57600);
}

unsigned char buf[16] = {0};
unsigned char len = 0;

void loop()
{
  if ( ble_available() )
  {
    while ( ble_available() )
      Serial.write(ble_read());
     
    Serial.println();
  }
 
  if ( Serial.available() )
  {
    delay(5);
   
    while ( Serial.available() ){
   
      //////This line from the example works
         ble_write( Serial.read() );

    //////These two lines of mine don't
        String testString = "test content";
       
        ble_write( testString );
    //////////////
  }
 
  }
 
  ble_do_events();
}

AWOL

Why convert?
Why not simply use a string?

evanmars

Serial.read() returns 1 byte...your testString is 13 bytes

nonlinearmind

#3
Dec 16, 2016, 06:55 pm Last Edit: Dec 16, 2016, 08:46 pm by nonlinearmind
I tried doing this:

Code: [Select]


transmit("testing1234");

transmit("testingABCD");

transmit("testing5678");

transmit("testingEFGH");



void transmit(String testString){uint8_t sendbuffer[20];
          testString.getBytes(sendbuffer, 20);
           char sendbuffersize = min( 20, testString.length());
      //I also tried:      uint8_t sendbuffersize = min( 20,testString.length());

          Serial.print(F("\n* Sending -> \"")); Serial.print((char *)sendbuffer); Serial.print("        ");Serial.print(sendbuffersize);Serial.println("");

          // write the data
    ble_write_bytes(sendbuffer, sendbuffersize);}


Which sends the whole string, but links consecutive sends together into 20 character strings.  For example, the above sends the following:

testing1234testingAB
CDtesting5678testing
EFGH


rather than:

testing1234
testingABCD
testing5678
testingEFGH

AWOL

Quote
I tried doing this:

Code: [Select]


transmit("testing1234";
What's that?

nonlinearmind

Sorry, I was typing in an example of what I was trying to do and forgot my closing parentheses. 

nonlinearmind

 I edited my previous post to fix the parentheses. But my problem is still the same. I need to know how to convert a string so that I can send it via Bluetooth.

gfvalvo

#7
Dec 16, 2016, 10:54 pm Last Edit: Dec 17, 2016, 12:10 am by gfvalvo
What parameter type does the ble_write()
 function require? Is it char *?

If so, why are you using C++ String instead of C string? Try:

Code: [Select]
const char[] = "test content";

No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

nonlinearmind

It still gives me the same error.

Code: [Select]
//////These two lines of mine don't
        const char testString = "test content";
       
        ble_write( testString );
    //////////////

gfvalvo

It still gives me the same error.
So, I ask again: what parameter type does the ble_write() function require? I don't have that libray.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

AWOL

It isn't a String, so it doesn't give the same error message.

Post your code, post your error message.

nonlinearmind


AWOL

The code "up top" has a String.

It has been suggested (at least twice) that you don't use a String, but a string.

How's that going?

nonlinearmind

I don't know the parameter type.  These are answers I'm trying to find out by posting here.  I've been searching forums and stack exchange and this problem for this board seems to come up a bunch, but there are either no answers or the ones I tried haven't worked.  There is little documentation and this is all very frustrating.

here is the library itself:

https://github.com/RedBearLab/nRF8001/blob/master/src/RBL_nRF8001.cpp

AWOL


Go Up