#include #include #include #include // #define FRAM_DEBUG // Demo PLC firmware using the ESPMegaPRO OOP library ESPMegaPRO espmega = ESPMegaPRO(); const uint16_t irCode[15][4][4][1] = {0}; const char *mode_names[] = {"Off", "Fan-only", "Cool"}; const char *fan_speed_names[] = {"Auto", "Low", "Medium", "High"}; size_t getInfraredCode(uint8_t mode, uint8_t fan_speed, uint8_t temperature, const uint16_t **codePtr) { // Change the code pointer to point to the IR timing array *codePtr = &(irCode[mode][fan_speed][temperature][0]); return sizeof(irCode[mode][fan_speed][temperature]) / sizeof(uint16_t); } AirConditioner ac = { .max_temperature = 30, .min_temperature = 16, .modes = 4, .mode_names = mode_names, .fan_speeds = 4, .fan_speed_names = fan_speed_names, .getInfraredCode = &getInfraredCode}; ClimateCard climateCard = ClimateCard(14, ac); void input_change_callback(uint8_t pin, uint8_t value) { Serial.print("Input change callback: "); Serial.print(pin); Serial.print(" "); Serial.println(value); } void setNetworkConfig() { NetworkConfig config = { .ip = {192, 168, 0, 11}, .gateway = {192, 168, 0, 1}, .subnet = {255, 255, 255, 0}, .dns1 = {192, 168, 0, 1}, .dns2 = {192, 168, 0, 1}, .useStaticIp = true, .useWifi = false, .wifiUseAuth = false, }; strcpy(config.ssid, "ssid"); strcpy(config.password, "password"); strcpy(config.hostname, "espmega"); Serial.println("Setting network config"); espmega.iot->setNetworkConfig(config); espmega.iot->saveNetworkConfig(); } void setMqttConfig() { MqttConfig config = { .mqtt_port = 1883, .mqtt_useauth = false}; strcpy(config.mqtt_server, "192.168.0.26"); strcpy(config.base_topic, "/espmegaoop"); espmega.iot->setMqttConfig(config); espmega.iot->saveMqttConfig(); } void setup() { ESP_LOGI("Initializer", "Starting ESPMegaPRO OOP demo"); espmega.begin(); ESP_LOGI("Initializer", "Enabling IOT module"); espmega.enableIotModule(); ESP_LOGI("Initializer", "Enabling Ethernet"); ETH.begin(); ESP_LOGI("Initializer", "Binding Ethernet to IOT module"); espmega.iot->bindEthernetInterface(Ð); ESP_LOGI("Initializer", "Loading network config"); espmega.iot->loadNetworkConfig(); ESP_LOGI("Initializer", "Connecting to network"); espmega.iot->connectNetwork(); ESP_LOGI("Initializer", "Loading MQTT config"); espmega.iot->loadMqttConfig(); ESP_LOGI("Initializer", "Connecting to MQTT"); espmega.iot->connectToMqtt(); ESP_LOGI("Initializer", "Registering cards 0"); espmega.iot->registerCard(0); ESP_LOGI("Initializer", "Registering cards 1"); espmega.iot->registerCard(1); ESP_LOGI("Initializer", "Registering Input change callback"); espmega.inputs.registerCallback(input_change_callback); ESP_LOGI("Initializer", "Installing climate card"); espmega.installCard(2, &climateCard); ESP_LOGI("Initializer", "Binding climate card to FRAM"); climateCard.bindFRAM(&espmega.fram, 301); ESP_LOGI("Initializer", "Loading climate card state from FRAM"); climateCard.loadStateFromFRAM(); ESP_LOGI("Initializer", "Enabling climate card FRAM autosave"); climateCard.setFRAMAutoSave(true); ESP_LOGI("Initializer", "Enabling internal display"); espmega.enableInternalDisplay(&Serial); ESP_LOGI("Initializer", "Binding climate card to internal display"); espmega.display->bindClimateCard(&climateCard); } void loop() { espmega.loop(); #ifdef FRAM_DEBUG // Every 20 seconds, dump FRAM 0-500 to serial static uint32_t last_fram_dump = 0; if (millis() - last_fram_dump >= 20000) { last_fram_dump = millis(); Serial.println("Dumping FRAM"); espmega.dumpFRAMtoSerial(0, 500); Serial.println("Dumping FRAM ASCII"); espmega.dumpFRAMtoSerialASCII(0, 500); } #endif }