LoRa on 2 LOPYs plus serial



  • Dear All,

    I need your help. I am working on connection serial -lopy-lopy-serial.
    I need to send an alert to 1 lopy via serial and than forward it to a second lopy via LoRa.
    Since I want to be sure that the whole alert has been sent it has to finish with special character \n. It works well till i am using LoRa, where what I am getting on second lopy is my alert but without \n. Could you help me to understand what i am doing wrong?
    Computer code is this one:

    import os
    import serial
    import time
    
    
    ser = serial.Serial("/COM6", 115200, timeout = 10)
    alert0 = 'message' + '\n'
    alert = alert0.encode('utf-8')
    ser.write(bytearray(alert))
            
    ser.close()
            
    time.sleep(3)
    

    First lopy:

    from machine import UART
    from network import LoRa
    import socket
    import pycom
    import time
    
    uart = UART(1, baudrate=115200, pins = ('P20', 'P21'), timeout_chars=2000)
    lora = LoRa(mode=LoRa.LORA, frequency=863000000)
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setblocking(False)
    
    while True:
        data = uart.read(1000)
        print ('.')
        if (data is not None) and ('\n' in (data.decode('utf-8'))):
            print(data)
            s.send(data)
        final = s.recv(64)
        print(final)
        time.sleep_ms(1000)
    

    and second lopy, where I receive the alert via LoRa and save it on SD is here:

    from network import LoRa
    import socket
    import pycom
    import time
    
    lora = LoRa(mode=LoRa.LORA, frequency=863000000)
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setblocking(False)
    
    while True:
        data1 = s.recv(64)
        if len(data1) > 0:
            s.send('data here')
            with open('/sd/lora_alert.txt','ab') as f:
                f.write(data1)
        else:
            s.send('no alerts')
        time.sleep(2)
    

    and here i got stuck as what i get on SD is not what I have sent...



  • @monersss
    Can you post a sample of the file stored on the SD card? I programmed the first lopy to constantly send hello, and this was my result:

    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    Hello
    ...
    


  • @seb Sorry, I have missed this part of the code. Second lopy has this code in main, together with importing .py file torub LoRa , so here everythins works well, The message is stored on SD but without \n at the end....



  • Hi,

    Looking over your code I noticed you do not mount the SD card, please add this code:

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

    I just tried this code running on two lopys with the latest firmware and everything seems to work as expected.



  • @livius maybe You could help me. .please... :)



Pycom on Twitter