Search code examples
cesp32

ESP32 Serial Output


I am trying to get my ADC values in a comma seperated format. In doing so, I have tried to include #include "sstream" and when I go to build the project, I get the following error:

/home/dlaroche/ESP32C3_Dual_Wifi/main/continuous_read_main.c:5:10: fatal error: sstream: No such file or directory
    5 | #include <sstream>
      |          ^~~~~~~~~
compilation terminated.

When using the autocomplete in VScode, it does identify or see the sstream as a possible include file, however, the compiler is seeming to have issues with the use of sstream. Is there any work around to this? Here is the code that is trying to utilize sstream:

while (1) {
    ret = adc_continuous_read(handle, result, EXAMPLE_READ_LEN, &ret_num, 0);

    // THIS SECTION OF CODE BELOW USES "sstream"
    stringstream ss;
    ////////////////////////////////////////////

    if (ret == ESP_OK) {
        for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES) {
            adc_digi_output_data_t *p = (adc_digi_output_data_t*)&result[i];
            uint32_t chan_num = EXAMPLE_ADC_GET_CHANNEL(p);
            uint32_t data = EXAMPLE_ADC_GET_DATA(p);
            if (chan_num < SOC_ADC_CHANNEL_NUM(EXAMPLE_ADC_UNIT)) {

              // THIS SECTION OF CODE USES "sstream"
              ss << chan_num << "," << data << ",";
              //////////////////////////////////////

            } 
        }

        // THIS SECTION OF CODE BELOW USES "sstream"
        std::string result_str = ss.str();
        if (!result_str.empty()) {
            result_str.pop_back();
        }
        ESP_LOGI(TAG, "ADC Values: %s", result_str.c_str());
        ////////////////////////////////////////////////////

    }
}

Solution

  • I ended up using this code that uses #include <stdio.h>.

       while (1) {
           // Send the ADC buffer over USB
           if (buffer_index >= BUFFER_SIZE) {
               // Send buffer to USB
               for (size_t i = 0; i < BUFFER_SIZE; i++) {
                 printf("ADC0: %d, ADC1: %d\n", adc_buffer[i * 2], adc_buffer[i * 2 + 1]);
               }
               buffer_index = 0; // Reset buffer index
           }
           vTaskDelay(pdMS_TO_TICKS(100)); // Adjust delay as needed
       }