Author Topic: Send is blocking forever  (Read 1670 times)

scttnlsn

  • NewMember
  • *
  • Posts: 3
Send is blocking forever
« on: March 28, 2018, 10:11:28 AM »
I'm using an RFM69HW with a bare Atmega328p running at 8mHz. I'm able to communicate with the module just fine but calls to send or sendWithRetry seems to block forever. Any ideas about what might be going on?

Here's the code I'm running:

Code: [Select]
#include <Arduino.h>
#include <avr/sleep.h>
#include <RFM69.h>
#include <RFM69_ATC.h>
#include <SPI.h>

#define FREQUENCY RF69_915MHZ
#define SECRET "ABCDEFGHIJKLMNOP"
#define ATC_RSSI -75
#define NETWORK_ID 100
#define NODE_ID 2
#define RECEIVER_ID 1

#define BUTTON_PIN 3

RFM69_ATC radio;

static volatile uint8_t button_pressed = 0;

void isr() {
  sleep_disable();
  detachInterrupt(digitalPinToInterrupt(BUTTON_PIN));
  button_pressed = 1;
}

void sleep() {
  //radio.sleep();

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  noInterrupts();
  sleep_enable();
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), isr, FALLING);
  interrupts();

  // sleep
  sleep_cpu();

  // wake
  sleep_disable();
}

void setup() {
  Serial.begin(115200);

  // button
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  // radio initialization
  if (!radio.initialize(FREQUENCY, NODE_ID, NETWORK_ID)) {
    Serial.println("error: could not initialize RFM69");
    while (1);
  }

  radio.encrypt(SECRET);
  radio.setHighPower();
  radio.enableAutoPower(ATC_RSSI);

  Serial.println("ready");
}

void loop() {
  Serial.println("loop");

  if (button_pressed) {
    button_pressed = 0;

    Serial.println("sending...");
    radio.sendWithRetry(RECEIVER_ID, "hello", 5);
    Serial.println("sent");

    // put radio in RX mode
    radio.receiveDone();
  }

  Serial.println("sleep");
  Serial.flush();
  sleep();
}

The receiver does receive the sent message but only the first once since the sender locks up after the first send.

Here's what I see on a serial console:

Code: [Select]
ready
loop
sleep
loop
sending...

I'm pressing the button between the "sleep" and the second "loop" line.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Send is blocking forever
« Reply #1 on: March 28, 2018, 10:53:06 AM »
Try adding Serial.flush(); after:
Code: [Select]
    radio.sendWithRetry(RECEIVER_ID, "hello", 5);
    Serial.println("sent");
Just to make sure that you're hanging in send... rather than receiveDone.

scttnlsn

  • NewMember
  • *
  • Posts: 3
Re: Send is blocking forever
« Reply #2 on: March 28, 2018, 05:10:40 PM »
I added `Serial.flush()` after all the `println` calls and the result is the same-  stuck inside `sendWithRetry`.

scttnlsn

  • NewMember
  • *
  • Posts: 3
Re: Send is blocking forever
« Reply #3 on: March 28, 2018, 08:38:01 PM »
I came across this post which describes a very similar situation: https://lowpowerlab.com/forum/rf-range-antennas-rfm69-library/rfm69hw-hangs-every-time-(solved-bad-multimeter)/msg11237/

Unfortunately the solution there does not apply in my case.  I have tried various power sources but still seem to encounter this issue.  Most of my experimentation so far has been using 2 AAA cells to provide the circuit with 3V.  I've checked the voltage while sleeping vs. sending it it remains constant around 3V.  Looking in the datasheet for the RFM69HCW it looks like this supply voltage should be fine.