hello there, I have used this sniffer code that has altering Spreading Factor, bandwidth and frequency. the change is operational although it doesn't capture any packets and i have a raspberry pi transmitting constantly right next to it. I am using the Radio Head Library. the two devices are on the same spreading factor frequency and bandwidth
i have tried various other alternatives but with no luck. It operates and can send packets to TTN from both devices so they're both functional. They both have an antenna and use the Dragino shield
My pins and includes
#include <SPI.h>
#include <RH_RF95.h>
#include <stdio.h>
#include <stdarg.h>
#define LED 8
#define ENABLE_BW_SCAN
#define ENABLE_SF_SCAN
#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 2
//#define LED LED_BUILTIN
#define RH_FLAGS_ACK 0x80
#define LOG_HEADER "MS,Freq,FreqPacketCount,RSSI,From,To,MsgId,Flags,Ack,DataLen,DataBytes,Data"
How my frequencies bandwidth and spreading factor are being defined
// Define the frequencies to scan
#define FREQ_COUNT 1
float _frequencies[] =
{
868.1, 868.3, 868.5, 867.1, 867.3, 867.5, 867.7, 867.9, 868.8, // Uplink (and PJON)
869.525
};
#ifdef ENABLE_BW_SCAN
#define BW_COUNT 3
long int _bandwidths[] =
{
500000,
250000,
125000
// etc.
};
#else
#define BW_COUNT 1
long int _bandwidths[] =
{
500000,
};
#endif
#ifdef ENABLE_SF_SCAN
#define SF_COUNT 4
int _spreadingfactors[] =
{
7,8,9,10,11,12
};
#else
#define SF_COUNT 1
int _spreadingfactors[] =
{
7
};
#endif
// How long should a frequency be monitored
#define FREQ_TIME_MS 4000
// Used to track how many packets we have seen on each frequency above
uint16_t _packetCounts[FREQ_COUNT][BW_COUNT][SF_COUNT];
Initiatlisation of the RF95 and all variables diaplying and frequencies,bandwidth and Spreading Factor
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
uint8_t _rxBuffer[RH_RF95_MAX_MESSAGE_LEN]; // receive buffer
uint8_t _rxRecvLen; // number of bytes actually received
char _printBuffer[512] = "\0"; // to send output to the PC
uint32_t _freqExpire = 0; // Millisecond at which frequency should change
uint32_t _freqIndex = 0; // Which frequency is currently monitored
uint32_t _bwIndex = 0; // Which bandwidth is currently monitored
uint32_t _sfIndex = 0; // Which spreading factor is currently monitored
Check if the frequency,bandwidth and spreading factor set failed and output
void rf95_setFrequency(uint32_t index)
{
if (!rf95.setFrequency(_frequencies[index]))
{
Serial.println(F("setFrequency failed "));
while (1);
}
rf95.setCodingRate4(5);
Serial.print(F("Freq: "));
Serial.print(_frequencies[index]);
// Serial.print(F(" ("));
// Serial.print(_packetCounts[index]);
// Serial.println(F(")\t"));
Serial.print(F("\t"));
}
void rf95_setBandwidth(uint32_t index)
{
rf95.setSignalBandwidth(_bandwidths[index]);
rf95.setCodingRate4(5);
Serial.print(F("BW: "));
Serial.print(_bandwidths[index]);
Serial.print(F("\t"));
}
void rf95_setSpreadingFactor(uint32_t index)
{
rf95.setSpreadingFactor(_spreadingfactors[index]);
Serial.print(F("SF: "));
Serial.print(_spreadingfactors[index]);
Serial.print(F("\t"));
}
Start initializing the pins and radio and set values
void setup()
{
for(int i=0; i < FREQ_COUNT; ++i)
for(int j=0; j < BW_COUNT; ++j)
for(int k=0; k < SF_COUNT; ++k)
_packetCounts[i][j][k] = 0;
// Configure pins
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);
// The RFM95 has a pulldown on this pin, so the radio
// is technically always selected unless you set the pin low
// this will cause other SPI devices to fail to function as
// expected because CS (active-low) will be selected for
// the RFM95 at the same time.
digitalWrite(RFM95_CS, HIGH);
pinMode(LED, OUTPUT);
// Wait for serial to initialize before proceeding, so all output can be seen
// This will block stand-alone operations
// while (!Serial);
// Initialize Serial
Serial.begin(115200);
delay(100);
Serial.println(F("LoRa Network Probe"));
// Initialize Radio
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init())
{
Serial.println(F("Failed"));
while (1);
}
Serial.println(F("Lora init Works"));
rf95_setFrequency(_freqIndex);
rf95.setCodingRate4(5);
//rf95.setSpreadingFactor(7);
//rf95.setSignalBandwidth(250000);
rf95.setCodingRate4(5);
rf95_setBandwidth(_bwIndex);
rf95_setSpreadingFactor(_sfIndex);
Serial.print(F(" ("));
Serial.print(_packetCounts[_freqIndex][_bwIndex][_sfIndex]);
Serial.println(F(")"));
rf95.setPromiscuous(true);
// Update time for changing frequency
_freqExpire = millis() + FREQ_TIME_MS;
// Log the header for CSV output
// Serial.println(LOG_HEADER);
}
Based on receive and the actions it needs to take and what to print
void loop()
{
_rxRecvLen = sizeof(_rxBuffer);
digitalWrite(LED, LOW);
rf95.setModeRx();
// Handle incoming packet if available
if (rf95.recv(_rxBuffer, &_rxRecvLen))
{
char isAck[4] = { " " };
digitalWrite(LED, HIGH);
_packetCounts[_freqIndex][_bwIndex][_sfIndex]++;
if (rf95.headerFlags() & RH_FLAGS_ACK)
{
memcpy(isAck, "Ack\0", 3);
}
_rxBuffer[_rxRecvLen] = '\0';
Serial.println(millis());
Serial.print(F("Signal(RSSI)= "));
Serial.print(rf95.lastRssi(), DEC);
Serial.print(F(" (Freq: "));
Serial.print((uint32_t)(_frequencies[_freqIndex] * 10), DEC);
Serial.println(F(")"));
Serial.print(F(" "));
Serial.print(rf95.headerFrom(), DEC);
Serial.print(F(" >> "));
Serial.print(rf95.headerTo(), DEC);
Serial.print(F(" MsgId:"));
Serial.print(rf95.headerId(), DEC);
Serial.print(F(" Flags:"));
Serial.print(rf95.headerFlags(), HEX);
Serial.print(F(" "));
Serial.println(isAck);
Serial.print(F(" "));
Serial.print(_rxRecvLen, DEC);
Serial.print(F(" => "));
Serial.println((char*)_rxBuffer);
// snprintf(_printBuffer, sizeof(_printBuffer), "Recv#:%d @ %d, Signal(RSSI)= %d", _packetCounts[FREQ_COUNT]++, millis(), rf95.lastRssi());
// Serial.println(_printBuffer);
// snprintf(_printBuffer, sizeof(_printBuffer), " From: %d >> To: %d MsgId: %d Flags: %2x %s", rf95.headerFrom(), rf95.headerTo(), rf95.headerId(), rf95.headerFlags(), isAck);
// Serial.println(_printBuffer);
// snprintf(_printBuffer, sizeof(_printBuffer), "Bytes: %d => %s \r\n", _rxRecvLen, _rxBuffer);
// Serial.println(_printBuffer);
// Add bytes received as hex values
for (int i = 0; i < _rxRecvLen; i++)
{
snprintf(_printBuffer, sizeof(_printBuffer), "%s %02X", _printBuffer, _rxBuffer[i]);
Serial.print(_rxBuffer[i], HEX);
Serial.print(" ");
}
// Add bytes received as string - this is usually ugly and useless, but
// it is here just in case. Maybe someone will send something in plaintext
//snprintf(_printBuffer, sizeof(_printBuffer), "%s,%s", _printBuffer, _rxBuffer);
Serial.println((char*)_rxBuffer);
}
// Change frequency if it is time
if (millis() > _freqExpire)
{
rf95.setModeIdle();
_freqExpire = millis() + FREQ_TIME_MS;
_freqIndex++;
if (_freqIndex == FREQ_COUNT)
{
_freqIndex = 0;
++_bwIndex;
}
if (_bwIndex == BW_COUNT)
{
_bwIndex = 0;
++_sfIndex;
}
if (_sfIndex == SF_COUNT)
{
_sfIndex = 0;
}
rf95_setFrequency(_freqIndex);
rf95_setBandwidth(_bwIndex);
rf95_setSpreadingFactor(_sfIndex);
Serial.print(F(" ("));
Serial.print(_packetCounts[_freqIndex][_bwIndex][_sfIndex]);
Serial.println(F(")"));
}
}
fourthsniff.ino (8.88 KB)