One code speaks over the serial, the other not. ESP32

Dear community!
I hope you're doing all well.

I'm programming an ESP32 using the Arduino Ide. For some reason, the code on which I'm working now does not speak over the serial port (the serial monitor of the ide doesn't show never anything), but when I use the standard analog.read example, the serial communication works perfectly. I'm posting both codes below.
Do anybody see any difference that may cause this?

To be precise, I'm using the <Ticker.h> lib in my code and put it also into the analog.read example to use the timer interrupt function, to see if perhaps it was the problem.
Perhaps I will have to do the same by trying to use the "uRTCLib.h" to see if the problem is there...
As you can see in the analog.read example, the problem does not appear to lie in the usage of Serial.println or Serial.printf.

I double checked the things I think are obvious: correct baud rate on PC and ESP32, that the device is properly connected, etc.

Thank you very much!!!

Code that works:

#include <Ticker.h>

Ticker timer;

void setup() {
  Serial.begin(115200);
  analogReadResolution(12); //set the resolution to 12 bits (0-4095)

  timer.attach(5, ejecutarSubrutina);  // 300 segundos = 5 minutos
}

void loop() {
}

void ejecutarSubrutina() {
  Serial.println("Ejecutando subrutina cada 5 seg");
  int analogValue = analogRead(2);
  int analogVolts = analogReadMilliVolts(2);

  // print out the values you read:
  Serial.printf("ADC analog value = %d\n", analogValue);
  Serial.printf("ADC millivolts value = %d\n", analogVolts);

  delay(100);  // delay in between reads for clear read from serial
}

The code that don't want to write anything over the serial:

#include "uRTCLib.h"
#include <Ticker.h>

Ticker timer;

uRTCLib rtc;
//rtc.set(0, 42, 16, 6, 2, 5, 15);
//  RTCLib::set(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year), rtc.refresh();*/

const byte luz_Activo = 2;    // Aquí es donde se definden los números de salida o entrada de cada cosa.
const byte luz_Error = 15;
const byte luz_Vacaciones = 1;
const byte selector_Actividad = 1;

const byte bomba_carga = 4;
const byte soplador = 5;
const byte valvula_difusor = 18;
const byte valvula_descarga_clarificado = 19;
const byte valvula_descarga_lodos = 21;
const byte valvula_4 = 22;

bool FlagCargaR1, FlagDecantacionR1, FlagEvacuacionR1, FlagOxigenacionR1, FlagAnoxiaR1, FlagCargaR2, FlagDecantacionR2, FlagEvacuacionR2, FlagOxigenacionR2, FlagAnoxiaR2;
bool Programa; // true: modo activo. false: modo vacaciones.
int paso,contador_ejecutar_secuencia=0,temp,hora_actual,minuto_actual;

void estado_general();
void ejecutar_secuencia();

void setup() {
  Serial.begin(115200);

  pinMode(luz_Activo,OUTPUT);
  pinMode(luz_Error,OUTPUT);
  pinMode(luz_Vacaciones,OUTPUT);
  pinMode(selector_Actividad,INPUT);
  pinMode(bomba_carga,OUTPUT);
  pinMode(soplador,OUTPUT);
  pinMode(valvula_difusor,OUTPUT);
  pinMode(valvula_descarga_clarificado,OUTPUT);
  pinMode(valvula_descarga_lodos,OUTPUT);
  pinMode(valvula_4,OUTPUT);

  Serial.printf("Inicio ------------------------------- %d\n");

  timer.attach(5, estado_general);  // 5 segundos
}

void loop() {
}

void estado_general() {
  /*temp=paso;
  hora_actual = rtc.hour();
  minuto_actual = rtc.minute();
  paso=(hora_actual*60)+minuto_actual; // Se calcula lo que va del día en minutos.
  if(paso>=5) paso=(paso/5)+1;
  else paso=1;
  if(paso!=temp) ejecutar_secuencia();*/

  digitalWrite(luz_Activo, HIGH);
  delay(100);
  digitalWrite(luz_Activo, LOW);

  contador_ejecutar_secuencia = contador_ejecutar_secuencia + 1;
  if(contador_ejecutar_secuencia > 10) {    // Cada 10 veces 5 segundos, ejecutar secuencia.
    contador_ejecutar_secuencia = 0;
    ejecutar_secuencia();
  }
}

void ejecutar_secuencia() {
  paso=paso+1;
  Serial.printf("ES Paso: %d\n", paso);
  //Serial.printf(", Time: ");
etc....

You should never attempt to do serial I/O of any sort within an ISR, or call a function from an ISR that does so, as that often causes the MCU to crash, hang or otherwise misbehave.

The ISR should set a global flag (declared volatile) that some condition is met, then check for that condition in the main loop, and do the printing there.

The problem is that interrupts are required for serial I/O, and interrupts are turned off within an ISR.

Thank you for your good advice, jremington. I will take it in account.

In this case, something else is happening, since not even the line Serial.printf("Inicio ------------------------------- %d\n"); in the void setup() is doig it...

You need to allow time for the USB serial connection to be established before printing. One approach is the following:

  Serial.begin(115200);
  while (!Serial) delay(1);  // in some cases, need to use yield(); instead of delay(1);

I found the problem!

Since I hadn't planned yet on which pin to connect something, I wrote the following into my code:

const byte selector_Actividad = 1;

//and in the setup part:

pinMode(selector_Actividad, INPUT);

with which I annulated the TX of the ESP32, aviding that it can communicate something to the PC.

Thank you all!

You should also avoid using delay() within your ISR. That function relies on interrupts to maintain elapsed time but those are turned off within the ISR.

Apparently it works on the ESP32 since you said your code works, but it will break on an AVR architecture.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.