Reading and decoding Temperature Data from Arduino¶
(This is Part 2 of the Measuring Temperature Lab ).¶
For this lab you will need pyserial. This can be installed thru Anaconda the same way we did Basemap. Try either of the following options for installation.
-
>>> conda install -c anaconda pyserial
- Use Anaconda Navigator to install pyserial from the Enivronments tab.
Check out https://pythonhosted.org/pyserial/pyserial.html for more information on the Package.
For parsing the incoming serial data you will want to use Python's string Methods:
http://www.thehelloworldprogram.com/python/python-string-methods/ https://docs.python.org/2/library/string.html
Also check out the Regular Expressions (regex) modules. https://docs.python.org/2/library/re.html# These are more sophisticated than what we require, but very powerful.
Some Cliff Notes on Pandas¶
This exercise uses Pandas - a Python dataframe module, which is valuable for handling relational data. Brice will provide a more thorough intro to Pandas next week, when we talk about time series and regression statistics. For now, we will use Pandas to create a dataframe and save it to a .csv file. Pandas dataframes use the dict() or dictionary data type.
>>> df = pd.DataFrame(columns=('time','Resistence','Temp')) # This command declares a new Pandas dataframe with three columns: time, resistence, and Temp.
>>> df = df.append({'time':t,'Resistence': R,'Temp':T},ignore_index=True) # This command appends new values to each of the columns.
# Load the libraries
from matplotlib import pyplot as plt
import serial as ps
import datetime as dt
import time as time
import pandas as pd
Open the serial connection using e.g. S = ps.Serial() command.
You will need to find the exact port name on your computer. Hint: This can be found in the Arduino Tools menu under "port".
# Assign the serial object that you will write to and read from
Initialize the two variables you will be using to store xdata and ydata (resistance).
# Initialize Pandas dataframe to capture time, resistence, and Temp, as shown in datetime notes.
Set the coefficients for the Steinhart-Hart equation
##1/T = A + B*Ln(R) + C(Ln(R))^3
A = 0.001125308852122
B = 0.000234711863267
C = 0.000000085663516
# Set a while loop or for loop. You can make it go indefinitely until you kill the loop with Kernel -> Interrupt.
#lp = 0
#stop = 'go'
# Use a for or while loop. You can make it go indefinitely until you kill the loop with Kernel -> Interrupt.
#while stop == 'go':
# Use the serial .write(b' ') function to issue your measurement request to the Arduino.
# Read a line from the Serial data object stored in 'S'. Note, readline() line returns a byte array.
# You can convert to a string using decode('utf-8'). You may also want to investigate the .strip() function
# that will remove unwanted characters.
# Decode and strip to create a string.
#a = a.decode('utf-8').strip()
# Parse the string to make sure you have found the right line. Remember you can index strings the way you index
# other objects.
# Example: >>> mystr = "This string is short"
# >>> print mystr[12:13] (this produces 'is')
#b = a.find("This is my resistence line")
# Use an 'if statement' if necessary and append the new resistance value to the array 'R' that was initialized
# above.
# Compute Temperature using A, B, C coefficients and the resistance value.
# Convert Temp in deg Kelvin to deg C.
# Record the date and time using the datetime package.
# Append the datetime, resistance, and temperature as a new row in your Pandas Dataframe.
#if b != -1:
# try:
# except:
# print("incomplete string")
# Add a delay to set the measurement frequency with the time.sleep() function.
# Save the data to a csv using the pandas function df.to_csv() where df is the name of your dataframe.
# You can use a conditional statement to do this every N'th time, e.g. every 100th time.
#df.to_csv()
Completing your data collection¶
- Close the connection to the Serial data object. (THIS IS REALLY IMPORTANT)!!!
- Make a plot of Temperature vs. time.
Make a plot of Temperature vs. datetime. Save that plot using plt.savefig()
What to turn in:¶
See instructions in Thermistor Lab Part 2.