1 | #include <RF24Network.h>
|
2 | #include <RF24.h>
|
3 | #include <SPI.h>
|
4 | #include <dht.h>
|
5 | #include "MyTypes.h"
|
6 | #include <Narcoleptic.h>
|
7 |
|
8 | #define DHTPIN 3 // what pin we're connected to
|
9 | #define DHTTYPE DHT22 // DHT 22 (AM2302)
|
10 |
|
11 | DHT dht(DHTPIN, DHTTYPE);
|
12 |
|
13 | // nRF24L01(+) radio attached
|
14 | RF24 radio(9,10);
|
15 |
|
16 | // Network uses that radio
|
17 | RF24Network network(radio);
|
18 |
|
19 | // Address of our node
|
20 | const uint16_t this_node = 1;
|
21 |
|
22 | // Address of the other node (by default send it to the base)
|
23 | const uint16_t other_node = 0;
|
24 |
|
25 | // How often to send 'hello world to the other unit
|
26 | unsigned long interval = 5000; //ms
|
27 |
|
28 | // When did we last send?
|
29 | unsigned long last_sent;
|
30 |
|
31 | // How many have we sent already
|
32 | unsigned long packets_sent;
|
33 |
|
34 | payload_t myData;
|
35 |
|
36 | // Number of packets we've failed to send since we last sent one
|
37 | // successfully
|
38 | uint16_t lost_packets = 0;
|
39 |
|
40 | void setup(void)
|
41 | {
|
42 | Serial.begin(57600);
|
43 | Serial.println("RF24Network/examples/helloworld_tx/");
|
44 |
|
45 | SPI.begin();
|
46 | radio.begin();
|
47 | network.begin(/*channel*/ 90, /*node address*/ this_node);
|
48 | dht.begin();
|
49 |
|
50 | RF24NetworkHeader header(network.parent(), 'k');
|
51 | Serial.print("APP Sending type: k to node:");
|
52 | Serial.println(header.to_node);
|
53 | if ( ! network.write(header, NULL, 0) )
|
54 | Serial.println("failed.");
|
55 |
|
56 | }
|
57 |
|
58 | void loop(void)
|
59 | {
|
60 | delay(1000);
|
61 |
|
62 | // Pump the network regularly
|
63 | network.update();
|
64 |
|
65 | // If so, grab it and print it out
|
66 | // RF24NetworkHeader header;
|
67 | // network.peek(header);
|
68 |
|
69 |
|
70 | if ( this_node > 0 )
|
71 | {
|
72 |
|
73 | float h = dht.readHumidity();
|
74 | float t = dht.readTemperature();
|
75 | if (isnan(h) || isnan(t)) {
|
76 | Serial.println("Failed to read from DHT");
|
77 | }
|
78 | else{
|
79 | myData.value = h;
|
80 | myData.unity[0] = '%';
|
81 | myData.lost_packets = lost_packets;
|
82 |
|
83 | sendData(&myData);
|
84 |
|
85 | delay(1000);
|
86 |
|
87 | myData.value = t;
|
88 | myData.unity[0] = 'C';
|
89 | myData.lost_packets = lost_packets;
|
90 |
|
91 | sendData(&myData);
|
92 | }
|
93 |
|
94 |
|
95 | //radio.powerDown();
|
96 |
|
97 | //radio.powerDown();
|
98 |
|
99 | }
|
100 | }
|
101 |
|
102 | boolean sendData(payload_t* data) {
|
103 | unsigned long now = millis();
|
104 | if ( now - last_sent >= interval )
|
105 | {
|
106 | Serial.println("Sending...");
|
107 | last_sent = now;
|
108 |
|
109 | boolean ok = false;
|
110 |
|
111 | RF24NetworkHeader header(other_node, 'S');
|
112 | ok = network.write(header, &myData, sizeof(myData));
|
113 | if (!ok) {
|
114 | ++lost_packets;
|
115 | Serial.println("failed.");
|
116 | }
|
117 |
|
118 | return ok;
|
119 | }
|
120 |
|
121 | }
|