diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.cpp b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.cpp index ce0ed20..e03a204 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.cpp @@ -10,6 +10,13 @@ bool DigitalInputIoT::begin(uint8_t card_id, DigitalInputCard *card, PubSubClien return true; } + +void DigitalInputIoT::subscribe() { + char topic[64]; + sprintf(topic, "%s/%d/%s", this->base_topic, this->card_id, PUBLISH_ENABLE_TOPIC); + this->subscribeRelative(topic); +} + void DigitalInputIoT::handleMqttMessage(char *topic, char *payload) { // payload is char '0' or '1' if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) { @@ -52,4 +59,13 @@ void DigitalInputIoT::publishReport() { } uint8_t DigitalInputIoT::getType() { return CARD_TYPE_DIGITAL_INPUT; +} + + +void DigitalInputIoT::publishDigitalInput(uint8_t pin) { + char topic[64]; + char payload[2]; + sprintf(topic, "%s/%d/%d", this->base_topic, this->card_id, pin); + sprintf(payload, "%d", this->card->digitalRead(pin, false)); + this->publishRelative(topic, payload); } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp index 51d9a62..574058c 100644 --- a/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/DigitalInputIoT.hpp @@ -2,6 +2,7 @@ #include #include +#include #define PUBLISH_ENABLE_TOPIC "publish_enable" @@ -15,6 +16,7 @@ class DigitalInputIoT : public IoTComponent { void handleValueChange(uint8_t pin, uint8_t value); void registerChangeCallback(std::function callback); void publishReport(); + void subscribe(); uint8_t getType(); private: uint8_t card_id; diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp index e14903c..7bf11ea 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -1,5 +1,8 @@ #include +#define NETWORK_CONFIG_ADDRESS 34 +#define MQTT_CONFIG_ADDRESS 34 + sizeof(NetworkConfig) + ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient) { tcpClient.setTimeout(1); @@ -106,7 +109,7 @@ void ESPMegaIoT::registerCard(uint8_t card_id) Serial.println("Invalid card type"); return; } - Serial.println("Card registered"); + Serial.println("Card registered"); } void ESPMegaIoT::deregisterCard(uint8_t card_id) { @@ -305,4 +308,95 @@ void ESPMegaIoT::subscribeRelative(char *topic) void ESPMegaIoT::registerSubscribeCallback(void (*callback)(void)) { user_subscribe_callback = callback; +} + +void ESPMegaIoT::setNetworkConfig(NetworkConfig network_config) +{ + this->network_config = network_config; + this->connectNetwork(); +} + +void ESPMegaIoT::loadNetworkConfig() +{ + // Load the network config from FRAM + fram->read(0, (uint8_t *)&network_config, sizeof(NetworkConfig)); +} + +void ESPMegaIoT::saveNetworkConfig() +{ + // Save the network config to FRAM + fram->write(NETWORK_CONFIG_ADDRESS, (uint8_t *)&network_config, sizeof(NetworkConfig)); +} + +void ESPMegaIoT::ethernetBegin() +{ + ETH.begin(); + ETH.setHostname(network_config.hostname); +} + +void ESPMegaIoT::loadMqttConfig() +{ + // Load the mqtt config from FRAM + MqttConfig mqtt_config; + fram->read(sizeof(NetworkConfig), (uint8_t *)&mqtt_config, sizeof(MqttConfig)); + // Populate the mqtt connection parameters + strcpy(mqtt_server, mqtt_config.mqtt_server); + mqtt_port = mqtt_config.mqtt_port; + strcpy(mqtt_user, mqtt_config.mqtt_user); + strcpy(mqtt_password, mqtt_config.mqtt_password); + mqtt_useauth = mqtt_config.mqtt_useauth; +} + +void ESPMegaIoT::saveMqttConfig() +{ + // Save the mqtt config to FRAM + MqttConfig mqtt_config; + strcpy(mqtt_config.mqtt_server, mqtt_server); + mqtt_config.mqtt_port = mqtt_port; + strcpy(mqtt_config.mqtt_user, mqtt_user); + strcpy(mqtt_config.mqtt_password, mqtt_password); + mqtt_config.mqtt_useauth = mqtt_useauth; + fram->write(MQTT_CONFIG_ADDRESS, (uint8_t *)&mqtt_config, sizeof(MqttConfig)); +} + +void ESPMegaIoT::connectToMqtt() +{ + if (mqtt_useauth) + { + this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password); + } + else + { + this->connectToMqtt(client_id, mqtt_server, mqtt_port); + } +} + +void ESPMegaIoT::connectNetwork() +{ + if (network_config.useWifi) + { + if (network_config.wifiUseAuth) + this->connectToWifi(network_config.ssid, network_config.password); + else + this->connectToWifi(network_config.ssid); + if (network_config.useStaticIp) + WiFi.config(network_config.ip, network_config.gateway, network_config.subnet); + else + WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); + } + else + { + this->ethernetBegin(); + if (network_config.useStaticIp) + ETH.config(network_config.ip, network_config.gateway, network_config.subnet, network_config.dns1, network_config.dns2); + } +} + +void ESPMegaIoT::setMqttConfig(MqttConfig mqtt_config) +{ + strcpy(mqtt_server, mqtt_config.mqtt_server); + mqtt_port = mqtt_config.mqtt_port; + strcpy(mqtt_user, mqtt_config.mqtt_user); + strcpy(mqtt_password, mqtt_config.mqtt_password); + mqtt_useauth = mqtt_config.mqtt_useauth; } \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp index 845b952..f64190a 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -8,6 +8,32 @@ #include #include #include +#include +#include + +struct NetworkConfig +{ + IPAddress ip; + IPAddress gateway; + IPAddress subnet; + IPAddress dns1; + IPAddress dns2; + char hostname[32]; + bool useStaticIp; + bool useWifi; + bool wifiUseAuth; + char ssid[32]; + char password[32]; +}; + +struct MqttConfig +{ + char mqtt_server[32]; + uint16_t mqtt_port; + char mqtt_user[32]; + char mqtt_password[32]; + bool mqtt_useauth; +}; class ESPMegaIoT { @@ -28,6 +54,15 @@ public: void connectToWifi(char *ssid); void disconnectFromWifi(); bool wifiConnected(); + void ethernetBegin(); + void loadNetworkConfig(); + void saveNetworkConfig(); + void setMqttConfig(MqttConfig mqtt_config); + void saveMqttConfig(); + void loadMqttConfig(); + void connectNetwork(); + void setNetworkConfig(NetworkConfig network_config); + void connectToMqtt(); bool connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password); bool connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port); void disconnectFromMqtt(); @@ -39,6 +74,11 @@ public: IPAddress getETHIp(); private: + FRAM *fram; + bool useWifi; + bool WifiUseAuth; + char ssid[32]; + char password[32]; WiFiClient tcpClient; void sessionKeepAlive(); bool mqttReconnect(); @@ -64,4 +104,5 @@ private: char *client_id; bool mqtt_useauth; bool mqtt_connected; + NetworkConfig network_config; }; \ No newline at end of file diff --git a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp index fbe0bd2..ac06492 100644 --- a/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp +++ b/Template Project/lib/ESPMegaPRO/ESPMegaPRO_OOP.cpp @@ -4,14 +4,16 @@ ESPMegaPRO::ESPMegaPRO() { } bool ESPMegaPRO::begin() { Wire.begin(14, 33); + fram.begin(FRAM_ADDRESS); Serial.begin(115200); + this->installCard(1, &outputs); + outputs.bindFRAM(&fram,0); + outputs.loadFromFRAM(); if(!this->installCard(0, &inputs)) { Serial.println("Failed to initialize inputs"); Serial.println("Is this an ESPMegaPRO device?"); return false; } - this->installCard(1, &outputs); - fram.begin(FRAM_ADDRESS); uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8}; inputs.loadPinMap(pinMap); return true; diff --git a/Template Project/src/iot_framdemo.cpp b/Template Project/src/iot_framdemo.cpp new file mode 100644 index 0000000..f0ff3d6 --- /dev/null +++ b/Template Project/src/iot_framdemo.cpp @@ -0,0 +1,36 @@ +#include + +ESPMegaPRO espmega = ESPMegaPRO(); + +void setup() { + espmega.begin(); + espmega.enableIotModule(); + 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"); + espmega.iot.setNetworkConfig(config); + espmega.iot.connectNetwork(); + MqttConfig mqtt_config = { + .mqtt_port = 1883, + .mqtt_useauth = false + }; + strcpy(mqtt_config.mqtt_server, "192.168.0.26"); + espmega.iot.setMqttConfig(mqtt_config); + espmega.iot.connectToMqtt(); + espmega.iot.registerCard(0); + espmega.iot.registerCard(1); +} + +void loop() { + espmega.loop(); +} \ No newline at end of file diff --git a/Template Project/src/iotdemo.cpp b/Template Project/src/iotdemo.cpp index 2fa7e88..d2846a0 100644 --- a/Template Project/src/iotdemo.cpp +++ b/Template Project/src/iotdemo.cpp @@ -29,7 +29,6 @@ void setup() espmega.iot.registerMqttCallback(mqtt_callback); espmega.iot.registerCard(1); espmega.iot.publishCard(1); - } void loop()