diff --git a/src/user_code.cpp b/src/user_code.cpp index 7507243..929235b 100644 --- a/src/user_code.cpp +++ b/src/user_code.cpp @@ -1,7 +1,8 @@ #include -//Timer Components -ESPMega_Timer timer1(0, 50, timer1_callback, 15001); +// Timer Components +ESPMega_Timer timer1(0, 50, timer1_callback, 15001); // Using Memmory Address 15001 +ESPMega_CT ct0 = ESPMega_CT(0, adc2watts, 15002); // Using Memory Address 15002-15017 // Display Componets // Link with Dual state button object with id 2 on page 1 named bt0 @@ -32,6 +33,7 @@ This code will run after every component is initialized void user_init() { timer1.begin(); + ct0.begin(); ESPMega_EXTLCD.print("page home"); ESPMega_EXTLCD.write(0xFF); ESPMega_EXTLCD.write(0xFF); @@ -45,6 +47,7 @@ This code will run once every event loop void user_loop() { nexLoop(nex_listen_list); + ct0.loop(); } /* @@ -82,14 +85,28 @@ void timer1_callback() } } -void ac_changed_user_callback(int mode, int temperature, int fan_speed) { - +void ac_changed_user_callback(int mode, int temperature, int fan_speed) +{ } -void mqtt_connected_user_callback() { - +void mqtt_connected_user_callback() +{ } -void user_state_request_callback() { +void user_state_request_callback() +{ +} +float adc2watts(uint16_t adc_value) +{ + const float RATIO = 0.1; + const float BURDEN_RESISTANCE = 20; + const float VOLTAGE = 220; + const uint16_t ADC_RANGE_START = 500; + const uint16_t ADC_RANGE_END = 16000; + const float ADC_RANGE = 12; + float burden_voltage = (adc_value - ADC_RANGE_START) / (ADC_RANGE_END - ADC_RANGE_START) * ADC_RANGE; + float secondary_current = burden_voltage / BURDEN_RESISTANCE; + float primary_current = secondary_current / RATIO; + return primary_current * VOLTAGE; } \ No newline at end of file diff --git a/src/user_code.hpp b/src/user_code.hpp index 72cc5a0..0833f3f 100644 --- a/src/user_code.hpp +++ b/src/user_code.hpp @@ -5,6 +5,7 @@ #include #include "espmega_iot_timer.hpp" #include "espmega_iot_external_lcd.hpp" +#include "espmega_iot_emon.hpp" // External LCD Configuration #define ENABLE_EXTERNAL_LCD @@ -27,6 +28,7 @@ void timer1_callback(); void mqtt_connected_user_callback(); void bt0PopCallback(void *ptr); void user_state_request_callback(); +float adc2watts(uint16_t adc_value); // ESPMega IoT Core Build-in Functions