Hi everybody!
I am new to this forum and also relatively new to coding Arduino. My aim is to build a system to control different areas in my garden,pool area etc. I have started with RF24 Network library (BTW awesome work !) and I'm using the 'meshping' sketch as a template. I've managed to get communication between 2 Mega and 1 Uno to work without problems. Now I'm trying to implement that unit 00 works as a master, sending different commands to slaves and the slave acts depending of what ID sent (ex. 'Dxxxxxxxxxxxxxx = unit D etc). That is master sends a broadcast to all connected slaves. I've googled and read all posts in this forum but can't figure out how to code this. With my current code I can send a string but it is received by master!. If I send a message to unit 2 it seems to receive but hangs in an infinite loop in 'M-header'. To be honest I don't have the brain to fully understand how the Network library works so I more or less do 'trial and terror' but without success. Has anyone done this or can give me a hint how to do it. I really appreciate if someone could help me out. In conclusion : how to send a broadcast message ? How to get slave respond back to master ?
My present code : (some original code deleted to make it shorter)
/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
// Declarations etc DELETED for shorter code
void handle_T(RF24NetworkHeader& header);
void handle_N(RF24NetworkHeader& header);
//################################################
void handle_M(RF24NetworkHeader& header);
//################################################
void add_node(uint16_t node);
void setup(void)
{
//
// Print preamble
//
Serial.begin(57600);
printf_begin();
printf_P(PSTR("\n\rRF24Network/examples/meshping/\n\r"));
printf_P(PSTR("VERSION: %s\n\r"),program_version);
//
// Pull node address out of eeprom
//
// Which node are we?
this_node = nodeconfig_read();
//
// Bring up the RF network
//
SPI.begin();
radio.begin();
network.begin(/*channel*/ 100, /*node address*/ this_node );
}
void loop(void)
{
bool ok;
uint16_t to;
// Pump the network regularly
network.update();
// Is there anything ready for us?
while ( network.available() )
{
// If so, take a look at it
RF24NetworkHeader header;
network.peek(header);
// Dispatch the message to the correct handler.
switch (header.type)
{
case 'T':
handle_T(header);
Serial.println("T-handle");
break;
case 'N':
handle_N(header);
Serial.println("N-handle");
break;
//#################################################################################################
//Send string to connected nodes from serial port connected to unit 00
case 'M':
stringComplete = false;
handle_M(header);
Serial.println("M-handle");//for debug
Serial.println(stringComplete);
break;
//################################################################################################
default:
printf_P(PSTR("*** WARNING *** Unknown message type %c\n\r"),header.type);
network.read(header,0,0);
break;
};
}
// Send a ping to the next node every 'interval' ms
unsigned long now = millis();
if ( now - last_time_sent >= interval )
{
last_time_sent = now;
// Who should we send to?
// By default, send to base
uint16_t to = 00;
// Or if we have active nodes,
if ( num_active_nodes )
{
// Send to the next active node
to = active_nodes[next_ping_node_index++];
Serial.print("Current Node = ");
Serial.println(to);
// Have we rolled over?
if ( next_ping_node_index > num_active_nodes )
{
// Next time start at the beginning
next_ping_node_index = 0;
// This time, send to node 00.
to = 00;
}
}
// Normal nodes send a 'T' ping
if ( this_node > 00 || to == 00 && ! stringComplete)
ok = send_T(to);
// Base node sends the current active nodes out
else if(! stringComplete)
ok = send_N(to);
// Notify us of the result
if (ok)
{
printf_P(PSTR("%lu: APP Send ok\n\r"),millis());
}
else
{
printf_P(PSTR("%lu: APP Send failed\n\r"),millis());
// Try sending at a different time next time
last_time_sent -= 100;
}
}
//##########################################################################################
// HOW TO BROADCAST TO ALL UNIT'S ????????????????????????
//CHECK THIS LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//if ( this_node == 00 && stringComplete )
else if (stringComplete )
{
{
ok = send_M(to);// sending ok BUT message receieved by 00!!
//ok = send_M(2);// hardcoded to unit 2. Unit 2 receives data but hangs in infinite loop in header.type = 'M'
}
if (ok)
{
stringComplete = false;
msgtoread=true;
}
else
{
stringComplete = false;
msgtoread=false;
}
}
//#################################################################################
}
//END MAIN LOOP
/******** ORIGINAL CODE DELETED FOR :
// Send a 'T' message, the current time
// Send an 'N' message, the active node list
// Handle a 'T' message
// Handle an 'N' message, the active node list
void serialEvent() {
// Where in EEPROM is the address stored?
uint8_t* address_at_eeprom_location = (uint8_t*)10;
// What flag value is stored there so we know the value is valid?
const uint8_t valid_eeprom_flag = 0xdf;
// What are the actual node values that we want to use?
// EEPROM locations are actually just indices into this array
const uint16_t node_address_set[10] = { 00, 02, 05, 012, 015, 022, 025, 032, 035, 045 };
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// check if we want to set address
if ( inChar >= '0' && inChar <= '9' )
{
// It is our address
eeprom_write_byte(address_at_eeprom_location,valid_eeprom_flag);
eeprom_write_byte(address_at_eeprom_location+1,inChar-'0');
// And we are done right now (no easy way to soft reset)
printf_P(PSTR("\n\rManually reset index to: %inChar, address 0%o\n\rPress RESET to continue!"),inChar,node_address_set[inChar-'0']);
while(1);
}
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
//######################### END serialEvent #######################################
bool send_M(uint16_t to)
{
RF24NetworkHeader header(/*to node*/ to, /*type*/ 'M' /*Message*/);
Serial.println("SENDING... ");
inputString.trim();//remove CR
char str_data[data_size];
inputString.toCharArray(str_data, data_size);
bool ok = network.write(header,str_data,data_size);
if (ok)
{
Serial.println(inputString);
Serial.print("OK SENDING : ");
Serial.println(str_data);
inputString="";
msgtoread=true;
}
else
Serial.println("Failed to send message");
msgtoread=false;
inputString="";
}
//######################### END send_M #######################################
void handle_M(RF24NetworkHeader& header)
{
if ( msgtoread)
{
char str_data[data_size];
bool ok = network.read(header,str_data,data_size);
if (ok)
{
msgtoread=false;
//printf_P(PSTR("%lu: NODE Received %lu from 0%o\n\r"),millis(),str_data,header.from_node);
Serial.print("NODE recieved : ");
Serial.print(str_data);
Serial.print(" from : ");
Serial.println(header.from_node);
}
else
{
Serial.println("Failed to read message");
msgtoread=false;
}
//*********************************************************************************
// If this message is from ourselves or the base, don't bother adding it to the active nodes.
//if ( header.from_node != this_node || header.from_node > 00 )
// add_node(header.from_node);
}
}
//######################### END handle_M #######################################
Regards Hucke