Z uwagi na to, ze jestem zielony w temacie i posiłkowałem się do tej pory czatem gpt, proszę o wyrozumiałość
Code: Select all
import paho.mqtt.client as mqtt
import requests
# --- KONFIGURACJA MQTT ---
MQTT_BROKER = "xxx"
MQTT_PORT = 1883
MQTT_TOPIC = "solar_assistant/battery/soc"
# --- KONFIGURACJA SUPLA ---
SUPLA_URL = "https://svrXX.supla.org" # np. https://svr14.supla.org
SUPLA_CHANNEL_ID = 12345 # ID czujnika w SUPLI
SUPLA_AUTH_TOKEN = "twoj_token_z_supli" # Token z Cloud -> API
# Funkcja wysyłająca dane do SUPLI
def send_to_supla(soc_value):
url = f"{SUPLA_URL}/api/v2.3/channels/{SUPLA_CHANNEL_ID}/executions"
headers = {
"Authorization": f"Bearer {SUPLA_AUTH_TOKEN}",
"Content-Type": "application/json"
}
data = {
"action": "SET_VALUE",
"value": float(soc_value)
}
response = requests.post(url, json=data, headers=headers)
print(f"[SUPLA] Status: {response.status_code}, Odpowiedź: {response.text}")
# Gdy MQTT odbierze dane:
def on_message(client, userdata, msg):
value = msg.payload.decode()
print(f"[MQTT] {msg.topic} = {value}")
send_to_supla(value)
# Połącz z MQTT
client = mqtt.Client()
client.on_message = on_message
client.connect(MQTT_BROKER, MQTT_PORT, 60)
client.subscribe(MQTT_TOPIC)
print(f"[MQTT] Subskrybuję: {MQTT_TOPIC}")
client.loop_forever()