[SOLVED] LoPy SD card data saving



  • 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-hh f.flush() works just fine. Thank you a lot! It really helped me !



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



  • @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 ???



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



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



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



  • @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.



  • @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



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


 

Hello World?

Pylife on Kickstarter - November 2018








Back Us On Kickstarter >

Pycom on Twitter