Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Using SD Card for the Whole Project.

    Scheduled Pinned Locked Moved Discussion
    9 Posts 4 Posters 95 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      hakan kayan
      last edited by

      Hi,

      I am trying to implement a machine learning method on Pysense + LopY combo. I need to use Python libraries(e.g. Numpy, Scikit) that uses more a space than the flash memory, so I want to utilize SD card for that. I have several question that might help to achieve my task:

      • Can I use SD card as to manage the whole project? Currently upload & download via REPL is only using flash memory. I created a data.txt but I am not able to fetch it via REPL. Is there any ways to achieve these?
      • Can I create a lib folder (to upload python ML libraries) in SD card and use that instead of the lib folder that we upload to flash? Just achieving this might be adequate in my case.
        Note: The ML method is Isolation Forest. You are not actually training anything, so the TinyML is out of question, hence I just need more space to implement that.

      Cheers,

      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @hakan kayan
        last edited by

        @hakan-kayan
        Yes, you can import modules from sd card.

        first mount sd card

        from machine import SD
        import os
        import sys
        sd = SD()
        os.mount(sd, '/sd')
        

        now you must add sd card to search path

        sys.path.append('/sd')
        

        and you are ready to import any module from it e.g.: your module is placed in sd card and its name is mysdcardfile.py then you can import it like this

        import mysdcardfile
        

        think about people first, then computers

        H 1 Reply Last reply Reply Quote 5
        • H Offline
          hakan kayan @livius
          last edited by hakan kayan

          Hi @livius. Thanks for quick answer.
          So I used the example given in Pycom documents and created text.txt file in the sd card.

          from machine import SD
          import os
          import sys
          
          #sd = SD()
          #os.mount(sd, '/sd')
          # check the content
          os.listdir('/sd')
          

          I commented os.mount() because it is already mounted. When I run this main.py file via REPL, it does not show anything. However running os.listdir('/sd') from REPL console it outputs 'test.txt'.

          The firmware version is 1.20.2.r4. The SD card is 64 GB, FAT32. The document says it supports up to 32 GB, that might be the issue?

          Also regarding your answer, my guess is I should manually upload those libraries to SD card. There is no way to install via REPL.

          H 1 Reply Last reply Reply Quote 0
          • H Offline
            hakan kayan @hakan kayan
            last edited by

            @hakan-kayan

            Update: I have tried with 32 GB SD card, the issue persists. The commands from main.py is ignored, but via REPL, they are working.

            M 1 Reply Last reply Reply Quote 0
            • M Offline
              Matthew Felgate 0 Global Moderator @hakan kayan
              last edited by Matthew Felgate 0

              @hakan-kayan
              Hi Hakan

              In code, for example main.py you would have to wrap it in a print statement:
              print(os.listdir('/sd'))

              Let me know if this works.

              Writing files to SD card you can do manually by putting the SD card in your computer.

              You could upload files to the board, then use REPL to move them to the SD card with the rename command:
              os.rename('test.py','sd/test.py')

              Matt

              H robert-hhR 2 Replies Last reply Reply Quote 1
              • H Offline
                hakan kayan @Matthew Felgate 0
                last edited by

                @Matthew-Felgate-0
                Thanks Mathew, I don't know how I missed print(os.listdir('/sd'))to be honest. I need some fresh air I guess.

                Regarding implementing Isolation Forest, for now it does not seem possible, as the library itself uses huge modules like Numpy. The discussion here also claims that is not possible import libraries like Numpy.

                I will be looking for a workaround, even though I am not very hopeful.

                1 Reply Last reply Reply Quote 0
                • robert-hhR Offline
                  robert-hh @Matthew Felgate 0
                  last edited by

                  @Matthew-Felgate-0 said in Using SD Card for the Whole Project.:

                  os.rename('test.py','sd/test.py')

                  Sorry no, that is not possible. You cannot move files between volumes by renaming. You have to copy them.

                  H 1 Reply Last reply Reply Quote 0
                  • H Offline
                    hakan kayan @robert-hh
                    last edited by

                    @robert-hh

                    Hi Robert, could you provide an example please?

                    robert-hhR 1 Reply Last reply Reply Quote 0
                    • robert-hhR Offline
                      robert-hh @hakan kayan
                      last edited by

                      @hakan-kayan An example of a copy function:

                      import os
                      
                      def cp(s, t):
                          try: 
                              if os.stat(t)[0] & 0x4000:  # is directory
                                  t = t.rstrip("/") + "/" + s
                          except OSError:
                              pass
                          with open(s, "rb") as s:
                              with open(t, "wb") as t:
                                  while True:
                                      l = s.read(512)
                                      if not l: break
                                      t.write(l)
                      

                      So can call that with:

                      from cp import cp
                      cp("source_name", "target_name")

                      if target_name is a directory, the target name will be create as target_name/source_name. So you could use:
                      cp("source_file", "/sd")
                      If you want to copy more than one file at once, you have to create a loop.

                      1 Reply Last reply Reply Quote 3

                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                      With your input, this post could be even better 💗

                      Register Login
                      • 1 / 1
                      • First post
                        Last post

                      Pycom on Twitter

                      Pycom