#include <RH_RF95.h> // RadioHead library for RFM95 LoRa
#define RFM95_CS 10 // Chip select pin for the RFM95
#define RFM95_RST 9 // Reset pin for the RFM95
#define RFM95_INT 2 // Interrupt pin for the RFM95
RH_RF95 rf95(RFM95_CS, RFM95_INT);
void setup() {
Serial.begin(9600); // Start serial communication for output
pinMode(RFM95_RST, OUTPUT); // Set RFM95 reset pin as output
// Reset the RFM95 module
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
// Initialize the RF95 module with frequency (example: 915 MHz for USA)
if (!rf95.init()) {
Serial.println("RFM95 initialization failed.");
while (1); // Stop if initialization fails
}
rf95.setFrequency(915.0); // Set frequency (adjust for your region)
rf95.setTxPower(23, false); // Set transmission power
rf95.setModeRx(); // Set the module to receive mode
}
void loop() {
// Check if there's a received message
if (rf95.available()) {
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; // Buffer for incoming message
uint8_t len = sizeof(buf); // Length of the buffer
if (rf95.recv(buf, &len)) { // Receive the message
Serial.print("Received: ");
Serial.write(buf, len); // Output the message to the serial monitor
Serial.println(); // New line for readability
} else {
Serial.println("Receive failed"); // Error handling
}
}
delay(100); // Small delay to avoid busy loop
}
- This sketch initializes the RFM95 module and sets its frequency and transmission power.
- It initializes the DS18B20 sensor to get temperature readings.
- Every 15 minutes (900,000 milliseconds), it reads the temperature and sends it via LoRa.
- The message is formatted to include the temperature reading in Celsius.
- The sketch sends the message using the
RadioHead
library, waits for the packet to be sent, and then delays for 15 minutes before repeating the process.
Before uploading this sketch, ensure you’ve installed the RadioHead
, OneWire
, and DallasTemperature
libraries. The frequency and transmission power settings may vary depending on your region and specific application. Make sure to use the appropriate frequency and transmission power for your hardware and regulations in your country.
The program provided will receive any LoRa message on the same frequency and with the same modulation settings (e.g., bandwidth, spreading factor, coding rate). It does not inherently restrict itself to specific senders or messages.
To ensure that this receiver program only listens for messages sent by a specific source, there are several strategies one could employ:
- Unique Identifiers: Modify the sender program to prepend a unique identifier to each message. Then, the receiver can check for this identifier before processing the message.
- Addressing: The
RadioHead
library has an addressing mechanism that allows you to assign unique addresses to each node. This way, the receiver only processes messages intended for its address. - Encryption: If security is a concern, you can add encryption to ensure only authorized receivers can understand the messages. This would require additional libraries and complexity.
Here’s an updated version of the receiver program that looks for a specific identifier at the beginning of the message. This identifier could be a unique sequence of characters known to both the sender and receiver:
#include <RH_RF95.h> // RadioHead library for RFM95 LoRa
#define RFM95_CS 10 // Chip select pin for the RFM95
#define RFM95_RST 9 // Reset pin for the RFM95
#define RFM95_INT 2 // Interrupt pin for the RFM95
RH_RF95 rf95(RFM95_CS, RFM95_INT);
const char expectedIdentifier[] = "SENSOR01"; // Unique identifier for valid messages
void setup() {
Serial.begin(9600); // Start serial communication for output
pinMode(RFM95_RST, OUTPUT); // Set RFM95 reset pin as output
// Reset the RFM95 module
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
// Initialize the RF95 module with frequency (adjust as needed)
if (!rf95.init()) {
Serial.println("RFM95 initialization failed.");
while (1); // Stop if initialization fails
}
rf95.setFrequency(915.0); // Set frequency (adjust for your region)
rf95.setTxPower(23, false); // Set transmission power
rf95.setModeRx(); // Set the module to receive mode
}
void loop() {
// Check if there's a received message
if (rf95.available()) {
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN]; // Buffer for incoming message
uint8_t len = sizeof(buf); // Length of the buffer
if (rf95.recv(buf, &len)) { // Receive the message
buf[len] = '\0'; // Null-terminate the received message
// Check if the message starts with the expected identifier
if (strncmp((char*)buf, expectedIdentifier, strlen(expectedIdentifier)) == 0) {
Serial.print("Received valid message: ");
Serial.write(buf, len); // Output the message to the serial monitor
Serial.println(); // New line for readability
} else {
Serial.println("Received invalid message");
}
} else {
Serial.println("Receive failed"); // Error handling
}
}
delay(100); // Small delay to avoid busy loop
}