When I split the code in Libraries, no serial monitor output is working

Please include the following information, in order for us to help you as effectively as possible.

  • What product do you wish to discuss? RAK4631

  • What firmware are you using? VSCode/PlatformIO

  • What firmware version? No support do AT, Arduino BSP

  • Computer OS? (MacOS, Linux, Windows) Windows

  • What Computer OS version? Windows 11

  • How often does the problem happen? Yes

  • How can we replicate the problem? Yes

  • Provide source code if custom firmware is used or link to example if RAKwireless example code is used.

I had a code in traditional shape “setup and loop”, but when I split the code in Libraries, no serial monitor output is working. The monitor serial output that I get, it is a “serial.print” that I have in the “loop()”

I already tried to change the “monitor_dtr = 0 and monitor_rts = 0”, but no effect.

Any ideas?

The partial code is below:

How the code was splited in Libraries:

The serial monitor output:

Platformio.ini

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[upload_settings]
device_eui 	= '0xAC, 0x1F, 0x09, 0xFF, 0xFE, 0x0C, 0xF0, 0x41'
app_eui		= '0xAC, 0x1F, 0x09, 0xFF, 0x00, 0x00, 0x00, 0x02'
app_key		= '0x4B, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x4B, 0x65, 0x79'

[env:wiscore_rak4631]
platform = nordicnrf52
board = wiscore_rak4631
framework = arduino
; upload_port = COM16
; monitor_speed = 115200
; monitor_dtr = 0
; monitor_rts = 0
lib_deps = 
	sparkfun/SparkFun LIS3DH Arduino Library @ ^1.0.3
	adafruit/Adafruit BME680 Library @ ^2.0.4
	sparkfun/SparkFun u-blox GNSS Arduino Library @ ^2.2.25
	beegee-tokyo/SX126x-Arduino @ ^2.0.24
; build_type = debug
build_flags = 
 ;   -DCFG_DEBUG=2
	-D MONITOR_SPEED=115200
	-D UPLOAD_SPEED=115200
	; LEDs
	-D LED_GREEN=35
	-D PIN_LED1=35
	-D LED_BLUE=36
	-D PIN_LED2=36
	-D LED_STATE_ON=1
	; calibração das variáveis ambientais
	-D SEALEVELPRESSURE_HPA=1010.0
	; Desabilitar INT1/INT2 no RAK1904 para evitar conflito com RAK5801
	; INT1 de RAK1904
	; -D LIS3DH_CTRL_REG3=0x25
	; INT2 de RAK1904 .
	; -D LIS3DH_CTRL_REG6=0x25
	; LoRa
	-D SCHED_MAX_EVENT_DATA_SIZE=APP_TIMER_SCHED_EVENT_DATA_SIZE ; /**< Maximum size of scheduler events. */
	-D SCHED_QUEUE_SIZE=60										 ; /**< Maximum number of events in the scheduler queue. */
	-D LORAWAN_DATERATE=DR_0
	-D LORAWAN_TX_POWER=TX_POWER_0
	-D JOINREQ_NBTRIALS=3

	-D DEV_EUI=${upload_settings.device_eui}
	-D APP_EUI=${upload_settings.app_eui}
	-D APP_KEY=${upload_settings.app_key}

	; Private defination
	-D LORAWAN_APP_DATA_BUFF_SIZE=64	; tamanho do buffer para o payload a ser enviado via LoRa 
	-D LORAWAN_APP_INTERVAL=20000		; definição do temporizador do intervalo de transmissão dos dados da aplicação. 20 mili segundos> 

The main.h:

/*
*   1) rastreador baseado em GPS: latitude, longitude e altura
*   2) um acelerômetro: quantidade de movimento
*   3) medição de variáveis ambientais: temperatura e humidade
*   4) pressão duas linhas pneumáticas: através da leitura de um transdutor de pressão para variação de corrente 4-20 mA
*/

// bibliotecas do sistema
#ifndef _ARDUINO_H
#define _ARDUINO_H
#include <Arduino.h>
#endif

#ifndef _STDINT_H
#define _STDINT_H
#include <stdint.h> // usa int sem sinal, por exemplo uint8_t, que ocupa apenas 1 byte 
#endif

#ifndef _WIRE_H
#define _WIRE_H
#include <Wire.h>
#endif

#ifndef _SPI_H
#define _SPI_H
#include <SPI.h>
#endif

// Bibliotecas personalizadas
#include <RAK4631.h>
#include <RAK1904.h>
#include <RAK1906.h>
#include <RAK12500.h>
#include <RAK15007.h>

/*
// sensores 
class Sensores
    {
    public:
    String TipoMod [5] = {"RAK4631","RAK1904","RAK1906","RAK5801","RAK12500"};
    uint8_t BatInt, BatDec, HumVal, TempInt, TempDec, PresBarInt, PresBarDec ;
    uint8_t BatCarga [5] = {1,116,BatInt,BatDec,0};
    uint8_t Hum      [4] = {6,104,HumVal,2};
    uint8_t Temp     [5] = {7,103,TempInt,TempDec,2};
    uint8_t PresBar  [5] = {8,115,PresBarInt, PresBarDec,2};
    };
    
*/



The main.cpp:


#include <main.h>



uint8_t count1=0;
 
void setup()
{
	void IniciaAmbiente(void);	// Incia porta Serial, Temporizador
		
	void bme680_init(void); 	// Inicia RAK1906 BME680 sensor de variáveis ambientais

	void IniciaGNSS(void); 		// Inicia RAK12500 sensor de geo-posicionamento

	void IniciaAcel(void); 		// Inidiq RAK1904 sensor de movimento
		
	void IniciaPress(void); 	// Inicial o sensor de variação de corrente 4-20Ma  WisBLOCK 5801
		
	void LoRaInit(void); 		// Inicia LoRa
}

