Josar / RandomStuff

8 stars 6 forks source link

Connection to mosquitto fails #4

Open irosaurus opened 4 years ago

irosaurus commented 4 years ago

Hello Josar,

first of all thank you very much for releasing this awesome tool!

Since a couple of days I have the issue that RFmqtt cant connect to mosquitto anymore.

If I try it on the command line:

# ./RFmqtt
Running with -h localhost -p 1883 -u openhabian -x *************** -t 433MHz -g 3 -w 0
Can't connect to Mosquitto server

Service shows the following:

# systemctl status rfmqtt.service -l
● rfmqtt.service - RFmqtt Sniffer
   Loaded: loaded (/etc/systemd/system/rfmqtt.service; enabled)
   Active: activating (auto-restart) (Result: exit-code) since Sa 2020-06-13 22:12:12 CEST; 269ms ago
  Process: 12948 ExecStart=/srv/scripts/RFmqtt (code=exited, status=255)
 Main PID: 12948 (code=exited, status=255)

Jun 13 22:12:12 openHAB RFmqtt-Sniffer[12948]: Running with -h localhost -p 1883 -u openhabian -x *************** -t 433MHz -g 3 -w 0
Jun 13 22:12:12 openHAB systemd[1]: rfmqtt.service: main process exited, code=exited, status=255/n/a
Jun 13 22:12:12 openHAB systemd[1]: Unit rfmqtt.service entered failed state.

Already recompiled RFmqtt with the last RFmqtt.cpp, but didnt make any difference. RFSniffer is working fine though.

I noticed that there was also a new version of mosquitto installed, 1.6.10. Before I was on 1.6.8.

Maybe there was a slight change how to connect?

All my other clients can connect to mosquitto without any issues though. Tried the highest debug level on mosquitto, but I cant even see the attempt from RFmqtt to connect to the service.

OS Openhabian on Raspberry:

# cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Any hint would be very much appreciated.

Cheers iro

y8s commented 4 years ago

I am also running 1.6.10 and have noticed the can't connect error. I tried downgrading to 1.6.8 again and it doesn't work with that either. even using RFmqtt with no arguments doesn't work.

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
irosaurus commented 4 years ago

I upgraded to Raspbian GNU/Linux 9 (stretch) in the meantime. Still the same issue - Can't connect to Mosquitto server.

@y8s Did you manage to fix it?

Cheers iro

ramybt commented 4 years ago

I have similar problem where only RFSNiffer work but RFmqtt not showing any received code . If you guys find a solution please post here i would appreciate this alot..

Thank you

Here where all happened for me!

https://community.openhab.org/t/433mhz-module/95515/27?u=ramy_rutu

irosaurus commented 4 years ago

Hey Ramy,

I saw your posts and I think you have a different issue, as y8s an me both see a message, that the connection to Mosquitto fails.

Whats your output when you run

# ./RFmqtt

?

Cheers iro

y8s commented 4 years ago

@irosaurus I didn't fix this issue. I circumvented it.

I was using this in nodered primarily, but since this issue never got fixed, I just put an exec node with RFSniffer in it. I then send the output payload of that to a big fat switch node with all the RF codes sorted appropriately.

It's still the biggest resource hog on my pi, but I guess that's life.

ramybt commented 4 years ago

Hey Ramy,

I saw your posts and I think you have a different issue, as y8s an me both see a message, that the connection to Mosquitto fails.

Whats your output when you run

# ./RFmqtt

?

Cheers iro

I dont have anything showing when i run ./RFmqtt

When i run ./RFSniffer i do get to see received code from my sensor

ramybt commented 4 years ago

20201102_151654.jpg

Josar commented 4 years ago

@ramybt try to comment in the code which states for debbug and rebuild rfmqtt. Then run it again to see what is printet out. Also make sure to send something to the receiver.

And try to run it with "sudo".

Josar commented 4 years ago

@irosaurus can you publish a message from cmd line to your server?

y8s commented 3 years ago

@Josar mosquitto_pub and sub work just fine on my machine.

2020-11-16

However:

2020-11-16 (1)

Could it be something with RFmqtt forcing additional arguments (port, user, passwd...)?

irosaurus commented 3 years ago

Hi guys,

sorry for the late reply. Missed the email notifications...

@Josar: I have exactly the same result as @y8s - I can pub and sub without any issues. And also the same error when I try to run ./RFmqtt

