I have connected the RFM95W 915MHz module to 3.3v Arduino nano pin directly.
I guess I set TX power to 23 earlier by mistake and due to this issue one module might have been damaged that's why not initializing. However, the two other modules are working well right now with direct power connection to nano 3.3V(I decreased the transmission power to accommodate the current supplied by 3.3v nano pin.
There were supposed to be 3 devices in the system and the code for them was supposed to be as following. Since two modules are with me so I have tested the following codes between the two by interchanging the codes, and these are working
- For transmitter
#include <SPI.h>
#include <LoRa.h>
#define LORA_SS 10
#define LORA_RST 9
#define LORA_DIO0 2
#define BUTTON_PIN 3
#define BUZZER_PIN 7
const long RF95_FREQ = 915E6; // Frequency
const int SF = 8; // Spreading Factor
const long BW = 125E3; // Bandwidth (125 kHz)
const int TX_POWER = 13; // Transmission Power in dBm
// Configurable IDs
const String transmissionID = "001";
const String centralID = "999";
const String postID = "123"; // Allowed relay node
const int hopLimit = 4; // Maximum hops allowed
unsigned long lastButtonPress = 0;
const unsigned long debounceDelay = 500; // Debounce delay in milliseconds
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(RF95_FREQ)) {
Serial.println("Failed to initialize LoRa!");
while (1);
}
LoRa.setSpreadingFactor(SF);
LoRa.setSignalBandwidth(BW);
LoRa.setTxPower(TX_POWER);
Serial.println("LoRa Transmitter Ready");
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(BUTTON_PIN) == LOW && (currentMillis - lastButtonPress >= debounceDelay)) {
lastButtonPress = currentMillis;
String messageContent = "ALERT";
String preamble = transmissionID + centralID + postID + String(hopLimit);
String packet = preamble + messageContent;
Serial.print("Button pressed, sending message: ");
Serial.println(packet);
LoRa.beginPacket();
LoRa.print(packet);
LoRa.endPacket();
Serial.println("Message sent.");
}
// Check for incoming packets
int packetSize = LoRa.parsePacket();
if (packetSize) {
String receivedPacket = "";
while (LoRa.available()) {
receivedPacket += (char)LoRa.read();
}
// Ensure the packet is long enough before parsing
if (receivedPacket.length() >= 10) {
// Extract preamble and message
String receivedTransmissionID = receivedPacket.substring(0, 3);
String receivedCentralID = receivedPacket.substring(3, 6);
String receivedPostID = receivedPacket.substring(6, 9);
int receivedHops = receivedPacket.substring(9, 10).toInt();
String messageContent = receivedPacket.substring(10);
Serial.print("Received message from ");
Serial.print(receivedTransmissionID);
Serial.print(": ");
Serial.println(messageContent);
// If this node is allowed to relay the message
if (receivedPostID == transmissionID && messageContent == "ACKTT") {
digitalWrite(BUZZER_PIN, HIGH);
delay(2000);
digitalWrite(BUZZER_PIN, LOW);
} else {
Serial.println("Acknowledgement not received");
}
} else {
Serial.println("Received packet is too short");
}
}
}
- For Relay Post
#include <SPI.h>
#include <LoRa.h>
#define LORA_SS 10
#define LORA_RST 9
#define LORA_DIO0 2
#define BUZZER_PIN 7
const long RF95_FREQ = 915E6; // Frequency
const int SF = 8; // Spreading Factor
const long BW = 125E3; // Bandwidth (125 kHz)
const int TX_POWER = 13; // Transmission Power in dBm
String myPostID = "123"; // This node's Post ID (allowed to relay)
const int hopLimit = 4; // Maximum hops allowed
void setup() {
pinMode(BUZZER_PIN,OUTPUT);
Serial.begin(9600);
while (!Serial);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(RF95_FREQ)) {
Serial.println("Failed to initialize LoRa!");
while (1);
}
LoRa.setSpreadingFactor(SF);
LoRa.setSignalBandwidth(BW);
LoRa.setTxPower(TX_POWER);
Serial.println("LoRa Relay Node Ready");
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
String receivedPacket = "";
while (LoRa.available()) {
receivedPacket += (char)LoRa.read();
}
// Extract preamble and message
String receivedTransmissionID = receivedPacket.substring(0, 3);
String receivedCentralID = receivedPacket.substring(3, 6);
String receivedPostID = receivedPacket.substring(6, 9);
int receivedHops = receivedPacket.substring(9, 10).toInt();
String messageContent = receivedPacket.substring(10);
Serial.print("Received message from ");
Serial.print(receivedTransmissionID);
Serial.print(": ");
Serial.println(messageContent);
// If this node is allowed to relay the message
if (receivedPostID == myPostID && receivedHops > 0) {
String newPreamble = receivedCentralID + receivedCentralID + receivedTransmissionID + String(receivedHops - 1);
String forwardPacket = newPreamble + "ACKTT";
digitalWrite(BUZZER_PIN,HIGH);
Serial.print("Sending Acknoledgement");
Serial.println(forwardPacket);
LoRa.beginPacket();
LoRa.print(forwardPacket);
LoRa.endPacket();
delay(2000);
digitalWrite(BUZZER_PIN,LOW);
} else {
Serial.println("Not forwarding message (Post ID mismatch or hop limit reached).");
}
}
}
- For Final Central Point (Point where all the transmission are intended to reach)
#include <SPI.h>
#include <LoRa.h>
#define LORA_SS 10
#define LORA_RST 9
#define LORA_DIO0 2
#define BUZZER_PIN 7
const long RF95_FREQ = 915E6; // Frequency
const int SF = 8; // Spreading Factor
const long BW = 125E3; // Bandwidth (125 kHz)
const int TX_POWER = 13; // Transmission Power in dBm
// Configurable IDs
String myCentralID = "999"; // Central Node ID (this node's ID)
const int hopLimit = 4; // Maximum hops allowed
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(BUZZER_PIN, OUTPUT);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(RF95_FREQ)) {
Serial.println("Failed to initialize LoRa!");
while (1);
}
LoRa.setSpreadingFactor(SF);
LoRa.setSignalBandwidth(BW);
LoRa.setTxPower(TX_POWER);
Serial.println("LoRa Receiver Ready");
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
String receivedPacket = "";
// Read the received packet
while (LoRa.available()) {
receivedPacket += (char)LoRa.read();
}
Serial.print("Received packet: ");
Serial.println(receivedPacket);
// Extract preamble and message
if (receivedPacket.length() >= 10) { // Ensure packet length is valid
String receivedTransmissionID = receivedPacket.substring(0, 3);
String receivedCentralID = receivedPacket.substring(3, 6);
int receivedHops = receivedPacket.substring(9, 10).toInt();
String messageContent = receivedPacket.substring(10);
Serial.println("Message breakdown:");
Serial.print("Transmission ID: ");
Serial.println(receivedTransmissionID);
Serial.print("Central ID: ");
Serial.println(receivedCentralID);
Serial.print("Hops: ");
Serial.println(receivedHops);
Serial.print("Message Content: ");
Serial.println(messageContent);
// Check if the message is for this central node
if (receivedCentralID == myCentralID) {
Serial.println("Message is for me.");
// Handle the received message
String newPreamble = myCentralID + myCentralID + receivedTransmissionID + hopLimit;
String newPacket = newPreamble + "ACKTT";
Serial.print("Sending Acknowledgement: ");
Serial.println(newPacket);
digitalWrite(BUZZER_PIN, HIGH);
LoRa.beginPacket();
LoRa.print(newPacket);
LoRa.endPacket();
delay(2000);
digitalWrite(BUZZER_PIN, LOW);
}
} else {
Serial.println("Received packet is too short.");
}
}
}
The central system has display too and it is connected to 5v nano pin and RFM95 at 3.3v pin of nano. I earlier used a code as follows and this was working earlier.
#include <SPI.h>
#include <Wire.h>
#include <LoRa.h>
#include <U8g2lib.h>
#define SS 10
#define RST 9
#define DIO0 2
#define BUZZER_PIN 7
#define OLED_I2C_ADDR 0x3C
#define LORA_FREQ 915E6
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(LORA_FREQ)) {
Serial.println(F("LoRa init failed"));
while (1);
}
LoRa.setTxPower(10); // Lower transmission power
u8g2.begin();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setCursor(0, 10);
u8g2.print(F("Waiting for messages..."));
u8g2.sendBuffer();
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
String message = "";
while (LoRa.available()) {
message += (char)LoRa.read();
}
Serial.print(F("Received: "));
Serial.println(message);
// Display message on OLED
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print(F("Received Message:"));
u8g2.setCursor(0, 30);
u8g2.print(message);
u8g2.sendBuffer();
// Check if the message is a "panic" message
if (message.equalsIgnoreCase("panic")) {
Serial.println(F("Panic message received! Sending ACK..."));
// Send back ACK
LoRa.beginPacket();
LoRa.print(F("ACK: Panic received"));
LoRa.endPacket();
// Activate buzzer (optional)
digitalWrite(BUZZER_PIN, HIGH);
delay(500); // Buzzer on for 500 ms
digitalWrite(BUZZER_PIN, LOW);
}
}
}
But now when I tried to upload this code again with another laptop or phone, it's just not working and the display along with the serial initialization is not happening and also the RFM95W is not working.
Suggest me the changes I need to incorporate in power supply management or anything other than that such that I can make a max output out of this system.