first commit

This commit is contained in:
Lazarewicz Julien
2025-07-22 15:27:00 +02:00
commit 6c6451c92c
205 changed files with 44418 additions and 0 deletions

53
examples/raspi/Makefile Normal file
View File

@@ -0,0 +1,53 @@
# Makefile
# Sample for RH_NRF24 on Raspberry Pi
# Caution: requires bcm2835 library to be already installed
# http://www.airspayce.com/mikem/bcm2835/
CC = g++
CFLAGS = -DRASPBERRY_PI -DBCM2835_NO_DELAY_COMPATIBILITY
LIBS = -lbcm2835
RADIOHEADBASE = ../..
INCLUDE = -I$(RADIOHEADBASE)
all: RasPiRH
RasPi.o: $(RADIOHEADBASE)/RHutil/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil/RasPi.cpp $(INCLUDE)
RasPiRH.o: RasPiRH.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_NRF24.o: $(RADIOHEADBASE)/RH_NRF24.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHMesh.o: $(RADIOHEADBASE)/RHMesh.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHNRFSPIDriver.o: $(RADIOHEADBASE)/RHNRFSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RasPiRH: RasPiRH.o RH_NRF24.o RHMesh.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHNRFSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o RasPiRH
clean:
rm -rf *.o RasPiRH

138
examples/raspi/RasPiRH.cpp Normal file
View File

@@ -0,0 +1,138 @@
// RasPiRH.cpp
//
// Example program showing how to use RH_NRF24 on Raspberry Pi
// Uses the bcm2835 library to access the GPIO pins to drive the NRF24L01
// Requires bcm2835 library to be already installed
// http://www.airspayce.com/mikem/bcm2835/
// Use the Makefile in this directory:
// cd example/raspi
// make
// sudo ./RasPiRH
//
// Creates a RHReliableDatagram manager and listens and prints for reliable datagrams
// sent to it on the default Channel 2.
//
// Contributed by Mike Poublon
#include <bcm2835.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
//Function Definitions
void sig_handler(int sig);
void printbuffer(uint8_t buff[], int len);
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Create an instance of a driver
// Chip enable is pin 22
// Slave Select is pin 24
RH_NRF24 nrf24(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24);
RHReliableDatagram manager(nrf24, SERVER_ADDRESS);
//Flag for Ctrl-C
volatile sig_atomic_t flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
signal(SIGINT, sig_handler);
if (!bcm2835_init())
{
printf( "\n\nRasPiRH Tester Startup Failed\n\n" );
return 1;
}
printf( "\nRasPiRH Tester Startup\n\n" );
/* Begin Driver Only Init Code
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
End Driver Only Init Code */
/* Begin Reliable Datagram Init Code */
if (!manager.init())
{
printf( "Init failed\n" );
}
/* End Reliable Datagram Init Code */
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
//Begin the main body of code
while (true)
{
uint8_t len = sizeof(buf);
uint8_t from, to, id, flags;
/* Begin Driver Only code
if (nrf24.available())
{
// Should be a message for us now
//uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len))
{
Serial.print("got request: ");
Serial.println((char*)buf);
Serial.println("");
}
else
{
Serial.println("recv failed");
}
}
End Driver Only Code*/
/* Begin Reliable Datagram Code */
if (manager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
}
/* End Reliable Datagram Code */
if (flag)
{
printf("\n---CTRL-C Caught - Exiting---\n");
break;
}
//sleep(1);
delay(25);
}
printf( "\nRasPiRH Tester Ending\n" );
bcm2835_close();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}
void printbuffer(uint8_t buff[], int len)
{
for (int i = 0; i< len; i++)
{
printf(" %2X", buff[i]);
}
}

View File

@@ -0,0 +1,84 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CROSSCOMPILER=$(shell if [ -z `which 'arm-linux-gnueabihf-g++'` ]; then echo "no"; else echo "yes"; fi)
ifeq ($(CROSSCOMPILER),yes)
CC = arm-linux-gnueabihf-g++
CFLAGS = -DRASPBERRY_PI -DRH_USE_MUTEX -pthread
RADIOHEADBASE = ../../../..
PIGPIOBASE = ../../../../../pigpio/
SHARED = ../shared/
INCLUDE = -I$(RADIOHEADBASE) -I$(PIGPIOBASE) -I$(SHARED)
LIBS = -lpigpio -lrt -L$(PIGPIOBASE) -lpthread -D_REENTRANT
else
CC = g++
CFLAGS = -DRASPBERRY_PI -DRH_USE_MUTEX -pthread
RADIOHEADBASE = ../../../..
PIGPIOBASE = ../../../../../pigpio/
SHARED = ../shared/
INCLUDE = -I$(RADIOHEADBASE) -I$(PIGPIOBASE) -I$(SHARED)
LIBS = -lpigpio -lrt -L$(PIGPIOBASE) -lpthread -D_REENTRANT
endif
ifeq ($(REGION),Europe)
CFLAGS += -DEUROPE
endif
ifeq ($(LORABOARD),DraginoLoraGPSHat)
CFLAGS += -DDRAGINO_GPS_HAT
endif
ifeq ($(DEBUG),1)
CFLAGS += -g3
endif
all: rf95_client1 rf95_client2
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
help_functions.o: $(SHARED)/help_functions.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_client1.o: rf95_client1.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_client2.o: rf95_client2.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
gpsMT3339.o: $(SHARED)/gpsMT3339.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_client1: rf95_client1.o RH_RF95.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_client1
rf95_client2: rf95_client2.o help_functions.o RH_RF95.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o gpsMT3339.o
$(CC) $^ $(LIBS) -o rf95_client2
clean:
rm -rf *.o rf95_client1 rf95_client2
deploy: rf95_client1 rf95_client2
sshpass -p"$(PASSWD)" scp rf95_client1 pi@$(HOST):/home/pi
sshpass -p"$(PASSWD)" scp rf95_client2 pi@$(HOST):/home/pi

View File

