add file for reading from serial input

This commit is contained in:
2025-02-08 20:32:25 -05:00
parent c8db5ce0bb
commit 7f24a585f8

View File

@@ -0,0 +1,107 @@
#include <SPI.h>
#include <RH_RF95.h>
/* for feather m0 */
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3
// Change to 434.0 or other frequency, must match RX's freq!
#define RF95_FREQ 915.0
// 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 high
digitalWrite(RFM95_RST, HIGH);
while (!Serial); // wait until serial console is open, remove if not tethered to computer
Serial.begin(115200);
delay(100);
Serial.println("Feather LoRa RX Test!");
// 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);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
}
int16_t packetnum = 0;
int16_t max_message_with_packet_num = RH_RF95_MAX_MESSAGE_LEN - 7;
int incomingByte = 0;
// Serial imput
const byte numChars = RH_RF95_MAX_MESSAGE_LEN;
void loop() {
// Transmit code
delay(1000); // Wait 1 second between transmits, could also 'sleep' here!
Serial.println("Transmitting..."); // Send a message to rf95_server
char radiopacket[RH_RF95_MAX_MESSAGE_LEN] = " You can't read thisf dsa fdsa fdsa fdsa fdsa fdsa fdsa fdsa fdsa fdsf";
itoa(packetnum++, radiopacket+max_message_with_packet_num, 10);
Serial.print("Sending "); Serial.println(radiopacket);
// Serial.println("Sending..."); delay(10);
rf95.send((uint8_t *)radiopacket, RH_RF95_MAX_MESSAGE_LEN);
// Serial.println("Waiting for packet to complete..."); delay(10);
rf95.waitPacketSent();
// Receiver code
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)) {
digitalWrite(LED_BUILTIN, HIGH);
// RH_RF95::printBuffer("Received: ", buf, len);
Serial.print("Got: ");
// Serial.println((char*)buf);
// Serial.print("RSSI: ");
// Serial.println(rf95.lastRssi(), DEC);
}
}
char receivedChars[numChars];
int dataReceived = 0;
while (Serial.available() > 0) {
dataReceived = 1;
static byte ndx = 0;
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
receivedChars[ndx] = incomingByte;
ndx++;
}
if (dataReceived) {
dataReceived = 0;
Serial.print("I received: ");
Serial.println(receivedChars);
}
}