-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoRaTx_Test.ino
126 lines (108 loc) · 2.73 KB
/
LoRaTx_Test.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <SPI.h>
#include <RH_RF95.h>
// Set pin for Feather32u4
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 7
// Set pin for distance sensor
#define trigPin 2
#define echoPin 3
// set frequency of transmission to 434.0, must match RX's frequency.
#define RF95_FREQ 434.0
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
void setup()
{
Serial.begin(9600);
pinMode(RFM95_RST, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(echoPin, HIGH);
digitalWrite(RFM95_RST, HIGH);
while (!Serial) {
delay(1);
}
delay(100);
Serial.println("Feather LoRa TX Test!");
// manual reset
digitalWrite(RFM95_RST, LOW);
delay(100);
digitalWrite(RFM95_RST, HIGH);
delay(100);
// check driver's initialisation
while(!rf95.init())
{
Serial.println("LoRa radio init failed, try again!");
while (1);
}
Serial.println("LoRa radio init OK!");
// check Transmitter's frequency
while(!rf95.setFrequency(RF95_FREQ))
{
Serial.println("set frequency failed, try again!");
}
Serial.print("Set Freq to: ");
Serial.println(RF95_FREQ);
// Sets the transmitter power output level
rf95.setTxPower(23, false);
}
int i = 1; // counter
void loop()
{
Serial.println("Preparing Package...");
delay(1000); // Wait 1 second between transmits, could also 'sleep' here!
char charBuf[25];
float distance = distanceCal();
String ii = String(i);
String temp = "distance "+ii;
String temp2 = " : ";
String cm = " cm";
String package = temp+temp2+String(distance)+cm;
package.toCharArray(charBuf,package.length()+1);
Serial.print("Sending \""); Serial.println(package+"\"");
// If there is no ack back, it will send current package again.
while(true)
{
Serial.println("Sending...");
delay(100);
rf95.send((uint8_t *)charBuf, sizeof(charBuf));
delay(100);
rf95.waitPacketSent();
// Now wait for an ACK
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
Serial.println("Waiting for ACK...");
if (rf95.waitAvailableTimeout(15000))
{
// check ACK
if (rf95.recv(buf, &len))
{
Serial.print("Got ACK: ");
Serial.println((char*)buf);
i++;
break;
}
else
{
Serial.println("Receive ACK failed");
}
}
else
{
Serial.println("No ACK, Send "+temp+" again");
}
}
}
// find distance
float distanceCal()
{
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2.0) / 29.1;
return distance;
}