Example: This is almost exactly the same as the Network_Ping example, but with use of the integrated sleep mode.
This example demonstrates how nodes on the network utilize sleep mode to conserve power. For example, the radio itself will draw about 13.5mA in receive mode. In sleep mode, it will use as little as 22ua (.000022mA) of power when not actively transmitting or receiving data. In addition, the Arduino is powered down as well, dropping network power consumption dramatically compared to previous capabilities.
Note: Sleeping nodes generate traffic that will wake other nodes up. This may be mitigated with further modifications. Sleep payloads are currently always routed to the master node, which will wake up intermediary nodes. Routing nodes can be configured to go back to sleep immediately. The displayed millis() count will give an indication of how much a node has been sleeping compared to the others, as millis() will not increment while a node sleeps.
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include "printf.h"
#include <SPI.h>
#include <RF24.h>
const uint16_t node_address_set[10] = { 00, 02, 05, 012, 015, 022, 025, 032, 035, 045 };
uint8_t NODE_ADDRESS = 1;
RF24 radio(7, 8);
uint16_t this_node;
const unsigned long interval = 1000;
unsigned long last_time_sent;
const short max_active_nodes = 10;
uint16_t active_nodes[max_active_nodes];
short num_active_nodes = 0;
short next_ping_node_index = 0;
bool send_T(uint16_t to);
bool send_N(uint16_t to);
void add_node(uint16_t node);
typedef enum { wdt_16ms = 0,
wdt_32ms,
wdt_64ms,
wdt_128ms,
wdt_250ms,
wdt_500ms,
wdt_1s,
wdt_2s,
wdt_4s,
wdt_8s } wdt_prescalar_e;
unsigned long awakeTime = 500;
unsigned long sleepTimer = 0;
void setup() {
Serial.begin(115200);
printf_begin();
while (!Serial) {
}
Serial.println(F("RF24Network/examples/meshping/"));
this_node = node_address_set[NODE_ADDRESS];
if (!radio.begin()) {
Serial.println(F("Radio hardware not responding!"));
while (1) {
}
}
radio.setPALevel(RF24_PA_HIGH);
radio.setChannel(100);
network.begin( this_node);
network.setup_watchdog(wdt_1s);
}
void loop() {
network.update();
while (network.available()) {
network.peek(header);
case 'T':
handle_T(header);
break;
case 'N':
handle_N(header);
break;
case 'S':
break;
default:
Serial.print(F("*** WARNING *** Unknown message type "));
Serial.println(header.
type);
network.read(header, 0, 0);
break;
};
}
if (millis() - sleepTimer > awakeTime && NODE_ADDRESS) {
Serial.println(F("Sleep"));
sleepTimer = millis();
delay(100);
radio.stopListening();
network.sleepNode(8, 0);
Serial.println(F("Awake"));
}
unsigned long now = millis();
if (now - last_time_sent >= interval) {
last_time_sent = now;
uint16_t to = 00;
if (num_active_nodes) {
to = active_nodes[next_ping_node_index++];
if (next_ping_node_index > num_active_nodes) {
next_ping_node_index = 0;
to = 00;
}
}
bool ok;
if (this_node > 00 || to == 00) {
ok = send_T(to);
} else {
ok = send_N(to);
}
if (ok) {
Serial.print(millis());
Serial.println(F(": APP Send ok"));
} else {
Serial.print(millis());
Serial.println(F(": APP Send failed"));
last_time_sent -= 100;
}
}
}
bool send_T(uint16_t to) {
unsigned long message = millis();
Serial.println(F("---------------------------------"));
Serial.print(millis());
Serial.print(F(": APP Sending "));
Serial.print(message);
Serial.print(F(" to "));
Serial.print(to);
Serial.println(F("..."));
return network.write(header, &message, sizeof(unsigned long));
}
bool send_N(uint16_t to) {
Serial.println(F("---------------------------------"));
Serial.print(millis());
Serial.print(F(": APP Sending active nodes to "));
Serial.print(to);
Serial.println(F("..."));
return network.write(header, active_nodes, sizeof(active_nodes));
}
unsigned long message;
network.read(header, &message, sizeof(unsigned long));
Serial.print(millis());
Serial.print(F(": APP Received "));
Serial.print(message);
Serial.print(F(" from "));
}
static uint16_t incoming_nodes[max_active_nodes];
network.read(header, &incoming_nodes, sizeof(incoming_nodes));
Serial.print(millis());
Serial.print(F(": APP Received nodes from "));
int i = 0;
while (i < max_active_nodes && incoming_nodes[i] > 00)
add_node(incoming_nodes[i++]);
}
void add_node(uint16_t node) {
short i = num_active_nodes;
while (i--) {
if (active_nodes[i] == node)
break;
}
if (i == -1 && num_active_nodes < max_active_nodes) {
active_nodes[num_active_nodes++] = node;
Serial.print(millis());
Serial.print(F(": APP Added "));
Serial.print(node);
Serial.print(F(" to list of active nodes."));
}
}
Definition RF24Network.h:384