Smart Pillow Prototype from Melodie on Vimeo.
Smart Pillow Prototype from Melodie on Vimeo.
A smart pillow that can record your sleep time and wake you up at the right time.
The target audience of this IoT appliance is my husband. As we talked about the problems he had when staying at home, he mentioned he had three pain points for sleep:
Step1: Define basic logic
Step2: Get familiar with the FSR sensor
Step3: Record sleep start and end status
Detect sleep start and end status, and publish the events to the Particle Cloud.
Step4: Sleep data persistence
Since the data on Particle console is temporary, I decided to transfer sleep data from Particle to Google Cloud to make it long-term. I referred to tutorials in Particle docs (https://docs.particle.io/tutorials/integrations/google-cloud-platform/) and here are my main steps:
Step5: Use IFTTT to tell the pillow when to buzz
Step6: Play the music!
I referred to a tutorial on Arduino (https://www.arduino.cc/en/Tutorial/ToneMelody) to learn how to code music for the buzzer.
// Alarm music related code is inspired by https://www.arduino.cc/en/Tutorial/ToneMelody
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
// Notes in the melody:
int melody[] = {
NOTE_C4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4, 0
};
// Note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4,8,8,8,8,4,4,4
};
// Define a pin for FSR
// The FSR will monitor if anyone is on the pillow, so we can track the sleeping time
int fsrPin = A0;
// Create a variable to hold the FSR reading
int fsrReading = 0;
// Create a variable to hold the last FSR reading, so we can compare the current reading to it
int lastFsrReading = 0;
// Define a pin for LED
// the LED will light up when someone is on the pillow
int ledPin = D2;
// Deine a flag that denotes whether to alarm to wake up someone
// 0 means no need to alarm, and 1 means need to alarm
int needAlarm = 0;
void setup() {
pinMode(ledPin, OUTPUT);
// Create a cloud variable so that we can monitor the change in console
Particle.variable("force", &fsrReading, INT);
// Create a cloud function so that IFTTT can call it
Particle.function("alarm", alarmPlease);
}
void loop() {
//If there is an alarm and someone is on the pillow, then play music, otherwise don't
if (needAlarm == 1 && lastFsrReading > 100) {
playMusic();
} else {
needAlarm = 0;
}
// Use analogRead to read the force
// This gives us a value from 0 to 4095
fsrReading = analogRead(fsrPin);
// Set a rough 100 threshold to detect if anyone is on the pillow
if (fsrReading > 100 && lastFsrReading <= 100) {
// Sleep starts
Particle.publish("sleepStart");
// Light up LED
analogWrite(ledPin, 255);
} else if (fsrReading <= 100 && lastFsrReading > 100) {
// Sleep ends
Particle.publish("sleepEnd");
// Turn off LED
analogWrite(ledPin, 0);
}
lastFsrReading = fsrReading;
// Wait a second and then loop
delay(1000);
}
// This function is meant to be call by IFTTT
// Users can set an alarm on IFTTT, and the applet will call this function to make the pillow play music
int alarmPlease(String command) {
// Set flag to 1 to play alarm music
needAlarm = 1;
return 1;
}
void playMusic() {
// Iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// To calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// To distinguish the notes, set a minimum time between them.
// The note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// Stop the tone playing:
noTone(8);
}
}
Click to Expand
Smart Pillow Prototype from Melodie on Vimeo.
For the current prototype, I roughly count all the time a user spends on the pillow as sleep time, which might not be very accurate. I did some research on how to detect if a person is asleep or awake. A common method used by sleep monitor apps like Sleep Cycle is using the accelerometer inside the phone (the part that knows when you turn it sideways) to detect the movements as you sleep. To make the Smart Pillow smarter, I still need to do more experiments with different sensors to find out the best and most accurate way to collect information.
A hands-on introductory course exploring the Internet of Things and connected product experiences.
A smart pillow that can record your sleep time and wake you up at the right time.
January 31st, 2019