code:
import serial
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import re
import time

# User input
json_key_file = input("Please enter the path to your JSON key file: ")
sheet_name = input("Please enter the name of your google sheet: ")
com_port = input("Please enter your COM port (e.g., ‘COM3’ or ‘/dev/ttyUSB0’ on Linux): ")
email = input(“Please enter your email: “)

# Google Sheets setup
scope = [‘https://www.googleapis.com/auth/spreadsheets’,
‘https://www.googleapis.com/auth/drive’]
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_file, scope)
gc = gspread.authorize(credentials)
worksheet = gc.open(sheet_name).sheet1

# Serial setup
ser = serial.Serial(com_port, 115200, timeout=1)

def write_to_sheet(tag_id, email):
try:
worksheet.append_row([time.strftime(”%Y-%m-%d %H:%M:%S”), tag_id, email])
print(“Data written to Google Sheet.”)
except Exception as e:
print("Failed to write data to Google Sheet: “, e)

def main():
print(“Reading serial output…”)
pattern = re.compile(r"tagId\s+is\s+:\s+(\d+.\d+.\d+.\d+)”)
try:
while True:
data = ser.readline().decode(‘utf-8’)
match = pattern.search(data)
if match:
tag_id = match.group(1)
write_to_sheet(tag_id, email)
except KeyboardInterrupt:
print(“Program stopped by user.”)
finally:
ser.close()

if name == “main”:
main()
code.py:

import serial
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import re
import time

# User input
json_key_file = input("Please enter the path to your JSON key file: ")
sheet_name = input("Please enter the name of your google sheet: ")
com_port = input("Please enter your COM port (e.g., ‘COM3’ or ‘/dev/ttyUSB0’ on Linux): ")
email = input(“Please enter your email: “)

# Google Sheets setup
scope = [‘https://www.googleapis.com/auth/spreadsheets’,
‘https://www.googleapis.com/auth/drive’]
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_file, scope)
gc = gspread.authorize(credentials)
worksheet = gc.open(sheet_name).sheet1

# Serial setup
ser = serial.Serial(com_port, 115200, timeout=1)

def write_to_sheet(tag_id, email):
try:
worksheet.append_row([time.strftime(”%Y-%m-%d %H:%M:%S”), tag_id, email])
print(“Data written to Google Sheet.”)
except Exception as e:
print("Failed to write data to Google Sheet: “, e)

def main():
print(“Reading serial output…”)
pattern = re.compile(r"tagId\s+is\s+:\s+(\d+.\d+.\d+.\d+)”)
try:
while True:
data = ser.readline().decode(‘utf-8’)
match = pattern.search(data)
if match:
tag_id = match.group(1)
write_to_sheet(tag_id, email)
except KeyboardInterrupt:
print(“Program stopped by user.”)
finally:
ser.close()

if name == “main”:
main()

in google spreadsheets its displaying:
2024-03-09 16:22:39 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:40 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:41 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:42 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:43 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:44 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:45 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:46 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:47 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:48 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:49 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:50 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:51 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:52 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:53 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:54 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:55 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:56 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:57 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:58 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:22:59 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:00 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:01 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:02 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:03 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:04 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:05 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:06 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:07 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>
2024-03-09 16:23:17 115.233.74.37 <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>

i need to display data inside serial monitor into google sheets