-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradio.ino
105 lines (86 loc) · 2.08 KB
/
radio.ino
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
#define ENABLE_RADIO_PIN 6
#define VCC_RADIO_PIN 8
LoraDriver radio; //controls the radio
//trys to send the message on each channel until it gets an ack back
bool sendReliably(const uint8_t *data, uint8_t len){
//temporary buffer to receive acks
uint8_t buf[20];
for(int ch=0;ch<3;ch++){
//send data
radio.send(data,len);
radio.waitPacketSent();
//listen until receive ack or timeout expires
if(!radio.waitAvailableTimeout(radio.chtimeout[i])){
//no message received. try again
#ifdef VERBOSE_MODE
Serial.println("no ack recvied");
#endif
continue;
}
len=8;
//read ack into buffer
if(radio.recv(buf,&len)){
#ifdef VERBOSE_MODE
Serial.print("got reply: ");
for(int i=0;i<len;i++){
Serial.print((char)buf[i]);
}
Serial.println("");
delay(50);
#endif
//check if message recevied is right ack
if(radio.testAck(buf,len,radio.waterLabel)){//good ack, return
return true;
}
//not an ack for us, go on to next test message
continue;
}
else
{
#ifdef VERBOSE_MODE
Serial.println("recv failed");
#endif
//received packet, but couldn't read it properly
}
}
//all attempts to send have failed
#ifdef VERBOSE_MODE
Serial.println("all attempts have failed");
#endif
return false;
}
void setupRadio(){
//set up radio pins and turn on radio
pinMode(ENABLE_RADIO_PIN,OUTPUT);
pinMode(VCC_RADIO_PIN,OUTPUT);
wakeRadio();
//ensure radio is properly configured
#ifdef VERBOSE_MODE
Serial.println("testing channels");
radio.testChannels();
#endif
radio.channelModem(2);
#ifdef VERBOSE_MODE
radio.printRadioConfig();
radio.printChannelConfig();
#endif
sleepRadio();
}
void wakeRadio(){
digitalWrite(LED_RADIO_PIN,HIGH);
digitalWrite(VCC_RADIO_PIN, HIGH);
digitalWrite(ENABLE_RADIO_PIN, HIGH);
delay(50);
if (!radio.init())
#ifdef VERBOSE_MODE
Serial.println("init failed");
#endif
;
}
void sleepRadio(){
radio.waitPacketSent();
radio.spiWrite(RH_RF95_REG_01_OP_MODE,RH_RF95_MODE_SLEEP | RH_RF95_LONG_RANGE_MODE);
digitalWrite(ENABLE_RADIO_PIN,LOW);
digitalWrite(VCC_RADIO_PIN,LOW);
digitalWrite(LED_RADIO_PIN,LOW);
}