Skip to content

Instantly share code, notes, and snippets.

@Abathargh
Created August 5, 2019 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abathargh/6c7ebcddc03cd3982c34bdff3317d8cf to your computer and use it in GitHub Desktop.
Save Abathargh/6c7ebcddc03cd3982c34bdff3317d8cf to your computer and use it in GitHub Desktop.
from threading import Lock
from paho.mqtt.client import Client
CLIENTID = "Mediator"
HOST = "localhost"
BASETOPIC = "actuator"
SUBTOPIC = "sensor/#"
table = dict()
mutex = Lock()
def to_color(data):
if data < 10:
return 0
elif data >= 10 and data < 15:
return 1
elif data >= 15 and data < 20:
return 2
elif data >= 20 and data < 25:
return 3
elif data >= 25 and data < 30:
return 4
return 5
def on_message(client, userdata, msg):
room = msg.topic.split("/")[1]
value = str(msg.payload.decode())
print("Received {} from room {}".format(value, room))
with mutex:
if not(room in table) or (room in table and not (value == table[room])):
table[room] = value
client.publish("{}/{}".format(BASETOPIC, room), to_color(int(value)))
def main():
client = Client(CLIENTID)
client.connect(HOST)
client.on_message = on_message
client.subscribe(SUBTOPIC)
client.loop_forever()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment