Minimizing the energy consumption of Internet of Things (IoT) devices is one of the biggest challenges and crucial issues for the future of a sustainable IoT vision. In order to estimate the remaining device lifetime and optimize its energy consumption, it is necessary to have an accurate online view on the consumed energy with minimal overhead. This is non-trivial, as many factors influence energy consumption, therefore requiring a generic measurement methodology. For example, the Medium Access Control (MAC) protocols have a very important influence on the energy consumption. This paper presents an accurate method for estimating the energy consumption of IoT devices using Energest. Our method combines a device-specific offline profiling phase, with a device and protocol-agnostic online energy estimation methodology. Energy measurements have been performed for different scenarios, using measured values and values from the datasheet, for Carrier Sense Multiple Access (CSMA) and Time Slotted Channel Hopping (TSCH) protocols. Results show that the accuracy of our method is very high, more than 96% for CSMA and more than 82% for TSCH, with very small overhead of 0.11%.