amc2021:groupl:extras:python:start
This is an old revision of the document!
Python Code
Python was used to parse the data received from the sensors.
- php CO2.php
<PHP> import serial import time import matplotlib.pyplot as plt # make sure the 'COM#' is set according the Windows Device Manager ser = serial.Serial('/dev/cu.usbmodem14101', 9600, timeout=1) time.sleep(2) data = [] for i in range(100): line = ser.readline() # read a byte string if line: string = line.decode() # convert the byte string to a unicode string num = float(string) # convert the unicode string to an int num2 = int(num) data.append(num2) # add int to data list ser.close() # build the plot plt.plot(data) plt.xlabel('Time[s]') plt.ylabel('CO2 [ppm]') plt.title('CO2 vs. Time') plt.show() </PHP> <PHP> # Importing library import csv import pandas as pd CO2 = pd.DataFrame(data) #convert data to a pandas dataframe # opening the csv file in 'w+' mode file = open('CO2_(insert-initial.csv', 'w+', newline ='') CO2.to_csv(file,index = False) </PHP> <PHP> #combining data taken from all three locations import pandas as pd L = pd.read_csv('CO2_L.csv') #reading the csv files K = pd.read_csv('CO2_K.csv') B = pd.read_csv('CO2_B.csv') df = pd.concat([L,K,B], axis = 1) #combing all the files horizontally </PHP> <PHP> # calculating the mean for each location mean = df.mean() ppm = 400 #defining ppm of CO2 in clean air ratio = mean/ppm #calculating the ratio of ppm of each location with the ppm of clean air #adding the mean and ratio to the dataframe df.loc['mean'] = mean df.loc['ratio'] = mean/ppm #converting the dataframe to a csv file file1 = open('CO2.csv', 'w+', newline ='') df.to_csv(file1,index = False) </PHP>
amc2021/groupl/extras/python/start.1630798035.txt.gz · Last modified: 2023/01/05 14:38 (external edit)