String to uint8_t data[]

Hi guys,

just started to play arround with Arduino. I want to transmit Strings via RF22 from Arduino1 to Arduino2

my string looks like this:

sendStr = "4    ; 59.105988 ;  1.865243   ;88   ;11/13/2015 17:15:27 ;    -18.40 ;     18.16  ;      1.50  ;2029  15        2        ;      3.89    ;     -1.83   ;    101.86  ;;;-------------";

i use the RF22.h libary for TX/RX actions. The function

RF22 myrf22;
myrf22.send(SendData, sizeof(SendData));

should be used for sending the string, but the problem is that the input SendData must be an Array from type uint8_t.

how to convert the String sendStr into unit8_t SendData [] ???? or is there a function for sending strings directly without conversation or cast??

I already tried to convert it into a cahr* data [] and cast in via (byte*), but this does not work.

So please help me, mabey with a short snipped code.

Br Carlos

how to convert the String sendStr into unit8_t SendData []

Don't put it in a String (uppercase S) in the first place. Use char arrays, aka strings (lowercase s), instead.

char sendStr[] = {"4    ; 59.105988 ;  1.865243   ;88   ;11/13/2015 17:15:27 ;    -18.40 ;     18.16"};

first, thx for your answer.

hä, get an error when using string with lowercase s. Type is not defined …

#include <stdlib.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_10DOF.h>
#include <SPI.h>
#include <RF22.h>


TinyGPS gps;
SoftwareSerial ss(4, 5);

Adafruit_10DOF                dof   = Adafruit_10DOF();
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified   mag   = Adafruit_LSM303_Mag_Unified(30302);
Adafruit_BMP085_Unified       bmp   = Adafruit_BMP085_Unified(18001);

RF22 rf22;

char c;       
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;


static void smartdelay(unsigned long ms);
String print_float_s(float val, float invalid, int len, int prec);
String print_int_s(unsigned long val, unsigned long invalid, int len);
String print_date_s(TinyGPS &gps);
static void print_str(const char *str, int len);

char buffer[25];

void setup()
{
  Serial.begin(115200);
  
  ss.begin(9600);
  initSensors();
  
   if (!rf22.init())
    Serial.println("RF22 init failed");
}

void initSensors()
{
  if(!accel.begin())
  {
    /* There was a problem detecting the LSM303 ... check your connections */
    Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
    while(1);
  }
  if(!mag.begin())
  {
    /* There was a problem detecting the LSM303 ... check your connections */
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while(1);
  }
  if(!bmp.begin())
  {
    /* There was a problem detecting the BMP180 ... check your connections */
    Serial.println("Ooops, no BMP180 detected ... Check your wiring!");
    while(1);
  }
}

void loop()
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  String  s;
  
  sensors_event_t accel_event;
  sensors_event_t mag_event;
  sensors_event_t bmp_event;
  sensors_vec_t   orientation;

  s = print_int_s(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  s += ";";
  
  gps.f_get_position(&flat, &flon, &age);
  s += print_float_s(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  s += ";";
  s += print_float_s(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  s += ";";
  
  /*s += print_int_s(age, TinyGPS::GPS_INVALID_AGE, 5);
  s += ";";*/
  s += print_date_s(gps);
  s += ";";
  
  s += print_float_s(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  s += ";";
  s += print_float_s(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  s += ";";
  s += print_float_s(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  s += ";";
  /*print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  /Serial.print(';');
  
  gps.stats(&chars, &sentences, &failed);
  s += print_int_s(chars, 0xFFFFFFFF, 6);
  s += print_int_s(sentences, 0xFFFFFFFF, 10);
  s += print_int_s(failed, 0xFFFFFFFF, 9);
  s += ";";
  */
  
  accel.getEvent(&accel_event);
  if (dof.accelGetOrientation(&accel_event, &orientation))
  {
    
    s += print_float_s(orientation.roll,dof.accelGetOrientation(&accel_event, &orientation),8,2);
    s += ";"; 
    s += print_float_s(orientation.pitch,dof.accelGetOrientation(&accel_event, &orientation),8,2);
    s += ";"; 
    
  }
  
  mag.getEvent(&mag_event);
  if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
  {
    s += print_float_s(orientation.heading,dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation) ,8,2);
    s += ";"; 
  }

  bmp.getEvent(&bmp_event);
  if (bmp_event.pressure)
  {
    float temperature;
    bmp.getTemperature(&temperature);
    s += print_float_s(bmp.pressureToAltitude(seaLevelPressure, bmp_event.pressure, temperature),bmp_event.pressure ,8,2); 
    s += ";"; 
    s += print_float_s(temperature,bmp_event.pressure ,10,2);
    s += ";"; 
  }


  Serial.print(s);
  test_tx(s);
  smartdelay(1000);

}

static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

String print_float_s(float val, float invalid, int len, int prec)
{
  String erg;
  
  if (val == invalid)
  {
    while (len-- > 1)
      erg ="*";
    erg += ' ';
  }
  else
  {
    erg = dtostrf(val,10,prec,buffer);
    
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      erg += ' ';
  }
  return erg;
  smartdelay(0);
}

String print_int_s(unsigned long val, unsigned long invalid, int len)
{
  String erg;
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  erg = sz;
  return erg;
  smartdelay(0);
}

String print_date_s(TinyGPS &gps)
{
  String erg;
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    erg="********** ******** ";
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
        month, day, year, hour, minute, second);
    erg = sz;
  }
  return erg;
  smartdelay(0);
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartdelay(0);
}

// This works with test_rx below
void test_tx(String sendStr)
{
  if (!rf22.setFrequency(434.0))
    Serial.println("setFrequency failed");
  if (!rf22.setModemConfig(RF22::GFSK_Rb2Fd5))
    Serial.println("setModemConfig failed");

  
  uint8_t data[] = "Hello World!"; //instead of this fixed text i want to use the sendStr. 
  rf22.send(data, sizeof(data));
  
  rf22.waitPacketSent();
  Serial.println("-------------");

}

here you find the full code of my program.
in the 4th last row uint8_t data[] = "Hello World!"; i want to use the sendStr instead of the static text “Hello World”.
so i do not want to change all the string operations to char operations.

are there other possibilties??

BR carlos

Look up c_str() in the reference to the String class. You can convert your String like this: sendStr.c_str().

Because string with a lower case s isn't a type. Its what we call null terminated char arrays in the C language. The type to use is an array of char.