void loop()
{
	count1++;
	Serial.print("========= Saida no Monitor ");
	Serial.print(String(count1));
	Serial.println("=======================");
	vTaskDelay(pdMS_TO_TICKS(1000));

	void LeAcel(void);		// Le quantidade de movimento nos eixos X, Y e Z
	
	void LeAmbiente(void);	// Le variáveis ambientais
		
	void LeGNSS(void);		// Le o geo-posicionamento via GPS
	
	float LePress(void);	// Le do transdutor de pressão das linhas pneumáticas da composição 
}



The RAK1904 library:
RAK1904.h


#ifndef _RAK1904_H

#define _RAK1904_H

// bibliotecas do sistema

#ifndef _ARDUINO_H

#define _ARDUINO_H

#include <Arduino.h>

#endif

#ifndef _STDINT_H

#define _STDINT_H

#include <stdint.h> // usa int sem sinal, por exemplo uint8_t, que ocupa apenas 1 byte

#endif

#ifndef _WIRE_H

#define _WIRE_H

#include <Wire.h>

#endif

// Bibliotecas do Firmware

// RAK1904 acelerômetro

#include <SparkFunLIS3DH.h> //http://librarymanager/All#SparkFun-LIS3DH

LIS3DH SensorTwo(I2C_MODE, 0x18);

void IniciaAcel(void);

void LeAcel(void);

#endif

RAK1904.cpp



#include <RAK1904.h>

void IniciaAcel(void)
	{
	if (SensorTwo.begin() != 0)
		{
			Serial.println("Problema iniciar o sensor no endereço 0x18.");
		}
	else
		{
			Serial.println("Sensor iniciado no endereço RAK1904 no endereço 0x18.");
			// Set low power mode
			uint8_t data_to_write = 0;
			SensorTwo.readRegister(&data_to_write, LIS3DH_CTRL_REG1);
			data_to_write |= 0x08;
			SensorTwo.writeRegister(LIS3DH_CTRL_REG1, data_to_write);
			vTaskDelay(pdMS_TO_TICKS(100));

			data_to_write = 0;
			SensorTwo.readRegister(&data_to_write, 0x1E);
			data_to_write |= 0x90;
			SensorTwo.writeRegister(0x1E, data_to_write);
			vTaskDelay(pdMS_TO_TICKS(100));
		}
	}

void LeAcel(void)
	{
		// Le quantidade de movimento
		Serial.println("========= Quantidade de Movimento============");
		Serial.print(" X(g) = ");
		Serial.println(SensorTwo.readFloatAccelX(), 4);
		Serial.print(" Y(g) = ");
		Serial.println(SensorTwo.readFloatAccelY(), 4);
		Serial.print(" Z(g)= ");
		Serial.println(SensorTwo.readFloatAccelZ(), 4);
		vTaskDelay(pdMS_TO_TICKS(1000));
	}

Where is Serial.begin()
Can’t find it in your code.

Sorry about this :slight_smile:

As it is a long code, I split it in some libraries.

All calls are made in the main.cpp and the serial.begin(115200) is in the first call inside of setup(): void IniciaAmbiente(void); // Inicia porta Serial, Temporizador

The code is below:

main.cpp


#include <main.h>



uint8_t count1=0;
 
void setup()
{
	void IniciaAmbiente(void);	// Incia porta Serial, Temporizador
		
	void bme680_init(void); 	// Inicia RAK1906 BME680 sensor de variáveis ambientais

	void IniciaGNSS(void); 		// Inicia RAK12500 sensor de geo-posicionamento

	void IniciaAcel(void); 		// Inidiq RAK1904 sensor de movimento
		
	void IniciaPress(void); 	// Inicial o sensor de variação de corrente 4-20Ma  WisBLOCK 5801
		
	void LoRaInit(void); 		// Inicia LoRa
}

void loop()
{
	count1++;
	Serial.print("========= Saida no Monitor ");
	Serial.print(String(count1));
	Serial.println("=======================");
	vTaskDelay(pdMS_TO_TICKS(1000));

	void LeAcel(void);		// Le quantidade de movimento nos eixos X, Y e Z
	
	void LeAmbiente(void);	// Le variáveis ambientais
		
	void LeGNSS(void);		// Le o geo-posicionamento via GPS
	
	float LePress(void);	// Le do transdutor de pressão das linhas pneumáticas da composição 
}

RAK4631.cpp


#include <RAK4631.h>

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

		while (!Serial)
		{
		vTaskDelay(pdMS_TO_TICKS(100));
		}

		vTaskDelay(pdMS_TO_TICKS(1000));
		Serial.println("INICIOU A PORTA SERIAL");
		Serial.flush();
		vTaskDelay(pdMS_TO_TICKS(1000));

		// Inicia a I2C para todos os sensores RAK1906, RAK1904, RAK12500, RAK15007, RAK15004
		Wire.begin();

		// Cria temporizador
		err_code = timers_init();
		if (err_code != 0)
		{
			Serial.printf("Inicio do temporizador falhou - %d\n", err_code);
			Serial.flush();
			return;
		}
	}

On my big projects I have split the code into ~20 files (not libraries, just .cpp files) and Serial works everywhere. Not sure why it doesn’t work for you.

We found the reason tô have no output in the serial monitor, I forgot to declare in the .h files the functions prototypes of functions declared in th CPP files

The code is working Fine with libraries(/lib).

Glad you could solve it.

What I usually do is to put

#include "...."

#define XYZ 0

and similar stuff into a single header file that then is included by all .ino and .cpp files in the project folder.

That makes maintenance easier, you just have to make sure this single header file is updated.

Thank very much again by your support.

Regards,

Claudio