mqtt connection implementation
This commit is contained in:
parent
0dfb066a74
commit
1163f2e30f
|
@ -1,6 +1,7 @@
|
|||
#include <ESPMegaIoT.hpp>
|
||||
|
||||
ESPMegaIoT::ESPMegaIoT() {
|
||||
ESPMegaIoT::ESPMegaIoT() : mqtt(tcpClient) {
|
||||
tcpClient.setTimeout(1);
|
||||
// Initialize the components array
|
||||
for (int i = 0; i < 255; i++) {
|
||||
components[i] = NULL;
|
||||
|
@ -41,7 +42,6 @@ void ESPMegaIoT::setBaseTopic(char *base_topic) {
|
|||
|
||||
void ESPMegaIoT::intr_begin(ExpansionCard *cards[]) {
|
||||
this->cards = cards;
|
||||
ETH.begin();
|
||||
active = true;
|
||||
}
|
||||
void ESPMegaIoT::loop() {
|
||||
|
@ -106,12 +106,6 @@ void ESPMegaIoT::subscribeToTopic(char *topic) {
|
|||
void ESPMegaIoT::unsubscribeFromTopic(char *topic) {
|
||||
mqtt.unsubscribe(topic);
|
||||
}
|
||||
void ESPMegaIoT::connectToEthernet() {
|
||||
ETH.begin();
|
||||
}
|
||||
bool ESPMegaIoT::ethernetConnected() {
|
||||
return ETH.linkUp();
|
||||
}
|
||||
void ESPMegaIoT::connectToWifi(char *ssid, char *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) {
|
||||
// Store mqtt connection parameters
|
||||
Serial.println("Storing mqtt connection parameters");
|
||||
this->mqtt_server = mqtt_server;
|
||||
this->mqtt_port = mqtt_port;
|
||||
this->mqtt_useauth = false;
|
||||
this->client_id = client_id;
|
||||
Serial.println("Setting mqtt server");
|
||||
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);
|
||||
mqtt.setCallback(boundCallback);
|
||||
Serial.println("Connecting to mqtt");
|
||||
if(mqtt.connect(client_id)) {
|
||||
Serial.println("Connected to mqtt");
|
||||
Serial.println("Calling session keep alive");
|
||||
sessionKeepAlive();
|
||||
Serial.println("Subscribing to topics");
|
||||
mqttSubscribe();
|
||||
mqtt_connected = true;
|
||||
return true;
|
||||
}
|
||||
Serial.println("Failed to connect to mqtt");
|
||||
mqtt_connected = false;
|
||||
return false;
|
||||
|
||||
|
@ -187,19 +189,6 @@ void ESPMegaIoT::publishRelative(uint8_t card_id, char *topic, char *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() {
|
||||
if (mqtt_useauth) {
|
||||
return this->connectToMqtt(client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password);
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
class ESPMegaIoT
|
||||
{
|
||||
public:
|
||||
ESPMegaIoT();
|
||||
void intr_begin(ExpansionCard *cards[]);
|
||||
void loop();
|
||||
void registerCard(uint8_t card_id);
|
||||
|
@ -23,8 +24,6 @@ public:
|
|||
void subscribeRelative(char *topic);
|
||||
void subscribeToTopic(char *topic);
|
||||
void unsubscribeFromTopic(char *topic);
|
||||
void connectToEthernet();
|
||||
bool ethernetConnected();
|
||||
void connectToWifi(char *ssid, char *password);
|
||||
void connectToWifi(char *ssid);
|
||||
void disconnectFromWifi();
|
||||
|
@ -36,12 +35,10 @@ public:
|
|||
void registerMqttCallback(void (*callback)(char *, char *));
|
||||
void registerRelativeMqttCallback(void (*callback)(char *, char *));
|
||||
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();
|
||||
|
||||
private:
|
||||
WiFiClient tcpClient;
|
||||
void sessionKeepAlive();
|
||||
bool mqttReconnect();
|
||||
void wifiReconnect();
|
||||
|
|
|
@ -11,11 +11,7 @@ bool ESPMegaPRO::begin() {
|
|||
return false;
|
||||
}
|
||||
this->installCard(1, &outputs);
|
||||
if(!fram.begin(FRAM_ADDRESS)) {
|
||||
Serial.println("Failed to initialize FRAM");
|
||||
Serial.println("Is this an ESPMegaPRO device?");
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
|
|
|
@ -9,5 +9,5 @@ class ExpansionCard {
|
|||
// Preform a loop to refresh the input buffers
|
||||
virtual void loop() {}
|
||||
// Get the card type
|
||||
virtual uint8_t getType() {}
|
||||
virtual uint8_t getType() {return 255;}
|
||||
};
|
|
@ -1,5 +1,5 @@
|
|||
#include <ESPMegaPRO_OOP.hpp>
|
||||
|
||||
#include <ETH.h>
|
||||
// Instantiate ESPMega
|
||||
ESPMegaPRO espmega = ESPMegaPRO();
|
||||
|
||||
|
@ -11,21 +11,34 @@ void mqtt_callback(char *topic, char *payload)
|
|||
Serial.println(payload);
|
||||
}
|
||||
|
||||
IPAddress ip(192, 168, 0, 11);
|
||||
IPAddress gateway(192, 168, 0, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize ESPMega
|
||||
espmega.begin();
|
||||
Serial.println("ESPMega initialized");
|
||||
Serial.println("Initializing ESPMega IoT");
|
||||
espmega.enableIotModule();
|
||||
espmega.iot.connectToEthernet();
|
||||
IPAddress ip(192, 168, 0, 11);
|
||||
IPAddress gateway(192, 168, 0, 1);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
espmega.iot.setETHStaticIp(ip, gateway, subnet);
|
||||
Serial.println("ESPMega IoT module enabled");
|
||||
Serial.println("Setting static IP");
|
||||
Serial.println("Connecting to Ethernet");
|
||||
ETH.begin();
|
||||
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");
|
||||
Serial.println("Initializing MQTT");
|
||||
espmega.iot.connectToMqtt("espmega", "192.168.0.26", 1883);
|
||||
Serial.println("Publishing a test message");
|
||||
espmega.iot.publishRelative("test", "test");
|
||||
Serial.println("Subscribing to test topic");
|
||||
espmega.iot.subscribeRelative("test");
|
||||
Serial.println("Registering MQTT Callback");
|
||||
espmega.iot.registerMqttCallback(mqtt_callback);
|
||||
Serial.println("ESPMega IoT initialized");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue