Skip to content

Commit 3437d9e

Browse files
authored
Merge pull request #25 from Capstone-Projects-2023-Spring/power-saving
Optimized power consumption, implemented Bluetooth test cases
2 parents c2667c0 + cfdf962 commit 3437d9e

File tree

2 files changed

+197
-30
lines changed

2 files changed

+197
-30
lines changed

arduino/arduino.ino

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ float threshold = 6.0;
2222

2323
String data = " ";
2424
int flag = 0;
25-
int sleep = 0;
2625
int start = 0;
2726
const unsigned long interval = 60000;
2827
unsigned long previousMillis = 0;
@@ -41,6 +40,9 @@ Adafruit_BLEGatt gatt(Bluetooth);
4140
void setup() {
4241
// put your setup code here, to run once:
4342
Serial.begin(115200);
43+
44+
/* Put the device into idle mode */
45+
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
4446

4547
/* Initialise the module */
4648
Serial.print(F("Initialising the Bluefruit LE module: "));
@@ -73,7 +75,18 @@ void setup() {
7375
/* Wait for connection */
7476
Serial.println("Looking for Bluetooth Device...");
7577
while (!Bluetooth.isConnected()){
76-
delay(500);
78+
unsigned long currentMillis = millis();
79+
// Power down if 1 minute of no connection passes
80+
if((unsigned long)(currentMillis - previousMillis) >= interval){
81+
Serial.println("Powering Down!");
82+
// Make the Bluetooth undiscoverable
83+
Bluetooth.sendCommandCheckOK("AT+GAPDEVNAME=Undiscoverable");
84+
// Turn off the Mode LED
85+
Bluetooth.sendCommandCheckOK("AT+HWModeLED=0");
86+
delay(500);
87+
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
88+
}
89+
delay(500);
7790
}
7891

7992
// Change Mode LED Activity
@@ -91,6 +104,9 @@ void setup() {
91104

92105
Bluetooth.begin();
93106

107+
// Initialize the characteristic to '0'
108+
gatt.setChar(1, 'B', BUFSIZE);
109+
94110
//accelerometer
95111
pinMode(9,INPUT); // Interrupt pin input
96112
pinMode(10, OUTPUT); // CS for SPI
@@ -153,19 +169,24 @@ void loop() {
153169
while(Bluetooth.isConnected()){
154170
unsigned long currentMillis = millis();
155171

172+
//put device into idle mode
173+
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
174+
156175
//reset sleep timer for first connection
157176
if(start == 1){
158177
previousMillis = currentMillis;
159178
start--;
179+
delay(500);
160180
}
161181

162182
//reset sleep timer for reconnection
163183
if(flag == 1){
164184
Serial.println(F("******************************"));
165185
Serial.println(F("Bluetooth Device Connected!"));
166186
Serial.println(F("******************************"));
167-
flag--;
168187
previousMillis = currentMillis;
188+
flag--;
189+
delay(500);
169190
}
170191

171192
float maxG;
@@ -176,54 +197,36 @@ void loop() {
176197
}
177198

178199
//checks if threshold might have been exceeded, then verifies
179-
gatt.setChar(1, 'B', BUFSIZE);
180200
if (digitalRead(9) == HIGH && (maxG = getMaxG()) > threshold) {
181-
if(sleep == 1){
182-
Serial.println("Woke Up!");
183-
sleep--;
184-
}
185201
Serial.println("Interrupt: " + String(maxG) + "g");
186202
Serial.println("Changing Characteristic!");
187-
//update characteristic value
203+
//change characteristic value to 'F'
188204
gatt.setChar(1, 'A', BUFSIZE);
189205
delay(1000);
206+
Serial.println("Resetting Characteristic!");
207+
//reset characteristic value back to '0'
208+
gatt.setChar(1, 'B', BUFSIZE);
209+
delay(1000);
210+
//reset the sleep timer
190211
previousMillis = currentMillis;
191212
}
192213

193-
//sleep if a minute of inactivity passes
194-
if((unsigned long)(currentMillis - previousMillis) >= interval && (unsigned long)(currentMillis - previousMillis) < interval * 5){
195-
if(sleep == 0){
196-
Serial.println("Going to Sleep!");
197-
sleep++;
198-
delay(500);
199-
LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF);
200-
}
201-
}
202-
203-
//power down if 5 minutes of inactivity passes
214+
//disconnect from bluetooth if 5 minutes of inactivity passes
204215
if((unsigned long)(currentMillis - previousMillis) >= interval * 5){
205-
Serial.println("Powering Down!");
216+
previousMillis = currentMillis;
217+
Bluetooth.disconnect();
206218
delay(500);
207-
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
208219
}
209220

210221
//check for incoming data from bluetooth device
211222
if(Bluetooth.available() > 0){
212-
if(sleep == 1){
213-
Serial.println("Woke Up!");
214-
sleep--;
215-
}
216223
data = Bluetooth.readString();
217224
Serial.print("Bluetooth: " + data);
218225
previousMillis = currentMillis;
219226
}
220227

221228
//check for incoming data from hardware
222229
if(Serial.available() > 0){
223-
if(sleep == 1){
224-
Serial.println("Woke Up!");
225-
sleep--;
226-
}
227230
data = Serial.readString();
228231
Bluetooth.print("Serial: " + data);
229232
Serial.println("Message sent to bluetooth.");
@@ -233,13 +236,29 @@ void loop() {
233236

234237
//check if hardware disconnects from bluetooth device
235238
while (!Bluetooth.isConnected()){
239+
unsigned long currentMillis = millis();
240+
241+
//put device into idle mode
242+
LowPower.idle(SLEEP_FOREVER, ADC_ON, TIMER2_OFF, TIMER1_OFF, TIMER0_ON, SPI_ON, USART0_OFF, TWI_OFF);
243+
236244
if(flag == 0){
237245
Serial.println(F("******************************"));
238246
Serial.println("Bluetooth Device Disconnected!");
239247
Serial.println(F("******************************"));
240248
flag++;
241249
}
242250
delay(500);
251+
252+
//power down if 3 minutes of no connection passes
253+
if((unsigned long)(currentMillis - previousMillis) >= interval * 3){
254+
Serial.println("Powering Down!");
255+
// Make the Bluetooth undiscoverable
256+
Bluetooth.sendCommandCheckOK("AT+GAPDEVNAME=Undiscoverable");
257+
// Turn off the Mode LED
258+
Bluetooth.sendCommandCheckOK("AT+HWModeLED=0");
259+
delay(500);
260+
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
261+
}
243262
}
244263
}
245264

bluetooth/bluetoothTest.ino

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include <LowPower.h>
2+
#include <Adafruit_ATParser.h>
3+
#include <Adafruit_BLE.h>
4+
#include <Adafruit_BLEBattery.h>
5+
#include <Adafruit_BLEEddystone.h>
6+
#include <Adafruit_BLEGatt.h>
7+
#include <Adafruit_BLEMIDI.h>
8+
#include <Adafruit_BluefruitLE_SPI.h>
9+
#include <Adafruit_BluefruitLE_UART.h>
10+
#include <Arduino.h>
11+
#include <SPI.h>
12+
#include "BluefruitConfig.h"
13+
14+
#define FACTORYRESET_ENABLE 1
15+
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
16+
#define MODE_LED_BEHAVIOUR "MODE"
17+
18+
String data = " ";
19+
int flag = 0;
20+
21+
Adafruit_BluefruitLE_SPI Bluetooth(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
22+
23+
/**
24+
* @brief Initialize input, output pins and values
25+
*
26+
* @param void takes nothing
27+
*
28+
* @return void returns nothing
29+
*/
30+
31+
void setup() {
32+
// put your setup code here, to run once:
33+
Serial.begin(115200);
34+
35+
/* Initialise the module */
36+
Serial.print(F("Initialising the Bluefruit LE module test: "));
37+
38+
TestBegin();
39+
40+
TestFactoryReset();
41+
42+
/* Disable command echo from Bluefruit */
43+
Bluetooth.echo(false);
44+
45+
Bluetooth.verbose(false);
46+
47+
/* Test for connection */
48+
Serial.println("Looking for Bluetooth Device...");
49+
TestConnected();
50+
51+
// Change Mode LED Activity
52+
if (Bluetooth.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION)){
53+
Bluetooth.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
54+
}
55+
56+
// Test if Bluefruit sets to DATA mode
57+
TestSetMode();
58+
}
59+
60+
/**
61+
* @brief Waits for a bluetooth device to connect, continuously gets accelerometer
62+
* readings, sends signal to mobile phone after exceeding a certain threshold
63+
*
64+
* @param void takes nothing
65+
*
66+
* @return void returns nothing
67+
*/
68+
69+
void loop() {
70+
// put your main code here, to run repeatedly:
71+
72+
//check if bluetooth device is connected to hardware
73+
while(Bluetooth.isConnected()){
74+
//test for incoming data from hardware
75+
TestAvailable();
76+
}
77+
78+
//test if hardware disconnects from bluetooth device
79+
TestDisconnected();
80+
}
81+
82+
//tests for incoming data from hardware
83+
void TestAvailable(){
84+
if(Serial.available() > 0){
85+
data = Serial.readString();
86+
Bluetooth.print("Bluetooth: " + data);
87+
Serial.println("Message sent to Bluetooth Device!");
88+
Serial.println("Available Test Passed!");
89+
}
90+
}
91+
92+
//tests if hardware can setup bluetooth connections
93+
void TestBegin(){
94+
if(!Bluetooth.begin(VERBOSE_MODE)){
95+
error(F("Couldn't find Bluefruit, make sure it's in Command mode & check wiring?"));
96+
}
97+
Serial.println("Begin Test Passed!");
98+
}
99+
100+
//test if hardware can connect to bluetooth devices
101+
void TestConnected(){
102+
while (!Bluetooth.isConnected()){
103+
delay(500);
104+
}
105+
if(Bluetooth.isConnected()){
106+
Serial.println("Device Connected!");
107+
Serial.println("Connected Test Passed!");
108+
}
109+
}
110+
111+
//test if hardware can disconnect from bluetooth devices
112+
void TestDisconnected(){
113+
while (!Bluetooth.isConnected()){
114+
if(flag == 0){
115+
Serial.println("Bluetooth Device Disconnected!");
116+
Serial.println("Disconnected Test Passed!");
117+
flag++;
118+
}
119+
delay(500);
120+
}
121+
}
122+
123+
//test if hardware can perform a factory reset
124+
void TestFactoryReset(){
125+
if(FACTORYRESET_ENABLE){
126+
/* Perform a factory reset to make sure everything is in a known state */
127+
Serial.println(F("Performing a factory reset: "));
128+
if (!Bluetooth.factoryReset()){
129+
error(F("Couldn't factory reset"));
130+
}
131+
delay(500);
132+
Serial.println("Factory Reset Test Passed!");
133+
}
134+
}
135+
136+
//test if hardware can change modes
137+
void TestSetMode(){
138+
if(Bluetooth.setMode(BLUEFRUIT_MODE_DATA)){
139+
Serial.println("Mode Changed!");
140+
Serial.println("Set Mode Test Passed!");
141+
}
142+
}
143+
144+
//error handler method
145+
void error(const __FlashStringHelper*err){
146+
Serial.println(err);
147+
while (1);
148+
}

0 commit comments

Comments
 (0)