I only noticed that mosquitto_pub/sub wants -P for the password and RFmqtt uses -x.

Let me know if I can test anything else.

Cheers iro

Josar commented 3 years ago

You have to insert the parameter or the default values are taken. This does not support no password or no user. It clearly states as output which values are used. So either config your server to except topics with this values or Insert the values your server is configured to.

@irosaurus -p is port, so I needed to use - x for password.

y8s commented 3 years ago

@Josar Here's the full version of the above: 2020-11-18 (1)

even adding the default user pass to the passwordfile and using those for mosquitto_sub and mosquitto_pub works fine, but not with RFmqtt.

I also notice that while I can see my mosquitto_pub/sub subscription/message in the mosquitto log, I do not see anything from RFmqtt. mosquitto log is set to debug. I'm not sure if I would expect to see.

If I deliberately try to connect with a wrong password, I still get something with mosquitto_pub (mosquitto config is set to allow anonymous)

example:

pi@iot:~ $ mosquitto_pub -h localhost -u admin -P thisisntit -t testtopic/failuretopic -m testmessage

(from log)
1605717736: No will message specified.
1605717736: Sending CONNACK to mosq-SIqLdE1Nh73d0Cau1i (0, 0)
1605717736: Received PUBLISH from mosq-SIqLdE1Nh73d0Cau1i (d0, q0, r0, m0, 'testtopic/failuretopic', ... (11 bytes))
1605717736: Received DISCONNECT from mosq-SIqLdE1Nh73d0Cau1i
irosaurus commented 3 years ago

Same here, I also see nothing in the mosquitto log when I use the correct password with RFmqtt.

@Josar mosquitto_pub uses -p for the port and -P for the password. But I guess that doesn't really matter here.

Let me know if I can help testing anything else.

Cheers iro

Ratsea34 commented 3 years ago

I have got it working and am not now getting the 'can't connect' message. The script says don't change the keep-alive value 0, but I changed it to 10 and everything now works. // Establish a connection to the MQTT server. Do not use a keep-alive ping int ret = mosquitto_connect (mosq, host, port, 10); // I changed this to 10 from 0

Update - it does however stop after a while, but this is clearly the problem area. I don't know how to resolve it other that a reboot every 12 hrs and a keep-alive time in excess of that. (Bodge!)

y8s commented 3 years ago

OK you have uncovered something...

the client from RFmqtt is not sending a PINGREQ for some reason.

It is supposed to connect, wait for the keepalive time, then send a PINGREQ to the broker which will respond with A PINGRESP and keep the connection open.

That's not happening here. I'm not sure why. I have other clients running on this pi that work fine.

Ratsea34 commented 3 years ago

I was really trying to get round the can't load the server. As a workaround I have set a reboot at midnight and stay alive 50000! This pi is just monitoring some temp and oil tank level. Richard

⁣Sent from BlueMail ​

On 24 Nov 2020, 15:38, at 15:38, y8s notifications@github.com wrote:

OK you have uncovered something...

the client from RFmqtt is not sending a PINGREQ for some reason.

It is supposed to connect, wait for the keepalive time, then send a PINGREQ to the broker which will respond with A PINGRESP and keep the connection open.

That's not happening here. I'm not sure why. I have other clients running on this pi that work fine.

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/Josar/RandomStuff/issues/4#issuecomment-733054582

irosaurus commented 3 years ago

@Josar any idea how to fix the missing PINGREQ? A daily reboot isn't really an option for me, as there is too much stuff running on my Pi :/

Josar commented 3 years ago

Sorry I am quite busy. Maybe I will find some time in the next free days.

Ratsea34 commented 3 years ago

I have just set a time of 50000 and not doing a reboot. It seems to be still working after 5 days according to the sysemctl status. I don't understand ! Richard

⁣Sent from BlueMail ​

On 12 Dec 2020, 17:08, at 17:08, irosaurus notifications@github.com wrote:

@Josar any idea how to fix the missing PINGREQ? A daily reboot isn't really an option for me, as there is too much stuff running on my Pi :/

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/Josar/RandomStuff/issues/4#issuecomment-743785059

irosaurus commented 3 years ago

Sorry I am quite busy. Maybe I will find some time in the next free days.

No worries! Take your time. Appreciate you're taking care :)

I changed the keep-alive now to 50000, too. Service is at least up and running again. Lets see for how long.

Cheers iro

