-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patha2b_RECIEVE.ino
92 lines (77 loc) · 2.71 KB
/
a2b_RECIEVE.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
// Receive
#include <SoftwareSerial.h>
SoftwareSerial hc12Serial(2, 3); // RX, TX
int ledPin = 13;
int ledPin2 = 12;
const long BAUD_RATE = 9600;
const char * ID = "1234"; // Unique ID for RX TX pair
int idVerify;
int A2BStatus;
int batteryPercent;
int sleepStatus;
void setup()
{
hc12Serial.begin(BAUD_RATE);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(4800); // For debugging
}
void readSerial(){
char idRead[5]; // ID read buffer
char A2B[2]; // Anti-two-block status read
char batteryRead[4]; // Battery read of TX
char sleepRead[2]; // Sleep status of TX
char garbage[100];
hc12Serial.readBytesUntil('\0',garbage,100); // serial flush for safer first read
hc12Serial.readBytes(idRead, 4); // Read bytes into char arrays
hc12Serial.readBytes(A2B, 1);
hc12Serial.readBytes(batteryRead, 3);
hc12Serial.readBytes(sleepRead, 1);
idRead[4] = '\0'; A2B[1] = '\0'; // Insert null terminators for safer read
batteryRead[3] ='\0'; sleepRead[1] ='\0';
idVerify = strncmp(ID, idRead, 4); // returns 0 if ID is valid
A2BStatus = atoi(A2B); // Convert char to int
batteryPercent = atoi(batteryRead);
sleepStatus = atoi(sleepRead);
Serial.println("Receiving Status Update");
Serial.print("ID Compare: ");Serial.println(idVerify);
Serial.print("A2B: ");Serial.println(A2BStatus);
Serial.print("Battery: ");Serial.println(batteryPercent);
Serial.print("Sleep: ");Serial.println(sleepStatus);
Serial.println();
}
void loop()
{
boolean ledState = digitalRead(ledPin);
if (hc12Serial.available() >= 10) { // minimum valid serial length
readSerial();
if (!idVerify) { // Valid ID
if(A2BStatus == 1){
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
if (batteryPercent <= 25) {
digitalWrite(ledPin2, HIGH);
Serial.println("Low battery");
} else {
digitalWrite(ledPin2, LOW);
}
while (sleepStatus >= 2) {
Serial.println("Transmitter is Sleeping");
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
if (hc12Serial.available() >= 10) { // Serial Message recieved
readSerial();
if (!idVerify){ // Valid Serial
break;
}
}
}
}
hc12Serial.flush();
}
delay(20);
}