I need some help on my project.
I had interfaced the Raspberry Pi and Arduino Uno through USB. I used the Arduino as a slave to obtain data from sensors. The data is then transferred to the Raspberry Pi. I would like to store these useful data. May I know how do I get about it? I'm very new to Python.
Any help is much appreciated. Thank you. =D
-
Your question is about how to transfer data between both devices, or about how to store them in files once they are in the RPi?Roberto– Roberto2014年04月08日 13:10:22 +00:00Commented Apr 8, 2014 at 13:10
-
I would like to store them in a file once they are in the RPi. Then, when needed, I would like to access these data for further processing.wengzhe– wengzhe2014年04月08日 15:34:47 +00:00Commented Apr 8, 2014 at 15:34
-
If you want to use Python, this might (or might not) help you: raspberrypi.org/forums/viewtopic.php?p=368522#p368522developius– developius2014年05月10日 03:25:23 +00:00Commented May 10, 2014 at 3:25
2 Answers 2
There is a lot of ways to do this, and the best one depends on how will you use the generated data. Not having all the details of your particular setup, I'll assume the following:
get_arduino_string()that returns the string being read, or None if there is nothing more to read (I'll asume that you already have this developed as you say you have already interfaced both devices.)
In this situation, this python script would create a file into the RPi filesystem and append the readings to it:
def get_arduino_string(): ... code for reading the string from the arduino... if __name__ == "__main__": f=open('filename','a') # 'a' is for append reading=get_arduino_string() # get the reading from the arduino while reading is not None: # loop while we get a string from arduino f.write(reading) # write the string into the file reading=get_arduino_string() # read a new string f.close() # close the file when nothing more to receive
This is a very basic script without exception handling, but gives an idea about how to generate this file. In this case this is a text file. You could see live what's being written in the file by issuing a
tail -f filenamefrom the RPi's shell prompt while the script is running in another shell, or in background.
Being this a regular (unix) text file, you can do whatever you want with it. You can transfer it to another machine, send it by mail, or directly process it from within the RPi. For example, if you want to process the file line by line, you could run the following python script:
f=open('filename','r') # open for read lines=f.readlines() # read the contents of the file into the variable lines for line in lines: # loop for each line of the file print line # print the contents .... # or process its contents f.close() # close the file
-
Hey thanks a lot! Your answer is really detailed. I'll try it out soon. Appreciate you help =Dwengzhe– wengzhe2014年04月09日 04:51:47 +00:00Commented Apr 9, 2014 at 4:51
You should consider storing your values in a database. I usualy use node.js and mongoose but here's a tutorial on how to use mongodb with python. JSON is also a nice/universal way to store data.
I use mongohq a lot because it takes away most of the headaches of setting up a local db and makes your data available anywhere. Unfortunately all of you reads and writes will take more time than if you maintain a local db.
Hope this helps!