Introduction: Raspberry Pi - Data Logging
In this project, we will learn how to logging data from Raspberry Pi using Python and Excel which collect data and save the output of the collection or analysis.
It’s a quite quick project and can be used either on its own or part of something bigger. Check it out >> Integrated Weather Station(IWS).
Step 1: List of Material
For this project, we will be using:
- Raspberry Pi (mine Raspberry Pi 2)
- Flashdrive cost: ~5.00 USD
Step 2: Hardware Connections
In general, the connections are fairly easy. Just connect Flash Drive to Raspberry PI with USB slot and check the name of Flash Drive in Raspberry pi by type "ls /media" in Raspberry Pi terminal, the result should be content Flash Drive's name and you are good to go.
Step 3: Raspberry Pi Programming
Below is the Python code. Edit the code in any way you prefer, run the module , and you should have no problems.
import os import time
from time import sleep
from datetime import datetime
file = open("/home/pi/data_log.csv", "a")
i=0
if os.stat("/home/pi/data_log.csv").st_size == 0:
file.write("Time,Sensor1,Sensor2,Sensor3,Sensor4,Sensor5\n")
while True:
i=i+1
now = datetime.now()
file.write(str(now)+","+str(i)+","+str(-i)+","+str(i-10)+","+str(i+5)+","+str(i*i)+"\n")
file.flush()
time.sleep(5)<br>file.close()
Attachments
Step 4: Read Your Data Log
Your file that located in your /home/pi/ or any location you prefer is a comma-separated value(CVS) file. Comma-separated values (CVS) file is a formatted file that stores tabular data (numbers and text) which separated by comma. To open and modify this file, simply double click on it, or open it with Microsoft Excel. You can plot your data with Chart tools that available in Microsoft Excel.
Step 5: Enjoy!
After making sure that everything works correctly, you can take this project into bigger project or integrated it with Real Time Clock (RTC) for accurate logging. Check it out >> Raspberry Pi - Real Time Clock (RTC) !
Power through power bank or a plug and you’re good to go!
32 Comments
1 year ago
Brilliant
A spreadsheet work definitely easier as collecting data with a SQL database.
It is also much easier to work with and to process the data (for me)
Will add this with MQTT for distribution via the broker
Just love it
Question 2 years ago
What would be the direction I need to go to use the data received from my solar inverters to push the data to a webpage within the pi or external site?
Answer 1 year ago
Very easy to implement:
https://github.com/miguelgrinberg/Flask-SocketIO/i...
And this is a good example for asynchronous threads (if you want "real time data", you'll need them)
https://github.com/miguelgrinberg/Flask-SocketIO/issues/651
Question 2 years ago on Step 5
What is the USB stick for if you save the files on pi/home (SD Card)?
Question 3 years ago on Step 3
Are there any comments for each line of this code for us newbies?
Question 4 years ago
Hi Adrie, this is a great piece of code, thank you, just wanted to know, how do I connect 7 x distance sensors to this code so I can get the data sent to the csv file? The code for the sensors work perfectly and I can see the data when running the code, but don't know how to connect it to your cool piece of code, can you help please?
6 years ago
What would be some real life applications of this project?
Reply 6 years ago
Data logging for example =D
It logs data from sensors, and that's the real life application. Weather monitoring, movement tracker, use your imagination ^^
Reply 4 years ago
Hi GoodyPles, I'm new to RPi, I have 7 x directional sensors connected to RPi 3 all working perfectly, I just need to download the data to csv file, above code is very useful and works very well. How to I connect the sensor data to this code? Please help really struggling. Cheers
Reply 4 years ago
Sorry should be distance sensors
4 years ago
Hi ArdieSentosa,
I like this project!
@Hejhooo:
1) try ls /media/*
2) got it running by re- opening file before closing again in the while true loop ; dunno why it closes after header line - I'm also a newbie
3) if running well you will not see it opening; in folder you will see file size raising
4 years ago
like
4 years ago
Help!!!
there is 3 things that is totally black for me
1) ls /media ? the responses i got is - pi?? do i tell my RB it have USB ports?
2) in the code i got a fault on i=i+1
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
from datetime import datetime
file = open("/home/pi/data_log.csv", "a")
i=0
if os.stat("/home/pi/data_log.csv").st_size == 0: file.write("Time,Sensor1,Sensor2,Sensor3,Sensor4,Sensor5\n")
while True:
i=i+1
now = datetime.now()
file.write(str(now)+","+str(i)+","+str(-i)+","+str(i-10)+","+str(i+5)+","+str(i*i)+"\n")
file.flush()
time.sleep(5)
file.close()
3) It don't open the file data_log.csv
best regards Henrik
4 years ago
sir can you help me how to put input pin on this data logger?. example how can i code input in sensor 1. im new in raspberry pi and i really want to explore more. please help me for my project thanks and best regards.
Reply 4 years ago
you can also email me at ginojenssen13@gmail.com
4 years ago
hi, hope you can help.. i get a error message for .. "time.sleep(5)
file.close()"
it says that the file name is not defined , why is this ??
also, ls /media brings up "pi" and doesn't bring up the name of my usb, any ideas of how to resolve this ?
Reply 4 years ago
you need to import time module in your code.
import os
import time
from datetime import datetime
file = open("/home/pi/data_log.csv", "a")
i=0
if os.stat("/home/pi/data_log.csv").st_size == 0: file.write("Time,Sensor1,Sensor2,Sensor3,Sensor4,Sensor5\n")
while True:
i=i+1
now = datetime.now()
file.write(str(now)+","+str(i)+","+str(-i)+","+str(i-10)+","+str(i+5)+","+str(i*i)+"\n")
file.flush()
time.sleep(5)
file.close()
it seems like that.....
5 years ago
The data logging gave me ideas on saving data collecting from RasPi.
5 years ago
Can you use this to log data off of a blood glucose monitor?
5 years ago
Hello,
I'm having a really hard time understanding your guide and how to use it in my project... maybe its me, since im a total newbie with python... will read the other two guides you shared tho, and try to understand how this data logger works.
But.... is it possible to explain what does the "file.write(str(now+","+....." actually do?
Thank you for your time!