Compare commits

..

No commits in common. "544a29f705e7d4d3f8e66c8e8b05a481f56658b2" and "076cc73941fa399d5f2f1e1b0aca735dd1f5c831" have entirely different histories.

2 changed files with 71 additions and 154 deletions

View file

@ -1,14 +1,9 @@
#ifndef BOARD_CONFIG_H #ifndef BOARD_CONFIG_H
#define BOARD_CONFIG_H #define BOARD_CONFIG_H
#define RETRIES 3
//Un-comment a line below for the target board/uC. //Un-comment a line below for the target board/uC.
#define FEATHER_RFM95 #define FEATHER_RFM95
//#define FEATHER_RFM96 //#define FEATHER_RFM96
//#define UNO
//#define MEAGA2560
//Only applied if defined //Only applied if defined
#ifdef FEATHER_RFM95 #ifdef FEATHER_RFM95
@ -40,14 +35,7 @@
#define RF9X_MIN_DB 5 #define RF9X_MIN_DB 5
#define RF9X_TIMEOUT 250 #define RF9X_TIMEOUT 250
#define RF9X_BUF_SZ 32 #define RF9X_BUF_SZ 32
#endif //FEATHER_RFM95 #endif //FEATHER_RFM9X
#ifdef UNO
#endif //UNO
#ifdef MEGA2560
#endif //MEGA2560
#endif //BOARD_CONFIG_H #endif //BOARD_CONFIG_H

View file

