Saturday, September 21, 2019

Rfm95 with arduino uno working with serial communication

Rfm95 with arduino uno working with serial communication:

connection with arduino uno

ARDUINO UNO
RFM 95
Gnd
Gnd
3.3v
3.3v
D5
reset
D2
DIO 0
D3
DIO1
D13(SCK)
SCK
D12(MISO)
MISO
D11(MOSI)
MOSI
D10(SS)
NSS



NOTE:
LEVEL SHIFTER FOR BETTER OUTPUT

sender side code:
#include <SPI.h>

#include <RH_RF95.h>

RH_RF95 rf95;
int led = 9;

void setup() 
{
  pinMode(led, OUTPUT);     
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");  
}

void loop()
{
  if (rf95.available())
  {// Should be a message for us now   
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf95.recv(buf, &len))
    {
      digitalWrite(led, HIGH);
     // RH_RF95::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);
      sender_fn();
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  delay(100);
}

void sender_fn()
{
  /***************************************/
      // Send a reply
  /***************************************/
    char data[32]="";
   int availableBytes = Serial.available();
    if(availableBytes>0)
   {
       for(int i=0; i<availableBytes; i++)
       {     
           data[i]=Serial.read();
       } 
          rf95.send(data, sizeof(data));
          rf95.waitPacketSent();
           Serial.print("Sent::");
           Serial.print(data);
           digitalWrite(led, LOW);
        
   }
  }



Receiver side code:


// rf95_client.pde

#include <SPI.h>
#include <RH_RF95.h>

int led = 9;
RH_RF95 rf95;
void setup()
{
  pinMode(led, OUTPUT); 
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
}

void loop()
{
  rfm95_uart();
}

void rfm95_uart()
{
   uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
     uint8_t len = sizeof(buf);
      if (rf95.waitAvailableTimeout(3000))
      {
         //Serial.print(rf95.recv(buf, &len));
           if (rf95.recv(buf, &len)>0)
           {
             Serial.print("got reply: ");
            Serial.println((char*)buf); 
          }
 
          else
          {
             Serial.println("recv failed");
          }
        }
        else
        {
          rfm95_uart_sndr();
            //Serial.println(".");
            //Serial.println("No reply, is rf95_server running?");
       }
  delay(10);
}

void rfm95_uart_sndr()
{
   //Serial.println("Sending to rf95_server::");
   char data[32]="";
   int availableBytes = Serial.available();
    //Serial.print(availableBytes);
   if(availableBytes>0)
   {
       for(int i=0; i<availableBytes; i++)
       {   
           data[i]=Serial.read();
       }
          rf95.send(data, sizeof(data));
          rf95.waitPacketSent();
           Serial.print("Sent::");
           Serial.print(data);
           digitalWrite(led, LOW);
   }

}

output :



for the code download the below link:

https://drive.google.com/file/d/1Hx0noPMxfg8zQdcvZqobS8y8dE7NbxSX/view?usp=sharing

No comments:

Post a Comment

python class topic video