Elias Tinajero
Created May 13, 2020

Check Sleep Position Device

Automatically detect body position when individual is sleeping or detect noise (snoring) and turn on vibration in order to accommodate body

IntermediateFull instructions provided4 hours12
Check Sleep Position Device

Things used in this project

Hardware components

WT8266-S1 (ESP8266) Wifi Module
Wireless-Tag WT8266-S1 (ESP8266) Wifi Module
×1
Inertial Measurement Unit (IMU) (6 deg of freedom)
Inertial Measurement Unit (IMU) (6 deg of freedom)
×1
Mini motor
×1
Microphone Ky-037
×1
Transistor BJT BC547C NPN
×1
Resistor 34 ohms
×1
Resistor 12 K ohms
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics

Setting Hardware

Circuit diagrams

Adding Motor motion circuits

Code

Check Sleep Position

Arduino
Program to check body position when slepping
// Librery for MPU
#include "MPU9250.h"

// Libreries for ESP to enable WiFi
#include <ESP8266WiFi.h>

// Generating MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
// MPU9250 is wired to ESP8266 MOD as follow: 
// MPU SCL Pin to ESP8266 D1 Pin
// MPU SDA Pin to ESP8266 D2 Pin
MPU9250 IMU(Wire,0x68);
// Status flag for MPU (available or not)
int status;

// percentage audible when divice ON
float percentage = 20;

//inclination degrees when dive ON
int incdegrees = 70;


// chnging rads to degrees operation
float deg;



// Generating WiFi zone to handle WEB Server from any Browser
// WiFi Name
const char* ssid  = "INFINITUM9203_2.4";
// WiFi Access
const char* password = "4584870";

// Creating Web Server object on port number 80
WiFiServer server(80);



// Procedure for Motor/Vibrator turning pin On/OFF into ESP8266

void vibrationONOFF() {
// sets the digital pin 2 ON 
  digitalWrite(2, HIGH); 
// waits for a 3 seconds
  delay(3000);          
// sets the digital pin 2 OFF
  digitalWrite(2, LOW);  

}

// Devices Setup 
  
void setup() {
// enable local serial to display data
  Serial.begin(115200);

// set pin modes for ESP8266 device
// selecting pin number 2 for output mode, turn ON/OFF motor
  pinMode(2, OUTPUT);
// selecting pin A0 (analog input) for microphone sensor
  pinMode(A0, INPUT);

// start communication with MPU Sensor 
// checking wire status with ESP8266 
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }

// manufacturer recommendetions settings
// setting the gyroscope full scale range to +/-500 deg/s
  IMU.setGyroRange(MPU9250::GYRO_RANGE_500DPS);
// setting DLPF bandwidth to 20 Hz
  IMU.setDlpfBandwidth(MPU9250::DLPF_BANDWIDTH_20HZ);
// setting SRD to 19 for a 50 Hz update rate
  IMU.setSrd(19);  

// Setting WiFi
// Print ESP8266 Local IP Address (default settings for this proposal IP address is 192.168.1.91)

Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() !=WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}
Serial.println(" ");
Serial.println("WiFi connected");

//Start web server
server.begin();
Serial.println("Server started");
Serial.print("http://");
Serial.println(WiFi.localIP());
Serial.println("/");
}



void loop() {

  // reading MPU sensor
  IMU.readSensor();

  // Changing rads to grades
  deg = abs((IMU.getGyroX_rads()) * 180);
  // changing celciuos to fahrenheit
  float cel = float(IMU.getTemperature_C());
  float far = (cel*9/5 + 32);
  
  //if inclination rise more than 70 degrees turn motor ON
 if (deg > incdegrees) {
    vibrationONOFF();
    delay(5000);
  }
  // Read Analog ESP8266 pin and check if percentage is more than 15%  
  float mp = analogRead(A0);
  delay(500);
  float temmp = analogRead(A0);
 
  float permp = mp * (percentage)*0.01;
  
  if ((mp + permp) < temmp) {
    vibrationONOFF();
    mp = 0.0;
    temmp = 0.0;
  }

//Setting Wifi Client

WiFiClient client = server.available();
if (!client) {
  return;
}

Serial.println("New client");
while (!client.available()){
  delay(1);
}

String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();



//percentages audio settings
if (request.indexOf("/AUDIO=30") != -1) {
  percentage = 30;
  
}
if (request.indexOf("/AUDIO=20") != -1) {
  percentage = 20;
  
}
if (request.indexOf("/AUDIO=10") != -1) {
  percentage = 10;
  
}

//inclination degrees settings
if (request.indexOf("/INCLINATION=90") != -1) {
  incdegrees = 90;
  
}
if (request.indexOf("/INCLINATION=80") != -1) {
  incdegrees = 80;
  
}
if (request.indexOf("/INCLINATION=70") != -1) {
  incdegrees = 70;
  
}

// return response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");


client.print("Set Alarm Audio Peack : ");
client.println("<br>");
client.println("<a href=\"/AUDIO=30\"\"><button>30 %</button></a>");
client.println("<a href=\"/AUDIO=20\"\"><button>20 % </button></a>");
client.println("<a href=\"/AUDIO=10\"\"><button>10 % </button></a><br/>");
//client.println("<br>");

if (percentage == 10) {
  client.print("Peack of 10 % ");
  client.println("<br>");
}
if (percentage == 20) {
  client.print("Peack 20 %");
  client.println("<br>");
} 
if (percentage == 30) {
  client.print("Peack 30 %");
  client.println("<br>");
}
client.println("<br>");
client.print("Set Alarm Inclination Degrees : ");
client.println("<br>");
client.println("<a href=\"/INCLINATION=90\"\"><button>90 deg</button></a>");
client.println("<a href=\"/INCLINATION=80\"\"><button>80 deg</button></a>");
client.println("<a href=\"/INCLINATION=70\"\"><button>70 deg</button></a><br/>");

if (incdegrees == 90) {
  client.print("90 inclination degrees");
  client.println("<br>");
}
if (incdegrees == 80) {
  client.print("80 inclination degrees");
  client.println("<br>");
} 
if (incdegrees == 70) {
  client.print("70 inclination degrees");
  client.println("<br>");
}

client.println("<br>");
client.println("Temperatures: ");
client.println("<br>");
client.print("Fahrenheit: ");
client.println(far);
client.println("<br>");
client.print("Celsius: ");
client.println(cel);
client.println("<br>");

client.println("</html>");

delay(1);
Serial.println("Client disconnected");
Serial.println("");

}

Credits

Elias Tinajero

Elias Tinajero

1 project • 0 followers

Comments