diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index 09111cd..84c1ef3 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include // #define FRAM_DEBUG // #define MQTT_DEBUG @@ -14,6 +16,7 @@ #define WEB_SERVER_ENABLE #define LCD_OTA_ENABLE #define REMOTE_VARIABLE_ENABLE +#define CT_ENABLE // Demo PLC firmware using the ESPMegaPRO OOP library @@ -28,6 +31,16 @@ RemoteVariable testVar = RemoteVariable(); ESPMegaDisplayOTA internalDisplayOTA = ESPMegaDisplayOTA(); #endif +#ifdef CT_ENABLE +float adc2current(uint16_t adcValue) +{ + return (adcValue - 2048) * 0.0005; +} +AnalogCard analogCard = AnalogCard(); +float voltage = 220.0; +CurrentTransformer ct = CurrentTransformer(&analogCard, 0, &voltage, adc2current, 1000); +#endif + #ifdef CLIMATE_CARD_ENABLE // Climate Card const char *mode_names[] = {"off", "fan_only", "cool"}; @@ -202,6 +215,15 @@ void setup() ESP_LOGI("Initializer", "Initializing testvar"); testVar.begin(32, "/testvar", espmega.iot, true,"/testvar/request"); testVar.enableSetValue("/testvar/set"); +#endif +#ifdef CT_ENABLE + ESP_LOGI("Initializer", "Initializing analog card"); + analogCard.begin(); + ESP_LOGI("Initializer", "Initializing current transformer"); + ct.begin(); + ct.bindFRAM(&espmega.fram, 7000); + ct.loadEnergy(); + ct.setEnergyAutoSave(true); #endif ESP_LOGI("Initializer", "Setup complete"); } @@ -249,4 +271,14 @@ void loop() testVar.setValue("Hello World"); } #endif +#ifdef CT_ENABLE + ct.loop(); + static uint32_t last_ct_print = 0; + if (millis() - last_ct_print >= 1000) + { + last_ct_print = millis(); + Serial.print("Current: "); + Serial.println(ct.getCurrent()); + } +#endif } \ No newline at end of file