Skip to content

Commit bd807bf

Browse files
TMRh20256dpi
authored andcommitted
Support IPAddress
- Instead of using hostname and DNS lookups, use IPAddress to remove need for UDP when used with software IP stacks. Saves program space and memory.
1 parent a86c225 commit bd807bf

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/MQTTClient.cpp

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

193220
void 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;

src/MQTTClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class MQTTClient {
4343

4444
Client *netClient = nullptr;
4545
const char *hostname = nullptr;
46+
IPAddress ipaddress;
4647
int port = 0;
4748
lwmqtt_will_t *will = nullptr;
4849
MQTTClientCallback callback;
@@ -65,6 +66,9 @@ class MQTTClient {
6566

6667
void begin(const char _hostname[], Client &_client) { this->begin(_hostname, 1883, _client); }
6768
void begin(const char hostname[], int port, Client &client);
69+
70+
void begin(IPAddress _ipAddr, Client &client) { this->begin(_ipAddr, 1883, client); }
71+
void begin(IPAddress ipAddr, int port, Client &client);
6872

6973
void onMessage(MQTTClientCallbackSimple cb);
7074
void onMessageAdvanced(MQTTClientCallbackAdvanced cb);
@@ -73,6 +77,9 @@ class MQTTClient {
7377

7478
void setHost(const char _hostname[]) { this->setHost(_hostname, 1883); }
7579
void setHost(const char hostname[], int port);
80+
81+
void setHost(IPAddress _ipAddr) { this->setHost(_ipAddr, 1883); }
82+
void setHost(IPAddress ipAddr, int port);
7683

7784
void setWill(const char topic[]) { this->setWill(topic, ""); }
7885
void setWill(const char topic[], const char payload[]) { this->setWill(topic, payload, false, 0); }

0 commit comments

Comments
 (0)