AttributeError: 'NoneType' object has no attribute 'recv'

329 Views Asked by At

I'm working on a Streamlit app with a real-time wordcloud dashboard. The data that will be used is from the Audio Classification app, whereas the prediction data is published in MQTTX. Now that the Audio Classification app and the MQTTX are already connected and the prediction data is already published in that client, I am having trouble getting the data from the MQTT.

Here's the code that I created for the streamlit dashboard:

import streamlit as st
import wordcloud
import matplotlib.pyplot as plt
import paho.mqtt.client as mqtt
import json

broker = 'broker.emqx.io'
port = 1883
topic = "audio_test"
client_id = 'audio_test'
username = 'emqx'
password = 'public'


def connect_mqtt() -> mqtt:
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
        else:
            print("Failed to connect, return code %d\n", rc)
    # Set Connecting Client ID
    client = mqtt.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker, port)
    client.loop_start()
    return client

def get_data_from_mqtt(client: mqtt):

    # Callback when a message is published to the specified topic.
    def on_message(client, userdata, message):
        topic = message.topic
        payload = json.loads(message.payload.decode('utf-8'))
        time_stamp = payload['Timestamp']
        category_name = payload['Classification']
        score = payload['Accuracy']
        st.write(f"Topic: {topic}\nTimestamp: {time_stamp}\nClassification: {category_name}\nAccuracy: {score}%")
        print(f"Received message from topic '{topic}'")

    client.subscribe(topic)
    client.on_message = on_message

def create_wordcloud(text):
    wc = wordcloud.WordCloud(width=800, height=200, max_words=200, background_color='white').generate(text)
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    plt.tight_layout(pad=0)
    st.pyplot()

def main():
    st.set_page_config(
        page_title="Real-Time Audio Classification Dashboard",
        page_icon="",
        layout="wide",
    )
    st.markdown("<h1 style='text-align: center; color: black;'> Real-Time Audio Classification Dashboard</h1>", unsafe_allow_html=True)

    client = connect_mqtt()
    get_data_from_mqtt(client)
    client.loop_forever()

    data = get_data_from_mqtt(client)
    text = ' '.join([d['Classification'] for d in data])
    if len(data) == 0 or text == '':
        st.write("No data to display wordcloud")
    else:
        create_wordcloud(text)

    create_wordcloud(text)

if __name__ == '__main__':
    main()

Also, it doesn't print any text if the message has been received so I am assuming there's a problem with the connection between this dashboard and MQTTX

This is the error that I've encountered:

AttributeError: 'NoneType' object has no attribute 'recv'
Traceback:
File "C:\Users\mayew\Desktop\Audio Classification\venv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
File "C:\Users\mayew\Desktop\Audio Classification\Audio-Classification\testing.py", line 73, in <module>
    main()
File "C:\Users\mayew\Desktop\Audio Classification\Audio-Classification\testing.py", line 61, in main
    client.loop_forever()
File "C:\Users\mayew\Desktop\Audio Classification\venv\lib\site-packages\paho\mqtt\client.py", line 1756, in loop_forever
    rc = self._loop(timeout)
File "C:\Users\mayew\Desktop\Audio Classification\venv\lib\site-packages\paho\mqtt\client.py", line 1164, in _loop
    rc = self.loop_read()
File "C:\Users\mayew\Desktop\Audio Classification\venv\lib\site-packages\paho\mqtt\client.py", line 1556, in loop_read
    rc = self._packet_read()
File "C:\Users\mayew\Desktop\Audio Classification\venv\lib\site-packages\paho\mqtt\client.py", line 2389, in _packet_read
    byte = self._sock_recv(1)
File "C:\Users\mayew\Desktop\Audio Classification\venv\lib\site-packages\paho\mqtt\client.py", line 640, in _sock_recv
    return self._sock.recv(bufsize)

0

There are 0 best solutions below