Node-Red MQTT node doesnt connect on every first start every morning?

Hello everyone,

I am having really strange problem and i am not able to understand why is it happening.
I am saving the MQTT credentials in settings.js process.env. I am using google cloud iot so i need a jwt to connect to broker.
I am generating a JWT and saving it in settings.js using python code and then i am getting all the credentials using $(MQTT_JWT) environement variables in node-red.

Now i have a shell script to generate a token and after generating token i start the node.red. And this shell script starts on every startup using systemctl on my RaspberryPI.

Now problem is that when i start the code every morning the MQTT connection doesnot establish and then i restart it and it works fine rest of the day on every restart. Can somebody help me what could be the reason for that?

Thanks!

Since MQTT is using TCP/IP, different implementations have different keep-alive configurations.

If you were running your own MQTT broker (like I do, for example), this might not be an issue; but when using a public service, like an IoT cloud service, they might drop connections after some time out value. This could be the reason you need to restart / reconnect (or it could be some other reason).

There are a number of approaches to solving this, but of course at this point in time, you / we do no know the cause of the connection loss. I have a number of MQTT clients that will "unsubscribe" after some time period, and others that never drop.

Do you know how long your IoT cloud server will keep an MQTT connection alive if no data is being transmitted (as I assume this is the case, please correct me if data is being actively transmitted when the connection drops).

Anyway, I might be way off track. Just an idea. Honestly, I do not fully understand your situation based on your write up.

I am having problem only on first start every morning. Thats means for example if i switch off my raspberryPI and then come back again next morning and switch it on, It doesnt not connect. And now if i restart/Shutdown(2nd restart) it again then it works without any problem through out the day. First i thought it might takes my old JWT from the settings.js and it might have been expired at that point. But today i debugged it and realised that it is not the problem and getting everytime the new generated JWT from the settings.js.

Got it... sorry, I misunderstood your original post. So, this is definitely not a TCP/IP connection timeout issue.

... and not being a public IoT cloud user, will drop off the discussion.

No problem. Thanks anyway :slight_smile:

How have you shown that?
When you say it connects ok if you restart node-red are you just restarting node-red or running the script again too?
Does node-red auto run on system start? If so it will be trying to start initially with the old credentials, could that be an issue?

Do you have an alternative means of connecting? Via the command line or other client? That way you could prove that it is not a problem at the other end.

image
I am also saving that generated JWT in a file and then comparing it with $(MQTT_JWT).

I have a shell script which run the script first and then run the node-red. I am running this shell script in systemctl. So first i do "systemctl stop shell.service" and then "systemctl start shell.service"

It could be issue but i can see by the debug payload that it has new credentials.

Do you mean just the mqtt connection via e.g. python code? its not a problem that way..

So after it does not run and you restart that means it is running the shell script again, so refreshing settings.js again. If after it fails to connect you stop node-red, but then start it not using the service, so just running node-red in a terminal for example, does it connect?

echo "Waiting for python script to success!"
while
      output=$(python3 jwt.py)
      [ "$output" != 'status=done' ]
do
      echo "The python script did not output 'status=done', repeating..."
      sleep 1
done
echo "Yay! python succeeded. Let's run node-red"
sleep 10
node-red

Thats my shell script...

yes

it runs second time without any problem. only problem is the first time

Very odd. If you don't use the service file at all, but stop node-red, run the script to update settings.js and then manually start node-red does it work?

Yes it works that way

I increase the expiry time of JWT and tested it today. It is works fine. So i think it takes the old JWT for some reason . do you have any idea about this?

Is it starting the service automatically on boot or are you manually starting after it has booted? If the former do you know what happens if you disable auto start and wait till it has fully booted and then start it manually?

It is starting automatically.. I think i got it whats happening. I think its something with the RaspberryPI clock. Its takes the old last day time. And the epxiry is of the JWT is for some hours. so e.g. if it takes the last day time and create a jwt then this JWT is already expired and hence the authentification fails

Ah yes, good thinking. You could try adding
After=multi-user.target
to the [Unit] section of the systemd service.

1 Like

I just connected a real time clock to my Raspi. I hope it will work. after that i will try this .. thanks a lot for your help! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.