Spaces:
Sleeping
Sleeping
File size: 2,807 Bytes
830ab0c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\jerin\\AppData\\Local\\Temp\\ipykernel_18680\\2251714346.py:13: DeprecationWarning: Callback API version 1 is deprecated, update to latest version\n",
" client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, clientId)\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[1], line 28\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m 27\u001b[0m publish_sensor_data()\n\u001b[1;32m---> 28\u001b[0m time\u001b[38;5;241m.\u001b[39msleep(\u001b[38;5;241m5\u001b[39m)\n\u001b[0;32m 29\u001b[0m client\u001b[38;5;241m.\u001b[39mdisconnect()\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"import paho.mqtt.client as mqtt\n",
"import time\n",
"import random\n",
"import pandas as pd\n",
"import json\n",
"\n",
"clientId = \"smartbuilding\"\n",
"broker_address = \"localhost\"\n",
"broker_port = 1883\n",
"\n",
"df = pd.read_csv(\"buildingdata.csv\")\n",
"\n",
"client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, clientId)\n",
"client.connect(broker_address, broker_port)\n",
"topic = \"sensor_data\"\n",
"\n",
"def publish_sensor_data():\n",
" # temp = round(random.uniform(20, 30), 2) \n",
" # pressure = round(random.uniform(900, 1100), 2) \n",
" for index, row in df.iterrows():\n",
" sa_temp = row['rtu_004_sa_temp']\n",
" ma_temp = row['rtu_004_ma_temp']\n",
" client.publish(topic, payload=json.dumps({\"sa_temp\": sa_temp, \"ma_temp\": ma_temp}))\n",
"\n",
"\n",
"while True:\n",
" publish_sensor_data()\n",
" time.sleep(5)\n",
"client.disconnect()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "smartbuilding",
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|