mqtt message directed to switc-case issue
-
It appears that the message from a mqtt subscription are bytes as when i print them, i get b'PON' for example. I have a switcher statement to execute a function based on the message but it appears to have issues with it being in bytes and i cannot decode it.
def sub_cb(topic, msg):
print("mqtt::",topic,"msg[",msg,"]")
switch_demo(msg.decode('utf-8')) #doesn't recognise decodedef PON():
p_out.value(True)
return "ON"def POFF():
p_out.value(False)
return "OFF"def switch_demo(argument):
print("switch_demo::argument[",argument,"]")
switcher = {
'PON': PON,
'POFF': POFF,
}
# Get the function from switcher dictionary
func = switcher.get(argument, lambda: "Invalid month")
# Execute the function
print(func()) -
@misterlisty
are you sure that your problem is decode? - what is the error message?
For medecodework without problem>>> a = b'PON' >>> print(a) b'PON' >>> b = a.decode("utf-8") >>> print(b) PON >>>please look at formating rules of the forum here
http://commonmark.org/help/e.g. surround your code with triple ``` to make python code format
-
its working now, not sure why, maybe the file were out of sync
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login