{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "3b92c632", "metadata": {}, "outputs": [], "source": [ "import paho.mqtt.client as mqtt\n", "import time\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "a6753fb1", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/tmp/ipykernel_2342/243927247.py:34: DeprecationWarning: Callback API version 1 is deprecated, update to latest version\n", " client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) # Use VERSION1 for broader compatibility\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Connecting to MQTT broker at 192.168.1.110:1883...\n", "Successfully connected to MQTT Broker!\n", "Subscribed to topic: 'vision/vibratory_conveyor/state'\n", "Listening for messages... (Press Ctrl+C to stop)\n", "\n", "--- MQTT MESSAGE RECEIVED! ---\n", " Topic: vision/vibratory_conveyor/state\n", " Payload: on\n", " Time: 2025-07-25 21:03:21\n", "------------------------------\n", "\n", "\n", "--- MQTT MESSAGE RECEIVED! ---\n", " Topic: vision/vibratory_conveyor/state\n", " Payload: off\n", " Time: 2025-07-25 21:05:26\n", "------------------------------\n", "\n", "\n", "Stopping MQTT listener.\n" ] } ], "source": [ "\n", "# --- MQTT Broker Configuration ---\n", "# Your Home Assistant's IP address (where your MQTT broker is running)\n", "MQTT_BROKER_HOST = \"192.168.1.110\"\n", "MQTT_BROKER_PORT = 1883\n", "# IMPORTANT: Replace with your actual MQTT broker username and password if you have one set up\n", "# (These are NOT your Home Assistant login credentials, but for the Mosquitto add-on, if used)\n", "# MQTT_BROKER_USERNAME = \"pecan\" # e.g., \"homeassistant_mqtt_user\"\n", "# MQTT_BROKER_PASSWORD = \"whatever\" # e.g., \"SuperSecurePassword123!\"\n", "\n", "# --- Topic to Subscribe To ---\n", "# This MUST exactly match the topic you set in your Home Assistant automation\n", "MQTT_TOPIC = \"vision/vibratory_conveyor/state\" # <<<< Make sure this is correct!\n", "MQTT_TOPIC = \"vision/blower_separator/state\" # <<<< Make sure this is correct!\n", "\n", "# The callback for when the client receives a CONNACK response from the server.\n", "def on_connect(client, userdata, flags, rc):\n", " if rc == 0:\n", " print(\"Successfully connected to MQTT Broker!\")\n", " client.subscribe(MQTT_TOPIC)\n", " print(f\"Subscribed to topic: '{MQTT_TOPIC}'\")\n", " print(\"Listening for messages... (Press Ctrl+C to stop)\")\n", " else:\n", " print(f\"Failed to connect, return code {rc}\\n\")\n", "\n", "# The callback for when a PUBLISH message is received from the server.\n", "def on_message(client, userdata, msg):\n", " received_payload = msg.payload.decode()\n", " print(f\"\\n--- MQTT MESSAGE RECEIVED! ---\")\n", " print(f\" Topic: {msg.topic}\")\n", " print(f\" Payload: {received_payload}\")\n", " print(f\" Time: {time.strftime('%Y-%m-%d %H:%M:%S')}\")\n", " print(f\"------------------------------\\n\")\n", "\n", "# Create an MQTT client instance\n", "client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) # Use VERSION1 for broader compatibility\n", "\n", "# Set callback functions\n", "client.on_connect = on_connect\n", "client.on_message = on_message\n", "\n", "# Set username and password if required\n", "# (Only uncomment and fill these if your MQTT broker requires authentication)\n", "# client.username_pw_set(MQTT_BROKER_USERNAME, MQTT_BROKER_PASSWORD)\n", "\n", "try:\n", " # Attempt to connect to the MQTT broker\n", " print(f\"Connecting to MQTT broker at {MQTT_BROKER_HOST}:{MQTT_BROKER_PORT}...\")\n", " client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT, 60)\n", "\n", " # Start the MQTT loop. This runs in the background and processes messages.\n", " client.loop_forever()\n", "\n", "except KeyboardInterrupt:\n", " print(\"\\nStopping MQTT listener.\")\n", " client.disconnect() # Disconnect gracefully\n", "except Exception as e:\n", " print(f\"An unexpected error occurred: {e}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "56531671", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "USDA-vision-cameras", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }