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

    Lopy sd card

    Scheduled Pinned Locked Moved LoPy
    lopysdcard
    17 Posts 5 Posters 7.2k Views 5 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.
    • L Offline
      livius @bessonf
      last edited by

      @bessonf
      If you look at my sample in that post you found how to "boot" from SD card
      You must mout your SD card in boot.py and then exec file from sd card

      for file operation use help in python
      https://docs.python.org/3/tutorial/inputoutput.html

      e.g.: https://docs.pycom.io/pycom_esp32/library/machine.SD.html

      from machine import SD
      import os
      
      sd = SD()
      os.mount(sd, '/sd')
      
      # check the content
      os.listdir('/sd')
      
      # try some standard file operations
      f = open('/sd/test.txt', 'w')
      f.write('Testing SD card write operations')
      f.close()
      f = open('/sd/test.txt', 'r')
      f.readall()
      f.close()
      

      think about people first, then computers

      A B 2 Replies Last reply Reply Quote 0
      • B Offline
        bessonf
        last edited by

        No it doesn't work ... In fact the execution of the file fails when loading the SD library, it doesn't recognize it. I use the LoPy , do I need to upgrade the LoPy or something else ?

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

          @bessonf

          1. What is your SD card size?
          2. And what error do you got and on which line of code?
          3. Do you use expansion board or direct connect?

          think about people first, then computers

          B 1 Reply Last reply Reply Quote 0
          • B Offline
            bessonf @livius
            last edited by

            @livius

            1. The card makes 32GB.
            2. The error is :

            Traceback (most recent call last):
            File "main.py", line 1, in <module>
            ImportError: cannot import name SD

            1. I use an expansion board.
            L 1 Reply Last reply Reply Quote 0
            • L Offline
              livius @bessonf
              last edited by livius

              @bessonf
              strange
              but try this and back with results - i have only Wipy and can not check this on Lopy:

              import machine
              help(machine)
              
              sd = machine.SD()
              

              and what is your firmware?

              os.uname()
              

              think about people first, then computers

              B 1 Reply Last reply Reply Quote 0
              • B Offline
                bessonf @livius
                last edited by livius

                @livius

                For help(machine) :

                 help(machine)
                <module 'umachine'>object  is of type module
                  __name__ -- umachine
                  mem8 -- <8-bit memory>
                  mem16 -- <16-bit memory>
                  mem32 -- <32-bit memory>
                  reset -- <function>
                  freq -- <function>
                  unique_id -- <function>
                  main -- <function>
                  rng -- <function>
                  idle -- <function>
                  sleep -- <function>
                  deepsleep -- <function>
                  reset_cause -- <function>
                  wake_reason -- <function>
                  disable_irq -- <function>
                  enable_irq -- <function>
                  Pin -- <class 'Pin'>
                  UART -- <class 'UART'>
                  SPI -- <class 'SPI'>
                  I2C -- <class 'I2C'>
                  PWM -- <class 'PWM'>
                  ADC -- <class 'ADC'>
                  DAC -- <class 'DAC'>
                  PWRON_RESET -- 0
                  SOFT_RESET -- 4
                

                And for os.uname() :

                >>> os.uname()
                (sysname='LoPy', nodename='LoPy', release='1.0.0.b1', version='v1.8.6-237-g9d21f17 on 2016-12-15', machine='LoPy with ESP32')
                
                L 1 Reply Last reply Reply Quote 0
                • L Offline
                  livius @bessonf
                  last edited by livius

                  @bessonf
                  you must upgrade your board firmware
                  current version is
                  release='1.6.9.b1'
                  https://www.pycom.io/resources/

                  you have really old one :release='1.0.0.b1

                  think about people first, then computers

                  B 1 Reply Last reply Reply Quote 0
                  • B Offline
                    bessonf @livius
                    last edited by

                    @livius
                    It works thank you very much. I work on the LoPy for 2 weeks and I completely missed this ...

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      assia @livius
                      last edited by

                      Hi @livius said in Lopy sd card:

                      You must mout your SD card in boot.py
                      you mean that we put in boot.py this:

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

                      and in the main.py we put the rest ?
                      because i try it but it does not work i get an OSError in :

                      f = open('/sd/test.csv', 'a')
                      

                      my main.py is in /flash and i have a csv file in /sd that I want to fill it every time.

                      thanks

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

                        @assia
                        Which error do you get?
                        And what you get after:

                        os.listdir('/sd')
                        

                        think about people first, then computers

                        A 1 Reply Last reply Reply Quote 0
                        • A Offline
                          assia @livius
                          last edited by assia

                          @livius

                          Which error do you get?

                          Traceback (most recent call last):
                          File "<stdin>", line 1, in <module>
                          File "main.py", line 9, in <module>
                          OSError: [Errno 2] ENOENT

                          and line 9 is: f = open('/sd/test.csv', 'a')

                          And what you get after:
                          os.listdir('/sd')

                          I get empty list

                          J 1 Reply Last reply Reply Quote 0
                          • J Offline
                            jmarcelino @assia
                            last edited by

                            @assia
                            What's the storage size of the SD card?

                            Larger cards tend to be pre-formatted as exFAT which is not enabled in the default Micropython configuration (as Microsoft demands a patent license)

                            A 1 Reply Last reply Reply Quote 0
                            • A Offline
                              assia @jmarcelino
                              last edited by

                              Hi @jmarcelino the storage size is 2 Go and i had formatted it as FAT32.
                              I have already managed to write in this card by executing the commands directly into the pycom console in pymakr but now as I want to execute it in main.py I do not succeed.
                              thanks

                              J 1 Reply Last reply Reply Quote 0
                              • J Offline
                                jmarcelino @assia
                                last edited by

                                @assia
                                Thanks, so f = open('/sd/test.csv', 'a') works from the REPL (python console) but not in main.py?

                                Does changing 'a' to 'w' in the open(...) mode change anything (other than that the file will be empty again with each open)

                                A 1 Reply Last reply Reply Quote 0
                                • A Offline
                                  assia @jmarcelino
                                  last edited by

                                  @jmarcelino
                                  I put the code in main.py :

                                  from machine import SD
                                  import os
                                  sd = SD()
                                  os.mount(sd, '/sd')
                                  f = open('/sd/test.csv', 'a')
                                  

                                  it worked but just the first time also in REPL and when I restart my lopy I get:
                                  OSError in os.mount(sd, '/sd') like:
                                  OSError: the requested operation is not possible

                                  I suppose that the SDcard remains mounted and we can not mount it a second time in this case we must check if it is already mounted and then writing on it

                                  1 Reply Last reply Reply Quote 0
                                  • B Offline
                                    barsznica @livius
                                    last edited by

                                    This post is deleted!
                                    1 Reply Last reply Reply Quote 0

                                    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