RF22 Library, how to use that?

Hi, first of all thanks to mikem for this library.
My project will see something like 10 RFM22B modules to comunicate on a theatre stage.
One is on a Arduino 2009, that receives MIDI signals from a PC (OK) and should send via RFM22B ‘commands’ to the other RF22s.
In this moment i’m trying to understand how to comunicate with two of them, both plugged to two stand-alone atmega328p, powered with 2 AA 1,5V batteries (2,9V or similar…).
I encountered first problems due to my lack of knowledge in programming, so i’m asking here an help to understand where i’m mistaking.

this is my client code:

#include <SPI.h>
#include <RF22.h>

RF22 rf22;

void setup() 
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, modulation GFSK_Rb2_4Fd36
  pinMode(8, INPUT);
  pinMode(9, OUTPUT);
    
}

void loop() {
  if (digitalRead(8) == HIGH) {
    digitalWrite(9, HIGH);
    uint8_t data[] = {65};
    rf22.send(data, 1);   
    rf22.waitPacketSent();
  }  
  else if (digitalRead(8) == LOW) {
    digitalWrite(9, LOW);
  }
}

and this is my server code:

#include <SPI.h>
#include <RF22.h>

RF22 rf22;

void setup() 
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, modulation GFSK_Rb2_4Fd36
  pinMode (9, OUTPUT);
}

void loop() {
  rf22.waitAvailable(); 
  uint8_t buf[RF22_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);
  int comp = int(buf);
  if (rf22.recv(buf, &len)) {
    blink1time();
    blink1time();
    blink1time();
    if (comp == 65) { 
      digitalWrite (9, HIGH);
    }
    else {
      digitalWrite (9, LOW);
    }
  }
}

void blink1time() {
  digitalWrite (9, HIGH);
  delay(50);
  digitalWrite (9, LOW);
  delay(50);
}

what should happen is that if pin 8 is HIGH on one chip, it should send the byte ‘65’, than if the server receive something it blinks a LED 3 times, and if the byte is ‘65’, the LED should remain ON.

But what happens is that when pin 8 becomes HIGH, i can see the three blinks on the second ‘station’, but the LED doesn’t remain ON, as it shoul be if the byte is recognized.

I have to say that i’m doing this, because, since the chips are standalone, i cannot debug seeing results in a serial monitor.

Wish you can help me in some way.
thanks!

Hi,

I think the problem i in this line in the receiver:

