I have a mqtt broker which I am connecting through python.
I want to check if I am able to connect to my broker and trigger a mail if my connection is successful.
I intend to use a global variable connected and if this is false after trying to connect I trigger an alarm.
My code:
import paho.mqtt.client as mqtt 
import time
broker_address="ip"
port = "port"
global connected
def mqttConnection():
    connected = False
    print(connected)
    client = mqtt.Client("BrokerCheck",clean_session=True) #create new instance
    client.on_connect = on_connect
    print('Connecting to broker')
    client.connect(broker_address, port=port) #connect to broker
def on_connect(client, userdata, flags, rc):
    if rc==0:
        print("connected OK Returned code=",rc)
        connected = True
    else:
        print("Bad connection Returned code=",rc)
if __name__ == '__main__':
    mqttConnection()
    time.sleep(60)
    if connected:
        pass
    else:
        #trigger an alarm
But I have a problem with my global variable connected. Am I using it in a right way?
 
     
     
    