LowPowerLab Forum

Hardware support => Moteino => Topic started by: nerdkingdan on May 14, 2017, 06:29:38 AM

Title: Moteino Losing Transmissions
Post by: nerdkingdan on May 14, 2017, 06:29:38 AM
We are working on a project using several moteinos to communicate with, a raspberry pi and an arduino. They keep losing Transmissions. We need some code that would allow us to send and receive four to five transmissions within a second. We are completely willing to rewrite our code to do this so all we need is some example code that can handle this.

The concept is one of the moteinos will transmit to the gateway connected to the pi. The Pi will than transmit to other moteinos with the same network id a series of commands in response to an input pullup change on the first moteino. When we send out this batch of commands the gateway stops receiving any other moteinos on the network detecting something. We have 20 or so moteinos on the network, each is sending an acknowledgement back that they completed their task.  Without the acknowledgement we were still losing transmissions we just didn't know which ones.
Title: Re: Moteino Loosing Transmissions
Post by: TomWS on May 14, 2017, 07:41:18 AM
Quote from: nerdkingdan on May 14, 2017, 06:29:38 AM
We are working on a project using several moteinos to communicate with, a raspberry pi and an arduino. They keep loosing Transmissions. We need some code that would allow us to send and receive four to five transmissions within a second. We are completely willing to rewrite our code to do this so all we need is some example code that can handle this.

The concept is one of the moteinos will transmit to the gateway connected to the pi.
Is this first transmission sent with radio.sendWithRetry()?  This doesn't guarantee delivery, but it certainly improves the likelihood and, with enough retries, should get through even in very dense network (unless, of course, the gateway is off doing something else and not listening - you need program logic to fix this).  Also, make sure the gateway replies with it's Ack as soon as it receives the packet.

Tom
Title: Re: Moteino Loosing Transmissions
Post by: nerdkingdan on May 14, 2017, 03:20:15 PM
Well we do have retry set, the best we can tell is that when it sends the 5-6 transmissions, with retry and a bunch of Ack, a transmission or two gets lost. 

When we have a transmission fail after the retries,  I've tried storing the failed transmission in an array to retry, this typically results in the moteino rebooting from lack of memory, seems to be the array method slowly leaks memory when its constantly reassigned, think that has nothing to do with the moteino, just something we have tried.  Our que works for about a half hour, then the memory leak catches up with the moteino.

We are sending serial information to the moteino, which then in turn sends it to the gateway, devices without Serial connections are much more stable.    Its the standard serial read code, that then just transmits it on.   

I've tried 8 character transmissions, at the minimum, and 20 at the max, could that be it?


Title: Re: Moteino Losing Transmissions
Post by: Felix on May 15, 2017, 05:55:12 PM
To little detail to really tell what's going on.
First how is the hardware setup, what antennas do you use, did you ensure your settings (especially the IS_RFM69HCW parameter) match the transiceivers?
Coding is obviously crucial also, people often insert delays() in there and that kills their link easily. Detail details...
Title: Re: Moteino Losing Transmissions
Post by: nerdkingdan on May 16, 2017, 02:20:20 AM
When I get back to the office I'll write up what I am trying better.   

The 3 MS delay after the blink for sending and receiving is my only delay, that came from the example code.   I'll double check the IS_RFM69HCW.   My hardware, is 5-12 volt power supply, except for the one directly powered by the USB pi.  All that is connected is some input_pullups/Output for some basic magnetic switches.    The outputs are set up to some relays, or LEDs...   

My antenna is the little wire that came with the moteinos.    Is that not good enough?    The distance is under 50 feet, and the only walls are basic drywall and some insulation.   

I am fairly confident, from reading what others are doing with the Moteino its my programming, I was hoping to find something with a Primary Control Unit, and multiple moteinos to use to trouble shoot it myself, but I'm clearly missing something.
Title: Re: Moteino Losing Transmissions
Post by: nerdkingdan on May 16, 2017, 12:11:03 PM
Here is what I am using....   

Moteino
  - Transceiver: RFM69HW - 868/915Mhz
  - Extras (headers): male side headers
  - Extras (flash memory): With 4Mbit flash chi..


#include <RFM69.h>         //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h>     //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPIFlash.h>      //get it here: https://www.github.com/lowpowerlab/spiflash
#include <SPI.h>         
//included with Arduino IDE install (www.arduino.cc)
#define NODEID        107   //must be unique for each node on same network (range up to 1, 255 is used for broadcast)
#define NETWORKID     100  //the same on all nodes that talk to each other (range up to 255)
#define GATEWAYID     1
#define FREQUENCY   RF69_868MHZ
#define ENCRYPTKEY    "################" //exactly the same 16 characters/bytes on all nodes!  Changed from what I am using, of course.
#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ENABLE_ATC    //comment out this line to disable AUTO TRANSMISSION CONTROL
#define SERIAL_BAUD   9600
#define ATC_RSSI      -80  //?????????
#ifdef __AVR_ATmega1284P__
  #define LED           15 // Moteino MEGAs have LEDs on D15
  #define FLASH_SS      23 // and FLASH SS on D23
#else
  #define LED           9 // Moteinos have LEDs on D9
  #define FLASH_SS      8 // and FLASH SS on D8
#endif
#ifdef ENABLE_ATC
  RFM69_ATC radio;
#else
  RFM69 radio;
#endif
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit  Windbond chip (W25X40CL)

//Radio/Serial Variables
boolean requestACK = false;
String SerialPacket;
String RadioPacket;
long lastPeriod = 0;
int sendSize = (0) % 31;


//Local Variables for Sensors and Controls
// For example a variable reset each game, or some such....

//Local Controls and sensors:



