update repo

This commit is contained in:
Sthope 2024-04-01 12:30:54 +02:00
parent a01c5d325c
commit dd9ddb06c1

182
testing/app.py Normal file
View File

@ -0,0 +1,182 @@
import serial
import time
import paho.mqtt.client as mqtt
# Define the serial port
SERIAL_PORT = '/dev/ttyUSB0'
# Define the baud rate
BAUD_RATE = 19200
# Define commands
SMARTTV_HELP = b'?\r'
SMARTTV_OFF = b'off\r'
SMARTTV_ON = b'on\r'
SMARTTV_MUTE_ON = b'set mute=on\r'
SMARTTV_MUTE_OFF = b'set mute=off\r'
SMARTTV_VOLUME_100 = b'set volume=100\r'
SMARTTV_VOLUME_90 = b'set volume=90\r'
SMARTTV_VOLUME_80 = b'set volume=80\r'
SMARTTV_VOLUME_70 = b'set volume=70\r'
SMARTTV_VOLUME_60 = b'set volume=60\r'
SMARTTV_VOLUME_50 = b'set volume=50\r'
SMARTTV_VOLUME_40 = b'set volume=40\r'
SMARTTV_VOLUME_30 = b'set volume=30\r'
SMARTTV_VOLUME_20 = b'set volume=20\r'
SMARTTV_VOLUME_10 = b'set volume=10\r'
SMARTTV_VOLUME_05 = b'set volume=05\r'
SMARTTV_VOLUME_04 = b'set volume=04\r'
SMARTTV_VOLUME_03 = b'set volume=03\r'
SMARTTV_VOLUME_02 = b'set volume=02\r'
SMARTTV_VOLUME_01 = b'set volume=01\r'
SMARTTV_VOLUME_00 = b'set volume=00\r'
# MQTT settings
MQTT_BROKER = "mqtt_broker_ip"
MQTT_PORT = 1883
MQTT_TOPIC = "smarttv/control"
MQTT_RESPONSE_TOPIC = "smarttv/response"
MQTT_USERNAME = "mqtt_username"
MQTT_PASSWORD = "mqtt_password"
ALIVE_MESSAGE = "Online"
# Function to send command to TV
def send_command(command):
with serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) as ser:
ser.write(command)
time.sleep(0.1)
reply = ser.read(100)
return reply.decode('utf-8')
# MQTT on_message callback
def on_message(client, userdata, msg):
command = msg.payload.decode("utf-8")
if command == "SMARTTV_OFF":
response = send_command(SMARTTV_OFF)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_ON":
response = send_command(SMARTTV_ON)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_100":
response = send_command(SMARTTV_VOLUME_100)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_90":
response = send_command(SMARTTV_VOLUME_90)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_80":
response = send_command(SMARTTV_VOLUME_80)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_70":
response = send_command(SMARTTV_VOLUME_70)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_60":
response = send_command(SMARTTV_VOLUME_60)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_50":
response = send_command(SMARTTV_VOLUME_50)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_40":
response = send_command(SMARTTV_VOLUME_40)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_30":
response = send_command(SMARTTV_VOLUME_30)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_20":
response = send_command(SMARTTV_VOLUME_20)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_10":
response = send_command(SMARTTV_VOLUME_10)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_05":
response = send_command(SMARTTV_VOLUME_05)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_04":
response = send_command(SMARTTV_VOLUME_04)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_03":
response = send_command(SMARTTV_VOLUME_03)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_02":
response = send_command(SMARTTV_VOLUME_02)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_01":
response = send_command(SMARTTV_VOLUME_01)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_VOLUME_00":
response = send_command(SMARTTV_VOLUME_00)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
# if command == "SMARTTV_":
# response = send_command(SMARTTV_)
# print(response)
# client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_MUTE_ON":
response = send_command(SMARTTV_MUTE_ON)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
if command == "SMARTTV_MUTE_OFF":
response = send_command(SMARTTV_MUTE_OFF)
print(response)
client.publish(MQTT_RESPONSE_TOPIC, response)
# MQTT on_connect callback
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT broker")
client.publish(MQTT_RESPONSE_TOPIC, ALIVE_MESSAGE)
# Create MQTT client
client = mqtt.Client()
client.on_message = on_message
client.on_connect = on_connect
# Set username and password for MQTT broker
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
# Connect to MQTT broker
client.connect(MQTT_BROKER, MQTT_PORT, 60)
# Subscribe to MQTT topic
client.subscribe(MQTT_TOPIC)
# Start MQTT loop
client.loop_forever()