Sending data (LoRa MAC) between two LoPy's

  • Pybytes Beta

    I have two LoPy's. One I set as LoRa Sender and second as receiver.
    I used the sample code from the website:

    I changed the sending part to:

    (Somehow the rendering of this page is not well, so indent is gone below).

    s.send('Hello from LoPy!')

    I changed the receiving part to:

    # get the data...
    data = s.recv(16*8)
    if data[0:5] == 'Hello':
    pycom.rgbled(0xff00) # make the LED light up in green color

    And what happens? Nothing.

    I did some debugging and find out the receiver only gets: b" (endless).

    What did I wrong?

  • @gertjanvanhethof you are working at a lower level than strings.

    In python 3.0 and later strings and raw bytes were separated to allow better control of string encoding with unicode. In general your "Hello" is a python unicode string and the sockets work on bytes. send and recv work on bytes, not strings.

    It looks like send did not error out on the data type, So it must be coping with unicode strings some how...

    so for a quick hack...

    if b'Hello' in data:
        print("Got my data!")

    For a better change, explicitly convert your strings at the appropriate level in your code.
    Like theses changes.
    Change your code to the following in the server.
    s.send('Hello from LoPy!'.encode('utf-8))

    Change your receive code

    My test sessions in the repl.


    >>> from network import LoRa
    >>> import socket
    >>> lora = LoRa(mode=LoRa.LORA)
    >>> s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    >>> s.set
    settimeout      setblocking
    >>> s.setblocking(False)
    >>> s.send(b'hello?')


    >>> from network import LoRa
    >>> import socket
    >>> lora = LoRa(mode=LoRa.LORA)
    >>> s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    >>> s.recv(64)

  • yes it does not work for you because you receive the message as byte, my solution for the first connection test looks like this:

    from network import LoRa
    from machine import Pin
    import socket
    import time
    import pycom
    led = Pin('G16', mode=Pin.OUT, value=1)
    button = Pin('G17', mode=Pin.IN, pull=Pin.PULL_UP)
    recieve = True
    count = 0
    # Initialize LoRa in LORA mode more params can be given, like frequency, tx power, spreading factor, etc
    lora = LoRa(mode=LoRa.LORA)
    # create a raw LoRa socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    def togglerecieve():
        if button() == 1:
            if receive:
                receive = False
                receive = True
    def blinkrgb(color):
        code = 0x0000FF
        if color == 'red':
            code = 0xFF0000 #RED
        if color == 'green':    
            code = 0x00FF00 #GREEN
        if color == 'blue':
            code = 0x0000FF #BLUE
    def receiver():
        if recieve:
            # get any data received...
            data = s.recv(64)
            if "Hello" in data:
                response = "data received, " + str(count)
    while True:
        count = count + 1

    I'm just checking if "Hello" is in the received message.
    First range test was up to 800meters, btw

  • @gertjanvanhethof Next guess then (sorry for not noting it earlier): I've only transferred bytes objects. Perhaps send doesn't like str, and even if it did, b'Hello' doesn't compare equal to 'Hello'. Feel free to try my clumsy test script at also.

  • Pybytes Beta

    I tried that (64) in the first place. So i tried another value which should include the whole 'Hello' string.
    But still I get b" output.

    So could there something others causing it?

  • @gertjanvanhethof 16*8 is 128. The only example I've seen uses 64, and in our testing it looks like any attempt to send more doesn't send anything at all (select doesn't return on the receiving side, and that doesn't have a size clue). So try recv(64).


Hello World?

Pylife on Kickstarter - November 2018

Back Us On Kickstarter >

Pycom on Twitter