if (comp == 65) {

You have previously set comp to be the address of the buffer (cast as an int: why?)

What you really want to do is check if the first byte in the buffer is ==65 like this:

if (buf[0] == 65) {

This is a basic C programming question, which is better handled in other forums.

Cheers.

Thank you very much, your little help is much appreciated.

i know the problem was not with the library usage, but i wanted to ask you, 'cause maybe this way it is simpler to not explaining the usage of the library to others.

again thank you for the basic C lesson!

Davide.

Hallo, i'm here again, this time my problem is library-related! i'm using RF22 library with success, between two arduino standalone. Now i'm trying to use RFM22B on the EL-SEQUENCER board, which use the pin D2 as an output on a triac gate. also D3 has the same use. into the RF22 library i could read only about the use of interrupts in for the nIRQ connection, so D2 and D3. is it possible in some way to use A0 for nIRQ pin? The EL-SEQUENCER board already has a JP dedicated to RF, but uses nRF24L01 nordic. and the nIRQ on this JP is connected to A0...so i was wandering if that was possible also using RF library with RFM22B.

any help is welcome, thanks, Davide.

AFAIK, it is not possible to use A0 as the interrupt line. On the smaller arduinos at least, the only options are D2 and D3. Cheers.

mmm, i expected it was like that.. so i think i have to modify the board or build my own. thanks mike for answering.

Hi all, I'm a newbie in Arduino. I'm planning to using a couple of Arduino and RFM22 to send data from Temperature Sensor (LM35). to be honest, I don't have back round in C++ programming so I really didn't understand how to read the library given in RF22 folder. Could you Guys help me with an example how to make a sketch using example from rf22_client.pde and rf22_server.pde Thank you for help.

Best Regards,

Handy

Could you Guys help me with an example how to make a sketch using example from rf22_client.pde and rf22_server.pde

Probably. You define some requirements, while the rest of us get busy coding. Maybe when we're done, you'll be able to tell us what we should have done.

Hi…
I’ve been working on simple rf22_client and server.
here’s my code:
rf22_client_pushBut.ino

// rf22_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RF22 class. RF22 class does not provide for addressing or reliability.
// It is designed to work with the other example rf22_server

#include <SPI.h>
#include <RF22.h>
int pushPIN = 7;

// Singleton instance of the radio
RF22 rf22;

void setup() 
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
  pinMode(pushPIN, INPUT);
  digitalWrite(pushPIN, HIGH);
}

void loop()
{
   while (1)
  {
    Serial.println("Sending to rf22_server");
    // Send a message to rf22_server
    int pushON = digitalRead(pushPIN);
    uint8_t data = pushON; 
    Serial.println(pushON);
    rf22.send((uint8_t*)&data, sizeof(data));
    Serial.println(data);
   
    rf22.waitPacketSent();


    // Now wait for a reply
    uint8_t buf[RF22_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);

    if (rf22.waitAvailableTimeout(400))
    { 
      // Should be a message for us now   
      if (rf22.recv(buf, &len))
      {
        Serial.print("got reply: ");
        Serial.println((char*)buf);
      }
      else
      {
        Serial.println("recv failed");
      }
    }
    else
    {
      Serial.println("No reply, is rf22_server running?");
    }
  }
}

and this is my server code:

// rf22_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing server
// with the RF22 class. RF22 class does not provide for addressing or reliability.
// It is designed to work with the other example rf22_client

#include <SPI.h>
#include <RF22.h>

// Singleton instance of the radio
RF22 rf22;
int ledPIN = 6;
int ledPIN2 = 5;
void setup() 
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
  pinMode(ledPIN, OUTPUT);
  pinMode(ledPIN2, OUTPUT);
}
// Turns on led at server with pushbutton at client
void loop()
{
  while (1)
  Serial.println("test 1");
  {
    int dataIN =0;
    rf22.waitAvailable();
    Serial.println("test 2");  
    // Should be a message for us now   
    uint8_t buf[RF22_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf22.recv(buf, &len))
    {
      Serial.print("got request: ");
     // Serial.println((char*)buf);
      Serial.println(dataIN);
      int dataIN = *((int*)buf);
      
      Serial.println(dataIN);
      // the dataIN resulting an integer with value 513 for 1, and 
      // 512 for 0, still can't figured why??..
    if (dataIN == 513){
        digitalWrite(ledPIN, HIGH);
        
      *buf=0; // make the buffer always zero in order to let dataIN 
                     // are always in array zero
            
        // Send a reply
      // uint8_t data[] = "And hello back to you";
      uint8_t data[] = "Green LED ON";
      rf22.send(data, sizeof(data));
      rf22.waitPacketSent();
      Serial.println("Sent a reply");
     }   
    else if(dataIN == 512) {
        digitalWrite(ledPIN2, HIGH);
        
        // Send a reply
      // uint8_t data[] = "And hello back to you";
      uint8_t data[] = "Red LED ON";
      rf22.send(data, sizeof(data));
      rf22.waitPacketSent();
      Serial.println("Sent a reply");
      }
        
    }
    else
    {
      Serial.println("recv failed");
    }
  }

}

did I do something wrong?? when the first running. its ok, but the server side didn’t loop when I press the button.
I’ve running few Serial.print to check where it stops looping. I’ve found out, it is in the function rf22.waitAvailable();
Could some one help me with this.
Many thanks.

Handy

void loop()
{
   while (1)
  {

There is no excuse for running an infinite loop inside an infinite loop.

Can I send array of another data type for this library e.g.(array of int)