//Transmit methods:
void transmitData(int Destination, String data)
{
  char payload[30];
  sendSize= (data.length()) % 31;
  data.toCharArray(payload, 30); 
    if(sendSize>0)
    {
      if (radio.sendWithRetry(Destination, payload, sendSize)){
        Blink(LED,3);
        sendSize = (0) % 31;
      }
      else {
        Serial.println("Failed to send");
        sendSize = (0) % 31;
      }
    } 
}

void Blink(byte PIN, int DELAY_MS)
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}

void setup() {
  Serial.begin(SERIAL_BAUD);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
  radio.setHighPower(); //uncomment only for RFM69HW!
#endif
  radio.encrypt(ENCRYPTKEY);
  //radio.setFrequency(919000000); //set frequency to some custom frequency

//Auto Transmission Control - dials down transmit power to save battery (-100 is the noise floor, -90 is still pretty good)
//For indoor nodes that are pretty static and at pretty stable temperatures (like a MotionMote) -90dBm is quite safe
//For more variable nodes that can expect to move or experience larger temp drifts a lower margin like -70 to -80 would probably be better
//Always test your ATC mote in the edge cases in your own environment to ensure ATC will perform as you expect
#ifdef ENABLE_ATC
  radio.enableAutoPower(ATC_RSSI);
#endif

  char buff[50];
  if (flash.initialize())
  {
    transmitData(1,"Moteino:Power:On");
    Serial.println("INIT\n");
  }
  else
    Serial.println("\nError Setting Up Transmition");
}

void loop() {

  //process any serial input
  if (Serial.available() > 0)
  {
    char input = Serial.read();
    SerialPacket += input;
        if (SerialPacket.endsWith( "\n" )) {
            SerialPacket = SerialPacket.substring(0, SerialPacket.length() - 1);
            //transmitData(1, SerialPacket); //sends all serial for testing
            transmitData(1, SerialPacket);
            SerialPacket = "";
        }
   
  }

  //check for any received packets 
  if (radio.receiveDone())
  {
    for (byte i = 0; i < radio.DATALEN; i++) {
    //  Serial.print((char)radio.DATA[i]);
      RadioPacket += (char)radio.DATA[i];
    }
    if (radio.ACKRequested())
    {
      radio.sendACK();
    }
   // Serial.println("");
    Serial.println(RadioPacket);
    RadioPacket = "";
    Blink(LED,3);
  }
}


Title: Re: Moteino Losing Transmissions
Post by: Felix on May 16, 2017, 01:39:52 PM
After a quick look the code looks ok.
Are antennas soldered? Motes powered via VIN?
Verify the transceiver is a HW or HCW: https://lowpowerlab.com/guide/moteino/transceivers
Title: Re: Moteino Losing Transmissions
Post by: nerdkingdan on May 16, 2017, 03:57:00 PM
Yes the motes are powered by Vin, and the antenna is soldered on.

Is it possible,  is there any code to que a retry so it waits a few seconds before trying again?

We have some HCW we mots we just ordered but they are still in the box.   

All the ones we have in use, according to our invoice are the HW.
Title: Re: Moteino Losing Transmissions
Post by: TomWS on May 16, 2017, 06:38:35 PM
I am virtually certain that you will never return an ACK in time if you print the received packet at 9600 baud before ACKing.

Tom
Title: Re: Moteino Losing Transmissions
Post by: nerdkingdan on May 17, 2017, 01:09:19 AM
if (radio.receiveDone())
  {
    for (byte i = 0; i < radio.DATALEN; i++) {
    //  Serial.print((char)radio.DATA[i]);
      RadioPacket += (char)radio.DATA[i];
    }
    if (radio.ACKRequested())
    {
      radio.sendACK();
    }
   // Serial.println("");
    Serial.println(RadioPacket);
    RadioPacket = "";
    Blink(LED,3);
  }
}


Isn't the serial print after the sendACK?    The serial print before should be commented out, was my notes from the original file I worked off.   I will however verify that is the same way it works on all the mots.   It is possible I have it before on some of them  I was trying lots of variations so its worth a look, I'll verify when I get to the office.

I will also try a higher rate, I had it 115200 and shifted it to 9600 as a potential fix for the original problem, I will admit I've tried so many different things it could be I solved one issue after creating another.
Title: Re: Moteino Losing Transmissions
Post by: TomWS on May 17, 2017, 07:48:02 AM
Quote from: nerdkingdan on May 17, 2017, 01:09:19 AM
Isn't the serial print after the sendACK?    The serial print before should be commented out, was my notes from the original file I worked off.   
My apologies, I hadn't noticed that the Serial.print was commented out.

Tom
Title: Re: Moteino Losing Transmissions
Post by: nerdkingdan on May 17, 2017, 12:05:04 PM
Quote from: TomWS on May 17, 2017, 07:48:02 AM
My apologies, I hadn't noticed that the Serial.print was commented out.

Tom

No don't apologize, the code for the gateway off github does in fact print serial before the ack as well, and while I don't have that in the nodes it is on the gateway.    While this isn't all of the issue I'm sure its contributing.    I'll be able to run through everything tonight with some tweaks on that.
Title: Re: Moteino Losing Transmissions
Post by: nerdkingdan on May 18, 2017, 04:50:22 PM
I have used what you suggested and got it so I'm only losing transmissions from a single moteino.

Its code is the same, but its connected to an arduino not a pi.   The pis are running python and I'm using multithreading on the pi side to que up transmissions so they are not sent to the moteino too fast.   

What speed does a transmission take?   Should I put a delay on the Arduino side before it sends its next serial command to be transmitted by the Moteino.    This particular node is connected to an arduino because I need too many pins for any of the moteino.   I'm using a 3 to 5 volt bi directional logic converter.    It basically works until the arduino sends more than 3 transmissions in a loop.    Any thoughts?