Project tutorial
A Posture Detector Sending Bluetooth Data to a Cordova App

A Posture Detector Sending Bluetooth Data to a Cordova App

Prevent back pain by detecting bad posture and correct it immediately!

  • 4,860 views
  • 2 comments
  • 21 respects

Components and supplies

Apps and online services

About this project

Back pain affects a large amount of people around the world, impacting the health of the spine and leading to many health problems (negative impacts on the quality of life, sleep disorders, respiratory problems...)

Half of back pains are caused by improper posture and could therefore been prevented and cured by correcting the posture. The only requirement is to actually detect a bad posture.

The focus of this project is to answer this problem: build a wearable device that's capable of detecting bad posture and providing instant feedback to allow the wearer to correct his posture immediately. Real-time posture data will also be transmitted by Bluetooth on a mobile app made with Cordova.

For this prototype, I used LEDs to give an indication of the posture but the final product will be equiped with a vibrating module to give haptic feedback.

Step 1: Wiring

Assemble the components by following this circuit diagram:

Step 2: Programming the Arduino board

The program sets the reference position by reading the momentary switch when pressed by the user and does all the calulcations and values conversion from the accelerometer.

See the code below or on codebender here : https://codebender.cc/embed/sketch:910134 or on GitHub here : https://github.com/cvirginie/ArduinoPostureDetector_BLE

This project uses the iShield library made by Victor Carreño to send the posture data to the app.

Step 3: Building the mobile application

The application displays the BLE device and allows to connect to it.

Once connected, the angle is received and displayed on the app, and a line indicating the direction of the bending is drawn at the same time.

If the position begins to be considered as bad (yellow led on), the app indicates "warning"; If the position is considered bad (red led on), the app indicates "wrong". You can see that behavior in the photos section below.

There is also a textbox on the app to communicate with the device in bluetooth (useless for this project), you can see those messages on the serial monitor.

See source code on GitHub: https://github.com/cvirginie/CordovaPostureDetector_BLE and follow the instructions.

Install the app on iOS:

Clone the repo https://github.com/cvirginie/CordovaPostureDetector_BLE

git clone https://github.com/cvirginie/CordovaPostureDetector_BLE.git

Then type the following commands :Navigate to the directory

cd CordovaPostureDetector_BLE/

Install the dependencies

npm install

Add the ios platform (not tested on Android)

cordova platform add ios

Build

cordova build ios

or

cordova build ios --buildFlag='-UseModernBuildSystem=0'

Open the project in XCode and deploy on your device.

The plugin used to communicate in BLE: https://github.com/don/cordova-plugin-ble-central/

Photos:

Code

Arduino codeC/C++
This code connects to BLE, calculate the back posture and send posture data.
https://create.arduino.cc/editor/Ginie76/b5d9142f-85dc-4dac-9036-a9c24e525a9e/preview
#include <math.h>
#include "Boards.h"
#include <SPI.h>
#include <Nordic_nRF8001.h>
#include <RBL_nRF8001.h>
#include "iShield.h"

iShield myiShield;

// momentary switch
const int buttonPin = 2;

int LED1 = 3; // Red
int LED2 = 4; // Green
int LED3 = 5; // Yellow


int buttonState = 0; 

// Position set when the momentary witch is pressed
int currentBackPos;

// Value set as the "normal position" (= flat value on my accelerometer as mine is not solder so the angle is already "forward")
int refPos = 15;

//vibration motor digital pin
//int vibePin = 9;

void setup()
{
  // Init. and start BLE library.
  ble_begin();
  Serial.begin(9600); 
  ble_set_name("BLEShield");
    
  //pinMode(buzzerPin, OUTPUT);
    
  pinMode(LED1, OUTPUT); // Red
  pinMode(LED2, OUTPUT); // Green
  pinMode(LED3, OUTPUT); // Yellow
  
  digitalWrite(LED1,LOW); // Red
  digitalWrite(LED2,LOW); // Green
  digitalWrite(LED3,LOW); // Yellow

  pinMode(2, INPUT); // SwitchButton (to set "normal" position)
}

