简体   繁体   中英

Arduino (C++) concatenate Char* array with float array

OK, so I have a set of sensors connected to an ESP8266 WiFi SoC which will populate an array with floats. I then need to concatenate that with an array of mqtt topics before publishing via an MQTT client. I have created a bare bones sketch as follows to try and solve an issue with the array / char manipulation:

#include <ESP8266WiFi.h>

float readingsArray[10]; //Store the sensor readings
float reading; // Current reading 
char* mqttPackage; // The concatenated output.
char* sensorIDArray[] = { // Used for the MQTT topic string
  "DS18B20_1",
  "DS18B20_2",
  "DS18B20_3",
  "DHT22_t",
  "DHT22_h",
  "Hygrometer_1",
  "Hygrometer_2",
  "Hygrometer_3",
  "Hygrometer_4",
  "Battery"
};

void setup(){

  Serial.begin(9600);

  for (int i = 0; i < 10; i++){ // Populate the readingsArray with dummy data.
    reading = random(0, 100);
    Serial.print("Reading: ");
    Serial.println(reading);
    readingsArray[i] = reading;
    delay(100);
  }

  Serial.print("readingsArray full");

  for (int i = 0; i < 10; i++){
    strcpy(mqttPackage, sensorIDArray[i]);     // copy topic to mqttPackage
    String tempStr = String(readingsArray[i]); // string from sensor float
    char* newChar;                             // I THINK THIS IS THE OFFENDING VARIABLE
    tempStr.toCharArray(newChar, tempStr.length() + 1); // string to char array
    strcat(mqttPackage, newChar);              // concatenate the chars to mqttPackage
    Serial.print("Element ");                  // Debug print the output.
    Serial.print(i);
    Serial.print(": ");
    Serial.println(mqttPackage);
    delay(100);
  }  
}

I am getting error 29 from the ESP8266, which suggests that the buffer is filling up with an unlimited variable. Please can someone give me some pointers on where I am going wrong.

Please don't roast me. I am an amateur coder just trying to learn, I have literally spent a week of evenings googling this as self help but have hit a block.

String::toCharArray copies the characters managed by the String object into the buffer supplied. You're passing newChar as the output buffer, but it's not pointing to any memory you can write to. You need to allocate a buffer that you can write the string into:

char newChar[100] = {};
tempStr.toCharArray(newChar, 99); // 99 so we can't overwrite the last nul character

Note that if tempStr is longer than 99 char s, the first 99 will be written into newChar .

Your strcpy and strcat calls share the same issue. They expect to fill a user-supplied buffer, but the pointer you're passing doesn't point to anything.


None of that dance is actually necessary though. String directly supports concatenation, so you can just do something like

String mqttPackage = sensorIDArray[i];
mqttPackage += readingsArray[i];

Of course, you don't really need to concatinate the values in memory at all, since you're just printing them to the serial port. You can just print them directly and let the Serial library worry about converting them

Serial.print("Element ");
Serial.print(i);
Serial.print(": ");
Serial.print(sensorIDArray[i]);
Serial.println(readingsArray[i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM