PiSupply / IoTLoRaRange

Repository for all of the IoT LoRa Range of Products
65 stars 11 forks source link

IoT LoRa pHAT node - Intermittent Serial communication problems. #33

Open peach-e opened 3 years ago

peach-e commented 3 years ago

I'm trying to follow the LoRA Node tutorial for Raspberry Pi here: https://learn.pi-supply.com/make/getting-started-with-the-raspberry-pi-lora-node-phat

I'm getting stuck during the hello world script due to the serial interface timing out. But not all the time. Sometimes it times out waiting for a response from a gateway (I don't have my own) and once or twice I got a handshake to appear on the TTN console.

Using

Steps to Reproduce

  1. Install the pHAT node onto the Pi as per tutorial.
  2. Enable serial but disable serial console, and set dtoverlay=pi3-miniuart-bt in /boot/config.txt as per tutorial.
  3. Install RAK811 library as per tutorial.
  4. Get Device EUI with commands:

    $ rak811 -vd hard-reset
    DEBUG:rak811.serial:Serial initialized
    Hard reset complete
    
    $ rak811 -vd get-config dev_eui
    DEBUG:rak811.serial:Serial initialized
    DEBUG:rak811.serial:Sending: >at+get_config=dev_eui\r\n<
    DEBUG:rak811.serial:Received: >OK34************14<
    34************14
  5. Create an Application on the V2 console for The Things Network.
    • Application ID & Description: Set
    • Handler: ttn-handler-eu (Automatically selected)
    • Application EUI: (Automatically chosen on v2 console.)
    • 1 Device:
      • Application ID: (Matches high-level application ID)
      • Device ID: (I typed something in)
      • Activation Method: OTAA
      • Device EUI: (Use my device EUI)
      • Application EUI: (Matches high-level application EUI)
      • App Key: (Auto Generated)
    • NOTES on V3 Console:
      • When adding device, use
        • OTAA
        • Specify LoRaWAN version MAC V1.0.2
        • Server nam1.cloud.thethings.network
        • Regional Parameters version PHY V1.0.2 REV B.
      • AppEUI is not automatically provided anymore. You make something up or make it match your old v2 app EUI.
      • Frequency Plan: US915 FSB 2
      • Regional Parameters Rev B
      • LoRaWAN Class Capabilities: Leave all unchecked.
        • However, I think I saw Rak811 Datasheet state that it supports Class C.
      • AppKey: Randomly generated
      • (All this stuff I got from the Migration Tutorial (https://www.thethingsnetwork.org/docs/the-things-stack/migrate-to-v3/migrate-using-console/)
  6. Create the lora_node.py script as per tutorial.
    • Notes:
      • from rak811 import Mode, Rak811 doesn't work any more. Now, you have to use from rak811.rak811 import Mode, Rak811.
      • Dev EUI, App Key, App EUI set according to how the TTN app configured.
      • Band: US915 (Because I live in Canada)
      • Additional channel masks set as per tutorial for US915, inserted between the lora.set_config(dev_eui, app_eui, app_key) and lora.join_otaa() commands.
      • Run chmod +x on the script.
  7. Run the script.

Expected Behavior

It either works, or maybe I'll get errors about talking to a gateway because I live a long ways away from one. (I bought the device for P2P communication primarily)

Observed Behavior

I get "Timeout while waiting for data" on the lora.join_otaa() step.

$ ./lora_node.py
Traceback (most recent call last):
  File "./lora_node.py", line 18, in <module>
    lora.join_otaa()
  File "/usr/local/lib/python3.7/dist-packages/rak811/rak811.py", line 411, in join_otaa
    self._send_command('join=otaa')
  File "/usr/local/lib/python3.7/dist-packages/rak811/rak811.py", line 237, in _send_command
    response = self._serial.receive()
  File "/usr/local/lib/python3.7/dist-packages/rak811/serial.py", line 156, in receive
    raise Rak811TimeoutError('Timeout while waiting for data')
rak811.serial.Rak811TimeoutError: Timeout while waiting for data

Run the script again. This time I get a similar timeout error on lora.mode = Mode.LoRaWan.

Okay, so I've got serial problems. But why did the first couple of commands go through? Let's try them again.

$ rak811 -vd get-config dev_eui
DEBUG:rak811.serial:Serial initialized
DEBUG:rak811.serial:Sending: >at+get_config=dev_eui\r\n<
RAK811 timeout: Timeout while waiting for data

Any ideas?

peach-e commented 3 years ago

I modified my lora_node.py script to print out the AT commands sent into the modem and the responses.

Script

#!/usr/bin/env python3
import logging
import sys

root = logging.getLogger()
root.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)

from rak811.rak811 import Mode, Rak811

lora = Rak811()
lora.hard_reset()
lora.mode = Mode.LoRaWan
lora.band = 'US915'
lora.set_config(dev_eui='34*********************14',
app_eui='70*********************06',
app_key='E8***************************14')

lora.set_config(ch_mask = '0,FF00')
lora.set_config(ch_mask = '1,0000')
lora.set_config(ch_mask = '2,0000')
lora.set_config(ch_mask = '3,0000')
lora.set_config(ch_mask = '4,0000')

lora.join_otaa()
lora.dr = 5
lora.send('Hello world')
lora.close()

Result

$ sudo ./lora_node.py
2021-06-09 12:29:07,355 - rak811.serial - DEBUG - Serial initialized
2021-06-09 12:29:09,377 - rak811.serial - DEBUG - Sending: >at+mode=0\r\n<
2021-06-09 12:29:10,597 - rak811.serial - DEBUG - Ignoring untagged: ><
2021-06-09 12:29:10,715 - rak811.serial - DEBUG - Ignoring untagged: >Selected LoraWAN 1.0.2 Region: US915 <
2021-06-09 12:29:10,820 - rak811.serial - DEBUG - Ignoring untagged: ><
2021-06-09 12:29:10,925 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:11,029 - rak811.serial - DEBUG - Sending: >at+band=US915\r\n<
2021-06-09 12:29:11,038 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:11,143 - rak811.serial - DEBUG - Sending: >at+set_config=dev_eui:34**********14&app_eui:70*********06&app_key:E8*********************14\r\n<
2021-06-09 12:29:12,366 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:12,471 - rak811.serial - DEBUG - Sending: >at+set_config=ch_mask:0,FF00\r\n<
2021-06-09 12:29:13,686 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:13,790 - rak811.serial - DEBUG - Sending: >at+set_config=ch_mask:1,0000\r\n<
2021-06-09 12:29:15,005 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:15,109 - rak811.serial - DEBUG - Sending: >at+set_config=ch_mask:2,0000\r\n<
2021-06-09 12:29:16,324 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:16,429 - rak811.serial - DEBUG - Sending: >at+set_config=ch_mask:3,0000\r\n<
2021-06-09 12:29:17,644 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:17,748 - rak811.serial - DEBUG - Sending: >at+set_config=ch_mask:4,0000\r\n<
2021-06-09 12:29:18,963 - rak811.serial - DEBUG - Received: >OK<
2021-06-09 12:29:19,068 - rak811.serial - DEBUG - Sending: >at+join=otaa\r\n<
Traceback (most recent call last):
  File "./lora_node.py", line 31, in <module>
    lora.join_otaa()
  File "/usr/local/lib/python3.7/dist-packages/rak811/rak811.py", line 411, in join_otaa
    self._send_command('join=otaa')
  File "/usr/local/lib/python3.7/dist-packages/rak811/rak811.py", line 237, in _send_command
    response = self._serial.receive()
  File "/usr/local/lib/python3.7/dist-packages/rak811/serial.py", line 157, in receive
    raise Rak811TimeoutError('Timeout while waiting for data')
rak811.serial.Rak811TimeoutError: Timeout while waiting for data

So it looks like that at+join=otaa command is gumming up the modem. Not even $ rak811 -vd reset lora will fix it; I actually have to power-cycle my Pi to get back to square 1.

Looking at the AT command reference here: https://downloads.rakwireless.com/LoRa/RAK811/Application_Notes/RAK811_AT_Command_Manual_V1.0.pdf

It looks like at+join does not accept any arguments. I'd investigate farther, but I'm having a heck of a time probing the serial port directly.

Question

Can someone please tell me a command that will let me talk to the modem with AT commands? I've been trying

$ picocom -b 115200 -d 8 -y n -p 1 /dev/serial0

But nothing appears in the console when I type things.

peach-e commented 3 years ago

All right. The correct serial command is

$ picocom --baud 115200 --echo --omap crcrlf /dev/serial0

And now I can run AT commands manually.

at+reset=0
OK
Welcome to RAK811

Selected LoraWAN 1.0.2 Region: US915 

at+mode=0

Selected LoraWAN 1.0.2 Region: US915 

OK
at+version
OK2.0.3.0
at+set_config=dev_eui:34*******14
OK
at+set_config=app_eui:70*********06
OK
at+set_config=app_key:E8*****************14
OK
at+join=otaa

It looks like I'm using the old version of the firmware, for which at+join=otaa is in fact a reasonable command (https://github.com/AmedeeBulle/pyrak811/blob/main/DOCS/RAK811%C2%A0LoRa%C2%A0AT%C2%A0Command%C2%A0V1.5.pdf).

But the command never returns it the modem doesn't get a lock on a gateway, so it just sits there without responding until I cycle the power. No wonder the serial interface was timing out.

Would upgrading the firmware help with this? I thought someone would have a gateway near here; the LoRa promotional material advertised a 10km radius and I'm in a populated area.