Changing Strings to Char arrays

Hi

I've seen many people talking about "don't use Strings, as they always bring troubles", and well, it's true indeed, I'm experiencing crashes sometimes, as they consume much dynamic memory.

The problem is, Strings are a lot easier to manipulate, and I'm having some problems doing the data treatment with the char arrays.

The problem is the following, I'm reading many parameters from sensors (temperature, humidity, gas levels, from MQ-4 and MQ-7 sensors), and I want to do some data treatment (if I can name it that way) with the chars, for example:

  • DHT11 return 28 (That means 28-degree Celcius), and I want to convert that 28 into !28;

You may be asking why I'm doing it, well it's because I'm having interference with jumpers and sometimes some trash comes with the message, so, when this !28; arrives at the ESP8266. it can clear all the data that comes with the 28 (it it does)

The code (with the Strings) that I have is the following:
Readings (all ok there, I think):

MQ4_Value = digitalRead(MQ4); // mq-4 gas sensor value
MQ7_Value = digitalRead(MQ7); // mq7 gas sensor value

float temp = dht.readTemperature(); // temperature value
float hmdt = dht.readHumidity(); // humidity value

Data treatment (there is the problem):

String MQ4_Value_forSend;
String MQ7_Value_forSend;
String temp_forSend;
String hmdt_forSend;

temp_forSend = String(F("!")) + String(temp) + String(F(";"));
hmdt_forSend = String(F("!")) + String(hmdt) + String(F(";"));
MQ4_Value_forSend = String(F("!")) + String(MQ4_Value) + String(F(";"));
MQ7_Value_forSend = String(F("!")) + String(MQ7_Value) + String(F(";"));

As you see, I'm making extreme use of String objects, as they are easier to manipulate. But, how can I do such treatment with char arrays?

Thanks in advance

Pedro

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  byte temp = 28;
  Serial.print("!");
  char tempAsChars[5];
  itoa(temp, tempAsChars, 10);
  Serial.print(tempAsChars);
  Serial.println(";");
  //or
  char buffer[12];
  sprintf(buffer, "!%d;", temp);
  Serial.println(buffer);
}

void loop()
{
}

UKHeliBob:

void setup()

{
  Serial.begin(115200);
  while (!Serial);
  byte temp = 28;
  Serial.print("!");
  char tempAsChars[5];
  itoa(temp, tempAsChars, 10);
  Serial.print(tempAsChars);
  Serial.println(";");
  //or
  char buffer[12];
  sprintf(buffer, "!%d;", temp);
  Serial.println(buffer);
}

void loop()
{
}

Thanks for your reply, helped me a lot :smiley:

Have a great day

There are several options.
First you have to explain, why you need to concatenate your message into a buffer?
Just send value by value, or stream to your target if possible.

noiasca:
There are several options.
First you have to explain, why you need to concatenate your message into a buffer?
Just send value by value, or stream to your target if possible.

Hi noiasca

Thanks for your reply.

You are right, I have already done that, just like the "UKHeliBob" example.

:smiley:

I'm experiencing crashes sometimes, as they consume much dynamic memory.

Really ?? with that bit of code on an ESP ? Look i am not saying you should not change the String to c-strings, but i find it a little hard to believe, that you manage to fragment your heap with just those few lines. Then again, with fragmentation, the context is important, and mainly, is there any modification of global Strings ? or are they all global ? Simply put, once you've sent the date, you don't really need it anymore, you can then let the objects be destroyed when you leave the function, and all fragmentation is gone.
If you have 'those' Strings global, then you should reserve sufficient space for them, and then just the extra created String used for concatenation are created, but they will be automatically destroyed when you leave the function in which they've been created.

recently I discovered this library

Safe, Robust, Debuggable String class for Arduino

You can download the library with the library-manager inside the Arduino-IDE
best regards Stefan

Deva_Rishi:
Really ?? with that bit of code on an ESP ? Look i am not saying you should not change the String to c-strings, but i find it a little hard to believe, that you manage to fragment your heap with just those few lines. Then again, with fragmentation, the context is important, and mainly, is there any modification of global Strings ? or are they all global ? Simply put, once you've sent the date, you don't really need it anymore, you can then let the objects be destroyed when you leave the function, and all fragmentation is gone.
If you have 'those' Strings global, then you should reserve sufficient space for them, and then just the extra created String used for concatenation are created, but they will be automatically destroyed when you leave the function in which they've been created.

Hi Deva_Rishi

Thanks for your reply :smiley:

Well, that's now the one part of the code, the main code (on ATmega328p-PU) has 385 lines and the ESP8266 has 400, I thought it wasn't necessary to put all the code here :slight_smile:

I just paste that code because the other treatment is almost the same.

The programme, in the total, uses 72% of the memory and 51% of the dynamic memory, so it is more sensitive to a huge amount of RAM being used.

StefanL38:
recently I discovered this library

Safe, Robust, Debuggable String class for Arduino

You can download the library with the library-manager inside the Arduino-IDE
best regards Stefan

Hello StefanL38

Thanks for your reply :smiley:

Really thanks for the recommendation, I will test it out soon

For now, the problem is solver with the char arrays :smiley:

Have a great day

Pedro

the main code (on ATmega328p-PU) has 385 lines

That changes things ! particularly that it is on an AVR.