diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index cfb7219..387d277 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -56,6 +56,7 @@ const int16_t HASH_KEYWORD_LCN = 15137; const int16_t HASH_KEYWORD_RESET = 26133; const int16_t HASH_KEYWORD_SPEED28 = -17064; const int16_t HASH_KEYWORD_SPEED128 = 25816; +const int16_t HASH_KEYWORD_MQTT = 11111; int16_t DCCEXParser::stashP[MAX_COMMAND_PARAMS]; bool DCCEXParser::stashBusy; diff --git a/DccMQTT.cpp b/DccMQTT.cpp index 0c18584..6f7e845 100644 --- a/DccMQTT.cpp +++ b/DccMQTT.cpp @@ -33,6 +33,8 @@ #include +DccMQTT DccMQTT::singleton; + void DccMQTT::setup() { @@ -43,11 +45,12 @@ void DccMQTT::setup() // PubSubClient mqttClient(ethClient); // PubSubClient *DccMQTT::mqClient = &mqttClient; - server = IPAddress(MQTT_BROKER_ADDRESS); - - // char _csidMsg[64]{'\0'}; //!< string buffer for the serialized message to return - // mqttClient.setServer(server, MQTT_BROKER_PORT); // Initalize MQ broker + // get a eth client session + ethClient = EthernetInterface::get()->getServer()->available(); + // initalize MQ Broker + mqttClient = PubSubClient(ethClient); + mqttClient.setServer(IPAddress(MQTT_BROKER_ADDRESS), MQTT_BROKER_PORT); // DBG(F("MQTT Client : Server ok ...")); // mqttClient.setCallback(mqttCallback); // Initalize callback function for incomming messages diff --git a/DccMQTT.h b/DccMQTT.h index 2027fb8..c195f28 100644 --- a/DccMQTT.h +++ b/DccMQTT.h @@ -62,7 +62,6 @@ public: void loop(); ~DccMQTT() = default; - DccMQTT() }; diff --git a/EthernetInterface.h b/EthernetInterface.h index e97ebfc..4d14543 100644 --- a/EthernetInterface.h +++ b/EthernetInterface.h @@ -51,14 +51,26 @@ class EthernetInterface { public: - static void setup(); - static void loop(); - + static void setup(); + static void loop(); + + bool isConnected() { return connected; }; + static EthernetInterface *get() { return singleton; }; + EthernetClient *getClient(int socket) { return &clients[socket]; }; + EthernetServer *getServer() { return server; }; + + ~EthernetInterface() = default; + private: - static EthernetInterface * singleton; - bool connected; - EthernetInterface(); - void loop2(); + + EthernetInterface(); + EthernetInterface(const EthernetInterface&); // non construction-copyable + EthernetInterface& operator=( const EthernetInterface& ); // non copyable + static EthernetInterface * singleton; + bool connected; + + void loop2(); + EthernetServer * server; EthernetClient clients[MAX_SOCK_NUM]; // accept up to MAX_SOCK_NUM client connections at the same time; This depends on the chipset used on the Shield uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv