adafruit / Adafruit_CircuitPython_TinyLoRa

Adafruit Tiny LoRa
MIT License
44 stars 44 forks source link

RFM9x, RPi Zero W, Adafruit Blinka, and TinyLoRa failure #27

Closed ctday closed 4 years ago

ctday commented 4 years ago

I have a newly purchased RFM9x bonnet on a new RPi Zero W, and am trying to run the tinylora_simpletest.py program. This fails every time with the claim that the rfm is the wrong version. Basically, the program attempts to read register 0x42 of the rfm, expecting 0x12 to be returned. It appears that the read never happens.

Here is Python3 debug session isolating the problem:

pi@LoRa-Edge:~$ ls pycache rfm9x_check.py rfm9x_transmit.py tinylora_simpletest_si7021.py font5x8.bin rfm9x_rpi_interrupt.py tinylora_simpletest.py tinylora_simpletest_single_channel.py

pi@LoRa-Edge:~$ python3 tinylora_simpletest.py

/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/spi.py(55)write() -> self._spi.writebytes2(buf[start:end]) (Pdb) w /home/pi/tinylora_simpletest.py(36)() -> lora = TinyLoRa(spi, cs, irq, rst, ttn_config) /home/pi/.local/lib/python3.7/site-packages/adafruit_tinylora/adafruit_tinylora.py(159)init() -> self._version = self._read_u8(_REG_VERSION) /home/pi/.local/lib/python3.7/site-packages/adafruit_tinylora/adafruit_tinylora.py(370)_read_u8() -> self._read_into(address, self._BUFFER, length=1) /home/pi/.local/lib/python3.7/site-packages/adafruit_tinylora/adafruit_tinylora.py(363)_read_into() -> device.write(self._BUFFER, end=1) /home/pi/.local/lib/python3.7/site-packages/busio.py(207)write() -> return self._spi.write(buf, start, end) /home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/spi.py(55)write() -> self._spi.writebytes2(buf[start:end]) (Pdb) ll 43 def write(self, buf, start=0, end=None): 44 if not buf: 45 return 46 if end is None: 47 end = len(buf) 48 try: 49 self._spi.open(self._port, 0) 50 self.set_no_cs() 51 self._spi.max_speed_hz = self.baudrate 52 self._spi.mode = self.mode 53 self._spi.bits_per_word = self.bits 54 breakpoint() 55 -> self._spi.writebytes2(buf[start:end]) 56 self._spi.close() 57 except FileNotFoundError as not_found: 58 print("Could not open SPI device - check if SPI is enabled in kernel!") 59 raise (Pdb) buf[0] 66 (Pdb) buf[1] 0 (Pdb) s /home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/spi.py(56)write() -> self._spi.close() (Pdb) ll 43 def write(self, buf, start=0, end=None): 44 if not buf: 45 return 46 if end is None: 47 end = len(buf) 48 try: 49 self._spi.open(self._port, 0) 50 self.set_no_cs() 51 self._spi.max_speed_hz = self.baudrate 52 self._spi.mode = self.mode 53 self._spi.bits_per_word = self.bits 54 breakpoint() 55 self._spi.writebytes2(buf[start:end]) 56 -> self._spi.close() 57 except FileNotFoundError as not_found: 58 print("Could not open SPI device - check if SPI is enabled in kernel!") 59 raise (Pdb) buf[0] 66 (Pdb) buf[1] 0 (Pdb)

The relevant point is that the SpiDev device’s method self._spi.writebytes2(buf[start:end]) doesn’t work. In fact, it doesn’t even appear in dir(self._spi). I assume the actual code of this method is supposed to be in the spidev.cpython-37m-arm-linux-gnueabihf.so file coming from the spi.cc C++ code somewhere; I can’t find it.

I installed all the python locally as a user. Hers what I’ve go.

pi@LoRa-Edge:~$ ls -a .local/lib/python3.7/site-packages/ . adafruit_circuitpython_si7021-3.1.5.dist-info neopixel_write.py .. adafruit_circuitpython_ssd1306-2.7.3.dist-info pulseio.py Adafruit_Blinka-4.1.0.dist-info adafruit_circuitpython_tinylora-2.0.2.dist-info pyftdi Adafruit_PlatformDetect-2.3.0.dist-info adafruit_framebuf.py pyftdi-0.44.2.dist-info Adafruit_PureIO adafruit_platformdetect pyserial-3.4.dist-info Adafruit_PureIO-1.0.4.dist-info adafruit_rfm9x.py pyusb-1.0.2.dist-info RPi adafruit_si7021.py rpi_ws281x RPi.GPIO-0.7.0.dist-info adafruit_ssd1306.py rpi_ws281x-4.2.3.dist-info pycache adafruit_tinylora serial _rpi_ws281x.cpython-37m-arm-linux-gnueabihf.so analogio.py spidev-3.4.dist-info adafruit_blinka bitbangio.py spidev.cpython-37m-arm-linux-gnueabihf.so adafruit_bus_device board.py sysv_ipc-1.0.1.dist-info adafruit_circuitpython_bitmap_font-1.0.5.dist-info busio.py sysv_ipc.cpython-37m-arm-linux-gnueabihf.so adafruit_circuitpython_busdevice-4.2.1.dist-info digitalio.py usb adafruit_circuitpython_framebuf-1.2.2.dist-info microcontroller adafruit_circuitpython_rfm9x-1.2.2.dist-info micropython.py pi@LoRa-Edge:~$

Help or pointers would be appreciated. Thank you.

ctday commented 4 years ago

Sorry about the strike-through.

ladyada commented 4 years ago

first, make sure you have the most recent version of all python libraires - you can use https://stackoverflow.com/questions/2720014/how-to-upgrade-all-python-packages-with-pip or similar then, run the default examples and post the output third, check your soldering, and verify you have nothing else attached on SPI

jerryneedell commented 4 years ago

FYI -- I just verified that TinyLora_simpletest.py works with a Pi ZeroW and rfm95 bonnet Did you try the rfm9x_radio_check.py? What pins are you using? I use

cs = digitalio.DigitalInOut(board.CE1)
irq = digitalio.DigitalInOut(board.D25)
rst = digitalio.DigitalInOut(board.D17)

also - I removed the use of the D13 LED for the pi. code below.

Here are the installed libraries:

-------------------------------- ---------
Adafruit-Blinka                  4.1.0    
adafruit-circuitpython-busdevice 4.0.1    
adafruit-circuitpython-framebuf  1.2.1    
adafruit-circuitpython-rfm9x     1.2.2    
adafruit-circuitpython-ssd1306   2.7.0    
adafruit-circuitpython-tinylora  2.0.2    
adafruit-io                      2.4.0    
Adafruit-PlatformDetect          2.3.0    
Adafruit-PureIO                  1.0.4    
spidev                           3.4 

code run with keys REDACTED

import time
import busio
import digitalio
import board
from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa

# Board LED
#led = digitalio.DigitalInOut(board.D13)
#led.direction = digitalio.Direction.OUTPUT

spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

cs = digitalio.DigitalInOut(board.CE1)
irq = digitalio.DigitalInOut(board.D25)
rst = digitalio.DigitalInOut(board.D17)

# TNN Configuration
# always 4b, msb
devaddr = bytearray([REDACTED])
# always 16b, msb 
app   = bytearray([REDACTED])
# always 16b, msb
nwkey = bytearray([REDACTED])

ttn_config = TTN(devaddr, nwkey, app, country='US')

lora = TinyLoRa(spi, cs, irq, rst, ttn_config)

while True:
    data = bytearray(b"\x43\x57\x54\x46")
    print('Sending packet...')
    lora.send_data(data, len(data), lora.frame_counter)
    print('Packet sent!')
#    led.value = True
    lora.frame_counter += 1
    time.sleep(1)
#    led.value = False
ctday commented 4 years ago

No soldering; ordered rmf9x with headers installed. Nothing but the bonnet on RPiZero W.

All installed Python modules:

pi@LoRa-Edge:~$ pip3 list Package Version


