@@ -140,12 +140,32 @@ MQTTClient::~MQTTClient() {
140140 free (this ->writeBuf );
141141}
142142
143- void MQTTClient::begin (const char _hostname[] , int _port , Client &_client ) {
143+ void MQTTClient::begin (IPAddress ipAddr , int port , Client &client ) {
144144 // set hostname and port
145- this ->setHost (_hostname, _port );
145+ this ->setHost (ipAddr, port );
146146
147147 // set client
148- this ->netClient = &_client;
148+ this ->netClient = &client;
149+
150+ // initialize client
151+ lwmqtt_init (&this ->client , this ->writeBuf , this ->bufSize , this ->readBuf , this ->bufSize );
152+
153+ // set timers
154+ lwmqtt_set_timers (&this ->client , &this ->timer1 , &this ->timer2 , lwmqtt_arduino_timer_set, lwmqtt_arduino_timer_get);
155+
156+ // set network
157+ lwmqtt_set_network (&this ->client , &this ->network , lwmqtt_arduino_network_read, lwmqtt_arduino_network_write);
158+
159+ // set callback
160+ lwmqtt_set_callback (&this ->client , (void *)&this ->callback , MQTTClientHandler);
161+ }
162+
163+ void MQTTClient::begin (const char hostname[], int port, Client &client) {
164+ // set hostname and port
165+ this ->setHost (hostname, port);
166+
167+ // set client
168+ this ->netClient = &client;
149169
150170 // initialize client
151171 lwmqtt_init (&this ->client , this ->writeBuf , this ->bufSize , this ->readBuf , this ->bufSize );
@@ -179,15 +199,22 @@ void MQTTClient::setClockSource(MQTTClientClockSource cb) {
179199 this ->timer2 .millis = cb;
180200}
181201
182- void MQTTClient::setHost (const char _hostname[], int _port) {
202+ void MQTTClient::setHost (IPAddress ipAddr, int port) {
203+ // set hostname and port
204+ this ->ipaddress = ipAddr;
205+
206+ this ->port = port;
207+ }
208+
209+ void MQTTClient::setHost (const char hostname[], int port) {
183210 // free hostname if set
184211 if (this ->hostname != nullptr ) {
185212 free ((void *)this ->hostname );
186213 }
187214
188215 // set hostname and port
189- this ->hostname = strdup (_hostname );
190- this ->port = _port ;
216+ this ->hostname = strdup (hostname );
217+ this ->port = port ;
191218}
192219
193220void MQTTClient::setWill (const char topic[], const char payload[], bool retained, int qos) {
@@ -285,7 +312,19 @@ bool MQTTClient::connect(const char clientId[], const char username[], const cha
285312
286313 // connect to host
287314 if (!skip) {
288- int ret = this ->netClient ->connect (this ->hostname , (uint16_t )this ->port );
315+ int ret = 0 ;
316+ if (this ->hostname != nullptr ){
317+ ret = this ->netClient ->connect (this ->hostname , (uint16_t )this ->port );
318+ }else {
319+ uint32_t thisIP = 0 ;
320+ thisIP = ipaddress[0 ];
321+ thisIP |= uint32_t (ipaddress[1 ] << 8 );
322+ thisIP |= uint32_t (ipaddress[2 ] << 16 );
323+ thisIP |= uint32_t (ipaddress[3 ] << 24 );
324+
325+ ret = this ->netClient ->connect (thisIP, (uint16_t )this ->port );
326+ }
327+
289328 if (ret <= 0 ) {
290329 this ->_lastError = LWMQTT_NETWORK_FAILED_CONNECT;
291330 return false ;
0 commit comments