0% found this document useful (0 votes)
7 views3 pages

Impot Data Koding Pemograman Python

This document is a Python script that utilizes the Paho MQTT client and Sense HAT to connect to an MQTT broker and display messages on the Sense HAT based on incoming MQTT messages. It subscribes to specific topics for messages, lamp control, and animations, and publishes sensor data (temperature, pressure, humidity) every 5 seconds. Additionally, it handles joystick events to display a right arrow on the Sense HAT when the joystick is pressed to the right.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Impot Data Koding Pemograman Python

This document is a Python script that utilizes the Paho MQTT client and Sense HAT to connect to an MQTT broker and display messages on the Sense HAT based on incoming MQTT messages. It subscribes to specific topics for messages, lamp control, and animations, and publishes sensor data (temperature, pressure, humidity) every 5 seconds. Additionally, it handles joystick events to display a right arrow on the Sense HAT when the joystick is pressed to the right.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import paho.mqtt.

client as mqtt
from sense_emu import SenseHat
import time

sense = SenseHat()
sense.clear()

# Konfigurasi MQTT
broker = "160.187.144.142"
port = 1883
topic_sub = "pcr/haqi14/#"

client = mqtt.Client()

def on_connect(client, userdata, flags, rc):


print("Terhubung ke broker dengan kode:", rc)
client.subscribe(topic_sub)

def on_message(client, userdata, msg):


topik = msg.topic
isi = msg.payload.decode("utf-8")
print(f"Topik Masuk: {topik}, Pesan: {isi}")

if topik.endswith("/pesan"):
sense.show_message(isi, scroll_speed=0.08, text_colour=[0, 255, 0])
elif topik.endswith("/pilih"):
if isi == "1":
sense.show_message("Halo!", scroll_speed=0.08, text_colour=[255, 0, 0])
elif isi == "2":
sense.show_message("Selamat!", scroll_speed=0.08, text_colour=[0, 0,
255])
else:
sense.show_message("Nomor?", scroll_speed=0.08, text_colour=[255, 255,
0])
elif "/lampu/" in topik:
try:
posisi = int(topik.split("/")[-1])
warna = isi.lower()
warna_rgb = {
"merah": (255, 0, 0),
"hijau": (0, 255, 0),
"biru": (0, 0, 255),
"kuning": (255, 255, 0),
"putih": (255, 255, 255),
"hitam": (0, 0, 0),
"limau": (100, 255, 100)
}.get(warna, (255, 255, 255))
pixels = [(0, 0, 0)] * 64
pixels[posisi] = warna_rgb
sense.set_pixels(pixels)
except:
print("Format lampu salah")
elif topik.endswith("/lampu"):
if isi == "senyum":
gambar = [(0,0,0)]*64
smile = [27, 28, 35, 36, 17, 22, 41, 46]
for i in smile:
gambar[i] = (255, 255, 0)
sense.set_pixels(gambar)
elif isi == "iloveu":
sense.show_message("I ❤ U", scroll_speed=0.08, text_colour=[255, 0,
255])
elif isi == "raspi":
sense.show_message("Raspberry Pi", scroll_speed=0.08, text_colour=[255,
0, 0])
elif topik.endswith("/animasi"):
if isi == "1":
for _ in range(2):
sense.show_message(":)", scroll_speed=0.08, text_colour=[0, 255,
0])
sense.show_message(":(", scroll_speed=0.08, text_colour=[255, 0,
0])
elif isi == "2":
for _ in range(2):
sense.clear((255, 0, 255))
sense.clear((0, 0, 255))
elif isi == "3":
for i in range(3):
for x in range(8):
for y in range(8):
sense.set_pixel(x, y, (255, 255, 0))
sense.clear()

# Fungsi untuk tampilkan panah ke kanan


def tampilkan_panah_kanan():
panah_kanan = [
(0,0,0),(0,0,0),(0,0,255),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),
(0,0,0),(0,0,0),(0,0,255),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),
(0,0,0),(0,0,0),(0,0,255),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),
(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),
(0,0,255),(0,0,0),(0,0,0),
(0,0,0),(0,0,0),(0,0,255),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),
(0,0,0),(0,0,0),(0,0,255),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),
(0,0,0),(0,0,0),(0,0,255),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),
(0,0,0),(0,0,0),(0,0,255),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),
]
sense.set_pixels(panah_kanan)

# Mulai koneksi MQTT


client.on_connect = on_connect
client.on_message = on_message
client.connect(broker, port, 60)
client.loop_start()

# Loop untuk publish data sensor setiap 5 detik


try:
while True:
suhu = round(sense.get_temperature(), 2)
tekanan = round(sense.get_pressure(), 2)
kelembapan = round(sense.get_humidity(), 2)

client.publish("pcr/haqi14/sensor/suhu", suhu)
client.publish("pcr/haqi14/sensor/tekanan", tekanan)
client.publish("pcr/haqi14/sensor/lembab", kelembapan)

print(f"Data terkirim → Suhu: {suhu}, Tekanan: {tekanan}, Lembab:


{kelembapan}")
# Cek joystick, tampilkan panah jika klik kanan
for event in sense.stick.get_events():
if event.action == 'pressed' and event.direction == 'right':
tampilkan_panah_kanan()

time.sleep(5)

except KeyboardInterrupt:
print("Program dihentikan.")
sense.clear()
client.loop_stop()
client.disconnect()

You might also like