ESPMegaPRO-v3-SDK/ESPMegaPRO-firmware/src/main.cpp

112 lines
3.9 KiB
C++
Raw Normal View History

2023-12-28 13:20:49 +00:00
#include <ESPMegaPRO_OOP.hpp>
2023-12-29 14:41:19 +00:00
#include <InternalDisplay.hpp>
2023-12-28 16:28:21 +00:00
#include <ETH.h>
2023-12-30 08:39:16 +00:00
#include <ClimateCard.hpp>
2023-12-28 13:20:49 +00:00
2023-12-30 11:47:52 +00:00
2023-12-30 11:27:39 +00:00
// Demo PLC firmware using the ESPMegaPRO OOP library
2023-12-28 13:20:49 +00:00
ESPMegaPRO espmega = ESPMegaPRO();
2023-12-30 17:25:07 +00:00
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);
2023-12-28 13:20:49 +00:00
2023-12-28 16:28:21 +00:00
void input_change_callback(uint8_t pin, uint8_t value) {
Serial.print("Input change callback: ");
Serial.print(pin);
Serial.print(" ");
Serial.println(value);
}
2023-12-28 13:20:49 +00:00
void setup() {
2023-12-30 11:47:52 +00:00
// Set each class's log level
esp_log_level_set("ESPMegaPRO", ESP_LOG_VERBOSE);
esp_log_level_set("DigitalOutputCard", ESP_LOG_VERBOSE);
esp_log_level_set("DigitalOutputIoT", ESP_LOG_VERBOSE);
esp_log_level_set("DigitalInputCard", ESP_LOG_VERBOSE);
esp_log_level_set("DigitalInputIoT", ESP_LOG_VERBOSE);
esp_log_level_set("AnalogCard", ESP_LOG_VERBOSE);
esp_log_level_set("AnalogIoT", ESP_LOG_VERBOSE);
esp_log_level_set("ClimateCard", ESP_LOG_VERBOSE);
esp_log_level_set("ClimateIoT", ESP_LOG_VERBOSE);
esp_log_level_set("InternalDisplay", ESP_LOG_VERBOSE);
esp_log_level_set("InternalDisplayIoT", ESP_LOG_VERBOSE);
2023-12-28 13:20:49 +00:00
espmega.begin();
espmega.enableIotModule();
2023-12-28 16:28:21 +00:00
ETH.begin();
espmega.iot->bindEthernetInterface(&ETH);
2023-12-28 13:20:49 +00:00
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");
2023-12-28 16:28:21 +00:00
Serial.println("Setting network config");
espmega.iot->setNetworkConfig(config);
2023-12-30 17:51:34 +00:00
espmega.iot->saveNetworkConfig();
2023-12-30 19:18:57 +00:00
// espmega.iot->loadNetworkConfig();
2023-12-28 16:28:21 +00:00
Serial.println("Connecting to network");
espmega.iot->connectNetwork();
2023-12-28 16:28:21 +00:00
Serial.println("Begin MQTT Modules");
2023-12-28 13:20:49 +00:00
MqttConfig mqtt_config = {
.mqtt_port = 1883,
.mqtt_useauth = false
};
2023-12-28 16:28:21 +00:00
Serial.println("Setting MQTT Server");
2023-12-28 13:20:49 +00:00
strcpy(mqtt_config.mqtt_server, "192.168.0.26");
2023-12-28 16:28:21 +00:00
strcpy(mqtt_config.base_topic, "/espmegaoop");
Serial.println("Loading MQTT Config Struct to IoT Module");
espmega.iot->setMqttConfig(mqtt_config);
2023-12-30 17:51:34 +00:00
espmega.iot->saveMqttConfig();
2023-12-30 19:18:57 +00:00
// espmega.iot->loadMqttConfig();
2023-12-28 16:28:21 +00:00
Serial.println("Connecting to MQTT");
espmega.iot->connectToMqtt();
2023-12-30 11:27:39 +00:00
Serial.println("Registering Output Card");
espmega.iot->registerCard(0);
2023-12-30 11:27:39 +00:00
Serial.println("Registering Input Card");
espmega.iot->registerCard(1);
2023-12-30 11:27:39 +00:00
Serial.println("Registering Input Change Callback");
2023-12-29 17:49:09 +00:00
espmega.inputs.registerCallback(input_change_callback);
2023-12-30 17:25:07 +00:00
Serial.println("Installing Climate Card");
espmega.installCard(2, &climateCard);
2023-12-30 19:18:57 +00:00
climateCard.bindFRAM(&espmega.fram, 301);
2023-12-30 17:37:50 +00:00
climateCard.loadStateFromFRAM();
climateCard.setFRAMAutoSave(true);
2023-12-30 11:27:39 +00:00
Serial.println("Enabling Internal Display");
espmega.enableInternalDisplay(&Serial);
2023-12-30 17:25:07 +00:00
espmega.display->bindClimateCard(&climateCard);
2023-12-30 11:27:39 +00:00
Serial.println("Initialization Routine Complete");
2023-12-30 19:18:57 +00:00
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFF);
Serial.println(sizeof(MqttConfig));
Serial.println(sizeof(NetworkConfig));
2023-12-28 13:20:49 +00:00
}
void loop() {
espmega.loop();
}