Computer_Science_Arbeit/Code/get_data.ipynb

110 lines
6.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-01-15T16:21:32.634426800Z",
"start_time": "2024-01-15T15:30:44.375000700Z"
}
},
"outputs": [
{
"data": {
"text/plain": " Date Time Temperature Barometric Pressure Humidity\n0 23-12-2023 10:31:44 9.14 962.54 61.900\n1 23-12-2023 10:33:26 9.19 962.56 62.583\n2 23-12-2023 10:34:26 9.14 962.59 61.835\n3 23-12-2023 10:35:26 8.95 962.61 62.270\n4 23-12-2023 10:36:26 8.91 962.67 61.414\n.. ... ... ... ... ...\n0 15-01-2024 17:16:34 1.18 951.71 79.291\n0 15-01-2024 17:17:34 1.19 951.74 78.544\n0 15-01-2024 17:18:34 1.22 951.72 78.812\n0 15-01-2024 17:19:33 1.23 951.74 79.127\n0 15-01-2024 17:20:33 1.23 951.65 78.431\n\n[530 rows x 5 columns]",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Date</th>\n <th>Time</th>\n <th>Temperature</th>\n <th>Barometric Pressure</th>\n <th>Humidity</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>23-12-2023</td>\n <td>10:31:44</td>\n <td>9.14</td>\n <td>962.54</td>\n <td>61.900</td>\n </tr>\n <tr>\n <th>1</th>\n <td>23-12-2023</td>\n <td>10:33:26</td>\n <td>9.19</td>\n <td>962.56</td>\n <td>62.583</td>\n </tr>\n <tr>\n <th>2</th>\n <td>23-12-2023</td>\n <td>10:34:26</td>\n <td>9.14</td>\n <td>962.59</td>\n <td>61.835</td>\n </tr>\n <tr>\n <th>3</th>\n <td>23-12-2023</td>\n <td>10:35:26</td>\n <td>8.95</td>\n <td>962.61</td>\n <td>62.270</td>\n </tr>\n <tr>\n <th>4</th>\n <td>23-12-2023</td>\n <td>10:36:26</td>\n <td>8.91</td>\n <td>962.67</td>\n <td>61.414</td>\n </tr>\n <tr>\n <th>...</th>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n <td>...</td>\n </tr>\n <tr>\n <th>0</th>\n <td>15-01-2024</td>\n <td>17:16:34</td>\n <td>1.18</td>\n <td>951.71</td>\n <td>79.291</td>\n </tr>\n <tr>\n <th>0</th>\n <td>15-01-2024</td>\n <td>17:17:34</td>\n <td>1.19</td>\n <td>951.74</td>\n <td>78.544</td>\n </tr>\n <tr>\n <th>0</th>\n <td>15-01-2024</td>\n <td>17:18:34</td>\n <td>1.22</td>\n <td>951.72</td>\n <td>78.812</td>\n </tr>\n <tr>\n <th>0</th>\n <td>15-01-2024</td>\n <td>17:19:33</td>\n <td>1.23</td>\n <td>951.74</td>\n <td>79.127</td>\n </tr>\n <tr>\n <th>0</th>\n <td>15-01-2024</td>\n <td>17:20:33</td>\n <td>1.23</td>\n <td>951.65</td>\n <td>78.431</td>\n </tr>\n </tbody>\n</table>\n<p>530 rows × 5 columns</p>\n</div>"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import paramiko\n",
"import pandas as pd\n",
"from datetime import datetime\n",
"import time\n",
"\n",
"\n",
"def get_data(hostname, port, username, password, script_path):\n",
" try:\n",
" client = paramiko.SSHClient()\n",
" client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n",
" client.connect(hostname, port, username, password)\n",
"\n",
" stdin, stdout, stderr = client.exec_command(f'python3 {script_path}')\n",
" output = stdout.read().decode('utf-8').strip()\n",
"\n",
" client.close()\n",
"\n",
" output_pairs = output.split(',')\n",
"\n",
" variable_values = [float(pair.split('=')[1]) for pair in output_pairs]\n",
"\n",
" variable1, variable2, variable3 = variable_values\n",
"\n",
" return variable1, variable2, variable3\n",
"\n",
" except Exception as e:\n",
" print(f\"An error occurred: {e}\")\n",
" return None, None, None\n",
"\n",
"\n",
"df = pd.read_csv('Data.csv')\n",
"while True:\n",
" try:\n",
" temp, pressure, humidity = get_data('192.168.1.123', 22, 'hoehenmichae', 'pi',\n",
" '/home/hoehenmichae/Documents/project/code.py')\n",
" time_1 = datetime.now().time().replace(microsecond=0)\n",
" date_1 = datetime.today().date().strftime('%d-%m-%Y')\n",
" df_temp = pd.DataFrame(\n",
" {'Date': [date_1], 'Time': [time_1], 'Temperature': [temp], 'Barometric Pressure': [pressure],\n",
" 'Humidity': [humidity]})\n",
" df = pd.concat([df, df_temp])\n",
" df.to_csv('Data.csv', index=False)\n",
" time.sleep(59)\n",
"\n",
" except KeyboardInterrupt:\n",
" break\n",
"\n",
"df.to_csv('Data.csv', index=False)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"id": "33ba79e654b7b277"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}