mqtt connection implementation

This commit is contained in:
Siwat Sirichai 2023-12-28 15:18:37 +07:00
parent 0dfb066a74
commit 1163f2e30f
7 changed files with 33 additions and 38 deletions

View File

@ -1,6 +1,7 @@
#include <ESPMegaIoT.hpp> #include <ESPMegaIoT.hpp>
ESPMegaIoT::ESPMegaIoT() { ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient) {
tcpClient.setTimeout(1);
// Initialize the components array // Initialize the components array
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++) {
components[i] = NULL; components[i] = NULL;
@ -41,7 +42,6 @@ void ESPMegaIoT::setBaseTopic(char *base_topic) {
void ESPMegaIoT::intr_begin(ExpansionCard *cards[]) { void ESPMegaIoT::intr_begin(ExpansionCard *cards[]) {
this->cards = cards; this->cards = cards;
ETH.begin();
active = true; active = true;
} }
void ESPMegaIoT::loop() { void ESPMegaIoT::loop() {
@ -106,12 +106,6 @@ void ESPMegaIoT::subscribeToTopic(char *topic) {
void ESPMegaIoT::unsubscribeFromTopic(char *topic) { void ESPMegaIoT::unsubscribeFromTopic(char *topic) {
mqtt.unsubscribe(topic); mqtt.unsubscribe(topic);
} }
void ESPMegaIoT::connectToEthernet() {
ETH.begin();
}
bool ESPMegaIoT::ethernetConnected() {
return ETH.linkUp();
}
void ESPMegaIoT::connectToWifi(char *ssid, char *password) { void ESPMegaIoT::connectToWifi(char *ssid, char *password) {
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
} }
@ -146,19 +140,27 @@ bool ESPMegaIoT::connectToMqtt(char*client_id, char *mqtt_server, uint16_t mqtt_
} }
bool ESPMegaIoT::connectToMqtt(char* client_id, char *mqtt_server, uint16_t mqtt_port) { bool ESPMegaIoT::connectToMqtt(char* client_id, char *mqtt_server, uint16_t mqtt_port) {
// Store mqtt connection parameters // Store mqtt connection parameters
Serial.println("Storing mqtt connection parameters");
this->mqtt_server = mqtt_server; this->mqtt_server = mqtt_server;
this->mqtt_port = mqtt_port; this->mqtt_port = mqtt_port;
this->mqtt_useauth = false; this->mqtt_useauth = false;
this->client_id = client_id; this->client_id = client_id;
Serial.println("Setting mqtt server");
mqtt.setServer(mqtt_server, mqtt_port); mqtt.setServer(mqtt_server, mqtt_port);
Serial.println("Setting mqtt callback");
auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); auto boundCallback = std::bind(&ESPMegaIoT::mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
mqtt.setCallback(boundCallback); mqtt.setCallback(boundCallback);
Serial.println("Connecting to mqtt");
if(mqtt.connect(client_id)) { if(mqtt.connect(client_id)) {
Serial.println("Connected to mqtt");
Serial.println("Calling session keep alive");
sessionKeepAlive(); sessionKeepAlive();
Serial.println("Subscribing to topics");
mqttSubscribe(); mqttSubscribe();
mqtt_connected = true; mqtt_connected = true;
return true; return true;
} }
Serial.println("Failed to connect to mqtt");
mqtt_connected = false; mqtt_connected = false;
return false; return false;
@ -187,19 +189,6 @@ void ESPMegaIoT::publishRelative(uint8_t card_id, char *topic, char *payload) {
mqtt.publish(absolute_topic, payload); mqtt.publish(absolute_topic, payload);
} }
void ESPMegaIoT::setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) {
ETH.config(ip, gateway, subnet, dns1, dns2);
}
void ESPMegaIoT::setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1) {
ETH.config(ip, gateway, subnet, dns1);
}
void ESPMegaIoT::setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet) {
ETH.config(ip, gateway, subnet);
}
IPAddress ESPMegaIoT::getETHIp() {
return ETH.localIP();
}
bool ESPMegaIoT::mqttReconnect() { bool ESPMegaIoT::mqttReconnect() {
if (mqtt_useauth) { if (mqtt_useauth) {
return this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password); return this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password);

View File

@ -12,6 +12,7 @@
class ESPMegaIoT class ESPMegaIoT
{ {
public: public:
ESPMegaIoT();
void intr_begin(ExpansionCard *cards[]); void intr_begin(ExpansionCard *cards[]);
void loop(); void loop();
void registerCard(uint8_t card_id); void registerCard(uint8_t card_id);
@ -23,8 +24,6 @@ public:
void subscribeRelative(char *topic); void subscribeRelative(char *topic);
void subscribeToTopic(char *topic); void subscribeToTopic(char *topic);
void unsubscribeFromTopic(char *topic); void unsubscribeFromTopic(char *topic);
void connectToEthernet();
bool ethernetConnected();
void connectToWifi(char *ssid, char *password); void connectToWifi(char *ssid, char *password);
void connectToWifi(char *ssid); void connectToWifi(char *ssid);
void disconnectFromWifi(); void disconnectFromWifi();
@ -36,12 +35,10 @@ public:
void registerMqttCallback(void (*callback)(char *, char *)); void registerMqttCallback(void (*callback)(char *, char *));
void registerRelativeMqttCallback(void (*callback)(char *, char *)); void registerRelativeMqttCallback(void (*callback)(char *, char *));
void setBaseTopic(char *base_topic); void setBaseTopic(char *base_topic);
void setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2);
void setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1);
void setETHStaticIp(IPAddress ip, IPAddress gateway, IPAddress subnet);
IPAddress getETHIp(); IPAddress getETHIp();
private: private:
WiFiClient tcpClient;
void sessionKeepAlive(); void sessionKeepAlive();
bool mqttReconnect(); bool mqttReconnect();
void wifiReconnect(); void wifiReconnect();

View File

@ -11,11 +11,7 @@ bool ESPMegaPRO::begin() {
return false; return false;
} }
this->installCard(1, &outputs); this->installCard(1, &outputs);
if(!fram.begin(FRAM_ADDRESS)) { fram.begin(FRAM_ADDRESS);
Serial.println("Failed to initialize FRAM");
Serial.println("Is this an ESPMegaPRO device?");
return false;
}
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

@ -9,5 +9,5 @@ class ExpansionCard {
// Preform a loop to refresh the input buffers // Preform a loop to refresh the input buffers
virtual void loop() {} virtual void loop() {}
// Get the card type // Get the card type
virtual uint8_t getType() {} virtual uint8_t getType() {return 255;}
}; };

View File

@ -1,5 +1,5 @@
#include <ESPMegaPRO_OOP.hpp> #include <ESPMegaPRO_OOP.hpp>
#include <ETH.h>
// Instantiate ESPMega // Instantiate ESPMega
ESPMegaPRO espmega = ESPMegaPRO(); ESPMegaPRO espmega = ESPMegaPRO();
@ -11,21 +11,34 @@ void mqtt_callback(char *topic, char *payload)
Serial.println(payload); Serial.println(payload);
} }
IPAddress ip(192, 168, 0, 11);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
void setup() void setup()
{ {
// Initialize ESPMega // Initialize ESPMega
espmega.begin(); espmega.begin();
Serial.println("ESPMega initialized"); Serial.println("ESPMega initialized");
Serial.println("Initializing ESPMega IoT");
espmega.enableIotModule(); espmega.enableIotModule();
espmega.iot.connectToEthernet(); Serial.println("ESPMega IoT module enabled");
IPAddress ip(192, 168, 0, 11); Serial.println("Setting static IP");
IPAddress gateway(192, 168, 0, 1); Serial.println("Connecting to Ethernet");
IPAddress subnet(255, 255, 255, 0); ETH.begin();
espmega.iot.setETHStaticIp(ip, gateway, subnet); delay(1000);
ETH.config(ip, gateway, subnet);
Serial.println("Static IP set");
Serial.println("Begin MQTT Initialization Routine");
Serial.println("Setting MQTT Base Topic");
espmega.iot.setBaseTopic("/testmegaoop"); espmega.iot.setBaseTopic("/testmegaoop");
Serial.println("Initializing MQTT");
espmega.iot.connectToMqtt("espmega", "192.168.0.26", 1883); espmega.iot.connectToMqtt("espmega", "192.168.0.26", 1883);
Serial.println("Publishing a test message");
espmega.iot.publishRelative("test", "test"); espmega.iot.publishRelative("test", "test");
Serial.println("Subscribing to test topic");
espmega.iot.subscribeRelative("test"); espmega.iot.subscribeRelative("test");
Serial.println("Registering MQTT Callback");
espmega.iot.registerMqttCallback(mqtt_callback); espmega.iot.registerMqttCallback(mqtt_callback);
Serial.println("ESPMega IoT initialized"); Serial.println("ESPMega IoT initialized");
} }