-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnode.ino
More file actions
168 lines (115 loc) · 3.46 KB
/
node.ino
File metadata and controls
168 lines (115 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(9,10);
const long long // Use pipe + 1 for nodes, pipe + 2 for relays
RELAYBROADCAST = 0xAA00000000LL,
NODEACK = 0xCC00000000LL;
struct SENSOR{
float temp;
float humidity;
float pressure;
};
struct HEADER{
long type;
long hops;
long src;
long ID;
SENSOR sensor;
};
long cnt[10] = {};
byte cntID = 0;
byte retries = 0; // How many times have we tried to rx
const byte MAX_RETRIES = 5; // How many times will we try?
long myID;
void setup(void){
Serial.begin(57600);
radio.begin();
radio.setRetries(15,15);
radio.enableDynamicPayloads();
randomSeed(analogRead(0));
Serial.println(F("node starting..."));
radio.openReadingPipe(1 ,NODEACK + 1); // Read the 'node' pipe
myID = random(1, 0xffff); // Identify this packet
xmit(myID, 2); // Send using the 'relay' pipe
}
void loop(void){
myID = random(1, 0xffff); // Identify this packet
xmit(myID, 1); // Send some data
wait(MAX_RETRIES, myID); // Wait for it to be acknowledged
delay(3000); // Pause before repeating
beARelay(); // If serial port connected send some more data (for what purpose?)
}
// Get Ack from relay or timeout
void wait(byte retries, long myID) {
bool reply;
do {
unsigned long started_waiting_at = millis();
while (millis() - started_waiting_at < 250) {
// Wait here until we get a response, or timeout (250ms)
if (reply = radio.available()) break;
}
retries--;
} while (retries > 0 && !reply);
if (reply) {
ack(reply, myID);
}
else {
nak(myID);
}
}
// Signal a NAK
void nak(long myID) {
Serial.print(F("NACK: ")); Serial.println(myID, HEX);
}
// Signal an ACK
void ack(bool reply, long myID) {
if (reply) {
long src; //ack returns just the header.ID, so check what was returned with what was sent
radio.read( &src, radio.getDynamicPayloadSize() );
if (src == myID) {
Serial.print(F("ACK: ")); Serial.println(src, HEX);
}
// else { // Display packets destined for nodes other than us
// Serial.print(src, HEX);Serial.print(" = "); Serial.println(myID, HEX);
// }
}
else {
Serial.println(F("NOT AVAILABLE"));
}
}
// If something is typed on the Serial console, pretend to be a relay and send a relay broadcast
void beARelay() {
if ( Serial.available() )
{
Serial.read();
myID = random(1, 0xffff);
xmit(myID, 2);
Serial.print(F("TEST: ")); Serial.println(myID, HEX);
}
}
// Send some data to the base
void xmit(long myID, byte pipe_id) {
HEADER header;
header.ID = myID;
header.type = 3;
header.hops = 0;
header.src = 0xabcd;
header.sensor.temp = 18.8;
Serial.print(F("XMIT: "));Serial.println(header.ID, HEX);
byte retries = 5;
bool ok;
do {
ok = relay(header, pipe_id); // Send using the 'node' pipe
} while (!ok && --retries > 0);
}
// Send packet to any relay that can hear this node
bool relay(struct HEADER header, byte pipe_id) {
radio.stopListening();
radio.openWritingPipe(RELAYBROADCAST + pipe_id);
bool ok = radio.write( &header, sizeof(header), true );
Serial.print(ok ? F("SENT ") : F("FAILED TO SEND ")); // Seems to fail to transmit often
Serial.println(header.ID, HEX);
radio.startListening();
return ok;
}