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

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

 
 
#include <WiFi.h>
 

This library allows ESP32 to:

  • Create WiFi hotspot

  • Act as TCP server

  • Handle WiFi communication


2️⃣ WiFi Name & Password

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

ESP32 will create WiFi network:

Name → BridgeMonitor
Password → 12345678


3️⃣ Create TCP Server

 
 
WiFiServer server(8080);
 

This creates a TCP server on Port 8080.

Port 8080 = Communication channel.

App will connect using:
IP + Port


4️⃣ WiFi Client Object

 
 
WiFiClient client;
 

Represents mobile app connection.


5️⃣ Pin & Variables

 
 
const int flexPin = 34;
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

 
 
Serial.begin(115200);
 

Used for debugging.


7️⃣ Set Pin Mode

 
 
pinMode(flexPin, INPUT);
 

Makes GPIO 34 input.


8️⃣ Start Hotspot Mode

 
 
WiFi.softAP(ssid, password);
 

ESP32 becomes WiFi router.


9️⃣ Print IP Address

 
 
Serial.println(WiFi.softAPIP());
 

Default IP will be:

192.168.4.1

This is VERY IMPORTANT.


🔟 Start TCP Server

 
 
server.begin();
 

Now ESP32 waits for client connection.


🔹 loop() Function

Runs continuously.


1️⃣1️⃣ Check if Mobile Connected

 
 
client = server.available();
 

If mobile connects via app → client becomes active.


1️⃣2️⃣ If Connected

 
 
if (client)
 

Now ESP32 starts sending data.


1️⃣3️⃣ While Connected

 
 
while (client.connected())
 

Keep sending data every 1 second.


1️⃣4️⃣ Read Sensor

 
 
flexValue = analogRead(flexPin);
 

ADC range = 0–4095.


1️⃣5️⃣ Send Data to App

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

Data appears in Serial WiFi Terminal app.


1️⃣6️⃣ Compare Threshold

 
 
if (flexValue > threshold)
 

If bending detected:

⚠️ BEND DETECTED

Else:

✅ Bridge Safe


1️⃣7️⃣ Delay

 
 
delay(1000);
 

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)

  1. Flex sensor changes resistance when bending.

  2. Voltage divider converts resistance to voltage.

  3. ESP32 ADC converts voltage to digital (0–4095).

  4. ESP32 creates WiFi hotspot.

  5. ESP32 creates TCP server on port 8080.

  6. Mobile app connects using IP + port.

  7. ESP32 sends live sensor data.

  8. If value > threshold → Stress warning.

  9. 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.

 

Scroll to Top