0

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

3
  • 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
    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
    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#p368522
    – developius
    May 10, 2014 at 3:25

2 Answers 2

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:

  • You want a regular text file stored with incoming data appended to the file.
  • You have already properly connected Arduino and RPi so you receive, say, a stream of characters on a buffer on the RPi.
  • Let's say you have your incoming string made available into your python script via a call to a function called
     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 filename
    from 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
    
  • 1
    • Hey thanks a lot! Your answer is really detailed. I'll try it out soon. Appreciate you help =D
      – wengzhe
      Apr 9, 2014 at 4:51
    0

    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!

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.