void loop()
{
  
  //start reading the button state
  buttonState = digitalRead(buttonPin);

  // if msg is sent by bluetooth from the app, write it on the console (not used in that project)
  myiShield.readConsole();
  String message = myiShield.incomingData();
  Serial.println(message);
  
  
  // Start reading and calculating angles
  float VREF = 5.0;
  
  // To get those values, play with your accelerometer and put the min and max you find on every x,y,z position
  int xMinVal = 265; int yMinVal = 259;int zMinVal = 281;
  int xMaxVal = 398; int yMaxVal = 393;int zMaxVal = 413;

  int xRead=0, yRead=0, zRead=0;
  
  int xAng=0, yAng=0, zAng=0;
  double x=0, y=0, z=0;
  
  int ave = 10;

  // read raw values
  for(int i=0; i<ave ; i++)
  {
    xRead += analogRead(A0);
    yRead += analogRead(A1);
    zRead += analogRead(A2);
  }

  xRead = xRead/ave;
  yRead = yRead/ave;
  zRead = zRead/ave;

  // convert to angles
  xAng = map(xRead, xMinVal, xMaxVal, -90, 90);
  yAng = map(yRead, yMinVal, yMaxVal, -90, 90);
  zAng = map(zRead, zMinVal, zMaxVal, -90, 90);
  
  x = 57.2957795 * (atan2(-yAng, -zAng) + 3.14159265359);
  y = 57.2957795 * (atan2(-xAng, -zAng) + 3.14159265359);
  z = 57.2957795 * (atan2(-yAng, -xAng) + 3.14159265359);


  char xTempString[10];  //  Hold The Convert Data
  
  // convert position in string to display it
  dtostrf(x,3,2,xTempString); // dtostrf( [doubleVar] , [sizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
  String xString = String(xTempString);  // cast it to string from char 
    
  // wrtie value that will be read on the mobile device
  myiShield.writeConsole(xString); 
 
  currentBackPos = x;

  //if the button is pressed set the value of base position to the current back position 
  // this sets a "normal" state that the code will then measure a difference from. 
  if (buttonState == HIGH) {  
    
    myiShield.writeConsole("button pressed"); 
    refPos = currentBackPos; 
    
    
    char bkpTempString[10];  //  Hold The Convert Data
    dtostrf(currentBackPos,3,2,bkpTempString); // dtostrf( [doubleVar] , [sizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
    String bkpString = String(bkpTempString);  // cast it to string from char 
    myiShield.writeConsole("currentBackPos :");
    myiShield.writeConsole(bkpString); 
    
    char bpTempString[10];  //  Hold The Convert Data
    dtostrf(refPos,3,2,bpTempString); // dtostrf( [doubleVar] , [sizeBeforePoint] , [sizeAfterPoint] , [WhereToStoreIt] )
    String bpString = String(bpTempString);  // cast it to string from char 
    myiShield.writeConsole("refPos :");
    myiShield.writeConsole(bpString); 
  } 
    
  //if backPos is in a bad postion
  if(currentBackPos > refPos + 20){
    myiShield.writeConsole("wrong");
  
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
  }
  else {
    if(currentBackPos > refPos + 10){
      myiShield.writeConsole("warning");
    
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, HIGH);
    }
    else{
      digitalWrite(LED2, HIGH);
      digitalWrite(LED1, LOW);
      digitalWrite(LED3, LOW);
  
    }
  }

  delay(2000);
  
}
Mobile Application code
SImple Cordova application to receive and display data sent by the posture detector

Schematics

Arduino schema
Connect the accelerometer on the 3.3V for the power
Posturedetector withoutbuzz sketch bb dbr8mq2w1h

Comments

Similar projects you might like

PIR Automated Intelligent Zebra Crossing

Project showcase by Adersh B

  • 2,421 views
  • 1 comment
  • 6 respects

Sound Sensor Activated LEDs with LCD for Sound Level Data!

Project tutorial by Heathen_Hacks-v2

  • 1,413 views
  • 2 comments
  • 8 respects

The TrafficLight

Project showcase by Advaith Jagathesan

  • 12,829 views
  • 1 comment
  • 20 respects

Control Arduino Uno Using ESP8266 WiFi Module and Blynk App

Project tutorial by Adithya TG

  • 6,094 views
  • 10 comments
  • 25 respects

LED Display with Arduino ADC and PWM

Project tutorial by Ian Etheridge

  • 1,042 views
  • 0 comments
  • 8 respects

Bluetooth control led with lcd led status display real time.

Project tutorial by Youssef Sabaa

  • 18,442 views
  • 4 comments
  • 37 respects
Add projectSign up / Login