Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Sharing data using minimqtt on_meessage callback
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Sharing data using minimqtt on_meessage callback

by tellingmachine on Tue Aug 04, 2020 12:02 pm

Hi,

I am having trouble with sharing data between minimqtt on_message callback with the main loop. I am using a Metro M4 Airlift with CircuitPython 6.0.2 (the drop from 8/3/2020).

Here is my on_message function call back definition:

Code: Select all | TOGGLE FULL SIZE
def message(client, topic, message):
    # This method is called when a topic the client is subscribed to
    print("Converting from Json to Object")
    testrig_received_value_dict=json.loads(message) #decode json data
    print(type(testrig_received_value_dict))
    print("fanload = ",testrig_received_value_dict["fanload"])
    print("New message on topic {0}: {1}".format(topic, message))
    fanload = testrig_received_value_dict["fanload"]
    print(fanload)
    fanpwm.duty_cycle = fanload


fanpwd is defined previously as:

Code: Select all | TOGGLE FULL SIZE
fanpwm = pulseio.PWMOut(board.D12, duty_cycle=0x7fff, frequency=25000)


My challenge is that whatever I try I can't get data exchanged between the callback function and the rest of the program in code.py.
I tried using a dictionary that is declared in the body of code.py. This did not work. I read that it should be defined in the main function. Where can I find the main function in CircuitPython. Can you please provide an example or point me to some documentation that can explain what I need to do to pass data between the on_message callback the io of my board?
All of the examples that I found on the internet do print(message), but It would be helpful to provide examples that take the incoming message and use it to set e.g. the duty_cycle of a pwm output.

Thanks for your help.

Btw CircuitPython is pretty awesome.

tellingmachine
 
Posts: 5
Joined: Fri Apr 15, 2011 7:20 pm

Re: Sharing data using minimqtt on_meessage callback

by tellingmachine on Tue Aug 04, 2020 12:23 pm

I did some more reading. It seems like not the Cortex M4 but the ESP32 co-processor is handing the incoming mqtt messages. The issue might not just be a CircuityPython syntax question, but looking at the data transfer between the ESP32 and the Cortex M4.

tellingmachine
 
Posts: 5
Joined: Fri Apr 15, 2011 7:20 pm

Re: Sharing data using minimqtt on_meessage callback

by brubell on Wed Aug 05, 2020 11:23 am

This did not work. I read that it should be defined in the main function. Where can I find the main function in CircuitPython. Can you please provide an example or point me to some documentation that can explain what I need to do to pass data between the on_message callback the io of my board?


Using callbacks is tricky. Without knowing what the rest of your code.py looks like, I'll explain the process in general terms:

After the MiniMQTT client has been defined, prior to establish a connection, you need to provide MiniMQTT's on_message method handler with the name of the callback method.
Reference: https://github.com/adafruit/Adafruit_Ci ... ing.py#L94

Within the "main loop" (while True), each call of mqtt_client.loop() will check for incoming packets. If the packet is a publish packet, the method you attached to on_message will be executed.


My challenge is that whatever I try I can't get data exchanged between the callback function and the rest of the program in code.py.

You may need to make the fanpwm variable a global variable.

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.
- https://docs.python.org/3/faq/programmi ... -in-python

brubell
 
Posts: 1079
Joined: Fri Jul 17, 2015 10:33 pm

Re: Sharing data using minimqtt on_meessage callback

by tellingmachine on Thu Aug 06, 2020 5:36 pm

Thanks for your feedback. I got it working by declaring the dictionary variable with global in the on_message callback function.

Code: Select all | TOGGLE FULL SIZE
    global testrig_received_value_dict


Here is the updated function that passes the received value back to the main loop.
Code: Select all | TOGGLE FULL SIZE

def message(client, topic, message):
    # This method is called when a topic the client is subscribed to
    print("Converting from Json to Object")
    global testrig_received_value_dict
    testrig_received_value_dict=json.loads(message) #decode json data
    print(type(testrig_received_value_dict))
    print("fanload = ",testrig_received_value_dict["fanload"])
    print("New message on topic {0}: {1}".format(topic, message))
    fanload = testrig_received_value_dict["fanload"]
    print(fanload)
    fanpwm.duty_cycle = fanload

tellingmachine
 
Posts: 5
Joined: Fri Apr 15, 2011 7:20 pm

Please be positive and constructive with your questions and comments.