0

I am having my main program subscribe to a topic that will receive messages every 15 seconds. I have my connection being made, and then foreverloop running. For additional information, I am currently using HiveMq Cloud as my broker, and paho-mqtt for my main system that is getting the error.

client = mqtt.Client(client_id='main', userdata=None,protocol=mqtt.MQTTv5)
client.on_connect = on_connect
client.tls_set(tls_version=ssl.PROTOCOL_TLS)
client.username_pw_set(userName,PassWord)
client.connect(broker,port,keepalive=60)
client.on_subscribe = on_subscribe
client.on_publish = on_publish
client.message_callback_add("Call/#",on_message_from_sensor)
client.loop_forever()

The program will run anywhere from a few hours, to almost a day. But recently I started receiving this error multiple times.

Traceback (most recent call last):
  File "/Users/austin/Desktop/Weather Project/subscribe.py", line 72, in <module>
    client.loop_forever()
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
         ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 1164, in _loop
    rc = self.loop_read()
         ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 1556, in loop_read
    rc = self._packet_read()
         ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
    rc = self._packet_handle()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 3033, in _packet_handle
    return self._handle_publish()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 3331, in _handle_publish
    return self._send_puback(message.mid)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 2601, in _send_puback
    return self._send_command_with_mid(PUBACK, mid, False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 2710, in _send_command_with_mid
    return self._packet_queue(command, packet, mid, 1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 3016, in _packet_queue
    return self.loop_write()
           ^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 1577, in loop_write
    rc = self._packet_write()
         ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 2464, in _packet_write
    write_length = self._sock_send(
                   ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/paho/mqtt/client.py", line 649, in _sock_send
    return self._sock.send(buf)
           ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1210, in send
    return self._sslobj.write(data)
           ^^^^^^^^^^^^^^^^^^^^^^^^
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:2423)

I am expecting this program to run continuously. However, due to this issue, it is randomly stopping and crashing. I have looked up this specific error, but I have not found anything about it. I am new to using mqtt & ssl, so I am not sure where to go from here.

I thought of just adding a try/catch, but I don’t know the underlying issue, so I don't want to just restart if it is something critical.

What should I do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

-4

The error message you are encountering indicates that an EOF (End-of-File) occurred in violation of the protocol while using SSL/TLS for the MQTT connection. This error typically occurs when there is an issue with the SSL/TLS handshake between the client and the broker.

Here are a few suggestions to troubleshoot and resolve the issue:

Verify the MQTT broker's address and port: Double-check that the broker's address and port are correct and accessible. Ensure that you have the correct hostname/IP address and port number for the broker.

Check TLS settings: Make sure you have the correct TLS settings for the connection. Verify that you are using the appropriate TLS version supported by the broker. You may need to update the tls_version parameter to the appropriate value. You can refer to the documentation or support resources provided by for the recommended TLS settings.

Validate username and password: Ensure that the userName and PassWord variables contain the correct credentials for your MQTT broker. Double-check the username and password and ensure they are valid.

Check for network connectivity issues: Verify that your system has a stable internet connection and there are no network-related issues. Unstable network connectivity can cause disruptions in the MQTT connection and result in SSL/TLS errors.

To handle the SSL error you can wrap your code into a try catch block

import paho.mqtt.client as paho
from paho import mqtt
from paho.mqtt.client import ssl

broker="e3577d1b89bc4983b2384901ef8c83ed.s1.eu.hivemq.cloud"
port=8883
userName="SuperUser1"
passWord="SuperUser1"

# setting callbacks for different events to see if it works, print the message etc.
def on_connect(client, userdata, flags, rc, properties=None):
    print("CONNACK received with code %s." % rc)

# with this callback you can see if your publish was successful
def on_publish(client, userdata, mid, properties=None):
    print("mid: " + str(mid))

# print which topic was subscribed to
def on_subscribe(client, userdata, mid, granted_qos, properties=None):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

# print message, useful for checking if it was successful
def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))

def main():
    try:
        # using MQTT version 5 here, for 3.1.1: MQTTv311, 3.1: MQTTv31
        # userdata is user defined data of any type, updated by user_data_set()
        # client_id is the given name of the client
        client = paho.Client(client_id="", userdata=None, protocol=paho.MQTTv5)
        client.on_connect = on_connect

        # enable TLS for secure connection
        client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
        # set username and password
        client.username_pw_set(userName, passWord)
        # connect to HiveMQ Cloud on port 8883 (default for MQTT)
        client.connect(broker, port, keepalive=60)

        # setting callbacks, use separate functions like above for better visibility
        client.on_subscribe = on_subscribe
        client.on_message = on_message
        client.on_publish = on_publish

        # subscribe to all topics of encyclopedia by using the wildcard "#"
        client.subscribe("encyclopedia/#", qos=1)

        # a single publish, this can also be done in loops, etc.
        client.publish("encyclopedia/temperature", payload="hot", qos=1)

        # loop_forever for simplicity, here you need to stop the loop manually
        # you can also use loop_start and loop_stop
        client.loop_forever()

    except ssl.SSLEOFError as e:
        print("An SSL/TLS error occurred:", e)
        # Add your error handling logic here, such as reconnecting or exiting the program

if __name__ == '__main__':
    main()
guinpin
  • 1
  • 4
  • Thanks so much! I may look into my network connectivity as I am constantly receiving CONNACK received with code Success. Subscribed: 21 []. So there must be some disconnect happening somewhere. – Austin L Jul 13 '23 at 17:12
  • 3
    This answer looks like ChatGPT – DavidW Jul 13 '23 at 20:17
  • 1
    This answer looks like it was generated by an AI (like ChatGPT), not by an actual human being. You should be aware that [posting AI-generated output is officially **BANNED** on Stack Overflow](https://meta.stackoverflow.com/q/421831). If this answer was indeed generated by an AI, then I strongly suggest you delete it before you get yourself into even bigger trouble: **WE TAKE PLAGIARISM SERIOUSLY HERE.** Please read: [Why posting GPT and ChatGPT generated answers is not currently allowed](https://stackoverflow.com/help/gpt-policy). – tchrist Jul 15 '23 at 12:31
  • 2
    This is an AI generated response. I put my question into chatgpt and got an almost identical response. – Austin L Jul 16 '23 at 21:21