Introduction
Data logging helps save information from sensors so you can check it later. It makes your IoT devices smarter by keeping track of what happens over time.
Data logging helps save information from sensors so you can check it later. It makes your IoT devices smarter by keeping track of what happens over time.
# Example of saving sensor data to a file with open('data_log.txt', 'a') as file: file.write(f"{timestamp}, {sensor_value}\n")
import time sensor_value = 25.3 with open('data_log.txt', 'a') as file: file.write(f"{time.time()}, {sensor_value}\n")
from datetime import datetime sensor_value = 60 with open('data_log.txt', 'a') as file: file.write(f"{datetime.now()}, {sensor_value}\n")
This program saves the current date and sensor value to a file and prints a confirmation.
import time from datetime import datetime sensor_value = 22.5 with open('data_log.txt', 'a') as file: timestamp = datetime.now() file.write(f"{timestamp}, {sensor_value}\n") print('Data logged:', timestamp, sensor_value)
Always include a timestamp to know when data was recorded.
Use simple text files or CSV for easy reading and analysis later.
Make sure to handle file errors in real projects to avoid losing data.
Data logging saves sensor info so you can review it anytime.
It helps find patterns and problems in IoT devices.
Use timestamps and append mode to keep data organized.
with open('log.txt', 'a') as f:
f.write('Temperature: 22\n')f = open('data.log', 'w')
f.write('Humidity: 45%')
f.close()Pass the test and this pattern moves unknown