diff --git a/src/main.cpp b/src/main.cpp index bbeb4bd..c5c9eda 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,32 @@ #include - /*********************************************** * Begin Configuration * ***********************************************/ // Analog Card & Current Transformer Configuration -// bool analogCardAvailable = false; -// AnalogCard analogCard = AnalogCard(); -// float voltage = CT_RMS_VOLTAGE; -// CurrentTransformerCard ct1 = CurrentTransformerCard(&analogCard, 0, &voltage, &adc2current, 5000); -// CurrentTransformerCard ct2 = CurrentTransformerCard(&analogCard, 1, &voltage, &adc2current, 5000); -// CurrentTransformerCard ct3 = CurrentTransformerCard(&analogCard, 2, &voltage, &adc2current, 5000); -// CurrentTransformerCard ct4 = CurrentTransformerCard(&analogCard, 3, &voltage, &adc2current, 5000); -// CurrentTransformerCard ct5 = CurrentTransformerCard(&analogCard, 4, &voltage, &adc2current, 5000); -// CurrentTransformerCard ct6 = CurrentTransformerCard(&analogCard, 5, &voltage, &adc2current, 5000); +#ifdef ANALOG_ENABLE +bool analogCardAvailable = false; +AnalogCard analogCard = AnalogCard(); +float voltage = CT_RMS_VOLTAGE; +#define CT_INTERVAL 5000 +CurrentTransformerCard ct1 = CurrentTransformerCard(&analogCard, 0, &voltage, &adc2current, CT_INTERVAL); +CurrentTransformerCard ct2 = CurrentTransformerCard(&analogCard, 1, &voltage, &adc2current, CT_INTERVAL); +CurrentTransformerCard ct3 = CurrentTransformerCard(&analogCard, 2, &voltage, &adc2current, CT_INTERVAL); +CurrentTransformerCard ct4 = CurrentTransformerCard(&analogCard, 3, &voltage, &adc2current, CT_INTERVAL); +CurrentTransformerCard ct5 = CurrentTransformerCard(&analogCard, 6, &voltage, &adc2current, CT_INTERVAL); +CurrentTransformerCard ct6 = CurrentTransformerCard(&analogCard, 7, &voltage, &adc2current, CT_INTERVAL); +float adc2current(uint16_t adc_val) +{ + // float voltage = adc_val * 0.0007-0.1994; + float adc_voltage = adc_val * 0.0007; + // 0-10V Output with 30A Current Rating CT + float ct_current = adc_voltage / 10.0 * 30.0; + return ct_current; +} + +#endif + // Remote Variables RemoteVariable pm25_in = RemoteVariable(); @@ -26,11 +38,7 @@ RemoteVariable pm_fan_speed = RemoteVariable(); RemoteVariable pm_lock = RemoteVariable(); RemoteVariable ac_lock = RemoteVariable(); -float adc2current(uint16_t adc_val) { - return adc_val * 1.0; -} -#define CT_INTERVAL 5000 // Light Configuration uint8_t row = 4; @@ -42,8 +50,8 @@ const uint8_t light_array[4][2] = { {LIGHT_ROW4_COLUMN1, LIGHT_ROW4_COLUMN2}}; // Air Conditioner Configuration -const char *mode_names_daikin[] = {"off", "cool", "fan_only", "dry"}; -const char *mode_names_york[] = {"off", "cool", "fan_only"}; +const char *mode_names_daikin[] = {"off", "fan_only", "cool", "dry"}; +const char *mode_names_york[] = {"off", "fan_only", "cool"}; const char *fan_speed_names[] = {"auto", "high", "medium", "low"}; AirConditioner ac_daikin = { @@ -121,11 +129,22 @@ void setup() // Disable factory reset for now espmega.inputs.loop(); - //set debounce time to 200 for pin 0-7 - for (uint16_t i = 0; i < 8; i++){ - espmega.inputs.setDebounceTime(i,200); + // set debounce time to 200 for pin 0-7 + for (uint16_t i = 0; i < 8; i++) + { + espmega.inputs.setDebounceTime(i, 200); } + // if (clear_fram) + // { + // Serial.print("boot_state.txt=\"Factory Resetting . . .\""); + // sendStopBytes(); + // for (uint16_t i = 0; i < 32768; i++) + // { + // espmega.fram.write8(i, 0); + // } + // esp_restart(); + // } // // ------------ End Factory Reset Routine ------------ @@ -161,36 +180,35 @@ void setup() } // ------------ End Inputs and Outputs Initialization Routine ------------ espmega.outputs.setState(12, true); - espmega.outputs.setValue(12,4095); + espmega.outputs.setValue(12, 4095); espmega.installCard(2, &climateCard_daikin); climateCard_daikin.bindFRAM(&espmega.fram, 5000); climateCard_daikin.loadStateFromFRAM(); climateCard_daikin.setFRAMAutoSave(true); espmega.display->bindClimateCard(&climateCard_daikin); - // // Current Transformers - // espmega.installCard(4, &analogCard); - - // espmega.installCard(5, &ct1); - // ct1.bindFRAM(&espmega.fram, 6000); - // espmega.iot->registerCard(5); - - // espmega.installCard(6, &ct2); - // ct2.bindFRAM(&espmega.fram, 6100); - // espmega.iot->registerCard(6); - - // espmega.installCard(7, &ct3); - // ct3.bindFRAM(&espmega.fram, 6200); - // espmega.iot->registerCard(7); - // espmega.installCard(8, &ct4); - // ct4.bindFRAM(&espmega.fram, 6300); - // espmega.iot->registerCard(8); - // espmega.installCard(9, &ct5); - // ct5.bindFRAM(&espmega.fram, 6400); - // espmega.iot->registerCard(9); - // espmega.installCard(10, &ct6); - // ct6.bindFRAM(&espmega.fram, 6500); - // espmega.iot->registerCard(10); +// Current Transformers +#ifdef ANALOG_ENABLE + espmega.installCard(4, &analogCard); + espmega.installCard(5, &ct1); + ct1.bindFRAM(&espmega.fram, 6000); + espmega.iot->registerCard(5); + espmega.installCard(6, &ct2); + ct2.bindFRAM(&espmega.fram, 6100); + espmega.iot->registerCard(6); + espmega.installCard(7, &ct3); + ct3.bindFRAM(&espmega.fram, 6200); + espmega.iot->registerCard(7); + espmega.installCard(8, &ct4); + ct4.bindFRAM(&espmega.fram, 6300); + espmega.iot->registerCard(8); + espmega.installCard(9, &ct5); + ct5.bindFRAM(&espmega.fram, 6400); + espmega.iot->registerCard(9); + espmega.installCard(10, &ct6); + ct6.bindFRAM(&espmega.fram, 6500); + espmega.iot->registerCard(10); +#endif // ------------ Climate Cards Initialization Routine ------------ ESP_LOGD("ISE OS", "Setting up climate cards"); @@ -206,47 +224,48 @@ void setup() // York Climate Card ESP_LOGD("ISE OS", "Installing york climate card"); - espmega.installCard(10, &climateCard_york); + espmega.installCard(3, &climateCard_york); climateCard_york.bindFRAM(&espmega.fram, 5005); climateCard_york.loadStateFromFRAM(); climateCard_york.setFRAMAutoSave(true); // ------------ End Climate Cards Initialization Routine ------------ // ------------ Current Transformer Cards Initialization Routine ------------ - //ESP_LOGD("ISE OS", "Installing current transformer cards"); + #ifdef ANALOG_ENABLE + ESP_LOGD("ISE OS", "Installing current transformer cards"); // First try to install the analog card - //analogCardAvailable = espmega.installCard(4, &analogCard); + analogCardAvailable = espmega.installCard(4, &analogCard); // If the analog card is available, install the current transformer cards // If the analog card is not available, current transformer cards will not be installed // Unless CT_FORCE_ENABLE is set to true // This is to prevent soft locking the device when the device tries to read from an ADC channel that does not exist - // if (analogCardAvailable || CT_FORCE_ENABLE) - // { - // ESP_LOGV("ISE OS", "Analog card available, installing current transformer cards"); - // // espmega.installCard(5, &ct1); - // // ct1.bindFRAM(&espmega.fram, 5010); - // // espmega.installCard(6, &ct2); - // // ct2.bindFRAM(&espmega.fram, 5020); - // // espmega.installCard(7, &ct3); - // // ct3.bindFRAM(&espmega.fram, 5030); - // // espmega.installCard(8, &ct4); - // // ct4.bindFRAM(&espmega.fram, 5040); - // // espmega.installCard(9, &ct5); - // // ct5.bindFRAM(&espmega.fram, 5050); - // // espmega.installCard(10, &ct5); - // // ct6.bindFRAM(&espmega.fram, 5060); - // espmega.iot->registerCard(5); - // espmega.iot->registerCard(6); - // espmega.iot->registerCard(7); - // espmega.iot->registerCard(8); - // espmega.iot->registerCard(9); - // espmega.iot->registerCard(10); - // } - // else - // { - // ESP_LOGE("ISE OS", "Analog card not available, current transformer cards cannot and will not be installed."); - // } - //ESP_LOGD("ISE OS", "Registering Current Transformer Cards with IoT Module") + if (analogCardAvailable || CT_FORCE_ENABLE) + { + ESP_LOGV("ISE OS", "Analog card available, installing current transformer cards"); + espmega.installCard(5, &ct1); + ct1.bindFRAM(&espmega.fram, 5010); + espmega.installCard(6, &ct2); + ct2.bindFRAM(&espmega.fram, 5020); + espmega.installCard(7, &ct3); + ct3.bindFRAM(&espmega.fram, 5030); + espmega.installCard(8, &ct4); + ct4.bindFRAM(&espmega.fram, 5040); + espmega.installCard(9, &ct5); + ct5.bindFRAM(&espmega.fram, 5050); + espmega.installCard(10, &ct5); + ct6.bindFRAM(&espmega.fram, 5060); + espmega.iot->registerCard(5); + espmega.iot->registerCard(6); + espmega.iot->registerCard(7); + espmega.iot->registerCard(8); + espmega.iot->registerCard(9); + espmega.iot->registerCard(10); + } + else + { + ESP_LOGE("ISE OS", "Analog card not available, current transformer cards cannot and will not be installed."); + } + #endif // ------------ End Current Transformer Cards Initialization Routine ------------ @@ -288,7 +307,7 @@ void setup() espmega.iot->registerCard(0); // Register the Input Card espmega.iot->registerCard(1); // Register the Output Card espmega.iot->registerCard(2); // Register the Climate Card Daikin - espmega.iot->registerCard(10); // Register the Climate Card York + espmega.iot->registerCard(3); // Register the Climate Card York // ------------ End IoT Card Registration Routine ------------ @@ -356,6 +375,28 @@ void loop() iseDisplay.updateDateTimeText(time); last_time_updated = millis(); } +#ifdef ANALOG_ENABLE + // Send out analog Data every 6 seconds + static uint32_t last_analog_sent = 0; + if (millis() - last_analog_sent > 6000) + { + espmega.iot->publish("/debug/up", "1"); + if (analogCardAvailable || CT_FORCE_ENABLE) + { + espmega.iot->publish("/debug/log", "Sending Analog Card Data"); + char topic_buffer[50]; + char payload_buffer[50]; + // Publish ADC Pin 0-7 + for (uint8_t i = 0; i < 8; i++) + { + sprintf(topic_buffer, "/debug/analog/%d", i); + sprintf(payload_buffer, "%d", analogCard.analogRead(i)); + espmega.iot->publish(topic_buffer, payload_buffer); + } + } + last_analog_sent = millis(); + } +#endif } void on_pin_change(uint8_t pin, uint8_t value)