# MQTT Dynamic connection with changing IP Adress

**URL:** https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299
**Category:** General
**Tags:** mqtt
**Created:** [20 October 2022 05:38 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299 "2022-10-20T05:38:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [20 October 2022 05:38 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/1 "2022-10-20T05:38:37Z")

</div>

Hello

I have an MQTT Server on a Raspberry Pi which is not manageable. Evry time my Wifi is down it receives a new IP Adress. Unfortunately, I can also not assign IP-Adress to MAC Adress on the Wifi.

I manged to retrieve in Node-Red the active IP-Adress. I failed to connect MQTT to the new Adress.

With dynamic connection it connects only when I use the same IP Adress as connected before. I'm also unsure if I can use an environment Variable in the broker settings. See below.  
But since it already fails to connect with the inject node and a new IP. I don't know what I do wrong. On the node it remains on status connecting.

{"broker":"192.168.1.7","port":1883,"force":true}

{"broker":MyActiveIP,"port":1883,"force":true}

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [20 October 2022 05:58 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/2 "2022-10-20T05:58:32Z")

</div>

Can you reach `raspberrypi.local` from your node-red instance ?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 October 2022 06:49 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/3 "2022-10-20T06:49:42Z")

</div>

It is easy to configure a PI with a fixed IP address by setting it up in /etc/dhcpcd.conf.  
I think that file probably includes a commented out example of how to do it. In case you can't work it out I will post again later showing how to do it.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [20 October 2022 07:22 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/4 "2022-10-20T07:22:53Z")

</div>

Are you sending msg.action = "connect"?  
This works for me (in a function node)

```auto
const broker = msg.payload
msg.broker = {
    "broker": broker,
    "username": "pie",
    "password": "chickenandmushroom",
    "port":1883,
    "force": true
}
msg.action = "connect"
msg.topic = "test"
return msg;

```

---

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [20 October 2022 07:43 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/5 "2022-10-20T07:43:35Z")

</div>

Yes I use the connect action and force. It will disconnect first and after remains on connecting.

Is broker the IP Adress of your MQTT Server?

Here my function to connect:  
var oldStr=msg.payload; //oldStr received IP String  
var newIP = oldStr.slice(0, oldStr.length-1);  
msg.broker={"broker":newStr,"port":1883,"force":true};  
msg.action="connect";  
return msg;

It is also not working if I use the Inject Node with the new IP.

---

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [20 October 2022 07:45 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/6 "2022-10-20T07:45:58Z")

</div>

