-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram4.ino
More file actions
70 lines (59 loc) · 2.47 KB
/
program4.ino
File metadata and controls
70 lines (59 loc) · 2.47 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
#include <SoftwareSerial.h>
//--- Begin Pin Declarations ---//
const byte HC12RxdPin = 4; // "RXD" Pin on HC12
const byte HC12TxdPin = 5; // "TXD" Pin on HC12
const byte HC12SetPin = 6; // "SET" Pin on HC12
const byte GPSTxdPin = 7; // "TXD" on GPS (if available)
const byte GPSRxdPin = 8; // "RXD" on GPS
//--- End Pin Declarations ---//
//--- Begin variable declarations ---//
char GPSbyteIn; // Temporary variable
String GPSBuffer3 = ""; // Read/Write Buffer 3 -- GPS
boolean debug = false;
boolean HC12End = false; // Flag for End of HC12 String
boolean GPSEnd = false; // Flag for End of GPS String
boolean commandMode = false; // Send AT commands to remote receivers
//--- End variable declarations ---//
// Create Software Serial Ports for HC12 & GPS
// Software Serial ports Rx and Tx are opposite the HC12 Rxd and Txd
SoftwareSerial HC12(HC12TxdPin, HC12RxdPin);
SoftwareSerial GPS(GPSRxdPin, GPSTxdPin);
void setup() {
buffer3.reserve(82); // Reserve 82 bytes for longest NMEA sentence
pinMode(HC12SetPin, OUTPUT); // Output High for Transparent / Low for Command
digitalWrite(HC12SetPin, HIGH); // Enter Transparent mode
delay(80); // 80 ms delay before operation per datasheet
HC12.begin(4800); // Open software serial port to HC12
GPS.begin(9600); // Open software serial port to GPS
GPS.listen();
// --- Begin One-Time HC12 Auto-Configuration ---
digitalWrite(HC12SetPin, LOW); // Enter command mode
delay(100); // Wait for HC12 to settle
HC12.println("AT+RX"); // Example AT command
delay(200); // Wait for reply
while (HC12.available()) {
Serial.write(HC12.read()); // Print HC12's response to serial monitor
}
digitalWrite(HC12SetPin, HIGH); // Return to transparent mode
delay(100); // Wait for HC12 to stabilize
// --- End Auto-Configuration ---
}
void loop() {
while (GPS.available()) {
byteIn = GPS.read();
buffer3 += char(byteIn);
if (byteIn == '\n') {
GPSEnd = true;
}
}
if (GPSEnd) {
// GPRMC, GPVTG, GPGGA, GPGSA, GPGSV, GPGLL
if (buffer3.startsWith("$GPRMC") || buffer3.startsWith("$GPGGA") || buffer3.startsWith("$GPGLL")) {
HC12.print(buffer3); // Transmit RMC, GGA, and GLL sentences
buffer3 = ""; // Clear buffer
} else {
buffer3 = ""; // Delete GSA, GSV, VTG sentences
}
GPSEnd = false; // Reset GPS flag
}
}