murphyslemon / Raspberry_Pi_Server

International Sensor Development Project: This project is a voting system that uses a raspberry pi server and multiple voting devices. The system is easy, secure, scalable, and supports anonymous and registered voting. This repository focuses on the Raspberry Pi server.
0 stars 3 forks source link

publishJSONtoMQTT function #2

Open MagnusLii opened 7 months ago

MagnusLii commented 7 months ago

MQTT publish function

Goal

The objective is to develop a robust function capable of accepting a JSON object, an integer representing the Quality of Service (QoS) level, and a topic name. The function tries to publish the JSON message as to the specified topic. The function should not modify the input arguments in any way.

Arguments

MagnusLii commented 7 months ago
def publishJSONtoMQTT(topic, message):
    try:
        mqtt.publish(topic, message, qos=mqttQoSLevel)
    except:
        print(f'Failed to publish to topic: {topic}')
        return False
    else:
        return True