# Project#14 - Logging Temperature # Information - In this project, we're going to send an instruction to the RPI_no_RPI asking for a temperature reading. # The RPI_no_RPI will then send over a converted temperature reading. We're going to take that reading and write # it to a .csv file. import time # The 'time' module is included with pycharm and allows for us to create delays. import serial # The 'serial' or 'pyserial' module needs to be imported. See the project video for more info. import csv # This module allows for us to read and write to .CSV files. import datetime # This project will allow for us to timestamp our measurements in the .csv file # You're going to need to create a .CSV file # The first thing that we need to do is to talk to our RPInoRPI. try: # Try to establish communication with the RPInoRPI board. if successful, only the 'try:' will be executed MCU = serial.Serial(port='COM7', baudrate=115200, timeout=1) # Notice the timeout being 1 second. # More on this timeout later. time.sleep(5) # Wait five seconds. Why five seconds? The first communication gets screwy without this delay except: # If communication with the RPInoRPI could not be established, then perform the following instead: print('System Locked - Cannot communicate with MCU. Close this program, then reconnect to MCU and restart program') while True: # Forever loop. Close the program and start again. Check connection to the RPInoRPI pass # Do nothing def writer(value): outputFile = open('C:/Reports/Logs/database.csv', 'a', newline='') # 'a' means amend. Starts on new line vs writing over old ones # You'll need to create this file by creating a new excel worksheet in the C:/Reports/Logs folder and save it # with the title of 'database', and in the file type dropdown menu, select "CSV Comma Delimited .csv" Current_Time = datetime.datetime.now() # Grab a date stamp and place the result in fail_time Current_Time = Current_Time.strftime("%H:%M:%S") # Remove everything but Hours, minutes, and seconds outputWriter = csv.writer(outputFile) # Create the file to write to outputWriter.writerow(['Temperature (Deg C: ', value, 'Time: ', Current_Time]) # Write the new temp and time to database.csv outputFile.close() # If you don't close the file, you'll get errors def slicer(value): # This function takes the returned value from the RPInoRPI and formats it. # What do I mean? The RPInoRPI returns values that look like this: b'5'. # So what? So we need to change that string value into a number for comparison. value = value.replace('b','') # We've received value, which looks like b'23.35\r\n'. Replace 'b' with nothing. value = value.replace("'",'') # Remove all ' from value. Now we're left with is 23.35/r/n value = value.replace("r", '') # Remove every 'r' from value. 23.35//n value = value.replace("n", '') # Remove all instances of 'n' from value. Now we're left with is 23.35\\ value = value.replace("\\", '') # Remove all ' from value. Now we're left with 5 # Now why did we have to create 3 steps to remove '\r\n'? Don't ask me. I just work here. # In all seriousness, the program didn't like remove("/r/n"), and so I had to remove this over several steps #value = int(value) # At this point value is still a string. int changes it into a comparable number =D return value # This is the end of this function. The original value was an unusable string. Now a usable number! # This function serves to send a request to the MCU and wait for returned data def write_read(x): MCU.write(bytes(x, 'utf-8')) # Unicode Transformation Format - 8 bits. Write an 8-bit value to the MCU global data # Set as a global varible data = MCU.readline() # Wait for a response from the MCU, and store the returned value in 'data' return data # Return the data to the main code # Main Code x = 0 # Create a variable named x and set it to 0 while True: # Run the following code until you close the program (loop forever) x = '5' value = write_read(x) # call the write_read function and send 'x' along with it. We can only send string data print('Returned Value = ' + str(value)) # Concatenate the string + the string version of 'value' together and print value = slicer(str(value)) # Less complicated than it looks. send 'value' to the slicer function in string form print('New number after slicer function = ' + str(value)) # Print the sliced string>number writer(value) time.sleep(1)