Hi,
Maybe you guys can help me. I'm building a spectrum monitor for a RF converter and use the RFM69 module to measure RSSI. The only thing I can't find out is what I need to send to set the receiver to the smallest bandwidth possible. This is needed to get the most accurate readout.
My current working testcode is:
#include <TFT_eSPI.h>
#include <RFM69.h> //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h> //included with Arduino IDE (www.arduino.cc)
#include <analogWrite.h> // https://github.com/ERROPiX/ESP32_AnalogWrite
#include "ADF4351.h" // Included with this file
#include <RotaryEncoder.h> // https://github.com/enjoyneering/RotaryEncoder
#define RFA 32
#define RFB 15
#define BANDBUTTON 27
#define BWBUTTON 33
#define ROTARY_BUTTON 39
#define PIN_POT 35
#define ROTARY_PIN_A 36
#define ROTARY_PIN_B 34
unsigned int frequency1 = 43600;
unsigned int frequency2 = 14460;
unsigned int frequency3 = 7100;
unsigned int frequency4 = 5100;
unsigned int frequencyold;
byte bandselect = 1;
byte bwselect = 1;
byte bw1;
byte bw2;
int position = 0;
RFM69 radio;
TFT_eSPI display = TFT_eSPI(176, 220);
RotaryEncoder encoder(ROTARY_PIN_A, ROTARY_PIN_B, ROTARY_BUTTON);
ADF4351 Frontend;
void ICACHE_RAM_ATTR encoderISR() {
encoder.readAB();
}
void ICACHE_RAM_ATTR encoderButtonISR() {
encoder.readPushButton();
}
void setup() {
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
pinMode(RFA, OUTPUT);
pinMode(RFB, OUTPUT);
pinMode(BANDBUTTON, INPUT);
pinMode(BWBUTTON, INPUT);
analogWrite(2, 511);
Serial.begin(115200);
Frontend.Init(4, 25000000);
Frontend.Power(1);
display.begin(0x3C);
display.init();
display.setRotation(3);
display.fillScreen(TFT_BLACK);
radio.initialize(-1, -1, -1);
display.drawRect(1, 1, 219, 175, TFT_BLUE);
showBand();
display.drawRect(4, 23, 213, 150, TFT_WHITE);
radio.setLNA(001);
radio.setMode(RF69_MODE_RX);
RF(4);
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_A), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_PIN_B), encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(ROTARY_BUTTON), encoderButtonISR, FALLING);
position = encoder.getPosition();
}
void RF(byte band) {
char* addon;
switch (band)
{
case 1:
digitalWrite(RFA, HIGH); // 6m
digitalWrite(RFB, HIGH);
display.setTextColor(TFT_BLACK);
if (frequencyold % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequencyold / 100) + "." + addon + String(frequencyold % 100), 110, 2, 4);
display.setTextColor(TFT_WHITE);
if (frequency4 % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequency4 / 100) + "." + addon + String(frequency4 % 100), 110, 2, 4);
frequencyold = frequency4;
Frontend.SetFreq(frequency4 + 31500, 0);
break;
case 2:
digitalWrite(RFA, LOW); // 4m
digitalWrite(RFB, HIGH);
display.setTextColor(TFT_BLACK);
if (frequencyold % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequencyold / 100) + "." + addon + String(frequencyold % 100), 110, 2, 4);
display.setTextColor(TFT_WHITE);
if (frequency3 % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequency3 / 100) + "." + addon + String(frequency3 % 100), 110, 2, 4);
frequencyold = frequency3;
Frontend.SetFreq(frequency3 + 31500, 0);
break;
case 3:
digitalWrite(RFA, HIGH); // 2m
digitalWrite(RFB, LOW);
display.setTextColor(TFT_BLACK);
if (frequencyold % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequencyold / 100) + "." + addon + String(frequencyold % 100), 110, 2, 4);
display.setTextColor(TFT_WHITE);
if (frequency2 % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequency2 / 100) + "." + addon + String(frequency2 % 100), 110, 2, 4);
frequencyold = frequency2;
Frontend.SetFreq(frequency2 + 31500, 0);
break;
case 4:
digitalWrite(RFA, LOW); // 70cm
digitalWrite(RFB, LOW);
display.setTextColor(TFT_BLACK);
if (frequencyold % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequencyold / 100) + "." + addon + String(frequencyold % 100), 110, 2, 4);
display.setTextColor(TFT_WHITE);
if (frequency1 % 100 < 10) addon = "0"; else addon = "";
display.drawCentreString(String(frequency1 / 100) + "." + addon + String(frequency1 % 100), 110, 2, 4);
frequencyold = frequency1;
Frontend.SetFreq(frequency1 - 31500, 0);
break;
}
}
void loop() {
SpectrumView();
if (digitalRead(BANDBUTTON) == LOW) {
bandselect++;
if (bandselect > 4) bandselect = 1;
RF(bandselect);
while (digitalRead(BANDBUTTON) == LOW);
}
if (digitalRead(BWBUTTON) == LOW) {
bwselect++;
if (bwselect > 6) bwselect = 1;
showBand();
while (digitalRead(BWBUTTON) == LOW);
}
if (position < encoder.getPosition()) KeyUp();
if (position > encoder.getPosition()) KeyDown();
if (encoder.getPushButton() == true) ButtonPress();
}
void KeyUp() {
while (digitalRead(ROTARY_PIN_A) == LOW || digitalRead(ROTARY_PIN_B) == LOW);
position = encoder.getPosition();
}
void KeyDown() {
while (digitalRead(ROTARY_PIN_A) == LOW || digitalRead(ROTARY_PIN_B) == LOW);
position = encoder.getPosition();
}
void ButtonPress() {
while (digitalRead(ROTARY_BUTTON) == LOW);
}
void showBand() {
switch (bwselect)
{
case 1:
bw1 = 74; // Symbolrate 35
bw2 = 146;
display.setTextColor(TFT_BLACK);
display.drawRightString("500ks/s", 50, 8, 1);
display.setTextColor(TFT_YELLOW);
display.drawRightString("35ks/s", 50, 8, 1);
break;
case 2:
bw1 = 70; // Symbolrate 66
bw2 = 150;
display.setTextColor(TFT_BLACK);
display.drawRightString("35ks/s", 50, 8, 1);
display.setTextColor(TFT_YELLOW);
display.drawRightString("66ks/s", 50, 8, 1);
break;
case 3:
bw1 = 65; // Symbolrate 125
bw2 = 155;
display.setTextColor(TFT_BLACK);
display.drawRightString("66ks/s", 50, 8, 1);
display.setTextColor(TFT_YELLOW);
display.drawRightString("125ks/s", 50, 8, 1);
break;
case 4:
bw1 = 50; // Symbolrate 250
bw2 = 170;
display.setTextColor(TFT_BLACK);
display.drawRightString("125ks/s", 50, 8, 1);
display.setTextColor(TFT_YELLOW);
display.drawRightString("250ks/s", 50, 8, 1);
break;
case 5:
bw1 = 40; // Symbolrate 333
bw2 = 180;
display.setTextColor(TFT_BLACK);
display.drawRightString("250ks/s", 50, 8, 1);
display.setTextColor(TFT_YELLOW);
display.drawRightString("333ks/s", 50, 8, 1);
break;
case 6:
bw1 = 20; // Symbolrate 500
bw2 = 200;
display.setTextColor(TFT_BLACK);
display.drawRightString("333ks/s", 50, 8, 1);
display.setTextColor(TFT_YELLOW);
display.drawRightString("500ks/s", 50, 8, 1);
break;
}
SpectrumView();
}
void SpectrumView() {
long scanfreq = 314495000;
for (uint8_t i = 0; i < 200; i++) {
scanfreq += 5000;
radio.setFrequency(scanfreq);
float level;
delayMicroseconds(550);
level = ((radio.readRSSI() + map(analogRead(PIN_POT), 0, 1023, 90, 95)) * 2);
if (level < 0) level = 0;
if (level > 146) level = 146;
display.drawLine(i + 10, 170, i + 10, 170 - int(level), TFT_YELLOW);
display.drawLine(i + 10, 170 - int(level), i + 10, 24, TFT_BLACK);
display.drawLine(bw1, 170, bw1, 40, TFT_RED);
display.drawLine(bw2, 170, bw2, 40, TFT_RED);
display.drawLine(bw1, 40, bw1 + 10, 30, TFT_RED);
display.drawLine(bw2, 40, bw2 - 10, 30, TFT_RED);
display.drawLine(bw1 + 10, 30, bw2 - 10, 30, TFT_RED);
}
}