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

minimqtt on_message callback data transfer
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

minimqtt on_message callback data transfer

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

I am having trouble with transfering data between the minimqtt on_message callback with code in code.py. I am using a Metro M4 Airlift with CircuitPython 6.0.2 (the drop from 8/3/2020).
I am guessing that 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 could be answered by understanding how data is being transferred from the ESP32 to the Cortex M4.

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.

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

Re: minimqtt on_message callback data transfer

by tellingmachine on Mon Aug 10, 2020 11:57 am

The following forum thread provides the solution for this problem: https://forums.adafruit.com/viewtopic.php?f=56&t=168065

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

Please be positive and constructive with your questions and comments.