Can ESP32 handle two sprinf() strings at the same time?

Oslaf Eman
Posts: 2
Joined: Sat Nov 17, 2018 12:40 pm

Can ESP32 handle two sprinf() strings at the same time?

Postby Oslaf Eman » Sat Nov 17, 2018 12:45 pm

Of course it does! But..
I have written a code using an ESP8266 microprocessor and it works fine. But because I need 2 additional pins I have upgraded to ESP32 (MH ET Live Mini). During the code upgrade all the peripherals worked well, but when loading the EEPROM sketch the ESP32 could not print and record more than one sprinf () chain at the same time.

I wrote this simple code for showing the problem:

[Codebox]
#include <EEPROM.h>
#include <RTClib.h>

RTC_DS1307 rtc;

char Fecha[10];
char Hora[5];

void setup() {
Serial.begin(115200);
EEPROM.begin(32);
if (! rtc.begin()) {
Serial.println("Modulo RTC no encontrado !");
while (1);
}

DateTime Ahora = rtc.now();

sprintf(Fecha,"%02u/%02u/%04u ",Ahora.day(),Ahora.month(),Ahora.year());
sprintf(Hora,"%02u:%02u ",Ahora.hour(),Ahora.minute());

EEPROM.put(2, Fecha);
Serial.println(Fecha);
EEPROM.put(12, Hora);
Serial.println(Hora);


EEPROM.get(2, Fecha);
Serial.println(Fecha);
EEPROM.get(12, Hora);
Serial.println(Hora);

for (int k = 0; k <= 17; k++){
int valor = EEPROM.read(k);
Serial.print("Celda ");
Serial.print(k);
Serial.print(": ");
Serial.println(valor);
}
}

void loop() {
}

[/Codebox]

When sprintf(Date) is alone it works fine, but if it is added the sprintf(Time) does not print or record the first one. If a third sprintf() is added, the first two do not print or record, but the last one does. And so.

I tried another ESP32 chip and the result was the same. There is something that I am doing wrong but I do not achieve what it is. Hope someone can help me. Thank you

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

Re: Can ESP32 handle two sprinf() strings at the same time?

Postby martinayotte » Sat Nov 17, 2018 3:23 pm

char Fecha[10];
char Hora[5];
The problem is probably related to the fact that your strings are to small to have their respective NULL ending characters, so memory get corrupted ...
Make them bigger than the length you expect, like :

Code: Select all

char Fecha[16];
char Hora[8];

Oslaf Eman
Posts: 2
Joined: Sat Nov 17, 2018 12:40 pm

Re: Can ESP32 handle two sprinf() strings at the same time?

Postby Oslaf Eman » Sat Nov 17, 2018 9:04 pm

Thank you very much martinayotte, I tried that before and did not work, but now I did it again and it's solved. I really appreciate your support.

Who is online

Users browsing this forum: No registered users and 57 guests