Hi,
Currently i’m working on RAK3172 and RUI3. I have some node sensors (TX) and only one receiver (RX) to collect some sensor datas on one point. I’m using LoRa P2P mode (not LoRaWAN mode). Code will be on arduino ide.
Node sensors sending data to receiver with constant target adress. It means that, when system deployed, in any error situation, it’s not possible to change target adresses without updating node firmware.
So i’m looking for how to establish the P2P communication with something like “mesh” network.
What i need:
- When node sensors powered up, they will scan another devices in range and be aware about them.
I found this code and don’t know it gonna work or not. Can anyone help me?
#include <RHMesh.h>
#include <RH_RF95.h>
#define NODE_ADDRESS 1
RH_RF95 rf95;
RHMesh mesh(rf95, NODE_ADDRESS);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(9600);
while (!Serial) ;
if (!rf95.init()) {
Serial.println("LoRa radio init failed");
while (1);
}
rf95.setFrequency(915.0);
rf95.setTxPower(23, false);
mesh.setPromiscuous(true);
mesh.setRetries(3);
}
void loop() {
mesh.update();
if (mesh.available()) {
RHMesh::Message msg;
if (mesh.recv(msg)) {
Serial.print("Received message from node ");
Serial.print(msg.headerFrom, DEC);
Serial.print(": ");
Serial.println((char*)msg.data);
// Forward message to other nodes in mesh network
msg.headerTo = RH_BROADCAST_ADDRESS;
mesh.send(msg, 0);
}
}
// Send a message to the receiver node
char msg[] = "Hello from node 1!";
RHMesh::Message message(msg, sizeof(msg));
message.setHeaderTo(2);
mesh.send(message, 0);
// Scan for nearby nodes and print their addresses
RH_RF95::Promiscuous promiscuousMode;
promiscuousMode = rf95.setPromiscuous(true);
delay(1000);
rf95.setPromiscuous(promiscuousMode);
Serial.println("Available nodes:");
for (int i = 1; i <= 255; i++) {
if (mesh.getAddressNodeList().contains(i)) {
Serial.println(i);
}
}
delay(1000);
}