Skip to content

Commit cfa9615

Browse files
Baby Bynk Monitor Thing Example Code Update
1 parent 2e5cc60 commit cfa9615

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/* *********************************************
2+
* SparkFun_BabyBlynkMonitorThing
3+
* Baby Monitor Thing Project Example Code
4+
* Blog Post: https://www.sparkfun.com/news/2185
5+
*
6+
* Utilizing
7+
* Sparkfun's ADXL345 Library
8+
* Blynk Library is licensed under MIT license
9+
* ESP8266WiFi Library
10+
* BlynkSimpleEsp8266 Library
11+
*
12+
* E.Robert @ SparkFun Electronics
13+
* Created: Sep 12, 2016
14+
* Updated: Sep 13, 2016
15+
*
16+
* Development Environment Specifics:
17+
* Arduino 1.6.11
18+
* Blynk App
19+
*
20+
* Blynk is a platform with iOS and Android apps to control
21+
* Arduino, Raspberry Pi and the likes over the Internet.
22+
* You can easily build graphic interfaces for all your
23+
* projects by simply dragging and dropping widgets.
24+
*
25+
* Downloads, docs, tutorials: http://www.blynk.cc
26+
* Blynk community: http://community.blynk.cc
27+
* Social networks: http://www.fb.com/blynkapp
28+
* http://twitter.com/blynk_app
29+
*
30+
* Hardware Specifications:
31+
* SparkFun Triple Axis Accelerometer ADXL345
32+
* SparkFun ESP8266 Thing
33+
* *********************************************/
34+
35+
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
36+
#include <ESP8266WiFi.h>
37+
#include <BlynkSimpleEsp8266.h>
38+
#include <SparkFun_ADXL345.h>
39+
#include <SimpleTimer.h>
40+
41+
/*********** COMMUNICATION SELECTION ***********/
42+
/* */
43+
//ADXL345 adxl = ADXL345(10); // USE FOR SPI COMMUNICATION, ADXL345(CS_PIN);
44+
ADXL345 adxl = ADXL345(); // USE FOR I2C COMMUNICATION
45+
46+
/****************** VARIABLES ******************/
47+
/* */
48+
int gotUpFlag = 0; // Flags first occurance
49+
int wentDownFlag = 0; // Flags first occurance
50+
int gotUp = 0; // Variable for number of times baby up
51+
unsigned long babyMovingStartTime = 0; // Will store time when baby starts moving
52+
unsigned long babySleepingStartTime = 0; // Will store time when baby starts sleeping
53+
unsigned long babySleepingEndTime = 0; // Will store time when baby wakes up
54+
long TimeLimit = 180000; // Notification in 3 minutes when awake
55+
double minutesTimeS = 0; // For minute conversion
56+
double minutesTimeA = 0; // For minute conversion
57+
58+
59+
/******************** BLYNK ********************/
60+
/* Communication with your BLYNK app */
61+
// You should get Auth Token in the Blynk App.
62+
// Go to the Project Settings (nut icon).
63+
char auth[] = "b6873bbab8fa449dbd0c4d8bfa3b38ca";
64+
#define XValue V0
65+
#define YValue V1
66+
#define ZValue V2
67+
#define VIRTUAL_LCD V3
68+
#define babyMoving V4
69+
#define awakeTime V5
70+
#define asleepTime V6
71+
#define awakeLED V7
72+
#define asleepLED V8
73+
WidgetLCD lcd(VIRTUAL_LCD);
74+
75+
// Your WiFi credentials.
76+
// Set password to "" for open networks.
77+
char ssid[] = "INSERT NETWORK HERE";
78+
char pass[] = "INSERT PASSWORD HERE";
79+
80+
SimpleTimer timer;
81+
82+
/*********** REFRESH APPLICATION NAME **********/
83+
/* Communication with your BLYNK app */
84+
void refreshTime()
85+
{
86+
long uptime = millis() / 60000L;
87+
88+
// Output the following every minute:
89+
lcd.clear(); // Clear LCD Screen on Blynk
90+
lcd.print(0, 0, " BABY BLYNK "); // Outputs Application Name
91+
lcd.print(0, 1, " MONITOR THING ");
92+
}
93+
94+
void setup()
95+
{
96+
Serial.begin(9600);
97+
Blynk.begin(auth, ssid, pass); // Give us access!
98+
99+
while (Blynk.connect() == false) { // Be patient.
100+
// Wait for Blynk to come online
101+
}
102+
103+
// Notify immediately on startup
104+
Blynk.notify("Device Started"); // Notification to smartphone
105+
106+
// Setup a function to be called every minute
107+
timer.setInterval(60000L, refreshTime);
108+
109+
adxl.powerOn(); // Power on the ADXL345
110+
111+
adxl.setRangeSetting(8); // Give the range settings
112+
// Accepted values are 2g, 4g, 8g or 16g
113+
// Higher Values = Wider Measurement Range
114+
// Lower Values = Greater Sensitivity
115+
116+
// Set values to zero
117+
gotUpFlag = 0;
118+
wentDownFlag = 0;
119+
gotUp = 0;
120+
Blynk.virtualWrite(awakeTime, 0);
121+
Blynk.virtualWrite(asleepTime, 0);
122+
Blynk.virtualWrite(awakeLED, LOW);
123+
Blynk.virtualWrite(asleepLED, LOW);
124+
125+
// Print a splash screen:
126+
lcd.clear();
127+
lcd.print(0, 0, " BABY MONITOR ");
128+
lcd.print(0, 1, " THING ");
129+
}
130+
131+
/******************* MAIN CODE *****************/
132+
/* */
133+
void loop()
134+
{
135+
Blynk.run();
136+
timer.run();
137+
138+
// ADXL345 Accelerometer Readings
139+
int x,y,z;
140+
adxl.readAccel(&x, &y, &z); // Read the accelerometer values in variables x,y,z
141+
142+
// Write the values to Blynk:
143+
Blynk.virtualWrite(XValue, x);
144+
Blynk.virtualWrite(YValue, y);
145+
Blynk.virtualWrite(ZValue, z);
146+
147+
// Monitoring Up and Down Time
148+
if (y >= 50 && y <= 200) {
149+
if (wentDownFlag == 1){
150+
babySleepingEndTime = millis(); // Stopped sleeping time
151+
}
152+
Blynk.virtualWrite(awakeLED, 1023); // Awake LED lit
153+
Blynk.virtualWrite(asleepLED, 0); // Asleep LED out
154+
155+
if (gotUpFlag == 0) { // If first time baby has gotten up
156+
babyMovingStartTime = millis(); // Baby moving start time
157+
gotUpFlag = 1;
158+
gotUp = gotUp + 1; // Count the number of times the baby gets up
159+
// in the middle of the night
160+
} else {
161+
checkBaby();
162+
}
163+
} else if (y <= 30) {
164+
Blynk.virtualWrite(awakeLED, 0); // Awake LED out
165+
Blynk.virtualWrite(asleepLED, 1023); // Asleep LED lit
166+
167+
// Print to VIRTUAL_LCD:
168+
lcd.clear();
169+
lcd.print(0, 0, " BABY SLEEPING ");
170+
lcd.print(0, 1, " SHHH!!! ");
171+
172+
if (wentDownFlag == 0) {
173+
babySleepingStartTime = millis(); // Baby sleeping start time
174+
wentDownFlag = 1;
175+
} else {
176+
babySleepingEndTime = millis(); // Stopped sleeping time
177+
}
178+
179+
if (gotUpFlag == 1) {
180+
wentDownFlag = 0; // Reset flag if baby went back down
181+
gotUpFlag = 0;
182+
}
183+
sleepTime(); // Time asleep
184+
} else {
185+
// do nothing
186+
}
187+
188+
// Write number of times baby has gotten up to Blynk
189+
Blynk.virtualWrite(babyMoving, gotUp);
190+
}
191+
192+
/***************** BABY IS AWAKE ***************/
193+
/* Time to get them yet? */
194+
void checkBaby() {
195+
long currentTime = millis(); // Current time
196+
long upTime = currentTime - babyMovingStartTime; // Time baby awake
197+
minutesTimeA = upTime * 1.6667E-5; // Time conversion to minutes
198+
199+
// Print to VIRTUAL_LCD:
200+
lcd.clear();
201+
lcd.print(0, 0, " BABY MOVING ");
202+
lcd.print(0, 1, " AROUND ");
203+
204+
// Check to see if baby has been awake for a while
205+
if (upTime >= TimeLimit) {
206+
Blynk.notify("BABY IS AWAKE!"); // Notification to smartphone
207+
} else {
208+
// do nothing
209+
}
210+
211+
// Baby awake time
212+
Blynk.virtualWrite(awakeTime, minutesTimeA);
213+
}
214+
215+
/***************** BABY IS Asleep ***************/
216+
/* But for how long? */
217+
void sleepTime(){
218+
// Calculat down time in millis and minutes
219+
long downTime = babySleepingEndTime - babySleepingStartTime;
220+
minutesTimeS = downTime * 1.6667E-5;
221+
222+
// Baby sleeping time
223+
Blynk.virtualWrite(asleepTime, minutesTimeS);
224+
}
225+

0 commit comments

Comments
 (0)