ramybt commented 3 years ago

I have been following all the answers here, your problem dont exist for me but i have related issue perhaps i mention this above and i have a feeling might be all tangled here!

can you please post the steps you done from 0 to now and maybe put up the links my problem so far is that my ./RFmqtt not receiving anything but ./RFSniffer works no problem and receives anything right away.

I have installed : sudo apt install -y mosquitto mosquitto-clients git clone --recursive https://github.com/WiringPi/WiringPi.git +./build git clone –recursive git://github.com/ninjablocks/433Utils.git + make stepes than everything here : https://github.com/Josar/RandomStuff/tree/master/Openhab-related than installed misc-mqttbroker - 2.5.11+binding-mqtt - 2.5.11+ MQTT Persistence set my openhab2/services/mqtt.cfg file to

mymosquitto.url=tcp://mylocalhost:1883
mymosquitto.user=admin
mymosquitto.pwd=password

my connection : header_pinout

it used to work for me until mid 2018 and than stopped

Ratsea34 commented 3 years ago

All I did was: line 117 int ret = mosquitto_connect (mosq, host, port, 0); in RFMQTT.cpp, changed the 0 to 50000. You then need to following the update instruction in the readme to recompile RFMQTT with 'make' in the directory /433Utils/RPi_utils.

Good luck

Josar commented 3 years ago

misc-mqttbroker needs to be properly configured. If it is used as broker. And if you have mosquitto broker you don`t need an additional broker. (might be problematic to run 2 broker with same port config.)

  1. For this to work a broker need to run somewhere, e.g mosquitto . And needs to be configured to recieve the messages from the user with the password.
  2. Also Openhab mqtt client needs to be configured. And recieve messages. Try to send them with comand line mosquitto.
  3. RFmqtt needs to run. 3.1 Recieve rf messages, try it be uncommenting the debug code parts and compile it with 'make' 3.2 RFmqtt needs to properly connect to the broker.

You might try each connection seperatly and figure out which one works and which not. Also there might be some resource conflicts when e.g. 2 broker are running on the same device and use the same ports.

@Ratsea34 @irosaurus i don't used the keepalive as i thought, broker and client run on RPI if there is something wrong with the network connection then the RPI has a bigger issue. But it should be ok to set the keep-alive ping to 3600 end execute a Ping every hour. This should ensure client broker connection and don't add to much unwanted trafic to the network.

ramybt commented 3 years ago

This thing isn't working!

my RFSniffer is fine x Untitled Unthitled sdsd rfmqtt not receiving anything! kk

`sudo cat /var/log/mosquitto/mosquitto.log mosquitto.log

Josar commented 3 years ago

As you don't receive rf packets there seems to already be a problem with the pin configuration. There might be different default pins for ninjablocks rfsniffer and Rfmqtt. (short look into the code, don't know why I choose pin 3)

Rfsniffer pin 2, Rfmqtt pin 3. Try to use the parameter when calling the program.

seems to snuck into the code when fixing something else. https://github.com/Josar/RandomStuff/commit/8480062dab6cbb6ed501a0fd005821137969c6b7#diff-04a336b2dab9f37975a2b8997a96e90feeb2fa9f0d5b3d4a308072f0c6a0145d

I changed it back to port 2.

ramybt commented 3 years ago

I did the steps above and no deal ! nothing shows up where i run ./RFmqtt

if anyone here in this form who got this thing working please help by posting all your steps and make it clear and detailed .

I tried both the old MQTT Binding (1.x) and the new one MQTT Binding-mqtt - 2.5.11

if anyone who has other option than this please share

Thank You

Josar commented 3 years ago

Did you try to run rfmqtt with -g 2? Do you receive rf messages?

ramybt commented 3 years ago

@Josar i have tried what you suggested, nothing happens and it's same.

i went through my old files and found this RFmqtt.cpp

[]()https://drive.google.com/file/d/1_jTgcATX2O9STfdD1UYlJ8uPprBC-b8V/view?usp=drivesdk

replaced it with the current one and than compiled it with make and it works now, i can receive my door sensor signal using both ./RFSniffer and ./RFmqtt 👍 on the other hand openhab2 not getting my signal !

i use habpanel and i have a dummy switch that i use for same project before, when door sensor runing the switch change to ON/OFF my OH2 broker is Online and set to mylocal IP of my RPI3.1883 for port and no username or password i left it on default

