150 lines
4.0 KiB
C++
150 lines
4.0 KiB
C++
#include <SPI.h>
|
|
#include <RH_RF95.h>
|
|
#include <string>
|
|
|
|
/* for feather m0 */
|
|
//#define RFM95_CS 8
|
|
const int RFM95_CS = 8;
|
|
//#define RFM95_RST 4
|
|
const int RFM95_RST = 4;
|
|
//#define RFM95_INT 3
|
|
const int RFM95_INT = 3;
|
|
|
|
|
|
// Change to 434.0 or other frequency, must match RX's freq!
|
|
//#define RF95_FREQ 915.0
|
|
const float RF95_FREQ = 915.0;
|
|
// Lower bandwidth means longer distance but lower data rate
|
|
// Valid values are:
|
|
// 0-7800khz: 7.8
|
|
// 7801-10400khz: 10.4
|
|
// 10401-15600 15.6
|
|
// 15601-20800 20.8
|
|
// 20801-31250 31.25
|
|
// 31251-41700 41.7
|
|
// 41701-62500 62.5
|
|
// 62501-12500 125.0 DEFAULT
|
|
// 12501-250000 250.0
|
|
// >250000 500.0
|
|
// #define RF95_BANDWIDTH 125
|
|
const long RF95_BANDWIDTH = 125;
|
|
|
|
// Higher spread factor means longer distance
|
|
// Valid values are 6-12
|
|
// #define RF95_SPREAD_FACTOR 6
|
|
const uint8_t RF95_SPREAD_FACTOR = 7;
|
|
|
|
// Higher TX power means longer distance
|
|
// Valid values are 5-23
|
|
// #define RF95_TX_POWER 23
|
|
const int8_t RF95_TX_POWER = 23;
|
|
|
|
// Singleton instance of the radio driver
|
|
RH_RF95 rf95(RFM95_CS, RFM95_INT);
|
|
|
|
void setup() {
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
pinMode(RFM95_RST, OUTPUT);
|
|
// Start with the LED on low
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
while (!Serial); // wait until serial console is open, remove if not tethered to computer
|
|
Serial.begin(115200);
|
|
delay(100);
|
|
Serial.println("Feather LoRa RX Debug Demo!");
|
|
|
|
// manual reset
|
|
digitalWrite(RFM95_RST, LOW);
|
|
delay(10);
|
|
digitalWrite(RFM95_RST, HIGH);
|
|
delay(10);
|
|
|
|
while (!rf95.init()) {
|
|
Serial.println("LoRa radio init failed");
|
|
while (1);
|
|
}
|
|
Serial.println("LoRa radio init OK!");
|
|
|
|
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
|
|
if (!rf95.setFrequency(RF95_FREQ)) {
|
|
Serial.println("setFrequency failed");
|
|
while (1);
|
|
}
|
|
Serial.print("Set Freq to: ");
|
|
Serial.println(RF95_FREQ);
|
|
|
|
// //Set the signal bandwidth
|
|
// rf95.setSignalBandwidth(RF95_BANDWIDTH);
|
|
// Serial.print("Set bandwidth to: ");
|
|
// Serial.println(RF95_BANDWIDTH);
|
|
|
|
// // Set the spreading factor
|
|
// rf95.setSpreadingFactor(RF95_SPREAD_FACTOR);
|
|
// Serial.print("Set spreading factor to: ");
|
|
// Serial.println(RF95_SPREAD_FACTOR);
|
|
|
|
// Set low transmission rate
|
|
// rf95.setLowDatarate();
|
|
// Serial.println("Set low data rate");
|
|
|
|
// The default transmitter power is 13dBm, using PA_BOOST.
|
|
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
|
|
// you can set transmitter powers from 5 to 23 dBm:
|
|
rf95.setTxPower(RF95_TX_POWER, false);
|
|
|
|
// Begin the user interaction piece
|
|
Serial.println("Please enter your name: ");
|
|
}
|
|
|
|
int32_t packet_num = 0;
|
|
String serial_input = "";
|
|
String chat_name = "";
|
|
String chat_message = "";
|
|
|
|
void loop() {
|
|
|
|
// We start in anonymous mode
|
|
// When we enter a name we can start messaging in the chat
|
|
if (chat_name == "") {
|
|
if (Serial.available()) {
|
|
// read the incoming byte:
|
|
chat_name = Serial.readStringUntil('\0');
|
|
String intro_message = chat_name + " has joined the chat!";
|
|
|
|
Serial.println("Welcome " + chat_name);
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
rf95.send((uint8_t *)(intro_message.c_str()), RH_RF95_MAX_MESSAGE_LEN);
|
|
rf95.waitPacketSent();
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
}
|
|
} else {
|
|
if (Serial.available()) {
|
|
// read the incoming byte:
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
chat_message = Serial.readStringUntil('\0');
|
|
String send_message = chat_name + ": " + chat_message;
|
|
|
|
Serial.println(send_message);
|
|
rf95.send((uint8_t *)(send_message.c_str()), RH_RF95_MAX_MESSAGE_LEN);
|
|
rf95.waitPacketSent();
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
}
|
|
}
|
|
// Receiver code
|
|
delay(100);
|
|
if (rf95.available()) {
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
// 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)) {
|
|
RH_RF95::printBuffer("Received: ", buf, len);
|
|
Serial.println((char *)buf);
|
|
Serial.print("RSSI: ");
|
|
Serial.println(rf95.lastRssi(), DEC);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
}
|
|
}
|
|
}
|