Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

Bridge Health Monitoring Using Flex Sensor

    ✅ ESP32 connects to your home router
    ✅ Mobile connects to same WiFi
    ✅ Serial WiFi Terminal app connects using ESP32 IP
    ✅ More professional setup (Good for final year project)

🌐 Bridge Health Monitoring – Home WiFi Mode (TCP Server)


💻 Complete ESP32 Code (Home WiFi + Serial WiFi Terminal)

// Bridge Health Monitoring System
// ESP32 + Flex Sensor + Home WiFi + TCP Server

#include <WiFi.h>

// 🔹 Enter your Home WiFi credentials
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

WiFiServer server(8080); // TCP Server on port 8080
WiFiClient client;

const int flexPin = 34;
int flexValue = 0;
int threshold = 2500;

void setup() {

Serial.begin(115200);
pinMode(flexPin, INPUT);

Serial.println("Connecting to Home WiFi...");

// 🔹 Connect to Home WiFi (Station Mode)
WiFi.begin(ssid, password);

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

Serial.println("\nConnected to WiFi");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());

server.begin();
Serial.println("TCP Server Started on Port 8080");
}

void loop() {

client = server.available(); // Check if mobile connected

if (client) {

Serial.println("Client Connected");

while (client.connected()) {

flexValue = analogRead(flexPin);

client.print("Flex Sensor Value: ");
client.println(flexValue);

if (flexValue > threshold) {
client.println("⚠️ BEND DETECTED - Bridge Under Stress!");
} else {
client.println("✅ No Bend - Bridge is Safe.");
}

client.println("-----------------------------");

delay(1000);
}

client.stop();
Serial.println("Client Disconnected");
}
}

🧠 FULL CODE EXPLANATION (Home WiFi Version)


1️⃣ Include WiFi Library

 
 
#include <WiFi.h>
 

This allows ESP32 to:

  • Connect to router

  • Create TCP server

  • Handle network communication


2️⃣ WiFi Credentials

 
 
const char* ssid = “YOUR_WIFI_NAME”;
const char* password = “YOUR_WIFI_PASSWORD”;
 

Replace with your:

WiFi name
WiFi password

Example:

 
 
const char* ssid = “ShivHomeWiFi”;
const char* password = “12345678”;
 

3️⃣ Create TCP Server

 
 
WiFiServer server(8080);
 

Creates server on Port 8080.

Mobile app will connect using:

IP address + Port 8080


4️⃣ setup() Function

Runs once when ESP32 starts.


🔹 Start Serial Communication

 
 
Serial.begin(115200);
 

Used to display connection status.


🔹 Connect to Home WiFi

 
 
WiFi.begin(ssid, password);
 

ESP32 connects to your router.


🔹 Wait Until Connected

 
 
while (WiFi.status() != WL_CONNECTED)
 

ESP32 waits until connection successful.


🔹 Print IP Address

 
 
Serial.println(WiFi.localIP());
 

Example output:

192.168.1.105

⚠️ IMPORTANT
This IP will be different for every network.


🔹 Start TCP Server

 
 
server.begin();
 

ESP32 now waits for mobile app connection.


5️⃣ loop() Function

Runs continuously.


🔹 Check Client Connection

 
 
client = server.available();
 

If mobile app connects → client becomes active.


🔹 Read Flex Sensor

 
 
flexValue = analogRead(flexPin);
 

ESP32 ADC range:

0 – 4095 (12-bit resolution)


🔹 Send Data to WiFi Terminal App

 
 
client.print(“Flex Sensor Value: “);
client.println(flexValue);
 

Data appears in mobile app.


🔹 Threshold Comparison

 
 
if (flexValue > threshold)
 

If value > 2500:

⚠️ Bridge Under Stress

Else:

✅ Bridge Safe


🔹 Delay

 
 
delay(1000);
 

Updates every 1 second.


📱 How To Connect in Serial WiFi Terminal (Home WiFi)


Step 1

Upload code.

Open Serial Monitor.

You will see:

Connected to WiFi
ESP32 IP Address: 192.168.1.105
TCP Server Started on Port 8080


Step 2

Connect your mobile to SAME WiFi network.


Step 3

Open Serial WiFi Terminal app.

Tap ➕ Add Connection.

Choose:

Raw Socket / TCP

Enter:

Host/IP → 192.168.1.105 (your ESP32 IP)
Port → 8080

Press CONNECT.


📊 Example Output in App

When Safe:

Flex Sensor Value: 1800
✅ No Bend – Bridge is Safe.

When Bent:

Flex Sensor Value: 3100
⚠️ BEND DETECTED – Bridge Under Stress!


🔎 Complete System Logic (For Viva)

  1. Flex sensor detects bending.

  2. Resistance changes.

  3. Voltage divider converts resistance to voltage.

  4. ESP32 ADC converts voltage to digital value.

  5. ESP32 connects to home WiFi (Station Mode).

  6. ESP32 creates TCP server on port 8080.

  7. Mobile connects using ESP32 IP address.

  8. ESP32 sends real-time sensor data.

  9. If value > threshold → Warning message.

  10. If value < threshold → Safe message.


🎓 Viva Questions (Very Important)

❓ Difference between Station Mode and SoftAP Mode?

Station Mode → ESP32 connects to router
SoftAP Mode → ESP32 creates its own WiFi


❓ What is WiFi.localIP()?

Returns IP assigned by router.


❓ Why IP changes sometimes?

Router uses DHCP (Dynamic IP assignment).

Scroll to Top