is the file i found fine ? seems to be almost the same except for the bottom part .

Josar commented 3 years ago

Can not open the file.

ramybt commented 3 years ago

Can not open the file.

Try again now or here

https://drive.google.com/file/d/1_jTgcATX2O9STfdD1UYlJ8uPprBC-b8V/view?usp=drivesdk

y8s commented 3 years ago

OK everyone, I think I got this sorted.

So after doing a bit more digging into the library functions, this program needs to have some implementation of the loop function:

mosquitto_loop*()

Otherwise the program connects, publishes, and walks away forever. The loop handles the pingreq/resp portion of the client connection.

SO here is a fix you can try. Insert the line mosquitto_loop_start(mosq); below the connection code:

// Establish a connection to the MQTT server. Do not use a keep-alive ping
int ret = mosquitto_connect (mosq, host, port, 60); // <==== SET KEEPALIVE TO 60 (mosquitto default)
mosquitto_loop_start(mosq);   // <==== ADD THIS LINE

THEN, at the end of the file, in the "Tidy Up" section, stop the loop after the disconnect:

// Tidy up
mosquitto_disconnect (mosq);
mosquitto_loop_stop(mosq, false);   // <==== ADD THIS LINE 
mosquitto_destroy (mosq);
mosquitto_lib_cleanup();

You should now see the PINGREQ and PINGRESP show up every 60 seconds. The loop_start() function starts a new loop thread to handle the keepalive and reconnection so you can let this thing sit for longer periods. Since I use mine to detect RF doorbell presses, it ends up sitting for hours or days at a time before it's called.

Sidenote: One other thing I did was to hardcode the client ID to be "RFmqtt" to make it easier to capture the data in logs, but you don't really need to. If you want you can do this:

Up at the / default falues / section:

char MQTT_CLIENT[] = "RFmqtt\0";

Then just below that where the variables are initialized:

char* client= MQTT_CLIENT;

And finally, when we define the mosquitto instance, replace the first NULL with "client":

mosq = mosquitto_new (client, true, NULL); // <=== put client where NULL was (NULL allows auto client id)

Volunteers?

y8s commented 3 years ago

update:

● RFmqtt.service - 433MHz Receiver on RPI GPIO sending to MQTT Broker
   Loaded: loaded (/lib/systemd/system/RFmqtt.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2021-01-15 15:58:12 EST; 3 days ago
 Main PID: 563 (RFmqtt)
    Tasks: 3 (limit: 2063)
   CGroup: /system.slice/RFmqtt.service
           └─563 /home/pi/433Utils/RPi_utils/RFmqtt -u user -x password

Jan 15 15:58:12 machine systemd[1]: Started 433MHz Receiver on RPI GPIO sending to MQTT Broker.

Been up 3 days with no interruption. just did an RF test and it worked fine 5 minutes ago.

irosaurus commented 3 years ago

Awesome, thanks @y8s ! Just applied your changes and the service is up and running. Will report in a couple of days.

● rfmqtt.service - RFmqtt Sniffer
   Loaded: loaded (/etc/systemd/system/rfmqtt.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-01-23 18:08:26 CET; 1s ago
 Main PID: 19625 (RFmqtt)
    Tasks: 3 (limit: 4915)
   CGroup: /system.slice/rfmqtt.service
           └─19625 /srv/scripts/RFmqtt

Jan 23 18:08:26 openHAB systemd[1]: Started RFmqtt Sniffer.

Cheers iro

irosaurus commented 3 years ago

After more than one month it's still working nicely :) I would say that fixes the issue!

root@openHAB:~# systemctl status rfmqtt.service
● rfmqtt.service - RFmqtt Sniffer
   Loaded: loaded (/etc/systemd/system/rfmqtt.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-01-23 18:08:26 CET; 4 weeks 2 days ago
 Main PID: 19625 (RFmqtt)
    Tasks: 3 (limit: 4915)
   CGroup: /system.slice/rfmqtt.service
           └─19625 /srv/scripts/RFmqtt

Thanks for sharing the solution @y8s !

Cheers iro

ramybt commented 3 years ago

@irosaurus can you post your full code and steps that you done to make it work? i stil lcant get it to receive signal when i run ./RFmqtt thanks in advanced for any help

ramybt commented 3 years ago

Guys im getting RFmqtt working now but NULL on OH. i checked connections and all good but not sure why im getting NULL. anyone know ?!