Rf12_crc failing but rf12demo works flawlessly?

Hi there,

Finally getting somewhere with this project after resolving some hardware issues. I can actually monitor temperature. Result.

Now getting to the communications bit. My emonTx (v2.2) has a sketch copied from some example code. The relevant bit is

void send_rf_data()
{
  rf12_sleep(RF12_WAKEUP);
  // if ready to send + exit loop if it gets stuck as it seems too
  int i = 0; while (!rf12_canSend() && i<10) {rf12_recvDone(); i++;}
  rf12_sendStart(0, &emontx, sizeof emontx);
  // set the sync mode to 2 if the fuses are still the Arduino default
  // mode 3 (full powerdown) can only be used with 258 CK startup fuses
  rf12_sendWait(2);
  rf12_sleep(RF12_SLEEP);
}

the temperature is read correctly from 1 sensor. emontx.T1 is therefor the only value that means anything currently. should be no problem for the purposes of testing.

if i run rf12demo on my nanodeRF everything works fine

Current configuration:
 A i1 g210 @ 433 MHz q1
OK 10 0 0 0 0 252 8 100 206 100 206 100 206
OK 10 0 0 0 0 252 8 100 206 100 206 100 206
OK 10 0 0 0 0 252 8 100 206 100 206 100 206
OK 10 0 0 0 0 71 10 100 206 100 206 100 206
OK 10 0 0 0 0 27 11 100 206 100 206 100 206
OK 10 0 0 0 0 127 11 100 206 100 206 100 206
OK 10 0 0 0 0 71 11 100 206 100 206 100 206
OK 10 0 0 0 0 2 11 100 206 100 206 100 206
OK 10 0 0 0 0 202 10 100 206 100 206 100 206

the 6th and 7th byte seem to represent the temperature on T1. as i hold the sensor it rises and then slowly falls when i let it go. excellent.

now i am trying to make my own nanodeRF sketch, this is where things fall apart.

#define RF_freq RF12_433MHZ                                                // Frequency of RF12B module can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.
const int nodeID = 1;                                                  // emonTx RFM12B node ID
const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD

const int UNO = 1;                                                      // Set to 0 if your not using the UNO bootloader (i.e using Duemilanove) - All Atmega's shipped from OpenEnergyMonitor come with Arduino Uno bootloader
#include <avr/wdt.h>  


#include <JeeLib.h>   // make sure V12 (latest) is used if using RFM69CW
ISR(WDT_vect) { Sleepy::watchdogEvent(); }                              // Attached JeeLib sleep function to Atmega328 watchdog -enables MCU to be put into sleep mode inbetween readings to reduce power consumption 
   

typedef struct {
    int realPower;                                          // RealPower
    int apparentPower;                                          // ApparentPower
    int T1;                                           // temperature sensor 1
    int T2;                                           // temperature sensor 2
    int T3;                                           // temperature sensor 3
    int T4;                                           // temperature sensor 4
} Payload;
Payload emontx;

const int LEDpin = 9;

void emontx_sleep(int seconds) {
    for (int i=0; i<seconds; i++) { 
      delay(1000); 
      if (UNO) wdt_reset();
    } 
}

void setup() {
  Serial.begin(9600);
  Serial.println("test"); 

  
  rf12_initialize(nodeID, RF_freq, networkGroup);                                       // initialize RF
  rf12_sleep(RF12_SLEEP);
  
  pinMode(LEDpin, OUTPUT);   
  digitalWrite(LEDpin, HIGH);
  
  if (UNO) wdt_enable(WDTO_8S); 
}

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

   if (rf12_recvDone())
   {
    if (rf12_crc != 0)
    {
      Serial.print("crc oops");
    }
    else if ((rf12_hdr & RF12_HDR_CTL) != 0)
    {
      Serial.print("rf12_hdr?");
    }
    else
    {
      Serial.println();
      Serial.print(rf12_data[0]);
      Serial.print(rf12_data[1]);
      Serial.print(rf12_data[2]);
      Serial.print(rf12_data[3]);
      Serial.print(rf12_data[4]);
      Serial.print(rf12_data[5]);
      Serial.print(rf12_data[6]);
      Serial.println();
      //Serial.println();
      //emontx = *(Payload*) rf12_data;
      //Serial.print(emontx.T1);
    }
  }
  emontx_sleep(10);                                                     // sleep or delay in seconds - see emontx_lib
  digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED

}

so… the output of this is sometimes like this:

test
..
0000399100
..crc oops..crc oops..crc oops..crc oops..crc oops..crc oops...crc oops..crc oops..crc oops..crc oops.

however sometimes it doesn’t even get the first one. The super weird thing is it keeps spitting out the crc oops message even when my emonTx is unpowered.

as far as i can tell i have initialised it with the same settings that were used in rf12demo eg. nodeID 1, netGroup 210, frequency 433mhz

hmmm i must be doing something quite wrong here…?

[It would have been helpful to continue in or link to Temperature sensor address search not working, how to debug? New to arduino but not programming - #3 by jimjamjahaa. ]

You’re using the Nanode to receive the data from the emonTx V2?

Why are you sleeping it at the end of loop? That means you’re ignoring anything that comes in while you’re asleep - apart from what might be in the RFM69’s buffer, then just waking up for a quick peek, then going to sleep again. Your main loop in the Nanode should never sleep.

[And I’m not even sure that JeeLib uses the RFM69’s internal buffer in the mode we use!]

Hi there, thanks for quick the reply!

I commented out the sleep and we have a different mess now :slight_smile:

void setup() {
  Serial.begin(9600);
  Serial.println("test"); 

  
  rf12_initialize(nodeID, RF_freq, networkGroup);                                       // initialize RF
  rf12_sleep(RF12_SLEEP);
  
  pinMode(LEDpin, OUTPUT);   
  digitalWrite(LEDpin, HIGH);
  
  if (UNO) wdt_enable(WDTO_8S); 
}

void loop()
{ 
  //Serial.print(".");

   if (rf12_recvDone())
   {
    if (rf12_crc != 0)
    {
      Serial.print("crc oops");
    }
    else if ((rf12_hdr & RF12_HDR_CTL) != 0)
    {
      Serial.print("rf12_hdr?");
    }
    else
    {
      Serial.println();
      Serial.print(rf12_data[0]);
      Serial.print(rf12_data[1]);
      Serial.print(rf12_data[2]);
      Serial.print(rf12_data[3]);
      Serial.print(rf12_data[4]);
      Serial.print(rf12_data[5]);
      Serial.print(rf12_data[6]);
      Serial.println();
      //Serial.println();
      //emontx = *(Payload*) rf12_data;
      //Serial.print(emontx.T1);
    }
  }
  //emontx_sleep(10);                                                     // sleep or delay in seconds - see emontx_lib
  //digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED

}

this gives output like this:

test

00002279100
test
test

00002279100
crc oopscrc oopstest

00002279100
test
test

00002279100
test

00002279100
test
test
crc oops
00002279100
test
crc oops
00002279100
test
test

00002279100
crc oopstest
crc oopstest
crc oopstest

00002279100
test
test

00002339100

it keeps firing the setup() function over and over. scratches head am i making the thing reset over and over?

Ok, i removed those other sleep related bits and bobs and i think we’re working as intended now.

The perils of copy pasting when you don’t really understand why it’s there to begin with :wink:

Old engineering maxim:
“If you’re going to copy somebody’s idea, make sure it’s a good one.”

[And btw, I’ve edited your post. The deleted word wasn’t necessary]

It appears

that was the watchdog doing its thing. It needs to be whipped into submission frequently to keep it under control.