Problem with data transmission

Started by Kitsibas, September 24, 2014, 10:33:27 AM

Kitsibas

i have two sketchs:

Sender:
#include "LowPower.h"
#include <RFM69.h>
#include <SPI.h>
#include <SPIFlash.h>
#include <dht.h>

#define NODEID      99
#define NETWORKID   100
#define GATEWAYID   1
#define FREQUENCY   RF69_433MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 115200
#define ACK_TIME    30  // # of ms to wait for an ack

dht DHT;

RFM69 radio;

int count = 1;


void setup() {
  Serial.begin(SERIAL_BAUD);
  
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  radio.encrypt(KEY);
}




void loop() {  
 
    int chk = DHT.read22(6);
    
    int temp = DHT.temperature * 100;
    int humi = DHT.humidity * 100;
    
    if(count > 1000) {
        count = 1;
    }

    String string = "{\"sensorId\": ";
    
    string += NODEID;
    string += ", \"series\": ";
    string += count;
    string += ", \"values\":[";
    string += temp;
    string += ", ";
    string += humi;
    string += ", 0, 0, 0]";
    string += ", \"volts\": 0}";
    
    
    char msg[string.length()+1];
    string.toCharArray(msg, string.length()+1); 
    
    Serial.println(msg);
    
    if(radio.sendWithRetry(GATEWAYID, msg, sizeof(msg))) {
          Serial.print("ok");      
    } else {
          Serial.print("nothing");
    }
    
    Serial.println();

    count++;
    
    Blink(LED,3);
    
    Serial.flush();
    
    radio.sleep();
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
    //LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
    //LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
    //LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
    //LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);  
  
}



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


Recaiver:
#include <RFM69.h>
#include <SPI.h>
#include <SPIFlash.h>

#define NODEID      1
#define NETWORKID   100
#define FREQUENCY   RF69_433MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 115200
#define ACK_TIME    30  // # of ms to wait for an ack

RFM69 radio;
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
bool promiscuousMode = true; //set to 'true' to sniff all packets on the same network



void setup() {
  Serial.begin(SERIAL_BAUD);
  delay(10);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
  //radio.setHighPower(); //uncomment only for RFM69HW!
  radio.encrypt(KEY);
  radio.promiscuous(promiscuousMode);
  char buff[50];
  sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);
  
}

byte ackCount=0;

void loop() {

  if (radio.receiveDone())
  {
    
    for (byte i = 0; i < radio.DATALEN; i++) {
          Serial.print((char)radio.DATA[i]);
    }
    Serial.println("");

    
    if (radio.ACKRequested())
    {
      radio.sendACK();
    }

      
    

    Blink(LED,3);
  }
}

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




There are two problems.

1. receiver gets not complete data () like this:
{"sensorId": 99, "series": 9, "values":[2520, 3770, 0, 0, 0],
{"sensorId": 99, "series": 10, "values":[2520, 3750, 0, 0, 0]
{"sensorId": 99, "series": 10, "values":[2520, 3750


Sender Serial.println(msg); return correct: {"sensorId": 101, "series": 1, "values":[31172, 31172, 0, 0, 0], "volts": 0}


2. recaiver not always send ACK


sender logs:
ok
{"sensorId": 99, "series": 10, "values":[2510, 3790, 0, 0, 0], "volts": 0}
nothing
{"sensorId": 99, "series": 11, "values":[2510, 3830, 0, 0, 0], "volts": 0}
nothing
{"sensorId": 99, "series": 12, "values":[2510, 3710, 0, 0, 0], "volts": 0}
ok
{"sensorId": 99, "series": 13, "values":[2510, 3780, 0, 0, 0], "volts": 0}
ok




Could someone tell me where i do mistakes?

kiwisincebirth

There are a couple of issues.

First: There is a limit on the amount of data you can send in a singe message (the following is from the RFM Library)

#define RF69_MAX_DATA_LEN 61 // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead)

I think you are going over this limit.

Second: The way you do string concatenation could be fragmenting SRAM. You need to reserve() memory for String's upfront, the documentation gives a good example - http://arduino.cc/en/Reference/StringReserve - There is also a very good article, which dscribes the constraints, issues (and fixes) for the memory poor Arduino

https://learn.adafruit.com/memories-of-an-arduino?view=all

Third: (Related Issue) Sending large String's over the air, will GULP your battery, you need to send as little as possible.

Solution: Send C Structures over he air, not strings. See This thread, Felix has some good example sketches which he links to in his response

https://lowpowerlab.com/forum/index.php/topic,94.0.html