debug start

This commit is contained in:
Siwat Sirichai 2023-12-28 20:20:49 +07:00
parent c1d598ab20
commit fc907e0c7a
7 changed files with 194 additions and 4 deletions

View File

@ -10,6 +10,13 @@ bool DigitalInputIoT::begin(uint8_t card_id, DigitalInputCard *card, PubSubClien
return true; 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) { void DigitalInputIoT::handleMqttMessage(char *topic, char *payload) {
// payload is char '0' or '1' // payload is char '0' or '1'
if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) { if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) {
@ -52,4 +59,13 @@ void DigitalInputIoT::publishReport() {
} }
uint8_t DigitalInputIoT::getType() { uint8_t DigitalInputIoT::getType() {
return CARD_TYPE_DIGITAL_INPUT; 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);
} }

View File

@ -2,6 +2,7 @@
#include <IoTComponent.hpp> #include <IoTComponent.hpp>
#include <DigitalInputCard.hpp> #include <DigitalInputCard.hpp>
#include <FRAM.h>
#define PUBLISH_ENABLE_TOPIC "publish_enable" #define PUBLISH_ENABLE_TOPIC "publish_enable"
@ -15,6 +16,7 @@ class DigitalInputIoT : public IoTComponent {
void handleValueChange(uint8_t pin, uint8_t value); void handleValueChange(uint8_t pin, uint8_t value);
void registerChangeCallback(std::function<void(uint8_t, uint8_t)> callback); void registerChangeCallback(std::function<void(uint8_t, uint8_t)> callback);
void publishReport(); void publishReport();
void subscribe();
uint8_t getType(); uint8_t getType();
private: private:
uint8_t card_id; uint8_t card_id;

View File

@ -1,5 +1,8 @@
#include <ESPMegaIoT.hpp> #include <ESPMegaIoT.hpp>
#define NETWORK_CONFIG_ADDRESS 34
#define MQTT_CONFIG_ADDRESS 34 + sizeof(NetworkConfig)
ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient) ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient)
{ {
tcpClient.setTimeout(1); tcpClient.setTimeout(1);
@ -106,7 +109,7 @@ void ESPMegaIoT::registerCard(uint8_t card_id)
Serial.println("Invalid card type"); Serial.println("Invalid card type");
return; return;
} }
Serial.println("Card registered"); Serial.println("Card registered");
} }
void ESPMegaIoT::deregisterCard(uint8_t card_id) void ESPMegaIoT::deregisterCard(uint8_t card_id)
{ {
@ -305,4 +308,95 @@ void ESPMegaIoT::subscribeRelative(char *topic)
void ESPMegaIoT::registerSubscribeCallback(void (*callback)(void)) void ESPMegaIoT::registerSubscribeCallback(void (*callback)(void))
{ {
user_subscribe_callback = callback; 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;
} }

View File

@ -8,6 +8,32 @@
#include <IoTComponent.hpp> #include <IoTComponent.hpp>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <ETH.h> #include <ETH.h>
#include <WiFi.h>
#include <FRAM.h>
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 class ESPMegaIoT
{ {
@ -28,6 +54,15 @@ public:
void connectToWifi(char *ssid); void connectToWifi(char *ssid);
void disconnectFromWifi(); void disconnectFromWifi();
bool wifiConnected(); 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, char *mqtt_user, char *mqtt_password);
bool connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port); bool connectToMqtt(char *client_id, char *mqtt_server, uint16_t mqtt_port);
void disconnectFromMqtt(); void disconnectFromMqtt();
@ -39,6 +74,11 @@ public:
IPAddress getETHIp(); IPAddress getETHIp();
private: private:
FRAM *fram;
bool useWifi;
bool WifiUseAuth;
char ssid[32];
char password[32];
WiFiClient tcpClient; WiFiClient tcpClient;
void sessionKeepAlive(); void sessionKeepAlive();
bool mqttReconnect(); bool mqttReconnect();
@ -64,4 +104,5 @@ private:
char *client_id; char *client_id;
bool mqtt_useauth; bool mqtt_useauth;
bool mqtt_connected; bool mqtt_connected;
NetworkConfig network_config;
}; };

View File

@ -4,14 +4,16 @@ ESPMegaPRO::ESPMegaPRO() {
} }
bool ESPMegaPRO::begin() { bool ESPMegaPRO::begin() {
Wire.begin(14, 33); Wire.begin(14, 33);
fram.begin(FRAM_ADDRESS);
Serial.begin(115200); Serial.begin(115200);
this->installCard(1, &outputs);
outputs.bindFRAM(&fram,0);
outputs.loadFromFRAM();
if(!this->installCard(0, &inputs)) { if(!this->installCard(0, &inputs)) {
Serial.println("Failed to initialize inputs"); Serial.println("Failed to initialize inputs");
Serial.println("Is this an ESPMegaPRO device?"); Serial.println("Is this an ESPMegaPRO device?");
return false; 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}; uint8_t pinMap[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8};
inputs.loadPinMap(pinMap); inputs.loadPinMap(pinMap);
return true; return true;

View File

@ -0,0 +1,36 @@
#include <ESPMegaPRO_OOP.hpp>
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();
}

View File

@ -29,7 +29,6 @@ void setup()
espmega.iot.registerMqttCallback(mqtt_callback); espmega.iot.registerMqttCallback(mqtt_callback);
espmega.iot.registerCard(1); espmega.iot.registerCard(1);
espmega.iot.publishCard(1); espmega.iot.publishCard(1);
} }
void loop() void loop()