@ -23,144 +23,97 @@ TinyGPSPlus gps; // TinyGPS++ object to process GPS data
RH_RF95 rf95(RFM9X_CS, RMF9X_INT); // Radio instance. RH_RF95 rf95(RFM9X_CS, RMF9X_INT); // Radio instance.
typedef struct Msg{
uint8_t id;
double latitude;
double longitude;
uint8_t crc;
}Msg;
// Variables // Variables
static int prevHeading = -1; // Store previous heading to avoid frequent updates static int prevHeading = -1; // Store previous heading to avoid frequent updates
unsigned long lastUpdate = 0; // Time tracking for updates unsigned long lastUpdate = 0; // Time tracking for updates
const int updateInterval = 500; // Update interval in milliseconds (500ms = 0.5s) const int updateInterval = 500; // Update interval in milliseconds (500ms = 0.5s)
NodeId node = {
typedef struct MSG{ id = 0;
uint8_t id; name = "name";
double latitude;
double longitude;
}MSG;
enum result {
Ok = 0,
Err,
ReceiveFailed,
NoReply,
}; };
uint8_t reciver_buffer[RF9X_BUF_SZ];
uint8_t db; //Used to hold the current setting for the TX power. void Radio_setup(void) {
uint8_t buffer[sizeof(MSG)]; pinMode(RFM9X_RST, OUTPUT);
digitalWrite(RFM9X_RST, HIGH);
MSG msg_out; while(!rf95.init()){}
msg_out.id = 0; //Put the system ID here Serial.println("radio: Initialized");
if(rf95.setFrequency(RF9X_FREQ)) {
Serial.println("radio: Error could not set frequency");
while(true){} //Loop forever
}
Serial.print("radio: Frequency = ");
Serial.println(RF9X_FREQ);
void MSG_Print(MSG *msg) { rf95.setTxPower(RF9X_MIN_DB, false);
Serial.print("ID: ");
Serial.println(msg->id);
Serial.print("Latitude: ");
Serial.println(msg->latitude);
Serial.print("Longitude: ");
Serial.println(msg->longitude);
} }
void Radio_Reset(void) {
void Radio_reset(void) {
digitalWrite(RFM9X_RST, LOW); digitalWrite(RFM9X_RST, LOW);
delay(10); delay(10);
digitalWrite(RFM9X_RST, HIGH); digitalWrite(RFM9X_RST, HIGH);
delay(10); delay(10);
} }
uint8_t Radio_Setup(void) {
Serial.println("Radio_Setup()");
pinMode(RFM95_RST, OUTPUT); int Radio_SendData() {
digitalWrite(RFM95_RST, HIGH); int8_t tx_power = RF9X_MIN_DB;
uint8_t received_id = 0;
Radio_Reset(); //Set the radio output to lowest power.
rf95.setTxPower(tx_power, false);
while (!rf95.init()) { //Set the radio into TX mode.
Serial.println("Radio_Setup(): Failed to initialize"); rf95.setModeTx();
Serial.println("Check SPI connections!");
return Err;
}
Serial.println("LoRa radio init OK!");
return Ok;
}
uint8_t Radio_Configure(void) { //Try transmitting data
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM Serial.println("Radio: Sending data...");
if(!rf95.setFrequency(RF95_FREQ)) { rf95.send(node.id, 1);
Serial.println("Radio_Configure(): failed to set frequency"); rf95.waitPacketSent();
return Err;
}
Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
rf95.setTxPower(TX_POWER_LOW, false); //Wait for ACK or Radio Msg. (Timeout Should be included).
//On timeout increase the transmit power and re-attempt.
if(!rf95.waitAvailbleTimeout(RF9X_TIMEOUT)) {
if(rf95.recv(reciver_buffer, RF9X_BUF_SZ)) {
return Ok; }
} else {
//Failed to receive the message.
uint8_t Radio_SendMsg(MSG *msg) { Serial.println("Radio: Error failed to receive msg.");
Serial.println("Radio_SendMsg()"); }
Serial.println("Sending Message: ");
Serial.print("Message size: ");
Serial.println(sizeof(MSG));
MSG_Print(msg);
rf95.send((uint8_t *)msg, sizeof(MSG));
Serial.println("waiting for packet sent...");
delay(10);
rf95.waitPacketSent();
return Ok;
}
uint8_t Radio_CheckForMsg(MSG *msg) {
Serial.println("Radio_CheckForMsg()");
//Now we wait for an packet.
if (rf95.waitAvailableTimeout(1000)) {
// Should be a reply message for us now
if (rf95.recv(buffer, sizeof(MSG))) {
Serial.print("Received reply: ");
msg->id = (uint8_t) buffer[0];
msg->latitude = *(double *) &buffer[1];
msg->longitude = *(double *) &buffer[5];
MSG_Print(msg);
Serial.print("RSSI: ");
Serial.println(rf95.lastRssi(), DEC);
} else {
Serial.println("Receive failed");
return ReceiveFailed;
} }
} else { else {
Serial.println("No reply"); //No reply of any kind.
return NoReply; Serial.println("Radio: Error no reply.");
} }
return Ok;
//Set the radio mode to idle.
rf95.setModeIdle();
} }
//The algo below allows us to minimize the needed TX power.
void Radio_Main(MSG *msg_out, MSG *msg_in) {
for(db = RF9X_MIN_DB; db <= RF9X_MAX_DB; db++) {
Radio_SendMsg(&msg_out);
if(Radio_CheckForMsg(&msg_in) == Ok) {
break;
}
else{
Serial.print("TX Power set to:");
Serial.println(db);
}
}
//We "delay" for 1 seconds by calling the radio check twice.
if(Radio_CheckForMsg(&msg_in) == Ok) {
Radio_SendMsg(&msg_out);
}
int Radio_ReceiveData() {
//Set radio into RX mode.
rf95.setModeRx();
//Check if radio has traffic
if()
//Set the radio mode to idle.
rf95.setModeIdle();
} }
@ -183,24 +136,6 @@ void setup(void) {
// GPS Setup // GPS Setup
gpsSerial.begin(GPS_BUADRATE); gpsSerial.begin(GPS_BUADRATE);
// Setup the Radio module.
//Retry until sucsess.
for(uint8_t i = 0; i < RETRIES; i++) {
delay(100);
if(Radio_Setup() == Ok){
break;
}
}
//Retry configuration until success
for(uint8_t i = 0; i < RETRIES; i++) {
delay(100);
if(Radio_Configure() == Ok){
break;
}
}
} }
void loop() { void loop() {
@ -210,14 +145,12 @@ void loop() {
if (gpsSerial.available() > 0) { // Check if data is available from the GPS module if (gpsSerial.available() > 0) { // Check if data is available from the GPS module
gps.encode(gpsSerial.read()); // Decode the GPS data gps.encode(gpsSerial.read()); // Decode the GPS data
if (gps.location.isValid()) { // Check if GPS location data is valid if (gps.location.isValid()) { // Check if GPS location data is valid
//double latitude = gps.location.lat(); // Get latitude double latitude = gps.location.lat(); // Get latitude
//double longitude = gps.location.lng(); // Get longitude double longitude = gps.location.lng(); // Get longitude
msg_out.latitude = gps.location.lat();
msg_out.longitude = gps.location.lat();
Serial.print("Lat: "); // Output latitude for debugging Serial.print("Lat: "); // Output latitude for debugging
Serial.println(msg_out.latitude, 6); // Print latitude with 6 decimal places Serial.println(latitude, 6); // Print latitude with 6 decimal places
Serial.print("Lon: "); // Output longitude for debugging Serial.print("Lon: "); // Output longitude for debugging
Serial.println(msg_out.longitude, 6); // Print longitude with 6 decimal places Serial.println(longitude, 6); // Print longitude with 6 decimal places
} else { } else {
Serial.println("Waiting for valid GPS data..."); // GPS signal lost or invalid Serial.println("Waiting for valid GPS data..."); // GPS signal lost or invalid
} }
@ -243,9 +176,6 @@ void loop() {
updateDisplay(); // Placeholder: Update the central TFT display with GPS data updateDisplay(); // Placeholder: Update the central TFT display with GPS data
lastUpdate = currentTime; // Reset the last update time lastUpdate = currentTime; // Reset the last update time
} }
//Handle the Radio data.
} }
// Function to update the LED ring based on heading direction // Function to update the LED ring based on heading direction
@ -290,7 +220,6 @@ void sendGPSData() {
// - Format the data for transmission // - Format the data for transmission
// - Use the LoRa library to send the data to the paired device // - Use the LoRa library to send the data to the paired device
// Example: LoRa.beginPacket(); LoRa.print(latitude); LoRa.print(longitude); LoRa.endPacket(); // Example: LoRa.beginPacket(); LoRa.print(latitude); LoRa.print(longitude); LoRa.endPacket();
} }
// Future function to receive GPS data from the other device via LoRa - See adafruit LORA code examples // Future function to receive GPS data from the other device via LoRa - See adafruit LORA code examples