Skip to content
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

222 lines (177 sloc) 6.07 KB
// rf95_client.cpp
// Example program showing how to use RH_RF95 on Raspberry Pi
// Uses the bcm2835 library to access the GPIO pins to drive the RFM95 module
// Requires bcm2835 library to be already installed
// Use the Makefile in this directory:
// cd example/raspi/rf95
// make
// sudo ./rf95_client
// Contributed by Charles-Henri Hallard based on sample RH_NRF24 by Mike Poublon
#include <bcm2835.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RH_RF69.h>
#include <RH_RF95.h>
// define hardware used change to fit your need
// Uncomment the board you have, if not listed
// uncommment custom board and set wiring tin custom section
// LoRasPi board
// see
// iC880A and LinkLab Lora Gateway Shield (if RF module plugged into)
// see
//#define BOARD_IC880A_PLATE
// Raspberri PI Lora Gateway for multiple modules
// see
// Dragino Raspberry PI hat
// see
// Now we include RasPi_Boards.h so this will expose defined
// constants with CS/IRQ/RESET/on board LED pins definition
#include "../RasPiBoards.h"
// Our RFM95 Configuration
#define RF_FREQUENCY 868.00
#define RF_GATEWAY_ID 1
#define RF_NODE_ID 10
// Create an instance of a driver
//RH_RF95 rf95(RF_CS_PIN);
//Flag for Ctrl-C
volatile sig_atomic_t force_exit = false;
void sig_handler(int sig)
printf("\n%s Break received, exiting!\n", __BASEFILE__);
//Main Function
int main (int argc, const char* argv[] )
static unsigned long last_millis;
static unsigned long led_blink = 0;
signal(SIGINT, sig_handler);
printf( "%s\n", __BASEFILE__);
if (!bcm2835_init()) {
fprintf( stderr, "%s bcm2835_init() Failed\n\n", __BASEFILE__ );
return 1;
printf( "RF95 CS=GPIO%d", RF_CS_PIN);
#ifdef RF_LED_PIN
digitalWrite(RF_LED_PIN, HIGH );
#ifdef RF_IRQ_PIN
printf( ", IRQ=GPIO%d", RF_IRQ_PIN );
// IRQ Pin input/pull down
bcm2835_gpio_set_pud(RF_IRQ_PIN, BCM2835_GPIO_PUD_DOWN);
#ifdef RF_RST_PIN
printf( ", RST=GPIO%d", RF_RST_PIN );
// Pulse a reset on module
digitalWrite(RF_RST_PIN, LOW );
digitalWrite(RF_RST_PIN, HIGH );
#ifdef RF_LED_PIN
printf( ", LED=GPIO%d", RF_LED_PIN );
digitalWrite(RF_LED_PIN, LOW );
if (!rf95.init()) {
fprintf( stderr, "\nRF95 module init failed, Please verify wiring/module\n" );
} else {
printf( "\nRF95 module seen OK!\r\n");
#ifdef RF_IRQ_PIN
// Since we may check IRQ line with bcm_2835 Rising edge detection
// In case radio already have a packet, IRQ is high and will never
// go to low so never fire again
// Except if we clear IRQ flags and discard one if any by checking
// Now we can enable Rising edge detection
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
//rf95.setTxPower(23, false);
// If you are using Modtronix inAir4 or inAir9,or any other module which uses the
// transmitter RFO pins and not the PA_BOOST pins
// then you can configure the power transmitter power for -1 to 14 dBm and with useRFO true.
// Failure to do that will result in extremely low transmit powers.
//rf95.setTxPower(14, true);
rf95.setTxPower(14, false);
// You can optionally require this module to wait until Channel Activity
// Detection shows no activity on the channel before transmitting by setting
// the CAD timeout to non-zero:
// Adjust Frequency
rf95.setFrequency( RF_FREQUENCY );
// This is our Node ID
// Where we're sending packet
printf("RF95 node #%d init OK @ %3.2fMHz\n", RF_NODE_ID, RF_FREQUENCY );
last_millis = millis();
//Begin the main body of code
while (!force_exit) {
//printf( "millis()=%ld last=%ld diff=%ld\n", millis() , last_millis, millis() - last_millis );
// Send every 5 seconds
if ( millis() - last_millis > 5000 ) {
last_millis = millis();
#ifdef RF_LED_PIN
led_blink = millis();
digitalWrite(RF_LED_PIN, HIGH);
// Send a message to rf95_server
uint8_t data[] = "Hi Raspi!";
uint8_t len = sizeof(data);
printf("Sending %02d bytes to node #%d => ", len, RF_GATEWAY_ID );
printbuffer(data, len);
printf("\n" );
rf95.send(data, len);
// Now wait for a reply
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.waitAvailableTimeout(1000)) {
// Should be a reply message for us now
if (rf95.recv(buf, &len)) {
printf("got reply: ");
printf("\nRSSI: %d\n", rf95.lastRssi());
} else {
printf("recv failed");
} else {
printf("No reply, is rf95_server running?\n");
#ifdef RF_LED_PIN
// Led blink timer expiration ?
if (led_blink && millis()-led_blink>200) {
led_blink = 0;
digitalWrite(RF_LED_PIN, LOW);
// Let OS doing other tasks
// Since we do nothing until each 5 sec
#ifdef RF_LED_PIN
digitalWrite(RF_LED_PIN, LOW );
printf( "\n%s Ending\n", __BASEFILE__ );
return 0;
You can’t perform that action at this time.