-2

https://diyi0t.com/microcontroller-to-raspberry-pi-wifi-mqtt-communication/

I used the way that article set the broker on Raspberry but strange thing happen

According to article , I need to use

sudo systemctl start mosquitto 

to start connection then can use

sudo systemctl status mosquitto

and yes the status is active , BUT actually can't use if i use the command to check

mosquitto

it shows Error : Address already in use

(Remember , my esp32 device actually fail to connection in this time)

And if I manually close broker

sudo systemctl stop mosquitto

then check status , really in inactive

sudo systemctl status mosquitto

Now if I use command

mosquitto

the esp32 device can connect successfully now (I have another python script using mqtt to get data)

I bet the problem is on sudo systemctl start mosquitto & mosquitto
permission problem but not sure how to solve (or maybe other problem)

----20201103 update ---- log file after sudo systemctl start mosquitto and then mosquitto

config loaded from /etc/mosquitto/mosquitto.conf.
Opening ipv4 listen socket on port 1883.
Error : Address already in use

So currently , the way I can connect esp32 device with raspberry by mqtt is using command

mosquitto

But not use

sudo systemctl start mosquitto

----20201104 update ---- I'll Reply you all here, maybe I didn't explain it clear:

Situation One

Running sudo systemctl start mosquitto and then mosquitto

It's because sudo systemctl start mosquitto can't make connection work

And I use mosquitto to check what happen

Situation Two

Running sudo systemctl stop mosquitto and then mosquitt

I use sudo systemctl stop mosquitto to stop broker first then use mosquitto and the connection work


But it shouldn't be like this the right way is use sudo systemctl start mosquitto only

situation1 is totally wrong

situation2 although it can work but not the right way to do

Chevady Ju
  • 43
  • 9
  • Please check `/var/log/mosquitto/mosquitto.log` for any errors (after running `sudo systemctl start mosquitto`). The path to the log is set in `mosquitto.conf` (I'm assuming you copied this from the tutorial). The error you get when running `mosquitto` is what I would expect to see if its already running. – Brits Nov 03 '20 at 04:43
  • @Brits it just show the same thing when I execute mosquitto I'll put log in question based on charc limits – Chevady Ju Nov 03 '20 at 05:13
  • Running `sudo systemctl start mosquitto` and then `mosquitto` sounds like you are starting Mosquitto twice? The `systemctl` command should be starting Mosquitto (in the background) so running it again will result in the error you are seeing. If you want to test if mosquitto is running then use `ps` and `mosquitto_sub`/`mosquitto_pub` see [this issue](https://stackoverflow.com/questions/30207649/address-already-in-use-error-in-mosquitto). Please show what happens in the log when you run `sudo systemctl start mosquitto` (and nothing else), – Brits Nov 03 '20 at 08:00
  • As per previous comments (and hardillb's answer) what you are seeing is expected if Mosquitto is already running (and exactly what I would expect from running `sudo systemctl start mosquitto` and then `mosquitto`) . Your real question appears to be "why cant I connect to Mosquitto when its running as a service"; and for that we would need the logs (if nothing is being logged then that is significant in itself). – Brits Nov 04 '20 at 03:19

1 Answers1

2

This is all working as intended.

sudo systemctl start mosquitto will start an instance of the broker as a background service, running mosquitto on the command line will try and start a second instance on the broker (in the foreground). This will fail because the background instance is already bound to port 1883 (the default MQTT port) and will report the error you are seeing.

If you want to test that the background instance is working properly then start at point 2 in this answer to how to test a mosquitto broker.

hardillb
  • 54,545
  • 11
  • 67
  • 105