Adafruit-Blinka 4.1.0
adafruit-circuitpython-bitmap-font 1.0.5
adafruit-circuitpython-busdevice 4.2.1
adafruit-circuitpython-framebuf 1.2.2
adafruit-circuitpython-rfm9x 1.2.2
adafruit-circuitpython-si7021 3.1.5
adafruit-circuitpython-ssd1306 2.7.3
adafruit-circuitpython-tinylora 2.0.2
Adafruit-PlatformDetect 2.3.0
Adafruit-PureIO 1.0.4
asn1crypto 0.24.0
certifi 2018.8.24 chardet 3.0.4
cryptography 2.6.1
entrypoints 0.3
idna 2.6
keyring 17.1.1
keyrings.alt 3.1.1
pip 18.1
pycrypto 2.6.1
pyftdi 0.44.2
PyGObject 3.30.4
pyserial 3.4
python-apt 1.8.4.1
pyusb 1.0.2
pyxdg 0.25
requests 2.21.0
rpi-ws281x 4.2.3
RPi.GPIO 0.7.0
SecretStorage 2.3.1
setuptools 40.8.0
six 1.12.0
spidev 3.4
ssh-import-id 5.7
sysv-ipc 1.0.1
urllib3 1.24.1
wheel 0.32.3
pi@LoRa-Edge:~$

I don’t know where rfm9x_radio_check.py is.

pi@LoRa-Edge:~$ python3 tinylora_simpletest.py Traceback (most recent call last): File "tinylora_simpletest.py", line 36, in lora = TinyLoRa(spi, cs, irq, rst, ttn_config) File "/home/pi/.local/lib/python3.7/site-packages/adafruit_tinylora/adafruit_tinylora.py", line 161, in init raise TypeError("Can not detect LoRa Module. Please check wiring!") TypeError: Can not detect LoRa Module. Please check wiring! pi@LoRa-Edge:~$

jerryneedell commented 4 years ago

it is in the guide https://learn.adafruit.com/adafruit-radio-bonnets/rfm9x-raspberry-pi-setup

ctday commented 4 years ago

Oh, you gave it a different name last time. Ok, that test has no console output, but “RFM9x: Detected” appears on the rfm9x screen, and all the buttons work.

jerryneedell commented 4 years ago

Sounds good. Can you post the code that is failing? Did you set the pins correctly in tinylora_simpletest?

ctday commented 4 years ago

I took the pins as set in the test program. Besides, those are supposed to be set by the rfm9x board module.

I have proper LoRaWAN keys, but that shouldn’t effect the failure which occurs earlier than any use of them.

pi@LoRa-Edge:~$ less tinylora_simpletest.py import time import busio import digitalio import board from adafruit_tinylora.adafruit_tinylora import TTN, TinyLoRa

Board LED

led = digitalio.DigitalInOut(board.D13) led.direction = digitalio.Direction.OUTPUT

spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

RFM9x Breakout Pinouts

cs = digitalio.DigitalInOut(board.D5) irq = digitalio.DigitalInOut(board.D6) rst = digitalio.DigitalInOut(board.D4)

Feather M0 RFM9x Pinouts

cs = digitalio.DigitalInOut(board.RFM9X_CS)

irq = digitalio.DigitalInOut(board.RFM9X_D0)

rst = digitalio.DigitalInOut(board.RFM9X_RST)

TTN Device Address, 4 Bytes, MSB

devaddr = bytearray([0x00, 0x00, 0x00, 0x00])

TTN Network Key, 16 Bytes, MSB

nwkey = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])

TTN Application Key, 16 Bytess, MSB

app = bytearray([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])

ttn_config = TTN(devaddr, nwkey, app, country='US')

lora = TinyLoRa(spi, cs, irq, rst, ttn_config)

while True: data = bytearray(b"\x43\x57\x54\x46") print('Sending packet...') lora.send_data(data, len(data), lora.frame_counter) print('Packet sent!') led.value = True lora.frame_counter += 1 time.sleep(1) led.value = False

jerryneedell commented 4 years ago

Those are not correct for the bonnet on a Raspberry Pi. They are for a different MCU board. Use the ones I posted above.

jerryneedell commented 4 years ago

Compare the settings in the "rfm9x_check" program to the ones in tinytora_simpletest.py The rfm9x_check works... those are the correct pins for the bonnet on a Raspberry Pi. tinylora_simpltest.py also uses the RST pin, not used in the rfm9x_check.py

and there is no D13 LED on a raspberry Pi.

ctday commented 4 years ago

Ok. Would be nice if examples worked.

jerryneedell commented 4 years ago

Is it working for you now? Is it OK to close this issue?

ctday commented 4 years ago

Working. You can close.

jerryneedell commented 4 years ago

Don't let this discourage you. The tinylora examples were not written for the bonnet/Raspberry Pi. I think that is why you have found them confusing. They were written for a more general case of using an RFM9x breakout board (or featherwing) with a microcontroller.

jerryneedell commented 4 years ago

Glad it is working -- good luck with your projects.