import socket
import time
from datetime import datetime
import pandas as pd
import os

ip = "0.0.0.0" # Listen to all channels
port = 5005 # Should match .ino

time.sleep (5)

Data_to_influx = pd.DataFrame ({"Date": [], "Time": [], "Temperature": []})

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((ip, port))
    s.listen()
    print(f"Listening")
    while True:
        conn, addr = s.accept()
        with conn:
            data = conn.recv(1024)
            if data:
                print(f"Received from {addr} at {datetime.now()}: {data.decode().strip()}")
                row = pd.DataFrame ({"Date": [str (datetime.now()) [:10]], "Time": [str (datetime.now()) [11:19]], "Temperature": [data.decode().strip()]})
                Data_to_influx = pd.concat ([Data_to_influx, row], ignore_index= 1)
                csv_file_name = "C:/Users/Iluz1/Desktop/Data/a.csv"
                Data_to_influx.to_csv (csv_file_name)