@@ -0,0 +1,161 @@
// rf95_client.cpp
// -*- mode: C++ -*-
// Example app showing how to create a simple messaging client
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf95_server.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_client.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_client.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#ifdef DRAGINO_GPS_HAT
// Pin definitions for lora dragino gps hat
#define RFM95_CS_PIN 25 // Slave Select on GPIO25 so P1 connector pin #22
#define RFM95_IRQ_PIN 4 // IRQ on GPIO4 so P1 connector pin #7
#define RFM95_RESET_PIN 17 // Reset on GPIO17 so P1 connector pin #11
#undef RFM95_LED // Dragino Board as no LED to drive
#else
//Pin definitions for other board
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
#endif
//Client and Server Addresses
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
//RFM95 Configuration
#ifdef EUROPE
#define RFM95_FREQUENCY 868.00
#else
#define RFM95_FREQUENCY 915.00
#endif
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_client startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_client startup OK.\n" );
printf( "\nRPI GPIO settings:\n" );
printf("CS-> GPIO %d\n", (uint8_t) RFM95_CS_PIN);
printf("IRQ-> GPIO %d\n", (uint8_t) RFM95_IRQ_PIN);
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!rf95.init())
{
printf( "\n\nRF95 driver failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client(This) Address= %d\n", CLIENT_ADDRESS);
printf("Server Address= %d\n", SERVER_ADDRESS);
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(CLIENT_ADDRESS);
rf95.setHeaderFrom(CLIENT_ADDRESS);
rf95.setHeaderTo(SERVER_ADDRESS);
rf95.setModemConfig(RH_RF95::Bw125Cr48Sf4096);
/* End Manager/Driver settings code */
/* Begin Datagram Client Code */
while(!flag)
{
Serial.println("Sending to rf95_server");
// Send a message to rf95_server
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
uint8_t data[] = "Hello World!";
rf95.send(data, sizeof(data));
rf95.waitPacketSent();
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
// Now wait for a reply
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.waitAvailableTimeout(3000))
{
// Should be a reply message for us now
if (rf95.recv(buf, &len))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got reply: ");
Serial.println((char*)buf);
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
else
{
Serial.println("recv failed");
}
}
else
{
Serial.println("No reply, is rf95_server running?");
}
gpioDelay(400000);
}
printf( "\nrf95_client Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,250 @@
// rf95_client.cpp
// -*- mode: C++ -*-
// Example app showing how to create a simple messaging client
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf95_server.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_client.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_client.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RH_RF95.h>
#include <help_functions.h>
//Function Definitions
void sig_handler(int sig);
#ifdef DRAGINO_GPS_HAT
pthread_mutex_t gps_data_mutex;
#include <gpsMT3339.h>
// Pin definitions for dragino gps hat
#define RFM95_CS_PIN 25 // Slave Select on GPIO25 so P1 connector pin #22
#define RFM95_IRQ_PIN 4 // IRQ on GPIO4 so P1 connector pin #7
#define RFM95_RESET_PIN 17 // Reset on GPIO17 so P1 connector pin #11
#undef RFM95_LED // Dragino Board as no LED to drive
#else
//Pin definitions for other board
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
#endif
//Client and Server Addresses
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
//RFM95 Configuration
#ifdef EUROPE
#define RFM95_FREQUENCY 868.00
#else
#define RFM95_FREQUENCY 915.00
#endif
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
#ifdef DRAGINO_GPS_HAT
gps_MT3339 gps(GPS_DEVICE, &gps_data_mutex);
#endif
//Flag for Ctrl-C
int flag = 0;
// function to handle Lora device
void* lora_main(void* ptr)
{
#ifndef DRAGINO_GPS_HAT
char temp[10];
uint8_t data[] = "Hello World(xxxxx)!";
#else
uint8_t data[50];
#endif
uint16_t sequenceCounter = 0;
printf("\nLORA Handler started\n");
print_scheduler();
print_scope();
while(!flag)
{
// printf("Sending to rf95_server");
// Send a message to rf95_server
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
#ifndef DRAGINO_GPS_HAT
snprintf(temp,6,"%05d",sequenceCounter);
strncpy((char*)data+12,temp,5);
#else
pthread_mutex_lock(&gps_data_mutex);
snprintf((char*)data,45,"(%05d):%10s,%10s,%11s",sequenceCounter,gps.getTimestamp(),gps.getLatitude(),gps.getLongitude());
pthread_mutex_unlock(&gps_data_mutex);
#endif
printf("Message=\"");
printf("%s",(char*)data);
printf("\"\n");
rf95.send(data, sizeof(data));
sequenceCounter++;
rf95.waitPacketSent();
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
// Now wait for a reply
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf95.waitAvailableTimeout(5000))
{
// Should be a reply message for us now
if (rf95.recv(buf, &len))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
printf("got reply: ");
printf("\"%s\"\n",(char*)buf);
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
else
{
printf("recv failed");
}
}
else
{
printf("No reply, is rf95_server running?\n");
}
#ifdef DRAGINO_GPS_HAT
pthread_yield();
#endif
}
}
//Main Function
int main (int argc, const char* argv[] )
{
// startup
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_client startup Failed.\n" );
gpioTerminate();
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_client startup OK.\n" );
printf( "\nRPI GPIO settings:\n" );
printf("CS-> GPIO %d\n", (uint8_t) RFM95_CS_PIN);
printf("IRQ-> GPIO %d\n", (uint8_t) RFM95_IRQ_PIN);
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
#ifdef RFM95_RESET_PIN
printf( "RST-> GPIO %d\n", (uint8_t) RFM95_RESET_PIN );
// Pulse a reset on module
gpioSetMode(RFM95_RESET_PIN, PI_OUTPUT);
digitalWrite(RFM95_RESET_PIN, LOW );
gpioDelay(150);
digitalWrite(RFM95_RESET_PIN, HIGH );
gpioDelay(100);
#endif
if (!rf95.init())
{
printf( "\n\nRF95 driver failed to initialize.\n\n" );
gpioTerminate();
return 1;
}
printf("\nRFM 95 Device Version:0x%x\n",rf95.getDeviceVersion());
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client(This) Address= %d\n", CLIENT_ADDRESS);
printf("Server Address= %d\n", SERVER_ADDRESS);
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(CLIENT_ADDRESS);
rf95.setHeaderFrom(CLIENT_ADDRESS);
rf95.setHeaderTo(SERVER_ADDRESS);
rf95.setModemConfig(RH_RF95::Bw125Cr48Sf4096);
/* End Manager/Driver settings code */
/* Begin Datagram Client Code */
#ifdef DRAGINO_GPS_HAT
pthread_t read_gps_thread, lora_thread;
int iret;
pthread_mutex_init(&gps_data_mutex,NULL);
iret = pthread_create( &read_gps_thread, NULL, ((void* (*)(void*))&gps_MT3339::read_gps), &gps);
if (iret != 0)
printf("\nCould not start thread to read gps data\n");
iret = pthread_create( &lora_thread, NULL, lora_main, NULL);
if (iret != 0)
printf("\nCould not start thread to handle lora\n");
print_scheduler();
#endif
// main loop
while(!flag)
{
#ifndef DRAGINO_GPS_HAT
lora_main(NULL);
#else
sleep(1);
#endif
}
printf( "\nrf95_client Tester Ending\n" );
#ifdef DRAGINO_GPS_HAT
pthread_join(lora_thread,NULL);
pthread_join(read_gps_thread,NULL);
#endif
gpioTerminate();
return 0;
}
// signal handler terminating the program on CTRL-c
void sig_handler(int sig)
{
flag=1;
#ifdef DRAGINO_GPS_HAT
gps.stop();
#endif
}

View File

@@ -0,0 +1,52 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_mesh_client
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_mesh_client.o: rf95_mesh_client.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHMesh.o: $(RADIOHEADBASE)/RHMesh.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_mesh_client: rf95_mesh_client.o RH_RF95.o RHMesh.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_mesh_client
clean:
rm -rf *.o rf95_mesh_client

View File

