Look for advice in way of programming

Hy,

I’m starting in ESP32 development.
I’m trying to do something like this.
A program :
sht21Helpers.cpp

#include <Arduino.h>
#include "SHT2x.h"

boolean initSht(SHT2x sht)
{
    sht.begin();
    Serial.println("SHT21 test");
    if (!sht.isConnected()  )
    {  
        Serial.println("Couldn't find SHT21");
        return false;
    }
    else
    {
        Serial.println("SHT21 OK");
        return true;
    }
}

String getInfoTemperature(SHT2x sht)
{
    float temperature = 0;
    sht.read();
    temperature = sht.getTemperature();
    return String(temperature).c_str();
}

String getInfoHumidity(SHT2x sht)
{
    float humidity = 0;
    sht.read();
    humidity = sht.getHumidity();
    return String(humidity).c_str();
}

an include file , sht21Helpers.h

#include <Arduino.h>

#include "SHT2x.h"

SHT2x sht;

boolean initSht(SHT2x sht);

String getInfoTemperature(SHT2x sht);

String getInfoHumidity(SHT2x sht);

In main.cpp (part of code)

#include "sht21Helpers.h"
.....
void setup(){

...
Serial.println("Test sht21Helpers");
if (initSht(sht))
{
    Serial.println("Connection OK");
}

void loo(){

    Serial.println("================================================");
    Serial.println("Test version helpers");
    Serial.print("Température: ");
    Serial.print(getInfoTemperature(sht));
    Serial.println(" ºC");

}

When program reach line getInfoTemperature(sht), the ESP32 reboot.

This code is functional when every thing in in main.cpp.

If someone can help me. I know it’s more a question about how to developp in cpp but I don’t know where to post my question.

Regards

I think it’s because getInfoTemperature() is returning a char * and not a String as you have declared it to return. The stack after your call to the function is possibly corrupted. You need to return an actual String or change it to declare the return type is char *.

Likewise getInfoHumidity().

HTH

Cheers,
Norm.

Thanks for answer.
I resolve my problem. I was the way, I split the code.
Here what I do and it’s working

The include file : sht21Helpers.h

#ifndef SHT21_HELPERS_H
    #define SHT21_HELPERS_H

    #include <Arduino.h>
    #include "SHT2x.h"

    extern SHT2x sht;  // Déclaration externe de la variable sht

    boolean initSht();
    String getInfoTemperature();
    String getInfoHumidity();

#endif  // SHT21_HELPERS_H

The sht21Helpers.cpp

#include <sht21Helpers.h>

// ============================================================================
//    initSht()
// ============================================================================
boolean initSht()
{
    sht.begin();
    Serial.println("Contrôle accès à la sonde SHT21");
    if (!sht.isConnected()  )
    {  
        Serial.println("Sonde SHT21 inaccessible");
        return false;
    }
    else
    {
        Serial.println("Accès à la sonde SHT21 OK");
        uint8_t stat = sht.getStatus();
        Serial.print(stat, HEX);
        Serial.println();
        return true;
    }
}

// ============================================================================
//    getInfoTemperature()
// ============================================================================
String getInfoTemperature()
{
    float temperature = 0;
    sht.read();
    temperature = sht.getTemperature();
    return String(temperature).c_str();
}

// ============================================================================
//    getInfoHumidity()
// ============================================================================
String getInfoHumidity()
{
    float humidity = 0;
    sht.read();
    humidity = sht.getHumidity();
    return String(humidity).c_str();
}

Finally part of main.cpp

#include <sht21Helpers.h>
...
SHT2x sht;
...

void loop() 
{
  unsigned long currentMillis = millis();
  // Every X number of seconds (interval = 10 seconds) it publishes a new MQTT message
  if (currentMillis - previousMillis >= interval) 
  {
    // Save the last time a new reading was published
    previousMillis = currentMillis;

    temperature = getInfoTemperature();
    humidity = getInfoHumidity();
    
    mqttClient.publish(strTopic1.c_str(), 1, true, shortTime().c_str());
    mqttClient.publish(strTopic2.c_str(), 1, true, temperature.c_str());
    mqttClient.publish(strTopic3.c_str(), 1, true, humidity.c_str());                            

    Serial.println("================================================");
    Serial.printf("Relevé : %s°C - %s%s à %s\n", temperature, humidity, "%", shortTime());
  }
}

I’m eager for advice

Regards

You still have ptoblems here:

String getInfoTemperature()
{
    float temperature = 0;
    sht.read();
    temperature = sht.getTemperature();
    return String(temperature).c_str();   <<<<<<<< This is not returning String but char *.
}

Similar in String getInfoHumidity().

You declare that they both return String but then both return char *. You need to drop the .c_str() from each to be correct.

Cheers,
Norm.

Thank you for your advice.
I’ll correct in my program.

1 Like