Hello,
Any help would be appreciated with this. I went around the forums here and other and tried implementing every advice but with no success.
I am trying to get data packets transferred using a RF module MRF24j40MA (datasheet) and Arduino Nano Every and Arduino Uno.
I am using the library and example made by Karl Palsson (library).
I get no compilations errors. One board is broadcasting the other listening.
Boards I/O:
MRF pins---->Bi-Directional Level Shifter (3.3v to 5V)---->Arduino Uno:
1 - GND to GND
2 - RESET to 6 (Digital I/O)
3 - Wake (Not connected)
4 - INT to 2 (INT0)
5 - SDI to 11 (MOSI)
6 - SCK to 13 (SCK not through level shifter)
7 - SDO to 12 (MISO)
8 - CS to 10 (SS)
9 - NC (Not Connected)
10 - Vin to 3v3 (not through level shifter)
11 - GND (Not Connected)
12 - GND (Not Connected)
As I mentioned, I have been around forums and I am a little desperate now. I bet it will be something obvious but I have been going over it over and over for months now so best get another pair of eyes on it.
Code for each board is below.
Sender code:
/**
* Example code for using a microchip mrf24j40 module to send and receive
* packets using plain 802.15.4
* Requirements: 3 pins for spi, 3 pins for reset, chip select and interrupt
* notifications
* This example file is considered to be in the public domain
* Originally written by Karl Palsson, karlp@tweak.net.au, March 2011
*/
#include <SPI.h>
#include "mrf24j.h"
const int pin_reset = 6;
const int pin_cs = 10; // default CS pin on ATmega8/168/328
const int pin_interrupt = 2; // default interrupt pin on ATmega8/168/328
Mrf24j mrf(pin_reset, pin_cs, pin_interrupt);
long last_time;
long tx_interval = 1000;
void setup() {
Serial.begin(9600);
mrf.reset();
mrf.init();
mrf.set_pan(0xcafe);
// This is _our_ address
mrf.address16_write(0x4202);
// uncomment if you want to receive any packet on this channel
//mrf.set_promiscuous(true);
// uncomment if you want to enable PA/LNA external control
//mrf.set_palna(true);
// uncomment if you want to buffer all PHY Payload
//mrf.set_bufferPHY(true);
attachInterrupt(0, interrupt_routine, CHANGE); // interrupt 0 equivalent to pin 2(INT0) on ATmega8/168/328
last_time = millis();
interrupts();
}
void interrupt_routine() {
mrf.interrupt_handler(); // mrf24 object interrupt routine
}
void loop() {
mrf.check_flags(&rx_handler, &tx_handler); //scan for rx and tx
unsigned long current_time = millis();
if (current_time - last_time > tx_interval) {
last_time = current_time;
Serial.println("broadcasting..."); // print broadcasting state
mrf.send16(0x6002, "hello"); // send string to defined address
}
}
void rx_handler() {
Serial.print("received a packet ");Serial.print(mrf.get_rxinfo()->frame_length, DEC);Serial.println(" bytes long");
if(mrf.get_bufferPHY()){
Serial.println("Packet data (PHY Payload):");
for (int i = 0; i < mrf.get_rxinfo()->frame_length; i++) {
Serial.print(mrf.get_rxbuf()[i]);
}
}
Serial.println("\r\nASCII data (relevant data):"); //print the data received
for (int i = 0; i < mrf.rx_datalength(); i++) {
Serial.write(mrf.get_rxinfo()->rx_data[i]);
}
Serial.print("\r\nLQI/RSSI="); // Print signal related information
Serial.print(mrf.get_rxinfo()->lqi, DEC);
Serial.print("/");
Serial.println(mrf.get_rxinfo()->rssi, DEC);
}
void tx_handler() { // confirmation of good transmission of packet.
if (mrf.get_txinfo()->tx_ok) {
Serial.println("TX went ok, got ack");
} else {
Serial.print("TX failed after ");Serial.print(mrf.get_txinfo()->retries);Serial.println(" retries\n");
}
}
Receiver code:
/**
* Example code for using a microchip mrf24j40 module to send and receive
* packets using plain 802.15.4
* Requirements: 3 pins for spi, 3 pins for reset, chip select and interrupt
* notifications
* This example file is considered to be in the public domain
* Originally written by Karl Palsson, karlp@tweak.net.au, March 2011
*/
#include <SPI.h> // SPI library
#include "mrf24j.h" // RF module library.
const int pin_reset = 6;
const int pin_cs = 10; // default CS pin on ATmega8/168/328
const int pin_interrupt = 2; // default interrupt pin on ATmega8/168/328
Mrf24j mrf(pin_reset, pin_cs, pin_interrupt);
long last_time;
long tx_interval = 1000;
void setup() {
Serial.begin(9600);
mrf.reset();
mrf.init();
mrf.set_pan(0xcafe); // network ID
// This is _our_ address
mrf.address16_write(0x6002); // node address
// uncomment if you want to receive any packet on this channel
mrf.set_promiscuous(true);
// uncomment if you want to enable PA/LNA external control
//mrf.set_palna(true);
// uncomment if you want to buffer all PHY Payload
//mrf.set_bufferPHY(true);
attachInterrupt(0, interrupt_routine, CHANGE); // interrupt 0 equivalent to pin 2(INT0) on ATmega8/168/328
last_time = millis();
interrupts();
}
void interrupt_routine() {
mrf.interrupt_handler(); // mrf24 object interrupt routine
}
void loop() {
mrf.check_flags(&rx_handler, &tx_handler);
unsigned long current_time = millis();
if (current_time - last_time > tx_interval) {
last_time = current_time;
Serial.println("broadcasting/awaiting message...");
//mrf.send16(0x4202, "abcd");
}
}
void rx_handler() {
Serial.print("received a packet ");Serial.print(mrf.get_rxinfo()->frame_length, DEC);Serial.println(" bytes long");
if(mrf.get_bufferPHY()){
Serial.println("Packet data (PHY Payload):");
for (int i = 0; i < mrf.get_rxinfo()->frame_length; i++) {
Serial.print(mrf.get_rxbuf()[i]);
}
}
Serial.println("\r\nASCII data (relevant data):");
for (int i = 0; i < mrf.rx_datalength(); i++) {
Serial.write(mrf.get_rxinfo()->rx_data[i]);
}
Serial.print("\r\nLQI/RSSI=");
Serial.print(mrf.get_rxinfo()->lqi, DEC);
Serial.print("/");
Serial.println(mrf.get_rxinfo()->rssi, DEC);
}
void tx_handler() {
if (mrf.get_txinfo()->tx_ok) {
Serial.println("TX went ok, got ack");
} else {
Serial.print("TX failed after ");Serial.print(mrf.get_txinfo()->retries);Serial.println(" retries\n");
}
}