Author Topic: Adafruit Feather Reciver and 2 Nodes (the nodes crash)  (Read 4409 times)

skl

  • NewMember
  • *
  • Posts: 6
  • Country: dk
Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« on: October 18, 2016, 03:32:07 AM »
My setup works fine with one node and one reciver, but when i power on the secound node, and it fires the first radio packet, it makes the other node crash.

i have tryed to use send instead of sendwithretry, with and without encryption... but as long as i use one node, it works fine.. do you guys have an idea of where to look?

Regards Søren Klein

Code: [Select]
//Receiver

#define NETWORKID     10  //the same on all nodes that talk to each other
#define NODEID        1 

  if (radio.receiveDone())
  {
      if (radio.ACKRequested())
      {
        radio.sendACK();
    }
    int RSI = radio.RSSI;
    int senderid = radio.SENDERID;
    char* command = strtok((char*)radio.DATA, "|");
    radio.receiveDone(); //put radio in RX mode
    Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU


Code: [Select]
//Node

#define NETWORKID     10  // The same on all nodes that talk to each other
#define NODEID        10    // The unique identifier of this node
#define RECEIVER      1    // The recipient of packets

void radiosend(String s)
{
  if (radio.sendWithRetry(RECEIVER, Packet, strlen(Packet),2,300))
  {
    powerreadings = 0;
  }
  radio.receiveDone(); //put radio in RX mode
}
« Last Edit: October 19, 2016, 11:28:35 AM by Felix »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #1 on: October 18, 2016, 08:20:40 AM »
I am not familiar with the Feathers, only Moteinos :)
The RFM69 lib and sketches are guaranteed to work, I would start with the Node and Gateway examples.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #2 on: October 18, 2016, 09:07:59 AM »
I don't know why you're crashing since you don't show all of your code, but the following code does have a flaw:
...
Code: [Select]
  if (radio.receiveDone())
  {
      if (radio.ACKRequested())
      {
        radio.sendACK();
    }
    int RSI = radio.RSSI;
    int senderid = radio.SENDERID;
    char* command = strtok((char*)radio.DATA, "|");
    radio.receiveDone(); //put radio in RX mode
    Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU
The function strtok() modifies the incoming character array (radio.DATA in this case) in situ, inserting null characters at each delimiter found inside the radio.DATA array. strtok() does not make a copy of the data. Hence the 'command' variable will reference the a token in the radio.DATA variable received in the previous receiveDone call.  As soon as you make a new receiveDone() call (as in your case immediately following the strtok() call), the radio.DATA variable is pretty much wiped out.

If you save the radio.DATA character array to a local buffer, you will eliminate this particular problem as in:
Code: [Select]
static char localBuffer[MAX_BYTES];        // needs to be statically defined so that it persists across loop() calls...            

if (radio.receiveDone())
  {
      if (radio.ACKRequested())
      {
        radio.sendACK();
    }
    int RSI = radio.RSSI;
    int senderid = radio.SENDERID;
    memcpy(localBuffer, radio.DATA, radio.DATALEN);   // grab the incoming data as soon as possible...
    char* command = strtok(localBuffer, "|");              // now you can tokenize it whenever you want...
    radio.receiveDone(); //put radio in RX mode
    Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU

Also note, that the variable 'command' has limited scope, it will only exist within the original 'if (radio.receiveDone()) {' clause.  The compiler should detect this as long as you don't have any other variables named 'command'...

Tom

skl

  • NewMember
  • *
  • Posts: 6
  • Country: dk
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #3 on: October 19, 2016, 03:49:00 AM »
Thanks for the reciver advice, but the reciver remains in action its only the nodes that are dying.. so the reciver just takes on the new node, and the node dies at the exact moment where the new node sends the first package.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #4 on: October 19, 2016, 07:39:30 AM »
Thanks for the reciver advice, but the reciver remains in action its only the nodes that are dying.. so the reciver just takes on the new node, and the node dies at the exact moment where the new node sends the first package.
There's nothing obviously wrong with the very brief snippet of code you included for the node (other than the fact you're sending 'Packet' when the argument to radiosend() is 's') so I don't think you'll be able to get much help on this unless you include more code.

Tom

skl

  • NewMember
  • *
  • Posts: 6
  • Country: dk
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #5 on: October 20, 2016, 02:47:13 AM »
I found the problem

https://learn.adafruit.com/adafruit-feather-m0-radio-with-rfm69-packet-radio/using-the-rfm69-radio

its actually adafruit that has the problem, If I remove the radioRecive.done() after sending and receiving it works..

so now I have 2 light sensors, and 1 power sensor that spits out data :-)

