MQTT Dynamic connection with changing IP Adress

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}

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

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.

1 Like

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

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

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.

Sorry it is NewIP not newStr

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

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

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

[{"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":""}]

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.

2 Likes

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

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.

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.

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

1 Like

What router make and model is it that you have?

does it actually work without dynamic subscription ?

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.

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.

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.

1 Like

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.