Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not getting signal strength of nodes #11

Open
gshubham586 opened this issue Mar 24, 2021 · 1 comment
Open

Not getting signal strength of nodes #11

gshubham586 opened this issue Mar 24, 2021 · 1 comment

Comments

@gshubham586
Copy link

I am using arduino nano and AI-Thinker SX1278 RF RA-02 lora module.

This is my code-

//#include <EEPROM.h>
//#include <Arduino.h>
#include <SPI.h>
//#include <ESP8266WiFi.h>
#include <RHRouter.h>
#include <RHMesh.h>
#include <RH_RF95.h>
//#define RH_HAVE_HARDWARE_SPI
#define RH_HAVE_SERIAL
#define LED 13
#define N_NODES 4

uint8_t nodeId;
uint8_t routes[N_NODES]; // full routing table for mesh
int16_t rssi[N_NODES]; // signal strength info

#define RFM95_CS 10
#define RFM95_RST 9
#define RFM95_INT 2

// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS,RFM95_INT);

// Class to manage message delivery and receipt, using the driver declared above
RHMesh *manager;

// message buffer
char buf[RH_MESH_MAX_MESSAGE_LEN];

/*int freeMem() {
extern int __heap_start, __brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
/

void setup() {
randomSeed(analogRead(A0));

//pinMode(9, OUTPUT);
// digitalWrite(9, HIGH);

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

pinMode(LED, OUTPUT);
Serial.begin(115200);
while (!Serial) ; // Wait for serial port to be available
Serial.println("program is going to execute");
//nodeId = EEPROM.read(0);
nodeId = 1;
if (nodeId > 10) {
Serial.print(F("EEPROM nodeId invalid: "));
Serial.println(nodeId);
nodeId = 1;
}
Serial.print(F("initializing node "));
delay(1000);
manager = new RHMesh(rf95, nodeId);

digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);

if (!manager->init()) {
//if (!rf95.init()) {
Serial.println(F("init failed"));
} else {
Serial.println("init successful");
}
rf95.setTxPower(23, false);
rf95.setFrequency(433.0);
//rf95.setCADTimeout(500);

////////////////////////////////////////////////////
if (!rf95.setFrequency(433.0)) {

Serial.println("setFrequency failed");

while (1);

}
else{
Serial.println("Set frequency successful");
}

///////////////////////////////////////////////////////

// Possible configurations:
// Bw125Cr45Sf128 (the chip default)
// Bw500Cr45Sf128
// Bw31_25Cr48Sf512
// Bw125Cr48Sf4096

// long range configuration requires for on-air time
boolean longRange = false;
if (longRange) {
RH_RF95::ModemConfig modem_config = {
0x78, // Reg 0x1D: BW=125kHz, Coding=4/8, Header=explicit
0xC4, // Reg 0x1E: Spread=4096chips/symbol, CRC=enable
0x08 // Reg 0x26: LowDataRate=On, Agc=Off. 0x0C is LowDataRate=ON, ACG=ON
};
rf95.setModemRegisters(&modem_config);
if (!rf95.setModemConfig(RH_RF95::Bw125Cr45Sf128)) {
Serial.println(F("set config failed"));
}
}

Serial.println("RF95 ready");

for(uint8_t n=1;n<=N_NODES;n++) {
routes[n-1] = 0;
rssi[n-1] = 0;
}

}

const __FlashStringHelper* getErrorString(uint8_t error) {
switch(error) {
case 1: return F("invalid length");
break;
case 2: return F("no route");
break;
case 3: return F("timeout");
break;
case 4: return F("no reply");
break;
case 5: return F("unable to deliver");
break;
}
return F("unknown");
}

void updateRoutingTable() {
for(uint8_t n=1;n<=N_NODES;n++) {
RHRouter::RoutingTableEntry *route = manager->getRouteTo(n);
if (n == nodeId) {
routes[n-1] = 255; // self
} else {
routes[n-1] = route->next_hop;
if (routes[n-1] == 0) {
// if we have no route to the node, reset the received signal strength
rssi[n-1] = 0;
}
}
}
}

// Create a JSON string with the routing info to each node
void getRouteInfoString(char *p, size_t len) {
p[0] = '\0';
strcat(p, "[");
for(uint8_t n=1;n<=N_NODES;n++) {
strcat(p, "{"n":");
sprintf(p+strlen(p), "%d", routes[n-1]);
strcat(p, ",");
strcat(p, ""r":");
sprintf(p+strlen(p), "%d", rssi[n-1]);
strcat(p, "}");
if (n<N_NODES) {
strcat(p, ",");
}
}
strcat(p, "]");
}

void printNodeInfo(uint8_t node, char *s) {
Serial.print(F("node: "));
Serial.print(F("{"));
Serial.print(F("""));
Serial.print(node);
Serial.print(F("""));
Serial.print(F(": "));
Serial.print(s);
Serial.println(F("}"));
}

void loop() {

for(uint8_t n=1;n<=N_NODES;n++) {
if (n == nodeId) continue; // self

updateRoutingTable();
getRouteInfoString(buf, RH_MESH_MAX_MESSAGE_LEN);

Serial.print(F("->"));
Serial.print(n);
Serial.print(F(" :"));
Serial.print(buf);

// send an acknowledged message to the target node
uint8_t error = manager->sendtoWait((uint8_t *)buf, strlen(buf), n);
if (error != RH_ROUTER_ERROR_NONE) {
  Serial.println();
  Serial.print(F(" ! "));
  Serial.println(getErrorString(error));
} else {
  Serial.println(F(" OK"));
  // we received an acknowledgement from the next hop for the node we tried to send to.
  RHRouter::RoutingTableEntry *route = manager->getRouteTo(n);
  if (route->next_hop != 0) {
    rssi[route->next_hop-1] = rf95.lastRssi();
  }
}
if (nodeId == 1) printNodeInfo(nodeId, buf); // debugging

// listen for incoming messages. Wait a random amount of time before we transmit
// again to the next node
unsigned long nextTransmit = millis() + random(3000, 5000);
while (nextTransmit > millis()) {
  int waitTime = nextTransmit - millis();
  uint8_t len = sizeof(buf);
  uint8_t from;
  if (manager->recvfromAckTimeout((uint8_t *)buf, &len, waitTime, &from)) {
    buf[len] = '\0'; // null terminate string
    Serial.print(from);
    Serial.print(F("->"));
    Serial.print(F(" :"));
    Serial.println(buf);
    if (nodeId == 1) printNodeInfo(from, buf); // debugging
    // we received data from node 'from', but it may have actually come from an intermediate node
    RHRouter::RoutingTableEntry *route = manager->getRouteTo(from);
    if (route->next_hop != 0) {
      rssi[route->next_hop-1] = rf95.lastRssi();
    }
  }
}

}

}

This is OUTPUT-

program is going to execute
initializing node init successful
Set frequency successful
RF95 ready
->2 :[{"n":255,"r":0},{"n":0,"r":0},{"n":0,"r":0},{"n":0,"r":0}]
! no route
node: {"1": [{"n":255,"r":0},{"n":0,"r":0},{"n":0,"r":0},{"n":0,"r":0}]}
->3 :[{"n":255,"r":0},{"n":0,"r":0},{"n":0,"r":0},{"n":0,"r":0}]

The problem is that I am not getting noe no. and rsi values of the nodes. I am using 4 nodes. Please help and thanks in advance.

@sitharthen
Copy link

am also getting the same output NO ROUTE is found. Did you solved it ? any one can help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants