Serial monitor Problem

Hello

Iam using 4 arduino boards they can communicate using the mrf24j40MA module.

My porblem is that i can receive data from other boards only if their serial monitor is open, if it's not I recieve no data anymore.

Please does anybody have an idea about this ?

Thanks :slight_smile:

Post your code and use the code button </> so it looks like this

Post a link to the datasheet for the mrf module

...R

Thanks, here's my 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>
#include <dht.h>

dht DHT;

#define DHT22_PIN 7

const int pin_reset = 6;
const int pin_cs = 52; // default CS pin on ATmega8/168/328
const int pin_interrupt = 5; // default interrupt pin on ATmega8/168/328

Mrf24j mrf(pin_reset, pin_cs, pin_interrupt);

long last_time;
long tx_interval = 1000;

void setup() {
 
  SPI.setBitOrder(MSBFIRST) ;
    SPI.setDataMode(SPI_MODE0);
    SPI.begin();
  Serial.begin(115200);
 
  mrf.reset();
  mrf.init();
 
  mrf.set_pan(0x1234);
  // This is _our_ address
  mrf.address16_write(0x6002);

  // 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(pin_interrupt, 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
}

int H;
String str;
char HH[3];

void loop() {
    mrf.check_flags(&handle_rx, &handle_tx);
    unsigned long current_time = millis();
    if (current_time - last_time > tx_interval) {
        last_time = current_time;
       
         uint32_t start = micros();
    int chk = DHT.read22(DHT22_PIN);
    uint32_t stop = micros();
   
   
//    // DISPLAY DATA
    Serial.print(DHT.humidity, 1);
    Serial.print(",\t");
    Serial.print(DHT.temperature, 1);
    Serial.print(",\t");
    Serial.print(stop - start);
    Serial.println();
   
    H=int(DHT.humidity);
    str=String(H);
    str.toCharArray(HH,3);
   
     Serial.print(HH);
     Serial.println();
   
        Serial.println("txxxing...");
        mrf.send16(0x6001, HH);
       
    }
}

void handle_rx() {
    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 handle_tx() {
    if (mrf.get_txinfo()->tx_ok) {
        Serial.println("TX went ok, got ack \n");
    } else {
        Serial.print("TX failed after \n ");Serial.print(mrf.get_txinfo()->retries);Serial.println(" retries\n");
    }
}

And here's the data sheet : http://ww1.microchip.com/downloads/en/DeviceDoc/70329b.pdf

When the Serial Monitor is opened it causes the Arduino to reset. It would be worth checking to see if pressing the reset button gets things moving when the Serial Monitor is not used.

There is a great deal of Serial stuff in your code. What happens if you comment it out ?
Though I doubt if that is the problem.

The device seems to be very similar to the NRF24 transceivers.

...R

You must use unsigned long, or you will have a problem when millis() rolls over to 0.

long last_time;
long tx_interval = 1000;

Robin2 I tried to reset it many times sometimes it works some times not, I tried to remove most of the serial stuff but it's not working too :frowning:

I tried to use unsigned long instead of long no results

What is the purpose of the handle_rx() and handle_tx() functions ?

If they are called by tge mrf library when data arrives (or is sent) I would certainly not put ANY print statements in them.

I notice, for example that you have

   Serial.print("received a packet ");Serial.print(mrf.get_rxinfo()->frame_length, DEC);Serial.println(" bytes long");

which would be much easier to read like this

   Serial.print("received a packet ");
Serial.print(mrf.get_rxinfo()->frame_length, DEC);
Serial.println(" bytes long");

but the main point is that it has that mrf.get in it and I have no idea how that might behave without a serial connection. It could be filling the serial tx buffer and getting stuck.

I would just use the handle_rx() function to place the received data in my own buffer from which I could print it from loop().

Also I rarely use the result of a function in another function. If I save the result to a variable it gives me the opportunity to print the value for debugging purposes (but not within handle_rx() )

...R

If you read your PM , I suspected this.

void Mrf24j::interrupt_handler(void) {

interrupt_handler disables interrupts, so just to test comment the noInterrupts out, and or find a way to invoke Serial prints after the interrupt_handler is finished.
Actually - using Serial in ISR leads to unreliable code, but it still does not explain why it works with the "remote" COM enabled.

I am not 100% sure this will solve the problem.

PS I now know how the TX / RX is invoked, so skip my request.

Vaclav:
If you read your PM ,

More unnecessary secrecy.

...R