Sending data (LoRa MAC) between two LoPy's
I have two LoPy's. One I set as LoRa Sender and second as receiver.
I used the sample code from the pycom.io website: https://docs.pycom.io/lopy/lopy/quickref.html#lora-loramac
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?') 6
>>> from network import LoRa >>> import socket >>> lora = LoRa(mode=LoRa.LORA) >>> s = socket.socket(socket.AF_LORA, socket.SOCK_RAW) >>> s.recv(64) b'hello?'
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 pycom.heartbeat(False) # 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) s.setblocking(False) def togglerecieve(): print(button()) if button() == 1: if receive: receive = False else: 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 pycom.rgbled(code) time.sleep(0.2) pycom.rgbled(0) def receiver(): if recieve: # get any data received... data = s.recv(64) if "Hello" in data: led(0) response = "data received, " + str(count) print(response) blinkrgb('green') time.sleep(1) else: led(1) blinkrgb('red') time.sleep(0.5) while True: blinkrgb('blue') count = count + 1 togglerecieve() receiver()
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
bytesobjects. 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 https://github.com/lonetech/LoPy/blob/master/lopychat.py also.
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).