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

    [SOLVED] LoPy SD card data saving

    Scheduled Pinned Locked Moved LoPy
    lopysd cardlora
    10 Posts 2 Posters 1.4k Views 2 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.
    • IplookyI Offline
      Iplooky
      last edited by Iplooky

      Hello,

      I have implemented a LoRa network and I have a station that I want to save the data recieved it on SD card. The problem (so far) is that it only saves the last data recieved ( it overwrites ... ). Code:

      from network import LoRa
      import socket
      import time
      import pycom
      from machine import RTC
      from machine import SD
      import os
      
      lora = LoRa(mode=LoRa.LORA, rx_iq=True, sf=12, frequency=869000000)
      s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
      s.setblocking(False)
      
      sd = SD()
      os.mount(sd,'/sd')
      os.listdir('/sd')
      
      pycom.heartbeat(False)
      f = open('/sd/NewDir','w')
      print('Starting up... ')
      while True:
          data=s.recv(64)
          data_2=data.decode("utf-8")
          if data != b'':
              print(data_2);
              f.write(data_2)
              f.close()
      
      
              pycom.rgbled(0xffffff)
              time.sleep(1)
              pycom.heartbeat(False)
      

      I do think that is a f.close() issue, I don't think that I should close the NotePad created. Any advice please ?

      robert-hhR 1 Reply Last reply Reply Quote 1
      • robert-hhR Offline
        robert-hh @Iplooky
        last edited by

        @iplooky You can open the file in append mode 'a' instead 'w'.

        IplookyI 1 Reply Last reply Reply Quote 2
        • IplookyI Offline
          Iplooky @robert-hh
          last edited by Iplooky

          @robert-hh thanks for the quick reply, so:

          At the second reception i have

          Traceback (most recent call last):
            File "<stdin>", line 26, in <module>
          OSError: [Errno 22] EINVAL
          

          Note that line 26 is the f.close() one

          robert-hhR 1 Reply Last reply Reply Quote 1
          • robert-hhR Offline
            robert-hh @Iplooky
            last edited by

            @iplooky You may write:

            f.write(data_2 + "\r\n")
            

            to get text file line ending. Anmd since you closed the file after writing, you cannot write to it any mode. This you get the error message. So better remove the close() and use with instead of the open() call.

            from network import LoRa
            import socket
            import time
            import pycom
            from machine import RTC
            from machine import SD
            import os
            
            lora = LoRa(mode=LoRa.LORA, rx_iq=True, sf=12, frequency=869000000)
            s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
            s.setblocking(False)
            
            sd = SD()
            os.mount(sd,'/sd')
            os.listdir('/sd')
            
            pycom.heartbeat(False)
            print('Starting up... ')
            with open('/sd/NewDir','a') as f:
                while true:
                    data=s.recv(64)
                    data_2=data.decode("utf-8")
                    if data != b'':
                        print(data_2);
                        f.write(data_2)
            
                        pycom.rgbled(0xffffff)
                        time.sleep(1)
                        pycom.heartbeat(false)
            

            That ensire that the file is closed, when you leave the with clause.

            IplookyI 1 Reply Last reply Reply Quote 0
            • IplookyI Offline
              Iplooky @robert-hh
              last edited by

              @robert-hh sorry, it does not write anymore in the NotePad, it is blank. Either with append or write mode

              robert-hhR 2 Replies Last reply Reply Quote 1
              • robert-hhR Offline
                robert-hh @Iplooky
                last edited by

                @iplooky Are you sure that you receive anything? What does the
                print(data_2)
                statement show.

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

                  @iplooky And also: how do you terminate the while True loop. Without proper termination, the file is not closed and the state is undefined.

                  1 Reply Last reply Reply Quote 0
                  • IplookyI Offline
                    Iplooky
                    last edited by

                    @robert-hh Yes, I am very sure that I recive data. It shows on terminal and besides that the LED blinks (it enters the "if" condition).

                    Solved it: the problem was that I didn't close the folder...but how should I close it when I don't know when I would disconnect the board to read the SD data ???

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

                      @iplooky you can add a
                      f.flush()
                      after the file write. That forces the buffered data to be written to the file.

                      IplookyI 1 Reply Last reply Reply Quote 0
                      • IplookyI Offline
                        Iplooky @robert-hh
                        last edited by

                        @robert-hh f.flush() works just fine. Thank you a lot! It really helped me !

                        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