import utime
import machine
import ujson as json

# Function to log data to a JSON file
def log_data(data):
    with open("data.json", "a") as f:
        json.dump(data, f)
        f.write("\n")


print('run...')
# Main loop
while True:
    try:
        # Replace this with your own data collection code
        data_to_log = {"value1": 1, "value2": 2, "value3": 3}  # Replace with your JSON data

        # Get the current timestamp
        timestamp = utime.localtime()
        timestamp_str = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
            timestamp[0], timestamp[1], timestamp[2], timestamp[3], timestamp[4], timestamp[5]
        )

        # Add timestamp to the data
        data_to_log["timestamp"] = timestamp_str

        # Log the data
        log_data(data_to_log)

        # Sleep for 10 minutes (600 seconds)
        utime.sleep(6)
        print('logged...')
    except Exception as e:
        print("Error:", e)
        # Sleep for 10 seconds if there is an error
        utime.sleep(10)
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23