@@ -0,0 +1,151 @@
// rf95_mesh_client.cpp
// -*- mode: C++ -*-
// Example application showing how to create a simple addressed, routed reliable messaging client
// with the RHMesh class.
// It is designed to work with the other examples rf95_mesh_server*
// Hint: you can simulate other network topologies by setting the
// RH_TEST_NETWORK define in RHRouter.h
// Mesh has much greater memory requirements, and you may need to limit the
// max message length to prevent wierd crashes
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_mesh_client.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_mesh_client.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHMesh.h>
#include <RH_RF95.h>
#define RH_MESH_MAX_MESSAGE_LEN 50
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHMesh manager(rf95, CLIENT_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_mesh_client startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_mesh_client startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nMesh Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client(This) Address= %d\n", CLIENT_ADDRESS);
printf("Server Address 1= %d\n", SERVER1_ADDRESS);
printf("Server Address 2= %d\n", SERVER2_ADDRESS);
printf("Server Address 3= %d\n", SERVER3_ADDRESS);
printf("Route: Client->Server 3 is automatic in MESH.\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
/* End Manager/Driver settings code */
uint8_t data[] = "Hello World!";
// Dont put this on the stack:
uint8_t buf[RH_MESH_MAX_MESSAGE_LEN];
while(!flag)
{
Serial.println("Sending to manager_mesh_server3");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
// Send a message to a rf95_mesh_server
// A route to the destination will be automatically discovered.
if (manager.sendtoWait(data, sizeof(data), SERVER3_ADDRESS) == RH_ROUTER_ERROR_NONE)
{
// It has been reliably delivered to the next node.
// Now wait for a reply from the ultimate server
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAckTimeout(buf, &len, 3000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is rf95_mesh_server1, rf95_mesh_server2 and rf95_mesh_server3 running?");
}
}
else
Serial.println("sendtoWait failed. Are the intermediate mesh servers running?");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
gpioDelay(400000);
}
printf( "\nrf95_mesh_client Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,52 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_mesh_server1
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_mesh_server1.o: rf95_mesh_server1.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHMesh.o: $(RADIOHEADBASE)/RHMesh.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_mesh_server1: rf95_mesh_server1.o RH_RF95.o RHMesh.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_mesh_server1
clean:
rm -rf *.o rf95_mesh_server1

View File

@@ -0,0 +1,138 @@
// rf95_mesh_server1.cpp
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, routed reliable messaging server
// with the RHMesh class.
// It is designed to work with the other examples rf95_mesh_*
// Hint: you can simulate other network topologies by setting the
// RH_TEST_NETWORK define in RHRouter.h
// Mesh has much greater memory requirements, and you may need to limit the
// max message length to prevent wierd crashes
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_mesh_server1.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_mesh_server1.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHMesh.h>
#include <RH_RF95.h>
#define RH_MESH_MAX_MESSAGE_LEN 50
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHMesh manager(rf95, SERVER1_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_mesh_server1 startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_mesh_server1 startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nMesh Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server(This) Address 1= %d\n", SERVER1_ADDRESS);
printf("Server Address 2= %d\n", SERVER2_ADDRESS);
printf("Server Address 3= %d\n", SERVER3_ADDRESS);
printf("Route: Client->Server 3 is automatic in MESH.\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
/* End Manager/Driver settings code */
uint8_t data[] = "And hello back to you from server1";
// Dont put this on the stack:
uint8_t buf[RH_MESH_MAX_MESSAGE_LEN];
while(!flag)
{
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (manager.sendtoWait(data, sizeof(data), from) != RH_ROUTER_ERROR_NONE)
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
}
printf( "\nrf95_mesh_server1 Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,52 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_mesh_server2
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_mesh_server2.o: rf95_mesh_server2.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHMesh.o: $(RADIOHEADBASE)/RHMesh.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_mesh_server2: rf95_mesh_server2.o RH_RF95.o RHMesh.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_mesh_server2
clean:
rm -rf *.o rf95_mesh_server2

View File

@@ -0,0 +1,138 @@
// rf95_mesh_server2.cpp
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, routed reliable messaging server
// with the RHMesh class.
// It is designed to work with the other examples rf95_mesh_*
// Hint: you can simulate other network topologies by setting the
// RH_TEST_NETWORK define in RHRouter.h
// Mesh has much greater memory requirements, and you may need to limit the
// max message length to prevent wierd crashes
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_mesh_server2.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_mesh_server2.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHMesh.h>
#include <RH_RF95.h>
#define RH_MESH_MAX_MESSAGE_LEN 50
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHMesh manager(rf95, SERVER2_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_mesh_server2 startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_mesh_server2 startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nMesh Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server Address 1= %d\n", SERVER1_ADDRESS);
printf("Server(This) Address 2= %d\n", SERVER2_ADDRESS);
printf("Server Address 3= %d\n", SERVER3_ADDRESS);
printf("Route: Client->Server 3 is automatic in MESH.\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
/* End Manager/Driver settings code */
uint8_t data[] = "And hello back to you from server2";
// Dont put this on the stack:
uint8_t buf[RH_MESH_MAX_MESSAGE_LEN];
while(!flag)
{
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (manager.sendtoWait(data, sizeof(data), from) != RH_ROUTER_ERROR_NONE)
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
}
printf( "\nrf95_mesh_server2 Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,52 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_mesh_server3
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_mesh_server3.o: rf95_mesh_server3.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHMesh.o: $(RADIOHEADBASE)/RHMesh.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_mesh_server3: rf95_mesh_server3.o RH_RF95.o RHMesh.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_mesh_server3
clean:
rm -rf *.o rf95_mesh_server3

View File

@@ -0,0 +1,138 @@
// rf95_mesh_server3.cpp
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, routed reliable messaging server
// with the RHMesh class.
// It is designed to work with the other examples rf95_mesh_*
// Hint: you can simulate other network topologies by setting the
// RH_TEST_NETWORK define in RHRouter.h
// Mesh has much greater memory requirements, and you may need to limit the
// max message length to prevent wierd crashes
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_mesh_server3.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_mesh_server3.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHMesh.h>
#include <RH_RF95.h>
#define RH_MESH_MAX_MESSAGE_LEN 50
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHMesh manager(rf95, SERVER3_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_mesh_server3 startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_mesh_server2 startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nMesh Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server Address 1= %d\n", SERVER1_ADDRESS);
printf("Server Address 2= %d\n", SERVER2_ADDRESS);
printf("Server(This) Address 3= %d\n", SERVER3_ADDRESS);
printf("Route: Client->Server 3 is automatic in MESH.\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
/* End Manager/Driver settings code */
uint8_t data[] = "And hello back to you from server3";
// Dont put this on the stack:
uint8_t buf[RH_MESH_MAX_MESSAGE_LEN];
while(!flag)
{
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (manager.sendtoWait(data, sizeof(data), from) != RH_ROUTER_ERROR_NONE)
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
}
printf( "\nrf95_mesh_server3 Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,46 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_reliable_datagram_client
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_reliable_datagram_client.o: rf95_reliable_datagram_client.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_reliable_datagram_client: rf95_reliable_datagram_client.o RH_RF95.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_reliable_datagram_client
clean:
rm -rf *.o rf95_reliable_datagram_client

View File

@@ -0,0 +1,141 @@
// rf95_reliable_datagram_client.cpp
// -*- mode: C++ -*-
// Example app showing how to create a simple addressed, reliable messaging client
// with the RHReliableDatagram class, using the RH_RF95 driver to control a RF95 radio.
// It is designed to work with the other example rf95_reliable_datagram_server.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_reliable_datagram_client.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_reliable_datagram_client.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
//Client and Server Addresses
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(rf95, CLIENT_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_reliable_datagram_client startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_reliable_datagram_client startup OK.\n" );
printf( "\nRPI GPIO settings:\n" );
printf("CS-> GPIO %d\n", (uint8_t) RFM95_CS_PIN);
printf("IRQ-> GPIO %d\n", (uint8_t) RFM95_IRQ_PIN);
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!rf95.init())
{
printf( "\n\nRF95 Driver Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client(This) Address= %d\n", CLIENT_ADDRESS);
printf("Server Address= %d\n", SERVER_ADDRESS);
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(CLIENT_ADDRESS);
rf95.setHeaderFrom(CLIENT_ADDRESS);
rf95.setHeaderTo(SERVER_ADDRESS);
/* End Manager/Driver settings code */
/* Begin Datagram Client Code */
uint8_t data[] = "Hello World!";
// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
while(!flag)
{
Serial.println("Sending to rf95_reliable_datagram_server");
// Send a message to manager_server
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
{
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is rf95_reliable_datagram_server running?");
}
}
else
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
gpioDelay(500000);
}
printf( "\nrf95_reliable_datagram_client Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,46 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_reliable_datagram_server
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_reliable_datagram_server.o: rf95_reliable_datagram_server.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_reliable_datagram_server: rf95_reliable_datagram_server.o RH_RF95.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_reliable_datagram_server
clean:
rm -rf *.o rf95_reliable_datagram_server

View File

@@ -0,0 +1,136 @@
// rf95_reliable_datagram_server.cpp
// -*- mode: C++ -*-
// Example app showing how to create a simple addressed, reliable messaging server
// with the RHReliableDatagram class, using the RH_RF95 driver to control a RF95 radio.
// It is designed to work with the other example rf95_reliable_datagram_client.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_reliable_datagram_server.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_reliable_datagram_server.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHReliableDatagram.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
//Client and Server Addresses
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(rf95, SERVER_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_reliable_datagram_server startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_reliable_datagram_server startup OK.\n" );
printf( "\nRPI GPIO settings:\n" );
printf("CS-> GPIO %d\n", (uint8_t) RFM95_CS_PIN);
printf("IRQ-> GPIO %d\n", (uint8_t) RFM95_IRQ_PIN);
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!rf95.init())
{
printf( "\n\nRF95 Driver Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server(This) Address= %d\n", SERVER_ADDRESS);
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(SERVER_ADDRESS);
rf95.setHeaderFrom(SERVER_ADDRESS);
/* End Manager/Driver settings code */
/* Begin Reliable Datagram Server Code */
uint8_t data[] = "And hello back to you";
// Dont put this on the stack:
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
while(!flag)
{
if (manager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (!manager.sendtoWait(data, sizeof(data), from))
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
}
}
printf( "\nrf95_reliable_datagram_server Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,49 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_router_client
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_router_client.o: rf95_router_client.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_router_client: rf95_router_client.o RH_RF95.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_router_client
clean:
rm -rf *.o rf95_router_client

View File

@@ -0,0 +1,155 @@
// rf95_router_client.cpp
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, routed reliable messaging client
// with the RHRouter class.
// It is designed to work with the other examples rf95_router_server*.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_router_client.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_router_client.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHRouter.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
// messages are routed via intermediate nodes to their destination
// node. All nodes can act as routers
// CLIENT_ADDRESS <-> SERVER1_ADDRESS <-> SERVER2_ADDRESS<->SERVER3_ADDRESS
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Create an instance of a driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHRouter manager(rf95, CLIENT_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_router_client startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_router_client startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nRouter Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client(This) Address= %d\n", CLIENT_ADDRESS);
printf("Server Address 1= %d\n", SERVER1_ADDRESS);
printf("Server Address 2= %d\n", SERVER2_ADDRESS);
printf("Server Address 3= %d\n", SERVER3_ADDRESS);
printf("Route: Client-> Server 1-> Server 2-> Server 3\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
// Manually define the routes for this network
manager.addRouteTo(SERVER1_ADDRESS, SERVER1_ADDRESS);
manager.addRouteTo(SERVER2_ADDRESS, SERVER2_ADDRESS);
manager.addRouteTo(SERVER3_ADDRESS, SERVER3_ADDRESS);
/* End Manager/Driver settings code */
uint8_t data[] = "Hello World!";
// Dont put this on the stack:
uint8_t buf[RH_ROUTER_MAX_MESSAGE_LEN];
while(!flag)
{
Serial.println("Sending to rf95_router_server3");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
// Send a message to a rf95_router_server
// It will be routed by the intermediate
// nodes to the destination node, accorinding to the
// routing tables in each node
if (manager.sendtoWait(data, sizeof(data), SERVER3_ADDRESS) == RH_ROUTER_ERROR_NONE)
{
// It has been reliably delivered to the next node.
// Now wait for a reply from the ultimate server
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAckTimeout(buf, &len, 3000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is rf95_router_server1, rf95_router_server2 and rf95_router_server3 running?");
}
}
else
Serial.println("sendtoWait failed. Are the intermediate router servers running?");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
gpioDelay(500000);
}
printf( "\nrf95_router_client Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,49 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_router_server1
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_router_server1.o: rf95_router_server1.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_router_server1: rf95_router_server1.o RH_RF95.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_router_server1
clean:
rm -rf *.o rf95_router_server1

View File

@@ -0,0 +1,143 @@
// rf95_router_server1.cpp
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, routed reliable messaging server
// with the RHRouter class.
// It is designed to work with the other example rf95_router_client.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_router_server1.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_router_server1.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHRouter.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
// messages are routed via intermediate nodes to their destination
// node. All nodes can act as routers
// CLIENT_ADDRESS <-> SERVER1_ADDRESS <-> SERVER2_ADDRESS<->SERVER3_ADDRESS
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Create an instance of a driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHRouter manager(rf95, SERVER1_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_router_server1 startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_router_server1 startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nRouter Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server(This) Address 1= %d\n", SERVER1_ADDRESS);
printf("Server Address 2= %d\n", SERVER2_ADDRESS);
printf("Server Address 3= %d\n", SERVER3_ADDRESS);
printf("Route: Client-> Server 1-> Server 2-> Server 3\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(SERVER1_ADDRESS);
// Manually define the routes for this network
manager.addRouteTo(CLIENT_ADDRESS, CLIENT_ADDRESS);
manager.addRouteTo(SERVER2_ADDRESS, SERVER2_ADDRESS);
manager.addRouteTo(SERVER3_ADDRESS, SERVER2_ADDRESS);
/* End Manager/Driver settings code */
uint8_t data[] = "And hello back to you from server1";
// Dont put this on the stack:
uint8_t buf[RH_ROUTER_MAX_MESSAGE_LEN];
while(!flag)
{
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (manager.sendtoWait(data, sizeof(data), from) != RH_ROUTER_ERROR_NONE)
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
}
printf( "\nrf95_router_server1 Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,49 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_router_server2
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_router_server2.o: rf95_router_server2.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_router_server2: rf95_router_server2.o RH_RF95.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_router_server2
clean:
rm -rf *.o rf95_router_server2

View File

@@ -0,0 +1,143 @@
// rf95_router_server2.cpp
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, routed reliable messaging server
// with the RHRouter class.
// It is designed to work with the other example rf95_router_client.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_router_server2.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_router_server2.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHRouter.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
// messages are routed via intermediate nodes to their destination
// node. All nodes can act as routers
// CLIENT_ADDRESS <-> SERVER1_ADDRESS <-> SERVER2_ADDRESS<->SERVER3_ADDRESS
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Create an instance of a driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHRouter manager(rf95, SERVER2_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_router_server2 startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_router_server2 startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nRouter Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server Address 1 = %d\n", SERVER1_ADDRESS);
printf("Server(This) Address 2 = %d\n", SERVER2_ADDRESS);
printf("Server Address 3= %d\n", SERVER3_ADDRESS);
printf("Route: Client-> Server 1-> Server 2-> Server 3\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(SERVER2_ADDRESS);
// Manually define the routes for this network
manager.addRouteTo(CLIENT_ADDRESS, CLIENT_ADDRESS);
manager.addRouteTo(SERVER2_ADDRESS, SERVER2_ADDRESS);
manager.addRouteTo(SERVER3_ADDRESS, SERVER2_ADDRESS);
/* End Manager/Driver settings code */
uint8_t data[] = "And hello back to you from server2";
// Dont put this on the stack:
uint8_t buf[RH_ROUTER_MAX_MESSAGE_LEN];
while(!flag)
{
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (manager.sendtoWait(data, sizeof(data), from) != RH_ROUTER_ERROR_NONE)
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
}
printf( "\nrf95_router_server2 Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,49 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_router_server3
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_router_server3.o: rf95_router_server3.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_router_server3: rf95_router_server3.o RH_RF95.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_router_server3
clean:
rm -rf *.o rf95_router_server3

View File

@@ -0,0 +1,143 @@
// rf95_router_server3.cpp
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, routed reliable messaging server
// with the RHRouter class.
// It is designed to work with the other example rf95_router_client.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_router_server3.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_router_server3.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHRouter.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
// In this small artifical network of 4 nodes,
// messages are routed via intermediate nodes to their destination
// node. All nodes can act as routers
// CLIENT_ADDRESS <-> SERVER1_ADDRESS <-> SERVER2_ADDRESS<->SERVER3_ADDRESS
#define CLIENT_ADDRESS 1
#define SERVER1_ADDRESS 2
#define SERVER2_ADDRESS 3
#define SERVER3_ADDRESS 4
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Create an instance of a driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHRouter manager(rf95, SERVER3_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_router_server3 startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_router_server3 startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nRouter Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server Address 1 = %d\n", SERVER1_ADDRESS);
printf("Server Address 2 = %d\n", SERVER2_ADDRESS);
printf("Server(This) Address 3 = %d\n", SERVER3_ADDRESS);
printf("Route: Client-> Server 1-> Server 2-> Server 3\n");
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(SERVER3_ADDRESS);
// Manually define the routes for this network
manager.addRouteTo(CLIENT_ADDRESS, CLIENT_ADDRESS);
manager.addRouteTo(SERVER2_ADDRESS, SERVER2_ADDRESS);
manager.addRouteTo(SERVER3_ADDRESS, SERVER2_ADDRESS);
/* End Manager/Driver settings code */
uint8_t data[] = "And hello back to you from server3";
// Dont put this on the stack:
uint8_t buf[RH_ROUTER_MAX_MESSAGE_LEN];
while(!flag)
{
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (manager.sendtoWait(data, sizeof(data), from) != RH_ROUTER_ERROR_NONE)
Serial.println("sendtoWait failed");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
}
printf( "\nrf95_router_server3 Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,49 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CC = g++
CFLAGS = -DRASPBERRY_PI -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
INCLUDE = -I$(RADIOHEADBASE)
all: rf95_router_test
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
rf95_router_test.o: rf95_router_test.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHRouter.o: $(RADIOHEADBASE)/RHRouter.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHReliableDatagram.o: $(RADIOHEADBASE)/RHReliableDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_router_test: rf95_router_test.o RH_RF95.o RHRouter.o RHReliableDatagram.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o
$(CC) $^ $(LIBS) -o rf95_router_test
clean:
rm -rf *.o rf95_router_test

View File

@@ -0,0 +1,183 @@
// rf95_router_test.cpp
// -*- mode: C++ -*-
//
// Test code used during library development, showing how
// to do various things, and how to call various functions
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_router_test.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(10/6/2019) Contributed by Brody M. Based off rf22_router_tester.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RHRouter.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
void test_tx(void);
void test_routes(void);
uint8_t data[] = "Hello World!";
// Dont put this on the stack:
//uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
#define CLIENT_ADDRESS 1
#define ROUTER_ADDRESS 2
#define SERVER_ADDRESS 3
//RFM95 Configuration
#define RFM95_FREQUENCY 915.00
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
// Class to manage message delivery and receipt, using the driver declared above
RHRouter manager(rf95, CLIENT_ADDRESS);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_router_tester startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_router_tester startup OK.\n" );
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("\nINFO: LED on GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!manager.init())
{
printf( "\n\nRouter Manager Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client(This) Address= %d\n", CLIENT_ADDRESS);
printf("Router Address = %d\n", ROUTER_ADDRESS);
printf("Server Address = %d\n", SERVER_ADDRESS);
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
/* End Manager/Driver settings code */
while(!flag)
{
Serial.println("Running test function...");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
// test_routes();
test_tx();
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
gpioDelay(500000);
}
printf( "\nrf95_router_test Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}
void test_routes()
{
manager.clearRoutingTable();
// manager.printRoutingTable();
manager.addRouteTo(1, 101);
manager.addRouteTo(2, 102);
manager.addRouteTo(3, 103);
RHRouter::RoutingTableEntry* e;
e = manager.getRouteTo(0);
if (e) // Should fail
Serial.println("getRouteTo 0 failed");
e = manager.getRouteTo(1);
if (!e)
Serial.println("getRouteTo 1 failed");
if (e->dest != 1)
Serial.println("getRouteTo 2 failed");
if (e->next_hop != 101)
Serial.println("getRouteTo 3 failed");
if (e->state != RHRouter::Valid)
Serial.println("getRouteTo 4 failed");
e = manager.getRouteTo(2);
if (!e)
Serial.println("getRouteTo 5 failed");
if (e->dest != 2)
Serial.println("getRouteTo 6 failed");
if (e->next_hop != 102)
Serial.println("getRouteTo 7 failed");
if (e->state != RHRouter::Valid)
Serial.println("getRouteTo 8 failed");
if (!manager.deleteRouteTo(1))
Serial.println("deleteRouteTo 1 failed");
// Route to 1 should now be gone
e = manager.getRouteTo(1);
if (e)
Serial.println("deleteRouteTo 2 failed");
Serial.println("-------------------");
// manager.printRoutingTable();
delay(500);
}
void test_tx()
{
manager.addRouteTo(SERVER_ADDRESS, ROUTER_ADDRESS);
uint8_t errorcode;
errorcode = manager.sendtoWait(data, sizeof(data), 100); // Should fail with no route
if (errorcode != RH_ROUTER_ERROR_NO_ROUTE)
Serial.println("sendtoWait 1 failed");
errorcode = manager.sendtoWait(data, 255, 10); // Should fail too big
if (errorcode != RH_ROUTER_ERROR_INVALID_LENGTH)
Serial.println("sendtoWait 2 failed");
errorcode = manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS); // Should fail after timeouts to 110
if (errorcode != RH_ROUTER_ERROR_UNABLE_TO_DELIVER)
Serial.println("sendtoWait 3 failed");
Serial.println("-------------------");
delay(500);
}

View File

@@ -0,0 +1,84 @@
# Makefile
# Example for RH_RF95 on Raspberry Pi
# Requires pigpio to be installed: http://abyz.me.uk/rpi/pigpio/
CROSSCOMPILER=$(shell if [ -z `which 'arm-linux-gnueabihf-g++'` ]; then echo "no"; else echo "yes"; fi)
ifeq ($(CROSSCOMPILER),yes)
CC = arm-linux-gnueabihf-g++
CFLAGS = -DRASPBERRY_PI -DRH_USE_MUTEX -pthread
LIBS = -lpigpio -lrt -L$(PIGPIOBASE) -lpthread -D_REENTRANT
RADIOHEADBASE = ../../../..
PIGPIOBASE = ../../../../../pigpio/
SHARED = ../shared/
INCLUDE = -I$(RADIOHEADBASE) -I$(PIGPIOBASE) -I$(SHARED)
else
CC = g++
CFLAGS = -DRASPBERRY_PI -DRH_USE_MUTEX -pthread
LIBS = -lpigpio -lrt
RADIOHEADBASE = ../../../..
PIGPIOBASE = ../../../../../pigpio/
SHARED = ../shared/
INCLUDE = -I$(RADIOHEADBASE) -I$(PIGPIOBASE) -I$(SHARED)
endif
ifeq ($(REGION),Europe)
CFLAGS += -DEUROPE
endif
ifeq ($(LORABOARD),DraginoLoraGPSHat)
CFLAGS += -DDRAGINO_GPS_HAT
endif
ifeq ($(DEBUG),1)
CFLAGS += -g3
endif
all: rf95_server1 rf95_server2
RasPi.o: $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp
$(CC) $(CFLAGS) -c $(RADIOHEADBASE)/RHutil_pigpio/RasPi.cpp $(INCLUDE)
help_functions.o: $(SHARED)/help_functions.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_server1.o: rf95_server1.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_server2.o: rf95_server2.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RH_RF95.o: $(RADIOHEADBASE)/RH_RF95.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHDatagram.o: $(RADIOHEADBASE)/RHDatagram.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHHardwareSPI.o: $(RADIOHEADBASE)/RHHardwareSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHSPIDriver.o: $(RADIOHEADBASE)/RHSPIDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericDriver.o: $(RADIOHEADBASE)/RHGenericDriver.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
RHGenericSPI.o: $(RADIOHEADBASE)/RHGenericSPI.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
gpsMT3339.o: $(SHARED)/gpsMT3339.cpp
$(CC) $(CFLAGS) -c $(INCLUDE) $<
rf95_server1: rf95_server1.o help_functions.o RH_RF95.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o gpsMT3339.o
$(CC) $^ $(LIBS) -o rf95_server1
rf95_server2: rf95_server2.o help_functions.o RH_RF95.o RHDatagram.o RasPi.o RHHardwareSPI.o RHSPIDriver.o RHGenericDriver.o RHGenericSPI.o gpsMT3339.o
$(CC) $^ $(LIBS) -o rf95_server2
clean:
rm -rf *.o rf95_server1 rf95_server2
deploy: rf95_server1 rf95_server2
sshpass -p"$(PASSWD)" scp rf95_server1 pi@$(HOST):/home/pi
sshpass -p"$(PASSWD)" scp rf95_server2 pi@$(HOST):/home/pi

View File

@@ -0,0 +1,147 @@
// rf95_server.cpp
// -*- mode: C++ -*-
// Example app showing how to create a simple messageing server
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf95_client.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_server.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_server.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RH_RF95.h>
//Function Definitions
void sig_handler(int sig);
#ifdef DRAGINO_GPS_HAT
// Pin definitions for dragino gps hat
#define RFM95_CS_PIN 25 // Slave Select on GPIO25 so P1 connector pin #22
#define RFM95_IRQ_PIN 4 // IRQ on GPIO4 so P1 connector pin #7
#define RFM95_RESET_PIN 17 // Reset on GPIO17 so P1 connector pin #11
#undef RFM95_LED // Dragino Board as no LED to drive
#else
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
#endif
//Client and Server Addresses
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
//RFM95 Configuration
#ifdef EUROPE
#define RFM95_FREQUENCY 868.00
#else
#define RFM95_FREQUENCY 915.00
#endif
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_server startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_server startup OK.\n" );
printf( "\nRPI GPIO settings:\n" );
printf("CS-> GPIO %d\n", (uint8_t) RFM95_CS_PIN);
printf("IRQ-> GPIO %d\n", (uint8_t) RFM95_IRQ_PIN);
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("LED-> GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!rf95.init())
{
printf( "\n\nRF95 Driver Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server(This) Address= %d\n", SERVER_ADDRESS);
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(SERVER_ADDRESS);
rf95.setModemConfig(RH_RF95::Bw125Cr48Sf4096);
/* End Manager/Driver settings code */
/* Begin Datagram Server Code */
while(!flag)
{
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))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
// RF95::printBuffer("request: ", buf, len);
Serial.print("got request: ");
Serial.println((char*)buf);
// Serial.print("RSSI: ");
// Serial.println(rf95.lastRssi(), DEC);
//Send a reply
uint8_t data[] = "And hello back to you";
rf95.send(data, sizeof(data));
rf95.waitPacketSent();
Serial.println("Sent a reply");
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
else
{
Serial.println("recv failed");
}
}
}
/* End Datagram Server Code */
printf( "\nrf95_server Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,195 @@
// rf95_server.cpp
// -*- mode: C++ -*-
// Example app showing how to create a simple messageing server
// with the RH_RF95 class. RH_RF95 class does not provide for addressing or
// reliability, so you should only use RH_RF95 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf95_client.
//
// Requires Pigpio GPIO library. Install by downloading and compiling from
// http://abyz.me.uk/rpi/pigpio/, or install via command line with
// "sudo apt install pigpio". To use, run "make" at the command line in
// the folder where this source code resides. Then execute application with
// sudo ./rf95_server.
// Tested on Raspberry Pi Zero and Zero W with LoRaWan/TTN RPI Zero Shield
// by ElectronicTricks. Although this application builds and executes on
// Raspberry Pi 3, there seems to be missed messages and hangs.
// Strategically adding delays does seem to help in some cases.
//(9/20/2019) Contributed by Brody M. Based off rf22_server.pde.
// Raspberry Pi mods influenced by nrf24 example by Mike Poublon,
// and Charles-Henri Hallard (https://github.com/hallard/RadioHead)
#include <pigpio.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <RH_RF95.h>
#include <help_functions.h>
//Function Definitions
void sig_handler(int sig);
#ifdef DRAGINO_GPS_HAT
pthread_mutex_t gps_data_mutex;
#include <gpsMT3339.h>
// Pin definitions for dragino gps hat
#define RFM95_CS_PIN 25 // Slave Select on GPIO25 so P1 connector pin #22
#define RFM95_IRQ_PIN 4 // IRQ on GPIO4 so P1 connector pin #7
#define RFM95_RESET_PIN 17 // Reset on GPIO17 so P1 connector pin #11
#undef RFM95_LED // Dragino Board as no LED to drive
#else
//Pin Definitions
#define RFM95_CS_PIN 8
#define RFM95_IRQ_PIN 25
#define RFM95_LED 4
#endif
//Client and Server Addresses
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
//RFM95 Configuration
#ifdef EUROPE
#define RFM95_FREQUENCY 868.00
#else
#define RFM95_FREQUENCY 915.00
#endif
#define RFM95_TXPOWER 14
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS_PIN, RFM95_IRQ_PIN);
#ifdef DRAGINO_GPS_HAT
gps_MT3339 gps(GPS_DEVICE, &gps_data_mutex);
#endif
//Flag for Ctrl-C
int flag = 0;
//Main Function
int main (int argc, const char* argv[] )
{
if (gpioInitialise()<0)
{
printf( "\n\nRPI rf95_server startup Failed.\n" );
return 1;
}
gpioSetSignalFunc(2, sig_handler); //2 is SIGINT. Ctrl+C will cause signal.
printf( "\nRPI rf95_server startup OK.\n" );
printf( "\nRPI GPIO settings:\n" );
printf("CS-> GPIO %d\n", (uint8_t) RFM95_CS_PIN);
printf("IRQ-> GPIO %d\n", (uint8_t) RFM95_IRQ_PIN);
#ifdef RFM95_LED
gpioSetMode(RFM95_LED, PI_OUTPUT);
printf("LED-> GPIO %d\n", (uint8_t) RFM95_LED);
gpioWrite(RFM95_LED, PI_ON);
gpioDelay(500000);
gpioWrite(RFM95_LED, PI_OFF);
#endif
if (!rf95.init())
{
printf( "\n\nRF95 Driver Failed to initialize.\n\n" );
return 1;
}
/* Begin Manager/Driver settings code */
printf("\nRFM 95 Settings:\n");
printf("Frequency= %d MHz\n", (uint16_t) RFM95_FREQUENCY);
printf("Power= %d\n", (uint8_t) RFM95_TXPOWER);
printf("Client Address= %d\n", CLIENT_ADDRESS);
printf("Server(This) Address= %d\n", SERVER_ADDRESS);
rf95.setTxPower(RFM95_TXPOWER, false);
rf95.setFrequency(RFM95_FREQUENCY);
rf95.setThisAddress(SERVER_ADDRESS);
rf95.setModemConfig(RH_RF95::Bw125Cr48Sf4096);
/* End Manager/Driver settings code */
rf95.printRegisters();
/* Begin Datagram Server Code */
// start gps of dragino gps hat
#ifdef DRAGINO_GPS_HAT
pthread_t read_gps_thread;
int iret;
pthread_mutex_init(&gps_data_mutex,NULL);
iret = pthread_create( &read_gps_thread, NULL, ((void* (*)(void*))&gps_MT3339::read_gps), &gps);
if (iret != 0)
printf("\nCould not start thread to read gps data\n");
#endif
print_scheduler();
/* Begin Datagram Server Code */
while(!flag)
{
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))
{
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_ON);
#endif
// RF95::printBuffer("request: ", buf, len);
printf("got request: \"%s\"\n", (char*)buf);
printf("RSSI: %d\n",rf95.lastRssi());
uint8_t data[50] = "And hello back to you";
//cut out sequence number from received message (format: "(...)") if in message
// and copy to outgoing message
char* temp1 = strchr((char*)buf,'(');
char* temp2 = strchr((char*)buf,')');
if ((temp1 != NULL) && (temp2 != NULL))
{
// if received message contains sequence number,
// reply with received sequence number
temp1++;
uint8_t wordlen = temp2 - temp1;
temp1[wordlen] = 0x00;
#ifndef DRAGINO_GPS_HAT
snprintf((char*)data+21,8,"(%5s)",temp1);
#else
// In Case of Dragino Lora/GPS hat add timestamp and gps position of server to reply
pthread_mutex_lock(&gps_data_mutex);
snprintf((char*)data,47,"R:(%5s):%10s,%10s,%11s",
temp1,gps.getTimestamp(),gps.getLatitude(),gps.getLongitude());
pthread_mutex_unlock(&gps_data_mutex);
#endif
}
//Send a reply including a trailing 0x00
rf95.send(data, strlen((char*)data)+1);
rf95.waitPacketSent();
printf("Sent a reply: \"%s\"\n",data);
#ifdef RFM95_LED
gpioWrite(RFM95_LED, PI_OFF);
#endif
}
else
{
Serial.println("recv failed");
}
}
}
/* End Datagram Server Code */
printf( "\nrf95_server Tester Ending\n" );
gpioTerminate();
return 0;
}
void sig_handler(int sig)
{
flag=1;
}

View File

@@ -0,0 +1,219 @@
// -*- mode: C++ -*-
/*
* gpsMT3339.cpp
*
* Created on: Jun 11, 2020
* Author: Tilman Glötzner
*/
#include <gpsMT3339.h>
#include <stdio.h>
#include <help_functions.h>
#include <unistd.h>
#include <stdlib.h>
gps_MT3339::gps_MT3339(const char* SerialDevice, pthread_mutex_t* mutex) {
// TODO Auto-generated constructor stub
this->mutex = mutex;
gpsDevice = (char*)malloc(strlen(SerialDevice)*sizeof(char));
if (gpsDevice == NULL)
return;
strcpy(gpsDevice,SerialDevice);
}
gps_MT3339::~gps_MT3339() {
// TODO Auto-generated destructor stub
stop_reading = true;
}
char* gps_MT3339::getLatitude() {
return latitude;
}
char* gps_MT3339::getLongitude() {
return longitude;
}
char* gps_MT3339::getTimestamp() {
return timestamp;
}
//function to read gps device of Dragino GPS hat (to be executed by a thread)
char* gps_MT3339::strnstr(const char *haystack, const char *needle, size_t len)
{
int i;
size_t needle_len;
if (0 == (needle_len = strnlen(needle, len)))
return (char *)haystack;
for (i=0; i<=(int)(len-needle_len); i++)
{
if ((haystack[0] == needle[0]) &&
(0 == strncmp(haystack, needle, needle_len)))
return (char *)haystack;
haystack++;
}
return NULL;
}
void* gps_MT3339::read_gps(void* ptr)
{
ssize_t n, gga_len,i, comma_count;
char* start_ptr;
char* end_ptr;
char gps_buffer[LEN_GPS_READ_BUFFER];
char GPGGA_sentence[LEN_GPS_SENTENCE_BUFFER];
int gps_fd = -1;
int nofchar;
unsigned int empty_read_counter = 0;
printf("\nGPS READER started\n");
print_scheduler();
print_scope();
memset(gps_buffer,0x00,LEN_GPS_READ_BUFFER);
while (!stop_reading)
{
if (gps_fd < 0)
{
// Open the device in non-blocking mode
gps_fd = open(gpsDevice, O_RDWR | O_NONBLOCK);
if(gps_fd < 0)
{
printf("\nWarning: Could not open device file of GPS\n" );
}
}
else
{
n = read(gps_fd, gps_buffer, 200);
if (n == 0)
{
// counter incremented if there was nothing read back
empty_read_counter++;
}
if (n <= 0)
{
if (((errno != EAGAIN) and (errno != EWOULDBLOCK))
|| (empty_read_counter > MAX_EMPTY_READ))
{
// on read error close file descriptor and restart
printf("GPS device being reset\n");
strcpy(longitude,"NoValue");
strcpy(latitude,"NoValue");
strcpy(timestamp,"NoValue");
close(gps_fd);
gps_fd = -1;
empty_read_counter = 0;
memset(gps_buffer,0x00,LEN_GPS_READ_BUFFER);
}
}
else
{
// sucessful read => reset counter tracking empty reads
empty_read_counter = 0;
// Scan for sentence read from GPS device containing position
start_ptr = strnstr (gps_buffer, "$GPGGA", LEN_GPS_READ_BUFFER);
if( start_ptr != NULL)
{
end_ptr = (char*)memchr(start_ptr, '*',
LEN_GPS_READ_BUFFER - (start_ptr - gps_buffer));
if (end_ptr != NULL)
{
gga_len = (end_ptr-start_ptr) < LEN_GPS_SENTENCE_BUFFER ?
(end_ptr-start_ptr) : LEN_GPS_SENTENCE_BUFFER - 1;
memcpy(GPGGA_sentence,gps_buffer,gga_len);
// make sure that string is delimited
GPGGA_sentence[gga_len+1] = '\0';
//printf(GPGGA_sentence);
// extract time and gps coordinates from GPGGA sentence
// Sample: GPGGA,193630.000,4846.8772,N,00912.2288,E,1,5,1.57,131.5,M,48.0,M,,
comma_count = 0;
//printf("%s\n", GPGGA_sentence);
start_ptr = strchr(GPGGA_sentence,',') + 1;
end_ptr=start_ptr;
pthread_mutex_lock(mutex);
while (end_ptr != NULL && start_ptr != NULL)
{
end_ptr = strchr(start_ptr,',');
if (end_ptr != NULL)
{
nofchar = (end_ptr - start_ptr);
if (nofchar < LEN_GPS_INFO_BUFFER)
end_ptr[0] = '\0';
else
break;
int temp = nofchar;
if (comma_count == 0)
{
nofchar = temp < LEN_GPS_INFO_BUFFER ?
temp : LEN_GPS_INFO_BUFFER - 1;
strncpy(timestamp,start_ptr,nofchar);
timestamp[nofchar] = '\0';
}
if (comma_count == 1)
{
nofchar = temp < LEN_GPS_INFO_BUFFER - 2 ?
temp : LEN_GPS_INFO_BUFFER - 2;
strncpy(latitude,start_ptr,nofchar);
latitude[nofchar] = '\0';
}
if (comma_count == 2)
{
nofchar = strlen(latitude);
strncpy(latitude+nofchar,start_ptr,1);
latitude[nofchar+1] = '\0';
}
if (comma_count == 3)
{
nofchar = temp < LEN_GPS_INFO_BUFFER - 2 ?
temp : LEN_GPS_INFO_BUFFER - 2;
strncpy(longitude,start_ptr,nofchar);
longitude[nofchar] = '\0';
}
if (comma_count == 4)
{
nofchar = strlen(longitude);
strncpy(longitude+nofchar,start_ptr,1);
longitude[nofchar+1] = '\0';
// all data extracted => bail out
end_ptr = NULL;
start_ptr = NULL;
}
start_ptr = end_ptr+1;
comma_count++;
}
}
pthread_mutex_unlock(mutex);
}
}
}
}
pthread_yield();
}
free(gpsDevice);
close(gps_fd);
}
void gps_MT3339::stop() {
stop_reading = true;
}

View File

@@ -0,0 +1,52 @@
// -*- mode: C++ -*-
/*
* gpsMT3339.h
*
* Created on: Jun 11, 2020
* Author: Tilman Glötzner
*/
#ifndef GPSMT3339_H_
#define GPSMT3339_H_
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#define LEN_GPS_READ_BUFFER 255
#define LEN_GPS_INFO_BUFFER 20
#define LEN_GPS_SENTENCE_BUFFER 100
// reading the serial port may eventually lock up.
// MAX_EMPTY_READ determines how many consecutive reads yielding no results, i.e.
// nofCharRead = 0, may occur before the program tries to reopen the gps device
#define MAX_EMPTY_READ 15
#define GPS_DEVICE "/dev/ttyS0"
class gps_MT3339 {
public:
gps_MT3339(const char* SerialDevice,pthread_mutex_t* mutex);
virtual ~gps_MT3339();
void* read_gps(void* ptr);
char* getLatitude();
char* getLongitude();
char* getTimestamp();
void stop();
private:
char *strnstr(const char *haystack, const char *needle, size_t len);
char longitude[LEN_GPS_INFO_BUFFER]="NoInit";
char latitude[LEN_GPS_INFO_BUFFER]="NoInit";
char timestamp[LEN_GPS_INFO_BUFFER]="NoInit";
bool stop_reading = false;
char *gpsDevice;
pthread_mutex_t* mutex;
};
#endif /* GPSMT3339_H_ */

View File

@@ -0,0 +1,56 @@
// -*- mode: C++ -*-
/*
* help_functions.cpp
*
* Created on: Jun 11, 2020
* Author: Tilman Glötzner
*/
// help functions
#include <help_functions.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
void print_scheduler(void)
{
int schedType;
schedType = sched_getscheduler(getpid());
switch(schedType)
{
case SCHED_FIFO:
printf("Scheduling Policy is SCHED_FIFO\n");
break;
case SCHED_OTHER:
printf("Scheduling Policy is SCHED_OTHER\n");
break;
case SCHED_RR:
printf("Scheduling Policy is SCHED_RR\n");
break;
default:
printf("Scheduling Policy is UNKNOWN\n");
}
}
void print_scope(void)
{
pthread_attr_t tattr;
int scope;
int ret;
/* get scope of thread */
ret = pthread_attr_getscope(&tattr, &scope);
switch(scope)
{
case PTHREAD_SCOPE_SYSTEM:
printf("Scheduling Scope is SYSTEM\n");
break;
case PTHREAD_SCOPE_PROCESS:
printf("Scheduling Scope is PROCESS\n");
break;
default:
printf("Scheduling Scope is UNKNOWN\n");
}
}

View File

@@ -0,0 +1,13 @@
// -*- mode: C++ -*-
/*
* help_functions.h
*
* Created on: Jun 11, 2020
* Author: Tilman Glötzner
*/
#ifndef HELP_FUNCTIONS_H_
#define HELP_FUNCTIONS_H_
void print_scheduler(void);
void print_scope(void);
#endif