I've been dealing with this for quite a while now and searching all over for answers.. I'm trying to put together two floats into a char*. Separated by a ",". This is the code I'm working with.
#include <DHT.h>
#include <RH_ASK.h>
#include <SPI.h>
#define DHT_pin 10
#define DHT_type DHT11
DHT dht(DHT_pin, DHT_type);
RH_ASK rf_driver;
float temp;
float hum;
char ch_temp[6];
char ch_hum[6];
char msg_out[12] = "";
void setup() {
Serial.begin (115200);
dht.begin();
if (rf_driver.init()) {
Serial.println("Radio transmitter initiated..");
}
else {
Serial.println("Radio transmitter failed to initiate..");
delay(1000);
}
}
void loop() {
temp = dht.readTemperature();
hum = dht.readHumidity();
dtostrf(temp, 5, 2, ch_temp);
dtostrf(hum, 5, 2, ch_hum);
strcat(msg_out, ch_temp);
strcat(msg_out, ",");
strcat(msg_out, ch_hum);
Serial.println(msg_out);
rf_driver.send((uint8_t *)msg_out, strlen(msg_out));
rf_driver.waitPacketSent();
Serial.print("data sent, package size: ");
Serial.println(strlen(msg_out));
delay(1000);
}
I have tried using different sizes of the char arrays, but it just goes crazy. This is the console output:
Radio transmitter initiated..
22.00,34.00
data sent, package size: 11
⸮⸮Radio transmitter initiated..
22.00,34.00
data sent, package size: 11
So on its way through the second loop it fails, restarts and then on its third way through it just gives up... What am I doing wrong? :( Really sad over this.