Hello,
first of all, sorry if this is the wrong section for my question, but lets get to the point. I want to build a relatively simple iot/homeautomation sensor mesh network using the RF24Mesh library by tmrh20. Everything works fine as long as every node has direct connection to my raspberry logger/gateway (still in the works), but as soon as a node gets too far away to have a direct connection to my gateway, it can
t connect to my network even though it clearly would be in range of several other nodes.
Here is my code:
Raspi gateway:
#include "RF24Mesh/RF24Mesh.h"
#include <RF24/RF24.h>
#include <RF24Network/RF24Network.h>
RF24 radio( 22, 0, BCM2835_SPI_SPEED_8MHZ);
RF24Network network(radio);
RF24Mesh mesh(radio,network);
const uint8_t channel = 0x40; //nrf24l01 type channel of the mesh network
std::string jsonValues;
int main(int argc, char** argv) {
mesh.setNodeID(0); // Set the nodeID to 0 for the master node
printf("start\n");
mesh.begin(channel,RF24_250KBPS,60000); // Connect to the mesh, connection speed 250kbps
radio.printDetails(); //print Radio Settings,for debugging
while(1) {
mesh.update(); // Call network.update as usual to keep the network updated
mesh.DHCP(); // In addition, keep the 'DHCP service' running on the master node so addresses will be assigned to the sensor nodes
while(network.available()){ // Check for incoming data from the sensors
RF24NetworkHeader header;
network.peek(header);
char buf[456];
switch(header.type){ //check which kind of message was received
case 'V':
network.read(header,&buf,456); //received value from sensor node,max size 456 bytes, same as set in RF24Network_config.h
printf("received V type header\n");
jsonValues = buf;
printf("%s\n",jsonValues.c_str());
break;
case 'R':
network.read(header,&buf,456); //received data request from actuator or display node, max size 456 bytes
printf("received R type header\n");
break;
default:
network.read(header,0,0); //unknown message type
printf("received unknown message type\n");
break;
}
memset(&buf[0],0,sizeof(buf));
}
delay(2);
}
return 0;
}
node with sensors that is too far from the gateway, would need to connect through another node:
#include <Adafruit_Sensor.h>
#include <Adafruit_AM2320.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include "RF24.h"
#include "RF24Network.h"
#include "RF24Mesh.h"
#include <EEPROM.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensors(&oneWire);
#define NUMBER_OF_SENSORS 3
Adafruit_AM2320 am2320 = Adafruit_AM2320();
//const String sensorName[NUMBER_OF_SENSORS] = {"airT", "waterT"};
const String sensorName[NUMBER_OF_SENSORS] = {"airT", "waterT", "relH"};
double sensorValue[NUMBER_OF_SENSORS];
RF24 radio(7,8);
RF24Network network(radio);
RF24Mesh mesh(radio, network);
#define nodeID 22
unsigned long lastsent = 0;
#define INTERVAL 2000
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
tempSensors.begin();
am2320.begin();
mesh.setNodeID(nodeID);
Serial.println(mesh.getNodeID());
mesh.setChild(1);
mesh.begin(0x40,RF24_250KBPS,60000);
}
String getJSON() {
String jsonObject = "{\"node\":\"teich\",\"data\":[";
for(int i = 0; i < NUMBER_OF_SENSORS; i++){
jsonObject = jsonObject + "{\"name\":\"" + sensorName[i] + "\",\"val\":" + sensorValue[i] + "},";
}
jsonObject = jsonObject + "],}";
return jsonObject;
}
void getSensorVals(){
tempSensors.requestTemperatures(); //Request new temp readings from the dallas temperature sensors (ds18b20)
double airtd = tempSensors.getTempCByIndex(0);
double airta = am2320.readTemperature();
if (airtd == -127.00 || airtd == 0.00){
sensorValue[0] = airta;
}
else{
sensorValue[0] = ((2*airtd + airta)/3);
}
//sensorValue[0] = tempSensors.getTempCByIndex(0);
sensorValue[1] = tempSensors.getTempCByIndex(1);
sensorValue[2] = am2320.readHumidity();
}
void loop() {
mesh.update();
if((millis()) >= (lastsent + INTERVAL)){
getSensorVals();
String jsonObject = getJSON(); //build a new JSON String with new sensor values
mesh.write( jsonObject.c_str() , 'V', jsonObject.length()); // send a "value" Type Message containing the jsonObject
Serial.println(mesh.mesh_address,OCT);
lastsent = millis();
}
if (!mesh.checkConnection()) {
mesh.renewAddress();
}
}
basic repeater node just for testing:
#include <SPI.h>
#include "RF24.h"
#include "RF24Network.h"
#include "RF24Mesh.h"
#include <EEPROM.h>
/*
#define NUMBER_OF_SENSORS 3
const String sensorName[NUMBER_OF_SENSORS] = {};
double sensorValue[NUMBER_OF_SENSORS];
*/
RF24 radio(8,7);
RF24Network network(radio);
RF24Mesh mesh(radio, network);
#define nodeID 02
unsigned long lastsent = 0;
#define INTERVAL 5000
const String hello = "hello from node 2";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mesh.setNodeID(nodeID);
Serial.println(mesh.getNodeID());
mesh.setChild(1);
mesh.begin(0x40,RF24_250KBPS,60000);
}
/*
String getJSON() {
String jsonObject = "{\"node\":\"teich\",\"data\":[";
for(int i = 0; i < NUMBER_OF_SENSORS; i++){
jsonObject = jsonObject + "{\"name\":\"" + sensorName[i] + "\",\"val\":" + sensorValue[i] + "},";
}
jsonObject = jsonObject + "],}";
return jsonObject;
}
*/
void loop() {
mesh.update();
if((millis()) >= (lastsent + INTERVAL)){
mesh.write( hello.c_str() , 'V', hello.length());
Serial.println(mesh.mesh_address,OCT);
lastsent = millis();
}
if (!mesh.checkConnection()) {
mesh.renewAddress();
}
}
i also heard that fake modules might cause a problem, i use these modules:
4Pcs Geekcreit NRF24L01 2.4GHz Wireless Transceiver Module Built-in 2.4Ghz Anten Sale - Banggood USA-arrival notice-arrival notice, because i read somewhere that they would have greater range than the standard, black 8 pin ones.
i also tried to set fixed addresses for my nodeson the gateway, forcing them to a specific network "route", but the command mesh.set address(nodeID, Address) doesn't seem to have any effect.
Help would be geatly appreciated!
Links:
RF24Mesh Class documentation
radio module
my Github page for this project that i only use for my personal stuff