AmedeeBulle / pyrak811

RAK811 Python 3 library for Raspberry Pi
Apache License 2.0
51 stars 25 forks source link

Issues running RAK commands #31

Open AchrafAcheche opened 1 year ago

AchrafAcheche commented 1 year ago

Hi Guys, I recently bought the IoT LoRa Node pHAT, using with Raspberry Pi 4 Model B 8GB RAM. My OS Version : Raspbian GNU/Linux 10 (buster) For reference, I did follow this link, I have run in to an issue with the initial setup, it seems when running rak811 commands I'm getting errors, for example, image

image

Maybe my Phat and Paspberry not connected correctly ? image image

AmedeeBulle commented 1 year ago

If it is a new module, it has most probably a newer firmware (v3), and the instructions you follow are a bit outdated...

You can get your module firmware version with rak811 version.

If it is a V3, the README at the top of this repo should help you to proceed.

AchrafAcheche commented 1 year ago

I tried ,but it's the same problem image image

AchrafAcheche commented 1 year ago

rak811 version dosen't work image

AmedeeBulle commented 1 year ago

The script can open the device but doesn't get any response.

I don't have an RPi4 available for testing right now, but it looks like you have have Bluetooth assigned to /dev/serial0 instead of the RAK811 module.

Can you completely disable Bluetooth and try again? ( dtoverlay=disable-bt in /boot/config.txt should do the trick)

You should then have a single /dev/serial* device

AchrafAcheche commented 1 year ago

Same problem Thank you anyway Philippe

AchrafAcheche commented 1 year ago

One other question , does the phat have a led ?

AmedeeBulle commented 1 year ago

No, there is no led on the module...

I tested again on an RPi4, and this is what I have:

$ ls -l /dev/serial*
lrwxrwxrwx 1 root root 5 Nov  4  2021 /dev/serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 Nov  4  2021 /dev/serial1 -> ttyAMA0

The rak811 library uses serial0 (primary UART), as it is normally the one assigned to the GPIO header used by the module. In my configuration, the primary UART (serial0) is the Mini UART (UART1 aka ttyS0). This should be the default according to the documentation. In your first screenshot you have it as secondary... I guess you could have it working with the PL011 (UART0 aka ttyAMA0), but then you have to re-assign Bluetooth to the Mini UART (I haven't tried this...)

More reading about this: Configuring UARTs

I have attached my config.txt, but the only relevant line is:

enable_uart=1

With all that, I have a successful connection to the module:

 $ rak811v3 -d version
DEBUG:rak811.serial:Serial initialized
DEBUG:rak811.serial:Sending: >at+version\r\n<
DEBUG:rak811.serial:Received: >OK V3.0.0.14.H<
V3.0.0.14.H

Hope this helps to solve your issue.

Limocello commented 7 months ago

Hello There, I seem to face the same Issue with a Compute Module 4 and the Raspberry Pi zero LoRa HAT: https://learn.pi-supply.com/make/getting-started-with-the-raspberry-pi-lora-node-phat/ https://www.waveshare.com/wiki/CM4-IO-BASE-B#USB2.0 If I check for the Version (or do any other command)

image

The same Issue happens regardless of wether I use rak811 or rak811v3

I assume the issue comes from the fact that I'm using a HAT designed for a Raspberry Pi zero on a Raspberry Pi Compute Module 4, which from my knowledge has more UARTs and a different configuration (see attachement). I have the same UART assignement:

image

I've disabled the serial login shell, tried switching the UART interfaces, adding the dtoverlay=disable-bt, reimaging the Raspberry Pi without any signs of success. I'm at my wits end either there's something grave I'm overseeing, my module is broken or there's an issue with configuring raspberry Pi 4's.

One question remains, that could give me a lead: Basically I've also tried running rak811v3 version without the LoRa HAT attached and got the same timeout error. Whilst it completely makes sense, that this causes an error should it be the same error (rak811.serial.Rak811TimeoutError) or should there be a different error? Depending on this I might be able to conclude whether the Error lies within the software or if its actually my HAT that's not working.

I'd really appreciate any help even if its not with a compute module but a Raspberry Pi 4 as I've spent hours trying to fix this. Thank you very much in advance config.txt

billz commented 5 months ago

I also encountered this but found a solution... sharing it here in the event it helps someone else.

After disabling the serial console (or so I'd thought) using raspi-config, I ran into the same timeouts with ttyAMA0. I used the following to inspect the device file and found that agetty was using it:

$ ls -la /dev/serial0
lrwxrwxrwx 1 root root 7 Jun  1 06:27 /dev/serial0 -> ttyAMA0

$ sudo fuser -v /dev/ttyAMA0
                     USER        PID ACCESS COMMAND
/dev/ttyAMA0:        root        856 F.... agetty

Sure enough, the serial-getty@ttyAMA0 service was active:

$ sudo systemctl status serial-getty@ttyAMA0.service
● serial-getty@ttyAMA0.service - Serial Getty on ttyAMA0
     Loaded: loaded (/lib/systemd/system/serial-getty@.service; enabled-runtime; preset: enabled)
     Active: active (running) since Sun 2024-06-02 07:46:15 CEST; 6min ago
       Docs: man:agetty(8)
             man:systemd-getty-generator(8)
             https://0pointer.de/blog/projects/serial-console.html
   Main PID: 856 (agetty)
      Tasks: 1 (limit: 174)
        CPU: 8ms
     CGroup: /system.slice/system-serial\x2dgetty.slice/serial-getty@ttyAMA0.service
             └─856 /sbin/agetty -o "-p -- \\u" --keep-baud 115200,57600,38400,9600 - vt220

Jun 02 07:46:15 loranode2 systemd[1]: Started serial-getty@ttyAMA0.service - Serial Getty on ttyAMA0.

Stopping the service permits use of the serial port on ttyAMA0. Disabling it does not prevent it from restarting on boot, though. This is due to systemd's use of generators. The solution in this case is to mask the service to prevent it from ever being enabled:

$ sudo systemctl mask serial-getty@ttyAMA0.service

After doing this I had no issues using rak811 or rak811v3.

Environment:

Pi Zero 2 W with Debian 12/Raspberry Pi OS (64-bit) Lite bookworm.

$ uname -a
Linux loranode2 6.6.28+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.6.28-1+rpt1 (2024-04-22) aarch64 GNU/Linux
BryanNsoh commented 4 months ago

You are much appreciated billz! I wrote a script anyone can run and it does everything recommended across all the issues on this problem hope someone finds it helpful. also thanks AmedeeBulle for diligent replies!!

Here's the script that finally solved my problem. It performs all the checks and fixes recommended throughout this thread:

#!/bin/bash

# Comprehensive LoRa pHAT Fix Script
# This script performs checks and fixes for common LoRa pHAT issues

# Function to log messages
log_message() {
    echo "$1"
}

# Check and fix UART configuration
fix_uart_config() {
    if ! grep -q "enable_uart=1" /boot/config.txt; then
        log_message "Adding enable_uart=1 to /boot/config.txt"
        echo "enable_uart=1" | sudo tee -a /boot/config.txt
    fi

    if ! grep -q "dtoverlay=disable-bt" /boot/config.txt; then
        log_message "Adding dtoverlay=disable-bt to /boot/config.txt"
        echo "dtoverlay=disable-bt" | sudo tee -a /boot/config.txt
    fi
}

# Stop and disable serial-getty service
fix_serial_getty() {
    log_message "Stopping and disabling serial-getty@ttyAMA0.service"
    sudo systemctl stop serial-getty@ttyAMA0.service
    sudo systemctl disable serial-getty@ttyAMA0.service
    sudo systemctl mask serial-getty@ttyAMA0.service
}

# Check RAK811 version
check_rak_version() {
    log_message "Checking RAK811 version:"
    rak811 -v -d version || log_message "Failed to get RAK811 version"
    rak811v3 -v -d version || log_message "Failed to get RAK811v3 version"
}

# Perform hard reset
perform_hard_reset() {
    log_message "Performing hard reset:"
    rak811 -v -d hard-reset || log_message "Failed to perform hard reset"
}

# Reset LoRa
reset_lora() {
    log_message "Resetting LoRa:"
    rak811 -v -d reset lora || log_message "Failed to reset LoRa"
}

# Check for processes using ttyAMA0
check_ttyAMA0_usage() {
    log_message "Checking for processes using ttyAMA0:"
    sudo fuser -v /dev/ttyAMA0 || log_message "No processes found using ttyAMA0"
}

# Main execution
log_message "Starting Comprehensive LoRa pHAT Fix Script"

fix_uart_config
fix_serial_getty
check_rak_version
perform_hard_reset
reset_lora
check_ttyAMA0_usage

log_message "Checks and fixes applied. Please reboot your Raspberry Pi."
log_message "After reboot, run this script again to verify all issues are resolved."
log_message "If problems persist, please check physical connections and antenna configuration."
log_message "Remember: If using an external antenna, ensure the INT inductor is desoldered for best range."

# Prompt for reboot
read -p "Would you like to reboot now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
    sudo reboot
fi

To use this script:

  1. Save it to a file named lora_phat_fix.sh
  2. Make it executable: chmod +x lora_phat_fix.sh
  3. Run it with sudo: sudo ./lora_phat_fix.sh

This script will:

After running this script and rebooting, most common LoRa pHAT issues should be resolved. If anyone is still having trouble, they should check their physical connections and antenna configuration.