Compile error on EmonLib/EmonLib.cpp:232:8: error: unused variable

Encountered this error after downloading github zip file and extracting and then
compiling ino example file.

IDE 2 preferences:

https://dl.espressif.com/dl/package_esp32_index.json
https://github.com/danpeig/ESP32EnergyMonitor.git
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

ino sketch:

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h"             // Include Emon Library
EnergyMonitor emon1;             // Create an instance

void setup()
{  
  Serial.begin(9600);
  
  emon1.voltage(2, 234.26, 1.7);  // Voltage: input pin, calibration, phase_shift
  emon1.current(1, 111.1);       // Current: input pin, calibration.
}

void loop()
{
  emon1.calcVI(20,2000);         // Calculate all. No.of half wavelengths (crossings), time-out
  emon1.serialprint();           // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)
  
  float realPower       = emon1.realPower;        //extract Real Power into variable
  float apparentPower   = emon1.apparentPower;    //extract Apparent Power into variable
  float powerFactor     = emon1.powerFactor;      //extract Power Factor into Variable
  float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable
  float Irms            = emon1.Irms;             //extract Irms into Variable

Serial.println();
  Serial.print(realPower);
Serial.println();
Serial.print(apparentPower);
Serial.println();
Serial.print(powerFactor);
Serial.println();
Serial.print(supplyVoltage);
Serial.println();
Serial.print(Irms);


}

partial error printout (it was really big but just directory stuff and no messaging):

/sdk/esp32/include/espressif__esp-dsp/modules/fft/include -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/tools/sdk/esp32/include/espressif__esp-dsp/modules/dct/include -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/tools/sdk/esp32/include/espressif__esp-dsp/modules/conv/include -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/tools/sdk/esp32/include/espressif__esp-dsp/modules/common/include -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/tools/sdk/esp32/include/espressif__esp-dsp/modules/kalman/ekf/include -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/tools/sdk/esp32/include/espressif__esp-dsp/modules/kalman/ekf_imu13states/include -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/tools/sdk/esp32/include/fb_gfx/include -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/tools/sdk/esp32/qio_qspi/include -mlongcalls -Wno-frame-address -ffunction-sections -fdata-sections -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-sign-compare -ggdb -freorder-blocks -Wwrite-strings -fstack-protector -fstrict-volatile-bitfields -Wno-error=unused-but-set-variable -fno-jump-tables -fno-tree-switch-conversion -std=gnu++11 -fexceptions -fno-rtti -MMD -c -Wall -Werror=all -Wextra -Os -DF_CPU=240000000L -DARDUINO=10607 -DARDUINO_ESP32_DEV -DARDUINO_ARCH_ESP32 "-DARDUINO_BOARD=\"ESP32_DEV\"" "-DARDUINO_VARIANT=\"esp32\"" -DARDUINO_PARTITION_default -DESP32 -DCORE_DEBUG_LEVEL=0 -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue -mfix-esp32-psram-cache-strategy=memw -DARDUINO_USB_CDC_ON_BOOT=0 @/tmp/arduino/sketches/D909CD5C9048558D00B5469470FF92A2/build_opt.h @/tmp/arduino/sketches/D909CD5C9048558D00B5469470FF92A2/file_opts -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/cores/esp32 -I/home/ed/.arduino15/packages/esp32/hardware/esp32/2.0.14/variants/esp32 -I/home/ed/Arduino/libraries/EmonLib /home/ed/Arduino/libraries/EmonLib/EmonLib.cpp -o /tmp/arduino/sketches/D909CD5C9048558D00B5469470FF92A2/libraries/EmonLib/EmonLib.cpp.o
/home/ed/Arduino/libraries/EmonLib/EmonLib.cpp: In member function 'long int EnergyMonitor::readVcc()':
/home/ed/Arduino/libraries/EmonLib/EmonLib.cpp:232:8: error: unused variable 'result' [-Werror=unused-variable]
   long result;
        ^~~~~~
cc1plus: some warnings being treated as errors

Using library EmonLib at version 1.1.0 in folder: /home/ed/Arduino/libraries/EmonLib 
exit status 1

Compilation error: exit status 1pe or paste code here

See ESP32 unused variable compile error - #15 by cgvrl

1 Like

Quoting user gcjr's response from your referenced thread:

"can you set the preferences so that warnings aren't treated as errors?

there's a reason treating warnings as errors, which i believe is very good practice, is optional. there's a need to be able to compile older working code that has acceptable flaws."

So I went in to Preferences on the IDE 2 and set the Compiler Warnings box
to NONE. It then compiled error free, or at least generated the "Done compiling."
message with no errors listed. Not sure how to interpret this as far as if this is in
fact a clean compile that will work in operation/execution.

I appreciate that lead Bob. :white_check_mark: :white_check_mark:

Anyone know where this same data is stored under Linux?

Detailed instructions for Windows:

Go to C:\Users\myName\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5 (or whatever version is current)

Make a copy of platform.txt and rename it to platform.local.txt.
Inside platform.local.txt, change the Compiler Warning levels from:

compiler.warning_flags=-w
compiler.warning_flags.none=-w
compiler.warning_flags.default=
compiler.warning_flags.more=-Wall -Werror=all
compiler.warning_flags.all=-Wall -Werror=all -Wextra

To:

compiler.warning_flags=-w
compiler.warning_flags.none=-w
compiler.warning_flags.default=
compiler.warning_flags.more=-Wall
compiler.warning_flags.all=-Wall -Wextra

This works when you set the Arduino Compiler warnings to 'More’ or 'All'. Tested on both 1.8.16 and 2.03 Arduino IDE.

Likely ~/.arduino15/packages/esp32/hardware/esp32/2.0.5 (or whatever version is current)

Yep, that's it....
So just make a new file [platform.local.txt]
in the same directory as platform.txt?
Seems easy enough...famous last words...
platform.txt:

# Arduino Compile Warning Levels
compiler.warning_flags=-w
compiler.warning_flags.none=-w
compiler.warning_flags.default=
compiler.warning_flags.more=-Wall -Werror=all
compiler.warning_flags.all=-Wall -Werror=all -Wextra

# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.S.extra_flags=
compiler.cpp.extra_flags=
compiler.ar.extra_flags=
compiler.objcopy.eep.extra_flags=
compiler.elf2hex.extra_flags=
compiler.libraries.ldflags=

FYI:
I went ahead and created the new platform.local.txt file as suggested
and inserted the changes to the compiler. I then went back and made
Preferences> Compiler Warnings say "All."
Restarted the IDE and compiled the EMon sketch with no problems.
This is using IDE 2.2.1.

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