Bridge Health Monitoring Using Flex Sensor
✅ No external WiFi required
✅ Works anywhere
✅ Mobile connects directly to ESP32
✅ Perfect for viva + practical
🌐 Bridge Health Monitoring – WiFi Hotspot Mode (ESP32 as Access Point)
💻 Complete ESP32 Code (Hotspot Mode + Live Terminal Page)
// Bridge Health Monitoring System
// ESP32 + Flex Sensor + Serial WiFi Terminal (TCP)
#include <WiFi.h>
const char* ssid = "BridgeMonitor";
const char* password = "12345678";
WiFiServer server(8080); // Create 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);
// Start ESP32 in Hotspot Mode
WiFi.softAP(ssid, password);
Serial.println("Hotspot Started");
Serial.print("IP Address: ");
Serial.println(WiFi.softAPIP());
server.begin();
Serial.println("TCP Server Started on Port 8080");
}
void loop() {
client = server.available(); // Check if client 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 (Line by Line)
1️⃣ Include WiFi Library
This library allows ESP32 to:
-
Create WiFi hotspot
-
Act as TCP server
-
Handle WiFi communication
2️⃣ WiFi Name & Password
const char* password = “12345678”;
ESP32 will create WiFi network:
Name → BridgeMonitor
Password → 12345678
3️⃣ Create TCP Server
This creates a TCP server on Port 8080.
Port 8080 = Communication channel.
App will connect using:
IP + Port
4️⃣ WiFi Client Object
Represents mobile app connection.
5️⃣ Pin & Variables
int flexValue = 0;
int threshold = 2500;
-
GPIO 34 → Analog input
-
flexValue → Stores ADC reading
-
threshold → Bending safety limit
🔹 setup() Function
Runs once when ESP32 starts.
6️⃣ Start Serial Monitor
Used for debugging.
7️⃣ Set Pin Mode
Makes GPIO 34 input.
8️⃣ Start Hotspot Mode
ESP32 becomes WiFi router.
9️⃣ Print IP Address
Default IP will be:
192.168.4.1
This is VERY IMPORTANT.
🔟 Start TCP Server
Now ESP32 waits for client connection.
🔹 loop() Function
Runs continuously.
1️⃣1️⃣ Check if Mobile Connected
If mobile connects via app → client becomes active.
1️⃣2️⃣ If Connected
Now ESP32 starts sending data.
1️⃣3️⃣ While Connected
Keep sending data every 1 second.
1️⃣4️⃣ Read Sensor
ADC range = 0–4095.
1️⃣5️⃣ Send Data to App
client.println(flexValue);
Data appears in Serial WiFi Terminal app.
1️⃣6️⃣ Compare Threshold
If bending detected:
⚠️ BEND DETECTED
Else:
✅ Bridge Safe
1️⃣7️⃣ Delay
Updates every 1 second.
📱 How To Connect in Serial WiFi Terminal App
Step 1:
Upload code
Open Serial Monitor.
You will see:
Hotspot Started
IP Address: 192.168.4.1
TCP Server Started on Port 8080
Step 2:
Connect Mobile WiFi to:
BridgeMonitor
Password: 12345678
Step 3:
Open Serial WiFi Terminal App
Click ➕ Add Connection
Choose:
Raw Socket / TCP
Enter:
Host/IP: 192.168.4.1
Port: 8080
Click CONNECT
📊 App Output Example
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)
-
Flex sensor changes resistance when bending.
-
Voltage divider converts resistance to voltage.
-
ESP32 ADC converts voltage to digital (0–4095).
-
ESP32 creates WiFi hotspot.
-
ESP32 creates TCP server on port 8080.
-
Mobile app connects using IP + port.
-
ESP32 sends live sensor data.
-
If value > threshold → Stress warning.
-
If value < threshold → Safe condition.
🎯 Important Viva Questions
❓ Why port 8080 used?
→ 8080 is common alternative HTTP/TCP communication port.
❓ What is TCP?
→ Transmission Control Protocol ensures reliable communication.
❓ Difference between Web Server & TCP Server?
→ Web server sends HTML pages.
→ TCP server sends raw text data.
❓ Why Hotspot mode used?
→ No external WiFi required.