Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Teensy software #2

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Added Receive class based off of the 900MHx_RX_Test.cpp. Make it easi…
…er to test rather than copying and pasting everything onto main.:w
  • Loading branch information
QuackWifHat committed Nov 20, 2024
commit ec0d73d03a58a83b899c3776270cc3bbee5bfbaf
72 changes: 72 additions & 0 deletions teensy_software/test/Receive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "Receive.h"

// Custom pinout for teensy 4.1
#define RFM95_CS 10 // "B"
#define RFM95_INT 0 // "C"
#define RFM95_RST 1 // "A"
#define RFM95_IRQN RFM69_INT
#define TRANSMITTER_POWER 23

Receive::Receive(){
// Setting pinouts
rf95(RFM95_CS, RFM95_INT);

pinMode(LED_BUILTIN, OUTPUT);
pinMode(RFM95_RST, OUTPUT);
digitalWrite(RFM95_RST, HIGH);

// Initializing radio
while (!rf95.init()){
Serial.println("LoRa radio init failed");
Serial.println("Uncomment '#define SERIAL_DEBUG' in RH_RF95.cpp for detailed debug info");
}
Serial.println("LoRa radio initialized!");

// Setting frequency of radio
// Note: Frequency must be same as RX
if (!rf95.setFrequency(RF95_FREQ)) {
Serial.println("setFrequency failed");
}

// Setting transmit power.
Serial.print("Setting frequency to: ");
Serial.println(RFM95_CS);

rf95.setTxPower(TRANSMITTER_POWER, false);
}

void _getMessage(int bufferSize){
const int secondBufferSize = 140;
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);

char secondBuffer[secondBufferSize];
snprintf(buffer1, sizeof(buffer1), "The value of the received message is: %s\n", (char*)buf);

Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);

_sendAck();
} else {
Serial.println("Receive failed!");
}
}

void _sendAck(){
// ACK message
uint8_t data[] = "And hello back to you";

// Sending the packet
rf95.send(data, sizeof(data));
rf95.waitPacketSent();
Serial.println("Sent a reply");
digitalWrite(LED_BUILTIN, LOW);
}

26 changes: 26 additions & 0 deletions teensy_software/test/Receive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include·"Arduino.h" #include·<Adafruit_BME680.h>
#include·<Adafruit_I2CDevice.h>
#include·<Adafruit_Sensor.h>
#include·"Wire.h"
#include·<SPI.h>
#include·<RH_RF95.h>

class Receive {
public:
/* Initializes the pins and ports of the rf95 */
Receive();

/* Tries to find the message, and prints out the message's output */
void _getMessage(int bufferSize);

_changeFrequency();

private:
/* Returns an ACK to the transimitter */
void _sendAck();

/* Singleton instance of the radio driver */
RH_RF95 rf95;
}