From ec854c1cd07ac9514c94fd013efcf22c1650ac07 Mon Sep 17 00:00:00 2001
From: Jasper Franse <w.j.m.franse@tudelft.nl>
Date: Thu, 29 Apr 2021 12:35:27 +0000
Subject: [PATCH] example coding

---
 example_code_switch.ipynb | 171 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 171 insertions(+)
 create mode 100755 example_code_switch.ipynb

diff --git a/example_code_switch.ipynb b/example_code_switch.ipynb
new file mode 100755
index 0000000..ee3856d
--- /dev/null
+++ b/example_code_switch.ipynb
@@ -0,0 +1,171 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "id": "clean-marsh",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# import warnings\n",
+    "# warnings.filterwarnings('ignore')\n",
+    "\n",
+    "# import sys\n",
+    "# import os\n",
+    "# import shutil\n",
+    "# from pyrpl import Pyrpl\n",
+    "# import numpy as np\n",
+    "# import time\n",
+    "# import matplotlib.pyplot as plts\n",
+    "\n",
+    "# import IPython\n",
+    "# import ipywidgets as widgets\n",
+    "\n",
+    "# from bokeh.plotting import figure, show\n",
+    "# from bokeh.io import output_notebook, push_notebook\n",
+    "# from bokeh.layouts import column, row\n",
+    "# from bokeh.models import ColumnDataSource, Toggle, Range1d\n",
+    "\n",
+    "# from scipy.signal import welch\n",
+    "\n",
+    "# from datetime import datetime\n",
+    "\n",
+    "#Arduino Serial connection\n",
+    "import serial"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "id": "variable-composite",
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "SerialException",
+     "evalue": "[Errno 2] could not open port COM9: [Errno 2] No such file or directory: 'COM9'",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
+      "\u001b[0;32m/opt/conda/lib/python3.8/site-packages/serial/serialposix.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    321\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 322\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mportstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mO_RDWR\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mO_NOCTTY\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mO_NONBLOCK\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    323\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'COM9'",
+      "\nDuring handling of the above exception, another exception occurred:\n",
+      "\u001b[0;31mSerialException\u001b[0m                           Traceback (most recent call last)",
+      "\u001b[0;32m<ipython-input-5-ccbc81474993>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mserial\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSerial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'COM9'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m9600\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset_input_buffer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/opt/conda/lib/python3.8/site-packages/serial/serialutil.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout, exclusive, **kwargs)\u001b[0m\n\u001b[1;32m    242\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    243\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mport\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 244\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    245\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    246\u001b[0m     \u001b[0;31m#  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;32m/opt/conda/lib/python3.8/site-packages/serial/serialposix.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    323\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    324\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 325\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mSerialException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merrno\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"could not open port {}: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_port\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    326\u001b[0m         \u001b[0;31m#~ fcntl.fcntl(self.fd, fcntl.F_SETFL, 0)  # set blocking\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    327\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
+      "\u001b[0;31mSerialException\u001b[0m: [Errno 2] could not open port COM9: [Errno 2] No such file or directory: 'COM9'"
+     ]
+    }
+   ],
+   "source": [
+    "ser = serial.Serial('COM9',9600,timeout=0.5)\n",
+    "ser.flush()\n",
+    "\n",
+    "time.sleep(0.5)                       \n",
+    "ser.reset_input_buffer()\n",
+    "ser.write(b'at')\n",
+    "ser.write(b'ch1')"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "rural-musical",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "vulnerable-leone",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "freelance-ticket",
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "continuing-article",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# Global variables \n",
+    "R = []\n",
+    "T = []\n",
+    "tm = []\n",
+    "Iavg = []\n",
+    "Iptp = []\n",
+    "Vavg = []\n",
+    "Vptp = []\n",
+    "t0 = time.time()\n",
+    "timer_t0 = time.time()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "verbal-blame",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def get_temperature(serial_port, baudrate, timeout):\n",
+    "    # serial_port='COM7', baudrate=9600, timeout=1\n",
+    "    \n",
+    "    #variables (could be adjusted to inputs?)\n",
+    "    alpha = 4.1139e-3; R0 = 1000; R1=1081; Vin=3.272        #should be according to literature: alpha = 3.85e-3 & R0 = 1000\n",
+    "    \n",
+    "    #open Serial port to Arduino + flush what's still on it\n",
+    "    ser = serial.Serial(serial_port,baudrate,timeout=0.5)\n",
+    "    ser.flush()\n",
+    "    \n",
+    "    #sent command to Arduino so we can read it out:\n",
+    "    time.sleep(0.5)                       #needs to sleep, otherwise we get a: 'can not convert float... at the Vout-line'\n",
+    "    ser.reset_input_buffer()\n",
+    "    ser.write(b'g')\n",
+    "    \n",
+    "    #Serial read + Temperature calculations:\n",
+    "    serial_reply = ser.readline()\n",
+    "    try:\n",
+    "        Vout = float(str(serial_reply.decode(\"utf-8\")))*(Vin/1023.0)        #calculates the voltage from the voltage divider (over the PT1000)\n",
+    "    except:\n",
+    "        return serial_reply\n",
+    "    Rpt = (Vout*R1)/(Vin-Vout)                                              #calculates the resistance of the PT1000\n",
+    "    Temp_C = ((Rpt/R0)-1)/alpha                                             #with the above calculated resistance and 'known' variable we calculate the temperature in Celsius\n",
+    "    Temp_K = Temp_C + 273.15                                                #Temperature in Kelvin\n",
+    "    return Temp_K"
+   ]
+  }
+ ],
+ "metadata": {
+  "hide_input": false,
+  "kernelspec": {
+   "display_name": "Python 3",
+   "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.8.8"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
-- 
GitLab