> [@manfred9999](#):
>
> newStr

Sorry it is NewIP not newStr

---

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [20 October 2022 07:48 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/7 "2022-10-20T07:48:29Z")

</div>

No the Pi is not accessible at all. It has an image with a software solution and nothing else than the application is accessible.

---

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [20 October 2022 07:50 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/8 "2022-10-20T07:50:24Z")

</div>

Thanks, but I can't configure the Pi. Access is blocked by the distributor of the application image.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [20 October 2022 08:48 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/9 "2022-10-20T08:48:01Z")

</div>

> [@manfred9999](#):
>
> Is broker the IP Adress of your MQTT Server?

In my example I passed the IP address as a string "192.168.1.100" in msg.payload.

```auto
[{"id":"d04192770059b0e9","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"a8a44d75fc130cd5","type":"inject","z":"d04192770059b0e9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"192.168.1.11","payloadType":"str","x":290,"y":160,"wires":[["931a276b1c3aec01"]]},{"id":"931a276b1c3aec01","type":"function","z":"d04192770059b0e9","name":"function 8","func":"const broker = msg.payload\nmsg.broker = {\n \"broker\": broker,\n \"username\": \"pie\",\n \"password\": \"chickenandmushroom\",\n \"port\":1883,\n \"force\": true\n}\nmsg.action = \"connect\"\nmsg.topic = \"test\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":160,"wires":[["3b0980807031b9fb"]]},{"id":"3b0980807031b9fb","type":"mqtt in","z":"d04192770059b0e9","name":"","topic":"","qos":"2","datatype":"auto-detect","broker":"a212f2307397ae96","nl":false,"rap":true,"rh":0,"inputs":1,"x":610,"y":160,"wires":[[]]},{"id":"a212f2307397ae96","type":"mqtt-broker","name":"","broker":"192.168.1.100","port":"1883","clientid":"","autoConnect":false,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 October 2022 13:16 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/10 "2022-10-20T13:16:19Z")

</div>

> [@manfred9999](#):
>
> I can't configure the Pi. Access is blocked by the distributor of the application image.

What sort of application image is it that does not allow you to configure the ip address? Take out the SD card, plug it in a PC and edit the file.

Alternatively, what sort of router is it that does not allow you to specify an IP for a MAC address? Even the supposedly locked down ones usually do have access to such things if you know where to find it. If not then get yourself a decent router.

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [20 October 2022 13:48 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/11 "2022-10-20T13:48:53Z")

</div>

> [@Colin](#):
>
> Even the supposedly locked down ones usually do have access to such things if you know where to find it. If not then get yourself a decent router.

I would certainly double check if you cannot configure this on your router/modem.

Maybe a bit overkill in this case but using a router that allows to assign fixed IP address to a MAC addresses might also resolve your issue. You can even consider adding this router in between your internet modem and the rest of your network

---

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [20 October 2022 15:02 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/12 "2022-10-20T15:02:48Z")

</div>

I already tried to use another router because my Internet/Wifi router can not manage to asign IP to MAC adress.

But since my application uses a special access from the Internet and the local IP adress is maped by a special cloudservice the application failed to run properly.

I'm stuck to reconfigure the MQTT server either automated or manual which sucks. Since I manage my solarsystem and need the values from my Inverter and the MQTT Server within this special app it complicates all the more.

---

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [20 October 2022 15:09 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/13 "2022-10-20T15:09:04Z")

</div>

The application is Solarassistant and has a special cloud service to get access from the internet. It fails to work when I use a router behind my Internet modem/wifi.

I'm also unsure if it would work to edit the files on the sd card on the PC, since the network part is special and needs some handshake with a cloud service.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 October 2022 15:28 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/14 "2022-10-20T15:28:12Z")

</div>

Since you presumably paid for this, I suggest you ask them how to fix the ip address.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 October 2022 15:37 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/15 "2022-10-20T15:37:16Z")

</div>

What router make and model is it that you have?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [20 October 2022 16:08 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/16 "2022-10-20T16:08:07Z")

</div>

does it actually work without dynamic subscription ?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 October 2022 16:10 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/17 "2022-10-20T16:10:41Z")

</div>

Do you mean it might not work if DHCP always gives it the same address? I don't see how it can make any difference to the Application.

---

<div class="post-metadata">

### Author: ![manfred9999](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@manfred9999](https://discourse.nodered.org/u/manfred9999)
#### Post date: [21 October 2022 00:03 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/18 "2022-10-21T00:03:24Z")

</div>

It will only have always the same address if I use a second router, but than the application is not running properly because it tries to connect to the cloud service. In this cloud service the local IP address is stored and updated enabling a weblink to access the app from the internet. When I tried with a second router the app rsponse was slow and sometimes failed to work.  
I was in contact with the developer of this app and he will implement a fixed IP address, but the question is when. I wait for that update 6 month already, but I need a solution now because of a problem with my solar system.....

My router is PLDT modem which is is a fiber modem/wifi here in Philippines. It has no option to configure the DHCP section.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [21 October 2022 00:40 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/19 "2022-10-21T00:40:50Z")

</div>

> [@manfred9999](#):
>
> but than the application is not running properly because it tries to connect to the cloud service. In this cloud service the local IP address is stored and updated enabling a weblink to access the app from the internet.

I don't understand this cloud service, local IP and connecting to an app from the internet.  
The cloud based service changes it's IP address? Doesn't it have a domain name?

Some ISP routers won't let you change the DHCP settings but usually you can at least disable the DHCP server.  
You could use the raspberry pi to provide your own DHCP server with address reservation. My Node-red Pi also runs PiHole, (a private DNS server and DHCP server.)

I recall a conversation in the raspberry pi forum about bizarre restrictions with a router provided by PLDT.  
Maybe there are tech groups in the Philippines who would have experience of using these strange devices.

Otherwise, talk to PLDT support.

Edit - another possible solution might be:  
Set up a Zerotier account and create a network ID.  
Install Zerotier on your Node-red and MQTT devices and join them to the network.  
They will each gain a second, fixed IP address over the VPN, such as 192.168.192.100  
Use this IP address in your MQTT config.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 October 2022 08:13 UTC](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299/20 "2022-10-21T08:13:12Z")

</div>

> [@jbudd](#):
>
> The cloud based service changes it's IP address?

It is the pi that is changing it's ip address. The app (in the pi) has to be configured with the ip address of the pi for some reason and the rubbish modem the OP has gives a different ip address every time the pi connects.

[Next page](https://discourse.nodered.org/t/mqtt-dynamic-connection-with-changing-ip-adress/69299.md?page=2)
