NodeRED/MQTT disconnect from AWS EC2

I have successfully setup NodeRED on an AWS EC2 instance and use MQTT as a means to communicate from/to my IoT devices (ESP8266/ESP32 at the moment).

I'm running a compiled version of Tasmota that has AWS IoT enabled.

What's strange is the MQTT nodes in my flow disconnect every few days (no pattern as far as I can tell). To reconnect I edit one of the nodes (same config for all MQTT nodes), edit the MQTT broker (simply 'edit' followed by 'update' without any changes) and deploy the flow. Nodes reconnect and everything works fine.

My Internet connection shouldn't come into play here as the NodeRED instance/app runs on AWS EC2 and connects to AWS IoT Core so this should be within the AWS Cloud environment as far as I can understand. Even if EC2 and the IoT Core were on different regions it's still AWS and any latency shouldn't influence this, right?

  • Does NodeRED have the ability to automatically reconnect on dropping of a connection?
  • What's the easiest way to diagnose this/what log files could I use to even start investigating?

Is your Node-RED instance set to run on start ? Maybe you AWS instance is rebooting/moving ?

Hmm. I haven't touched this instance in months. I'd hope that these don't just reboot by itself. Where would I find the NodeRED logs for a standard install? Maybe there's something to be found...

Depends how you start it. If as a service it should log to the system log. If via a command at start then to the console.

Ok, finally managed to logon to the AWS console... the Node-RED messages are coming through to the console.

I did a straight install via

sudo npm install -g --unsafe-perm node-red node-red-admin

Two questions

  • Is it possible to switch this over to a service so that this gets logged to the systemlog
  • How do I ensure Node-RED automatically restarts when the instance restarts?

Thanks

If you installed on Ubuntu then you could follow the instructions here ? https://nodered.org/docs/getting-started/aws#running-on-aws-ec2-with-ubuntu
Though the default debian script from the Pi install should also work - https://nodered.org/docs/getting-started/raspberrypi

I never really got along with AWS Iot, so launched an EC2 instance running mqtt server instead. From what I could tell it's cheaper to run as well.

At some point I experienced kinda the same issue on the EC2 instance, as you describe but after disabling logging, the problem went away.

Followed the AWS instructions. All ok except the last one where the pm2 startup asks you to edit a startup script. What file am I looking for and where would that be located?

I followed the setup section on https://nodered.org/docs/getting-started/aws#running-on-aws-ec2-with-ubuntu.

The last step

pm2 startup

shows this

ubuntu
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu

which startup script is this referring to? Folder?

As a followup question after this is done and Node-RED starts as a service where would I find the Node-RED log file?

PM2 is trying to tell you to run that command for PM2 to be set as a systemd script and start up automatically. After that is done, you'll need to start Node-RED using PM2 and then it should be managed by it.

Thanks. Well, that's almost embarrassing to have missed that :slight_smile:

Node-RED comes up automatically.

Where does Node-RED log to by default given it runs as a service? I'm not a Unix expert so struggling a bit here. Had a look at /node-red/settings.js and the section logging states only console logging is supported. Given I just moved away from the console what happens to logging?

If you're running it using PM2, the logs will be handled by it and by default placed under $HOME/.pm2. It will log what would otherwise have been displayed on the console. So I assume the logs will be under /home/ubuntu/.pm2 in your case.

Note that PM2 can write errors to a separate file if configure so. If I remember correctly that is the default.

Bingo!

Yes, it was in /home/ubuntu/.pm2/logs and it writes two separate files node-red-error.log and node-red-out.log

The log level detail would still be defined in settings.js, right?

Thanks heaps!

Yep, correct.

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