Created
November 29, 2023 13:50
-
-
Save ajangrahmat/c138b934848446f922674b6ed140957e to your computer and use it in GitHub Desktop.
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
import paho.mqtt.client as mqtt | |
import colorama as color | |
import random | |
import string | |
import time | |
topic_terima = "kbkm/2023/533534343" | |
topic_kirim = "kbkm/2023/6876766" | |
def ketika_terhubung(client,userdata,flags,rc): | |
print("Berhasil Terhubung") | |
client.subscribe(topic_terima) | |
def ketika_terima(client,userdata,msg): | |
print(color.Fore.GREEN + msg.payload.decode() + color.Fore.RESET) | |
#buat client id random string dan number | |
client_id = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(10)) | |
print(client_id) | |
client = mqtt.Client(client_id=client_id) | |
client.on_connect = ketika_terhubung | |
client.on_message = ketika_terima | |
client.connect("broker.emqx.io",1883,60) | |
client.loop_start() | |
while True: | |
pesan = input() | |
client.publish(topic_kirim, pesan) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment