/
RF_Blinds.ino
298 lines (273 loc) · 8.87 KB
/
RF_Blinds.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <RFM69.h>
#include <SPI.h>
#include <LowPower.h>
#include <Servo.h>
// Uncomment the appropriate frequency for your hardware
//#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
#define FREQUENCY RF69_915MHZ
//#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
//ENCRYPT_KEY must match gateway value and must be exactly 16 characters
#define ENCRYPT_KEY "sampleEncryptKey"
#define ADDRESS 1
#define NETWORK_ID 1
#define BLINDS_1 0b00000001
//#define BLINDS_2 0b00000010
//#define BLINDS_3 0b00000100
//#define BLINDS_4 0b00001000
//#define BLINDS_5 0b00010000
//#define BLINDS_6 0b00100000
//#define BLINDS_7 0b01000000
#define MY_NODE_FLAG BLINDS_1
struct PACKET
{
uint8_t percent;
uint8_t voltage1;
uint8_t voltage2;
char charger;
} packetOut;
struct inPACKET
{
uint8_t
group,
value;
} packetIn;
RFM69 radio;
const int wakeUpPin = 3;
const int moveUpPin = 4;
const int servoPowerPin = 5;
const int moveDownPin = 7;
const int servoPin = 6;
//const uint8_t boostPin = 'D15';
const int okStatus = 16;
const int chStatus = 15;
int currentAngle = 0;
Servo blindServo;
//Voltage Meter correction value: (Measured Voltage of Input) / (Voltage Reading when correctionValue = 1)
float correctionValue = 1.0000;
void setup() {
Serial.begin(115200);
radio.initialize(FREQUENCY, ADDRESS, NETWORK_ID);
radio.encrypt(ENCRYPT_KEY);
radio.listenModeEnd();
//Serial.print("Network ID: ");
//Serial.print(NETWORK_ID);
//Serial.print(", Address: ");
//Serial.println(ADDRESS);
pinMode(wakeUpPin, INPUT);
pinMode(moveUpPin, INPUT);
pinMode(moveDownPin, INPUT);
pinMode(servoPowerPin, OUTPUT);
//pinMode(boostPin, OUTPUT);
pinMode(A0, INPUT);
pinMode(okStatus, INPUT_PULLUP);
pinMode(chStatus, INPUT_PULLUP);
blindServo.attach(servoPin);
//digitalWrite(boostPin, HIGH);
}
void loop() {
int angle;
attachInterrupt(1, wakeUpSwitch, HIGH);
if (digitalRead(moveUpPin) == HIGH) {
while(digitalRead(moveUpPin) == HIGH){
//digitalWrite(boostPin, HIGH);
digitalWrite(servoPowerPin, HIGH);
delay(75);
currentAngle = blindServo.read();
if (currentAngle < 175) {
currentAngle += 5;
blindServo.write(currentAngle);
delay(50);
//Serial.print("Opening, Setting servo angle to: ");
//Serial.println(blindServo.read());
}
else if (currentAngle < 180 && currentAngle > 175) {
currentAngle += 1;
blindServo.write(currentAngle);
delay(50);
//Serial.print("Opening, Setting servo angle to: ");
//Serial.println(blindServo.read());
}
else {
blindServo.write(180);
//Serial.println("Opening, Setting servo angle to: 180");
}
}
}
else if (digitalRead(moveDownPin) == HIGH) {
while(digitalRead(moveDownPin) == HIGH){
//digitalWrite(boostPin, HIGH);
digitalWrite(servoPowerPin, HIGH);
delay(75);
currentAngle = blindServo.read();
if (currentAngle > 5) {
currentAngle -= 5;
blindServo.write(currentAngle);
delay(50);
//Serial.print("Closing, Setting servo angle to: ");
//Serial.println(blindServo.read());
}
else if (currentAngle > 0 && currentAngle < 5) {
currentAngle -= 1;
blindServo.write(currentAngle);
delay(50);
//Serial.print("Closing, Setting servo angle to: ");
//Serial.println(blindServo.read());
}
else {
blindServo.write(0);
//Serial.println("Closing, Setting servo angle to: 0");
}
}
}
else {
if (radio.receiveDone()) {
if (radio.ACKRequested()) {
radio.sendACK();
}
//Serial.println("Received a normal message");
//Serial.println((char*)radio.DATA);
Serial.flush();
}
radio.listenModeStart();
if (digitalRead(wakeUpPin) == LOW) {
//digitalWrite(boostPin, LOW);
digitalWrite(servoPowerPin, LOW);
delay(5);
//Serial.println("Entering low-power listen mode...");
Serial.flush();
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
// Woke up, check for a message
//delay(2000);
//Serial.println("Woke up!");
//delay(25);
//digitalWrite(boostPin, HIGH);
detachInterrupt(1);
}
int sensorValue = analogRead(A0);
////Serial.println(sensorValue);
// delay(1);
sensorValue = analogRead(A0);
// //Serial.println(sensorValue);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = ((sensorValue * (3.3 / 1023.0)) * 2);
voltage *= correctionValue;
// //Serial.println(voltage);
voltage *= 1000;
// //Serial.println(voltage);
float percent = (((voltage / 1000) - 3.3) / 0.9125) * 100;
// //Serial.print("Battery Voltage: ");
// //Serial.println(voltage);
// //Serial.print("Battery Percent: ");
// //Serial.println(percent);
packetOut.percent = (int)percent;
packetOut.voltage1 = (int)(voltage / 100);
packetOut.voltage2 = (int)(voltage - (packetOut.voltage1 * 100));
// //Serial.print("Battery Percent: ");
// //Serial.println(packetOut.percent);
// //Serial.print("Battery Voltage1: ");
// //Serial.println(packetOut.voltage1);
// //Serial.print("Battery Voltage2: ");
// //Serial.println(packetOut.voltage2);
//Check the status of the solar charger
if (digitalRead(okStatus) == LOW && digitalRead(chStatus) == HIGH) {
packetOut.charger = 'O';
// //Serial.println(digitalRead(okStatus));
// //Serial.println(digitalRead(chStatus));
// //Serial.println("Solar: Fully Charged");
}
else if (digitalRead(okStatus) == HIGH && digitalRead(chStatus) == LOW) {
packetOut.charger = 'C';
// //Serial.println(digitalRead(okStatus));
// //Serial.println(digitalRead(chStatus));
// //Serial.println("Solar: Charging");
}
else if (digitalRead(okStatus) == LOW && digitalRead(chStatus) == LOW) {
packetOut.charger = 'E';
// //Serial.println(digitalRead(okStatus));
// //Serial.println(digitalRead(chStatus));
// //Serial.println("Solar: ERROR");
}
else {
packetOut.charger = 'N';
// //Serial.println(digitalRead(okStatus));
// //Serial.println(digitalRead(chStatus));
// //Serial.println("Solar: Not Charging");
// //Serial.println(packetOut.charger);
}
int initialPosition = blindServo.read();
uint8_t from = 0;
long burstRemaining = 0;
if (radio.DATALEN > 0) {
////Serial.println("Received a message in listen mode");
////Serial.println((char*)radio.DATA);
if (radio.TARGETID == 255)
{
struct inPACKET
*myPacket = (struct inPACKET *)radio.DATA;
angle = (myPacket->value);
if (myPacket->group & MY_NODE_FLAG)
{
digitalWrite(servoPowerPin, HIGH);
delay(75);
currentAngle = blindServo.read();
//Serial.print("Angle: ");
//Serial.println(angle);
while (currentAngle != angle) {
if (currentAngle > angle) {
currentAngle -= 1;
blindServo.write(currentAngle);
delay(15);
}
else if (currentAngle < angle) {
currentAngle += 1;
blindServo.write(currentAngle);
delay(15);
}
}
}
}
else if ( radio.DATA != null && radio.TARGETID == ADDRESS) {
struct inPACKET
*myPacket = (struct inPACKET *)radio.DATA;
angle = (myPacket->value);
if ( angle != 0 ) {
digitalWrite(servoPowerPin, HIGH);
delay(75);
currentAngle = blindServo.read();
//Serial.print("Angle: ");
//Serial.println(angle);
while (currentAngle != angle) {
if (currentAngle > angle) {
currentAngle -= 1;
blindServo.write(currentAngle);
delay(15);
}
else if (currentAngle < angle) {
currentAngle += 1;
blindServo.write(currentAngle);
delay(15);
}
}
}
}
delay(125);
Serial.flush();
from = radio.SENDERID;
burstRemaining = radio.LISTEN_BURST_REMAINING_MS;
}
// Radio goes back to standby, ready for normal operations
radio.listenModeEnd();
if (from) {
while (burstRemaining > 0) {
LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF);
burstRemaining -= 60;
}
LowPower.powerDown(SLEEP_30MS, ADC_OFF, BOD_OFF);
radio.send(from, &packetOut, sizeof(packetOut));
}
}
}
void wakeUpSwitch() {
//Serial.println("Waking up due to switch.");
}