{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\jerin\\AppData\\Local\\Temp\\ipykernel_6272\\1211754903.py:21: DeprecationWarning: Callback API version 1 is deprecated, update to latest version\n", " client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "{'sa_temp': 68.6, 'ma_temp': 70.0}\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[2], line 25\u001b[0m\n\u001b[0;32m 23\u001b[0m client\u001b[38;5;241m.\u001b[39mconnect(broker_address, broker_port)\n\u001b[0;32m 24\u001b[0m client\u001b[38;5;241m.\u001b[39msubscribe(topic)\n\u001b[1;32m---> 25\u001b[0m \u001b[43mclient\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloop_forever\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[1;32mc:\\Users\\jerin\\anaconda3\\envs\\smartbuilding\\Lib\\site-packages\\paho\\mqtt\\client.py:2291\u001b[0m, in \u001b[0;36mClient.loop_forever\u001b[1;34m(self, timeout, retry_first_connection)\u001b[0m\n\u001b[0;32m 2289\u001b[0m rc \u001b[38;5;241m=\u001b[39m MQTTErrorCode\u001b[38;5;241m.\u001b[39mMQTT_ERR_SUCCESS\n\u001b[0;32m 2290\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m rc \u001b[38;5;241m==\u001b[39m MQTTErrorCode\u001b[38;5;241m.\u001b[39mMQTT_ERR_SUCCESS:\n\u001b[1;32m-> 2291\u001b[0m rc \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_loop\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2292\u001b[0m \u001b[38;5;66;03m# We don't need to worry about locking here, because we've\u001b[39;00m\n\u001b[0;32m 2293\u001b[0m \u001b[38;5;66;03m# either called loop_forever() when in single threaded mode, or\u001b[39;00m\n\u001b[0;32m 2294\u001b[0m \u001b[38;5;66;03m# in multi threaded mode when loop_stop() has been called and\u001b[39;00m\n\u001b[0;32m 2295\u001b[0m \u001b[38;5;66;03m# so no other threads can access _out_packet or _messages.\u001b[39;00m\n\u001b[0;32m 2296\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_thread_terminate \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m 2297\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_out_packet) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m 2298\u001b[0m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_out_messages) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m):\n", "File \u001b[1;32mc:\\Users\\jerin\\anaconda3\\envs\\smartbuilding\\Lib\\site-packages\\paho\\mqtt\\client.py:1657\u001b[0m, in \u001b[0;36mClient._loop\u001b[1;34m(self, timeout)\u001b[0m\n\u001b[0;32m 1654\u001b[0m rlist \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sock, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sockpairR]\n\u001b[0;32m 1656\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1657\u001b[0m socklist \u001b[38;5;241m=\u001b[39m select\u001b[38;5;241m.\u001b[39mselect(rlist, wlist, [], timeout)\n\u001b[0;32m 1658\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[0;32m 1659\u001b[0m \u001b[38;5;66;03m# Socket isn't correct type, in likelihood connection is lost\u001b[39;00m\n\u001b[0;32m 1660\u001b[0m \u001b[38;5;66;03m# ... or we called disconnect(). In that case the socket will\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1663\u001b[0m \u001b[38;5;66;03m# rc != MQTT_ERR_SUCCESS and we don't want state to change from\u001b[39;00m\n\u001b[0;32m 1664\u001b[0m \u001b[38;5;66;03m# mqtt_cs_disconnecting.\u001b[39;00m\n\u001b[0;32m 1665\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_state \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m (_ConnectionState\u001b[38;5;241m.\u001b[39mMQTT_CS_DISCONNECTING, _ConnectionState\u001b[38;5;241m.\u001b[39mMQTT_CS_DISCONNECTED):\n", "\u001b[1;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "import paho.mqtt.client as mqtt\n", "import json\n", "import pandas as pd\n", "\n", "broker_address = \"localhost\"\n", "broker_port = 1883\n", "topic = \"sensor_data\"\n", "\n", "df = pd.DataFrame(columns=[\"sa_temp\", \"ma_temp\"])\n", "\n", "def on_message(client, userdata, message):\n", " global df\n", " payload = json.loads(message.payload.decode())\n", " sa_temp = payload[\"sa_temp\"]\n", " ma_temp = payload[\"ma_temp\"]\n", " print(payload)\n", " # df.loc[len(df)] = {\"sa_temp\": sa_temp, \"ma_temp\": ma_temp}\n", " \n", "\n", "\n", "client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)\n", "client.on_message = on_message\n", "client.connect(broker_address, broker_port)\n", "client.subscribe(topic)\n", "client.loop_forever()\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | sa_temp | \n", "ma_temp | \n", "
---|---|---|
0 | \n", "68.6 | \n", "70.0 | \n", "
1 | \n", "68.6 | \n", "70.0 | \n", "
2 | \n", "68.6 | \n", "70.0 | \n", "
3 | \n", "68.6 | \n", "70.0 | \n", "