The error I am receiving is the comparison is not working.
def switch_on_off(topic, message):
print("Received message on topic: {}, message: {}".format(topic, message))
message_str = message.decode() # Convert bytes to string
if message_str == 'on':
print("Turning on the relay")
relay_pin.value(0) # Turn on the relay
elif message_str == 'off':
print("Turning off the relay")
relay_pin.value(1) # Turn off the relay
else:
print("Invalid message:", message_str)
def ai_posture(topic, message):
print("Received message on topic: {}, message: {}".format(topic, message))
message_str = message.decode() # Convert bytes to string
if message_str == 'Bad posture detected!':
print("buzzer beeping")
oled.text("Bad Posture Detected!", 0, 40)
oled.show()
buzzer_pin.value(1)
elif message_str == 'Good posture detected!':
print("buzzer off")
buzzer_pin.value(0)
else:
print("Invalid message:", message_str)
I am randomly receiving able to get the correct message and get the light to turn off and on. Even if the message subscribed is the same. I am receiving the following message:
Invalid message: on
on
Invalid message: on
off
on
off
on