Regards
Søren Klein

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #6 on: October 20, 2016, 08:04:28 AM »
Thanks for the update, does that mean the feathers don't work with the stock sketches or where do you have to remove receiveDone() from?

skl

  • NewMember
  • *
  • Posts: 6
  • Country: dk
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #7 on: October 20, 2016, 09:13:28 AM »
i removed the lines in bold :-) its adafruits guide thats wrong


void loop() {
  delay(1000);  // Wait 1 second between transmits, could also 'sleep' here!
   
  char radiopacket[20] = "Hello World #";
  itoa(packetnum++, radiopacket+13, 10);
  Serial.print("Sending "); Serial.println(radiopacket);
   
  if (radio.sendWithRetry(RECEIVER, radiopacket, strlen(radiopacket))) { //target node Id, message as string or byte array, message length
    Serial.println("OK");
    Blink(LED, 50, 3); //blink LED 3 times, 50ms between blinks
  }

  radio.receiveDone(); //put radio in RX mode
  Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU

}

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #8 on: October 20, 2016, 11:12:30 AM »
That receiveDone() function puts the radio in RX mode, why that should crash anything is not clear to me ???

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #9 on: October 20, 2016, 12:19:07 PM »
Unless it receives a packet but the length is wrong and pointers are going out of range? Is there pointer range checking in the library?
Mark.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #10 on: October 20, 2016, 01:04:10 PM »
Unless it receives a packet but the length is wrong and pointers are going out of range? Is there pointer range checking in the library?
Mark.
How would length be corrupted? A bug in the radio chip? Never see anything like that happen.
Here's the current receiveDone() in case what I said is not clear, nothing to do with packet length or pointers to data, just setting the radio mode:

Code: [Select]
bool RFM69::receiveDone() {
//ATOMIC_BLOCK(ATOMIC_FORCEON)
//{
  noInterrupts(); // re-enabled in unselect() via setMode() or via receiveBegin()
  if (_mode == RF69_MODE_RX && PAYLOADLEN > 0)
  {
    setMode(RF69_MODE_STANDBY); // enables interrupts
    return true;
  }
  else if (_mode == RF69_MODE_RX) // already in RX no payload yet
  {
    interrupts(); // explicitly re-enable interrupts
    return false;
  }
  receiveBegin();
  return false;
//}
}

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #11 on: October 20, 2016, 02:34:01 PM »
How would length be corrupted? A bug in the radio chip? Never see anything like that happen.
I think I may have misunderstood what's happening, but I assumed that there was a gateway and two or more nodes, and when one node tries to transmit to the gateway it crashes the other node. The nodes could be sending different packets with different lengths, but if the second node received the first node's transmit accidentally (even though it was destined for the gateway) and wasn't expecting a different length packet... but lots of ifs and maybes. I can't see how another node would die unless it's receiving something and that receive causes a crash of some sort, if so what could be the mechanism and pointers out of range could be one.
Mark.

skl

  • NewMember
  • *
  • Posts: 6
  • Country: dk
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #12 on: October 20, 2016, 02:37:24 PM »
i tryed 5 diffrent adafruit feathers, so i dont think its the radio, and it works fine until i add the secound node!

perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #13 on: October 20, 2016, 02:46:29 PM »
The problem might be that the transmitter is put back into permanent receive and starts to receive a packet from the other node, but because it's not addressed there's no payloadready (e.g. different sync words are used). The receiver might then be stuck waiting for some kind of radio reset, or maybe its AGC locked onto a slightly offset frequency which never gets reset.

Can you descrbe what you mean by a crash? Is this a lock up polling on a bit that never goes active, or does it really crash going off into never never land?

skl

  • NewMember
  • *
  • Posts: 6
  • Country: dk
Re: Adafruit Feather Reciver and 2 Nodes (the nodes crash)
« Reply #14 on: October 20, 2016, 02:59:48 PM »
When the added node is powered on, and fires the first package, det other node freezes, and i tried to use send (without ACK) and it still locked up.