Calling functions in uMQTT



  • So I tried sending some data to Adafruit IO. I followed the guide created by Chris from core electronics located here. All his code works perfect, I can turn on and off my LED and the random data is sent fine. However I tried to add some of my own functions and it seems that they are not called.

    I am used to the python way of creating and calling a function:

    def some_random_function
        print("Apples!")
    
    some_random_function()
    

    However the umqtt doesn't feature anything like that so it looks like chris' functions get called automatically.

    So I attempted to create my function following what he did, however my function doesn't even get called as I dont get my first print statement.

    def send_bme():
        print("Wipy trying to send: ", bme280.values)
        try:
            client.publish(topic=AIO_BME_FEED, msg=str(bme280.values))
            print("Success!")
            time.sleep(5)
        except Exception as e:
            print("Failed!")
            time.sleep(5)
    


  • @dezert754 We've all had those moments :P glad its all sorted.



  • @paul-thornton Thanks very much! So obvious, I dont know how I missed that, haha.



  • I took a quick glance at the code you linked.

    Looks like he calls his send_random function from the main loop each time.

    You'll need to do the same to have yours called

    while 1:              # Repeat this loop forever
            client.check_msg()# Action a message if one is received. Non-blocking.
            send_random() #here. 
            some_random_function() #add a call to your function here
            
    


Pycom on Twitter