Stack smashing protect failure after calling method of class with pointer
Posted: Sun Mar 06, 2022 5:25 pm
Hello,
I am trying to record temperature and humidity data with on ESP32-WROOM-32 with the Sensor DHT22.
Everything works fine until the first loop() is done. After that I get a 'Stack Smashing Protection failure' error and the esp32 reboots.
I am quite new to C++ but I think the problem is the usage of pointers here but I'm not sure tbh.
I tried debugging and found out that it crashes after the first loop because I'm calling:
'std::string temp = Convert(sensorPtr->readTemperature());'
But thats all I know. Can anyone help me?
This is the code:
sensor-controller.cpp
sensor-controler.h
main.cpp
This is the error message:
"
[...]
-----Sensor Controller-----
Temp: 21.7°C / Humidity: 39.8%
-------------------------
Stack smashing protect failure!
abort() was called at PC 0x4013860f on core 1
ELF file SHA256: 0000000000000000
Backtrace: 0x400887ac:0x3ffb1f10 0x40088a29:0x3ffb1f30 0x4013860f:0x3ffb1f50 0x400d0b9d:0x3ffb1f70 0x400d6239:0x3ffb1fb0 0x40089a3a:0x3ffb1fd0
Rebooting...
"
I am trying to record temperature and humidity data with on ESP32-WROOM-32 with the Sensor DHT22.
Everything works fine until the first loop() is done. After that I get a 'Stack Smashing Protection failure' error and the esp32 reboots.
I am quite new to C++ but I think the problem is the usage of pointers here but I'm not sure tbh.
I tried debugging and found out that it crashes after the first loop because I'm calling:
'std::string temp = Convert(sensorPtr->readTemperature());'
But thats all I know. Can anyone help me?
This is the code:
sensor-controller.cpp
- #include <sensor-controller.h>
- #include <Arduino.h>
- #include <sstream>
- // converts float to string
- std::string Convert(float number)
- {
- std::ostringstream buff;
- buff << number;
- return buff.str();
- }
- SensorController::SensorController(uint8_t gpio_data_)
- {
- gpio_data = gpio_data_;
- }
- DHT SensorController::init()
- {
- Serial.println("Starting temperature and humidity sensor...");
- // init dht with proper data port
- DHT sensor(gpio_data, DHT22);
- sensor.begin();
- sensorPtr = &sensor;
- // wait 4 second for booting
- delay(4000);
- Serial.println("Sensor connected!");
- return sensor;
- }
- std::string SensorController::readData()
- {
- Serial.println("-----Sensor Controller-----");
- std::string temp = Convert(sensorPtr->readTemperature());
- std::string hum = Convert(sensorPtr->readHumidity());
- Serial.print("Temp: ");
- Serial.print(temp.c_str());
- Serial.print("°C / Humidity: ");
- Serial.print(hum.c_str());
- Serial.println("%");
- std::string sensorData = "{\"temp\": \"" + temp + "\", \"hum\": \"" + hum + "\"}";
- Serial.println("-------------------------");
- return sensorData;
- }
- #ifndef temp_controller
- #define temp_controller
- // to make dht work with platform.io
- // https://community.platformio.org/t/pio-libdeps-esp32dev-dht-sensor-library-dht-u-h29-fatal-error-adafruit-sensor-h-no-such-file-or-directory/21861/2
- #include <Adafruit_Sensor.h>
- #include <DHT.h>
- class SensorController
- {
- public:
- SensorController(const uint8_t gpio_data);
- DHT init();
- std::string readData();
- private:
- uint8_t gpio_data;
- DHT *sensorPtr;
- };
- #endif
- #include <Arduino.h>
- #include <dotenv-parser.h>
- #include <network-controller.h>
- #include <http-controller.h>
- #include <mic-controller.h>
- #include <sensor-controller.h>
- #include <sstream>
- // mic gpios
- const byte gpio_sck = 33;
- const byte gpio_ws = 32;
- const byte gpio_sd = 34;
- // using led with g23 and gnd
- const byte gpio_led = 23;
- // sensor gpio
- const byte gpio_data = 26;
- // ip to my rpi in the home network
- HttpController httpController("http://{ip}:8080");
- SensorController sensorController(gpio_data);
- void setup()
- {
- // init g32 as an output
- pinMode(gpio_led, OUTPUT);
- // launch on proper port I guess
- Serial.begin(9600);
- // init env vars
- DotenvParser parser;
- parser.parseFileContent();
- // init network controller if both vars are set
- if (getenv("SSID") && getenv("PASSWORD"))
- {
- NetworkController networkController(getenv("SSID"), getenv("PASSWORD"));
- networkController.connect();
- }
- else
- {
- Serial.println("Ssid or password not given. Can't connect to network");
- }
- sensorController.init();
- // MicController micController(gpio_sck, gpio_ws, gpio_sd);
- // uint8_t *record = micController.record();
- }
- void loop()
- {
- // power led on
- // Serial.println("Led on!");
- digitalWrite(gpio_led, HIGH); // HIGH -> 3.3v output
- // send alive request
- // const char *data = "{\"data\": \"still alive\"}";
- // httpController.postRequest("/esp/", "application/json", data);
- std::string sensorData = sensorController.readData();
- // httpController.postRequest("/esp/sensor", "application/json", sensorData.c_str());
- // wait 10 sec
- // delay(10000);
- // wait 300 sec = 5min
- sleep(300);
- // turn off led
- digitalWrite(gpio_led, LOW); // set voltage level to LOW to power off gpio -> 0v
- // Serial.println("Led off!");
- // wait 1 sec
- delay(1000);
- }
"
[...]
-----Sensor Controller-----
Temp: 21.7°C / Humidity: 39.8%
-------------------------
Stack smashing protect failure!
abort() was called at PC 0x4013860f on core 1
ELF file SHA256: 0000000000000000
Backtrace: 0x400887ac:0x3ffb1f10 0x40088a29:0x3ffb1f30 0x4013860f:0x3ffb1f50 0x400d0b9d:0x3ffb1f70 0x400d6239:0x3ffb1fb0 0x40089a3a:0x3ffb1fd0
Rebooting...
"