Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

ESP32-Based Posture Monitoring System with Wi-Fi and Vibration Feedback

View and Edit Project In Cirkit DesignerImage of ESP32-Based Posture Monitoring System with Wi-Fi and Vibration Feedback

Circuit Documentation

Summary

This circuit is designed to monitor the flexion angle using flex sensors and control a vibration motor based on the detected angle. The circuit also sends the flexion data to a server over Wi-Fi. The main components include an ESP32 microcontroller, resistors, flex sensors, and a PWM vibration motor sensor module switch.

Component List

Resistor

Basic Flex Resistor

PWM Vibration Motor Sensor Module Switch

ESP32

Wiring Details

Resistor

Basic Flex Resistor

PWM Vibration Motor Sensor Module Switch

ESP32

Code Documentation

Code for ESP32

#include <WiFi.h>
#include <HTTPClient.h>

// Wi-Fi credentials
const char* ssid = "kishi";
const char* password = "bobSEM31";

// Sensor pin
const int flexPin = 34; // Flex sensor connected to analog pin 34
const int motorPin = 23; // Vibration motor module connected to digital pin 23

// Server URL to send data
const String serverURL = "http://192.168.91.139:3000/data"; // Replace with your server URL

// Constants
const int idleFlexValue = 1740;  // Idle value representing 0° flexion
const int maxFlexValue = 4095;  // Maximum raw value (ESP32 ADC: 12-bit)
const int maxDegrees = 180;     // Maximum angle in degrees
const int angleThreshold = 5;   // Threshold angle for triggering vibration

void setup() {
  // Start the serial monitor for debugging
  Serial.begin(115200);

  // Initialize motor pin as output
  pinMode(motorPin, OUTPUT);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);

  // Wait for the connection to establish
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Read raw value from the flex sensor
  int rawFlexValue = analogRead(flexPin);

  // Print the raw sensor value to the Serial Monitor
  Serial.print("Raw Flex Sensor Value: ");
  Serial.println(rawFlexValue);

  // Normalize the value by subtracting the idle value
  int normalizedValue = max(0, rawFlexValue - idleFlexValue); // Prevent negative values

  // Map the normalized value to degrees (0° to maxDegrees)
  int flexValue = map(normalizedValue, 0, maxFlexValue - idleFlexValue, 0, maxDegrees);

  // Print the mapped flex value to the Serial Monitor
  Serial.print("Flex Sensor Angle (°): ");
  Serial.println(flexValue);

  // Control the vibration motor based on the angle threshold
  if (flexValue >= angleThreshold && flexValue < 40) {
    // Slouching: Slow burst (1000ms)
    Serial.println("Slouching detected: Vibration Motor ON (slow burst)");
    digitalWrite(motorPin, HIGH);
    delay(1000); // Vibrate for 1000ms
    digitalWrite(motorPin, LOW);
    Serial.println("Vibration Motor OFF");
  } else if (flexValue >= 40) {
    // Very slouching: Long burst (2000ms)
    Serial.println("Very slouching detected: Vibration Motor ON (long burst)");
    digitalWrite(motorPin, HIGH);
    delay(2000); // Vibrate for 2000ms
    digitalWrite(motorPin, LOW);
    Serial.println("Vibration Motor OFF");
  }

  // Send the data to the server
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(serverURL); // Specify the server URL
    http.addHeader("Content-Type", "application/json");

    // Create a JSON payload with the angle
    String jsonPayload = "{\"flex_value\": " + String(flexValue) + "}";

    // Send the POST request
    int httpResponseCode = http.POST(jsonPayload);

    // Check the response code
    if (httpResponseCode > 0) {
      Serial.print("HTTP Response Code: ");
      Serial.println(httpResponseCode);
      String response = http.getString();
      Serial.println("Response: " + response);
    } else {
      Serial.print("Error sending POST request: ");
      Serial.println(httpResponseCode);
    }

    // Close the connection
    http.end();
  } else {
    Serial.println("Wi-Fi is not connected. Skipping POST request.");
  }

  // Wait before the next loop
  delay(2000);
}

This code initializes the ESP32, connects it to a Wi-Fi network, reads data from the flex sensor, controls the vibration motor based on the flexion angle, and sends the flexion data to a server.