Last active
October 15, 2022 00:40
-
-
Save geekscape/b227193c761c674db6c46d23f2428b78 to your computer and use it in GitHub Desktop.
Python MQTT example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# https://tinyurl.com/gs-py-mqtt | |
# https://gist.github.com/geekscape/b227193c761c674db6c46d23f2428b78 | |
# | |
# Eclipse Paho project | |
# - https://www.eclipse.org/paho | |
# Python MQTT client module | |
# - https://pypi.org/project/paho-mqtt | |
# pip install paho-mqtt | |
# | |
# MosQuiTTo open-source MQTT command line tools and server | |
# - https://mosquitto.org | |
# | |
# MQTT Transport standard / specification | |
# - https://mqtt.org | |
# | |
# mosquitto_sub -h 192.168.0.23 -t '#' -v | |
# mosquitto_pub -h 192.168.0.23 -t test -m message | |
import paho.mqtt.client as mqtt | |
import time | |
# MQTT_HOST = "localhost" | |
MQTT_HOST = "192.168.0.23" | |
# MQTT_HOST = "test.mosquitto.org" | |
MQTT_PORT = 1883 | |
MQTT_TOPIC = "test" | |
def on_connect(mqtt_client, user_data, dict_flags, result_code): | |
print("on_connect(): connected") | |
mqtt_client.subscribe(MQTT_TOPIC) | |
def on_message(mqtt_client, userdata, message): | |
print(f"on_message(): {message.topic}: {message.payload}") | |
mqtt_client = mqtt.Client() | |
mqtt_client.on_connect = on_connect | |
mqtt_client.on_message = on_message | |
mqtt_client.will_set(MQTT_TOPIC, payload="goodbye", retain=False) | |
mqtt_client.connect(host=MQTT_HOST, port=MQTT_PORT, keepalive=60) | |
mqtt_client.loop_start() # Performs background MQTT requirements | |
for i in range(5): | |
mqtt_client.publish(MQTT_TOPIC, "hello", retain=False) | |
time.sleep(1.0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# https://tinyurl.com/gs-py-mqtt | |
# https://gist.github.com/geekscape/b227193c761c674db6c46d23f2428b78 | |
import paho.mqtt.client as mqtt | |
import queue | |
import time | |
# MQTT_HOST = "localhost" | |
MQTT_HOST = "192.168.0.23" | |
# MQTT_HOST = "test.mosquitto.org" | |
MQTT_PORT = 1883 | |
MQTT_TOPIC = "bombe" | |
NAME = "Astra" | |
message_queue = queue.Queue() | |
def on_connect(mqtt_client, user_data, dict_flags, result_code): | |
print("on_connect(): connected") | |
mqtt_client.subscribe(MQTT_TOPIC) | |
def on_message(mqtt_client, userdata, message): | |
print(f"on_message(): {message.topic}: {message.payload}") | |
payload = message.payload.decode("utf-8").split() | |
message_queue.put(payload) | |
mqtt_client = mqtt.Client() | |
mqtt_client.on_connect = on_connect | |
mqtt_client.on_message = on_message | |
mqtt_client.will_set(MQTT_TOPIC, payload=f"leave {NAME}", retain=False) | |
mqtt_client.connect(host=MQTT_HOST, port=MQTT_PORT, keepalive=60) | |
mqtt_client.loop_start() # Performs background MQTT requirements | |
while True: | |
if message_queue.qsize(): | |
message = message_queue.get() | |
print(f"main_loop: {message}") | |
time.sleep(0.1) | |
# mqtt_client.publish(MQTT_TOPIC, "hello", retain=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# https://tinyurl.com/gs-py-mqtt | |
# https://gist.github.com/geekscape/b227193c761c674db6c46d23f2428b78 | |
import paho.mqtt.client as mqtt | |
import queue | |
import random | |
import time | |
PLAYER = "Astra" | |
TOPIC_PREFIX = "bombe" | |
# MQTT_HOST = "localhost" | |
MQTT_HOST = "192.168.0.23" | |
# MQTT_HOST = "test.mosquitto.org" | |
MQTT_PORT = 1883 | |
MQTT_TOPIC_PUBLISH = f"{TOPIC_PREFIX}/{PLAYER}" | |
MQTT_TOPIC_SUBSCRIBE = f"{TOPIC_PREFIX}/#" | |
game_over = False | |
haz_bombe = False | |
haz_bombe_warning = False | |
message_queue = queue.Queue() | |
players = [] | |
time_to_pass_bombe = None | |
def on_connect(mqtt_client, user_data, dict_flags, result_code): | |
# print("on_connect(): connected") | |
mqtt_client.subscribe(MQTT_TOPIC_SUBSCRIBE) | |
def on_message(mqtt_client, userdata, message): | |
# print(f"on_message(): {message.topic}: {message.payload}") | |
payload = message.payload.decode("utf-8").split() | |
message_queue.put(payload) | |
def exclude(group, exclusions): | |
new_group = group.copy() | |
for exclusion in exclusions: | |
if exclusion in new_group: | |
new_group.remove(exclusion) | |
return new_group | |
mqtt_client = mqtt.Client() | |
mqtt_client.on_connect = on_connect | |
mqtt_client.on_message = on_message | |
mqtt_client.will_set(MQTT_TOPIC_PUBLISH, payload=f"leave {PLAYER}", retain=True) | |
mqtt_client.connect(host=MQTT_HOST, port=MQTT_PORT, keepalive=60) | |
mqtt_client.loop_start() # Performs background MQTT requirements | |
mqtt_client.publish(MQTT_TOPIC_PUBLISH, f"join {PLAYER}", retain=True) | |
while not game_over: | |
# Catch Exception | |
if message_queue.qsize() > 0: | |
message = message_queue.get() | |
# print(f"main_loop: {message}") | |
if len(message) > 0: | |
command = message[0] | |
if command == "boom": | |
print(" ".join(message)) | |
mqtt_client.publish( | |
MQTT_TOPIC_PUBLISH, f"leave {PLAYER}", retain=True) | |
game_over = True | |
if len(message) > 1: | |
player = message[1] | |
if command == "join": | |
if not player in players: | |
players.append(player) | |
print(f"players: {players}") | |
if command == "leave": | |
if player in players: | |
players.remove(player) | |
print(f"players: {players}") | |
if command == "pass" and player == PLAYER: | |
haz_bombe = True | |
haz_bombe_warning = False | |
bombe_hold_time = random.randint(1, 4) # seconds | |
time_to_pass_bombe = time.time() + bombe_hold_time | |
print(f"I haz bombe for {bombe_hold_time} seconds!") | |
time.sleep(0.1) | |
if haz_bombe and time.time() >= time_to_pass_bombe: | |
victim_list = exclude(players, ["bombe", PLAYER]) | |
if len(victim_list) > 0: | |
victim_index = random.randint(0, len(victim_list) - 1) | |
victim = victim_list[victim_index] | |
mqtt_client.publish( | |
MQTT_TOPIC_PUBLISH, f"pass {victim}", retain=True) | |
print(f"Bomb passed to {victim}") | |
haz_bombe = False | |
else: | |
if not haz_bombe_warning: | |
print("Oh no ... no other players to pass on the bombe :(") | |
haz_bombe_warning = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# https://tinyurl.com/gs-py-mqtt | |
# https://gist.github.com/geekscape/b227193c761c674db6c46d23f2428b78 | |
import paho.mqtt.client as mqtt | |
import queue | |
import random | |
import time | |
PLAYER = "bombe" | |
TOPIC_PREFIX = "bombe" | |
# MQTT_HOST = "localhost" | |
MQTT_HOST = "192.168.0.23" | |
# MQTT_HOST = "test.mosquitto.org" | |
MQTT_PORT = 1883 | |
MQTT_TOPIC_PUBLISH = f"{TOPIC_PREFIX}/{PLAYER}" | |
MQTT_TOPIC_SUBSCRIBE = f"{TOPIC_PREFIX}/#" | |
bombe_armed = False | |
game_over = False | |
message_queue = queue.Queue() | |
players = [] | |
time_to_explode = None | |
def on_connect(mqtt_client, user_data, dict_flags, result_code): | |
# print("on_connect(): connected") | |
mqtt_client.subscribe(MQTT_TOPIC_SUBSCRIBE) | |
def on_message(mqtt_client, userdata, message): | |
# print(f"on_message(): {message.topic}: {message.payload}") | |
payload = message.payload.decode("utf-8").split() | |
message_queue.put(payload) | |
def exclude(group, exclusions): | |
new_group = group.copy() | |
for exclusion in exclusions: | |
if exclusion in new_group: | |
new_group.remove(exclusion) | |
return new_group | |
mqtt_client = mqtt.Client() | |
mqtt_client.on_connect = on_connect | |
mqtt_client.on_message = on_message | |
mqtt_client.will_set(MQTT_TOPIC_PUBLISH, payload=f"leave {PLAYER}", retain=True) | |
mqtt_client.connect(host=MQTT_HOST, port=MQTT_PORT, keepalive=60) | |
mqtt_client.loop_start() # Performs background MQTT requirements | |
mqtt_client.publish(MQTT_TOPIC_PUBLISH, f"join {PLAYER}", retain=True) | |
while not game_over: | |
# Catch Exception | |
if message_queue.qsize() > 0: | |
message = message_queue.get() | |
# print(f"main_loop: {message}") | |
if len(message) > 0: | |
command = message[0] | |
if len(message) > 1: | |
player = message[1] | |
if command == "join": | |
if not player in players: | |
players.append(player) | |
print(f"players: {players}") | |
if not bombe_armed and len(players) > 2: | |
bombe_armed = True | |
bombe_timer = random.randint(1, 15) # seconds | |
time_to_explode = time.time() + bombe_timer | |
print(f"Bombe explodes in {bombe_timer} seconds!") | |
victim_list = exclude(players, [PLAYER]) | |
if len(victim_list) > 0: | |
victim_index = random.randint(0, len(victim_list) - 1) | |
victim = victim_list[victim_index] | |
mqtt_client.publish( | |
MQTT_TOPIC_PUBLISH, f"pass {victim}", retain=True) | |
print(f"Bomb passed to {victim}") | |
if command == "leave": | |
if player in players: | |
players.remove(player) | |
print(f"players: {players}") | |
time.sleep(0.1) | |
if bombe_armed and time.time() >= time_to_explode: | |
print(f"BOOM !!!") | |
mqtt_client.publish(MQTT_TOPIC_PUBLISH, f"boom", retain=False) | |
mqtt_client.publish(MQTT_TOPIC_PUBLISH, f"leave {PLAYER}", retain=True) | |
game_over = True | |
time.sleep(1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment