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
This allows ESP32 to:
-
Connect to router
-
Create TCP server
-
Handle network communication
2️⃣ WiFi Credentials
const char* password = “YOUR_WIFI_PASSWORD”;
Replace with your:
WiFi name
WiFi password
Example:
const char* password = “12345678”;
3️⃣ Create TCP Server
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
Used to display connection status.
🔹 Connect to Home WiFi
ESP32 connects to your router.
🔹 Wait Until Connected
ESP32 waits until connection successful.
🔹 Print IP Address
Example output:
192.168.1.105
⚠️ IMPORTANT
This IP will be different for every network.
🔹 Start TCP Server
ESP32 now waits for mobile app connection.
5️⃣ loop() Function
Runs continuously.
🔹 Check Client Connection
If mobile app connects → client becomes active.
🔹 Read Flex Sensor
ESP32 ADC range:
0 – 4095 (12-bit resolution)
🔹 Send Data to WiFi Terminal App
client.println(flexValue);
Data appears in mobile app.
🔹 Threshold Comparison
If value > 2500:
⚠️ Bridge Under Stress
Else:
✅ Bridge Safe
🔹 Delay
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)
-
Flex sensor detects bending.
-
Resistance changes.
-
Voltage divider converts resistance to voltage.
-
ESP32 ADC converts voltage to digital value.
-
ESP32 connects to home WiFi (Station Mode).
-
ESP32 creates TCP server on port 8080.
-
Mobile connects using ESP32 IP address.
-
ESP32 sends real-time sensor data.
-
If value > threshold → Warning message.
-
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).