Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Kr-Yogsa/ECE-BOT/llms.txt

Use this file to discover all available pages before exploring further.

The ESP8266 IoT sensor node reads temperature and humidity from a DHT11 sensor and computes vibration from an MPU6050 accelerometer, then publishes all three values to ECE-BOT every two seconds via an encrypted MQTT connection. This page covers hardware wiring, vibration calculation, required libraries, and the steps to flash the firmware.

Hardware required

ComponentPurpose
NodeMCU ESP8266Microcontroller with built-in Wi-Fi
DHT11Temperature and humidity sensor
MPU60503-axis accelerometer used to derive vibration
10 kΩ resistorPull-up for bare 4-pin DHT11 (module variant already includes it)
Jumper wiresConnections

Wiring connections

All three components share the ESP8266’s 3.3 V rail and a common ground.

DHT11

DHT11 pinNodeMCU pin
VCC3V3
GNDGND
DATAD4 (GPIO2)
If you have the bare 4-pin DHT11 (not the ready-made module), place a 10 kΩ pull-up resistor between DATA and 3V3. Module variants already include this resistor on the breakout board.

MPU6050

MPU6050 pinNodeMCU pin
VCC3V3
GNDGND
SDAD2 (GPIO4)
SCLD1 (GPIO5)
AD0GND
INTNot connected (not required for this version)
Keep all grounds common. The ESP8266, DHT11, and MPU6050 must all share the same GND net. Many MPU6050 breakout boards are safe at 3.3 V — do not assume 5 V unless your module’s documentation explicitly states it. If the MPU6050 does not show up on an I2C scan, recheck SDA/SCL routing and power.

How vibration is calculated

The firmware computes vibration entirely on the ESP8266 before publishing. The server receives only the final scalar value, not raw accelerometer axes. The algorithm in readVibrationRms():
  1. Collect 50 accelerometer samples from the MPU6050 with a 20 ms delay between each.
  2. Convert each raw acceleration reading to g (divide by 9.80665 m/s²).
  3. Compute the vector magnitude for each sample: √(x² + y² + z²).
  4. Calculate the mean magnitude across all 50 samples.
  5. Compute the RMS of deviations from that mean: √(Σ(magnitude − mean)² / N).
  6. Publish the resulting vibration value to MQTT.
A machine at rest produces a value near 0. Increasing mechanical activity raises the value proportionally.

MQTT payload format

Each publish sends a JSON object to the topic factory/machine/<machine_id>/telemetry:
{
  "machine_id": "cnc",
  "temperature": 29.40,
  "humidity": 61.00,
  "vibration": 0.0314,
  "timestamp": "1970-01-01T00:00:12Z"
}
The timestamp field is currently uptime-based, not real UTC. The ESP8266 has no real-time clock and starts counting from the Unix epoch on each boot. For production use, add NTP sync so the timestamp reflects actual wall-clock time.

Arduino libraries required

Install all four libraries through the Arduino IDE Library Manager (Sketch → Include Library → Manage Libraries):
LibraryPurpose
PubSubClientMQTT client for the ESP8266
DHT sensor libraryReads temperature and humidity from DHT11
Adafruit MPU6050Reads accelerometer data from MPU6050
Adafruit Unified SensorDependency for the Adafruit MPU6050 library

Setup and flash

1

Install Arduino IDE

Download and install Arduino IDE 2. Version 2 is recommended for its improved library manager and board package support.
2

Add ESP8266 board support

Open File → Preferences and paste the following URL into the Additional boards manager URLs field:
https://arduino.esp8266.com/stable/package_esp8266com_index.json
Then open Tools → Board → Boards Manager, search for esp8266, and install the ESP8266 by ESP8266 Community package.
3

Install required libraries

Open Sketch → Include Library → Manage Libraries and install:
  • PubSubClient
  • DHT sensor library (by Adafruit)
  • Adafruit MPU6050
  • Adafruit Unified Sensor
4

Configure Wi-Fi and MQTT credentials

Open esp8266_mqtt_sensor.ino and edit the credential constants near the top of the file:
// Wi-Fi
const char* WIFI_SSID     = "your-wifi-ssid";
const char* WIFI_PASSWORD = "your-wifi-password";

// MQTT broker
const char* MQTT_HOST     = "your-broker.example.com";
const int   MQTT_PORT     = 8883;
const char* MQTT_USERNAME = "your-username";
const char* MQTT_PASSWORD = "your-password";

// Machine identity
const char* MQTT_TOPIC  = "factory/machine/cnc/telemetry";
const char* MACHINE_ID  = "cnc";
MACHINE_ID must match the machine ID configured in ECE-BOT. MQTT_TOPIC must follow the pattern factory/machine/<machine_id>/telemetry.
5

Select the board and port

Connect the NodeMCU to your computer via USB. In Arduino IDE, go to Tools → Board and select NodeMCU 1.0 (ESP-12E Module). Then go to Tools → Port and select the serial port for your device.
6

Upload the sketch

Click Upload (or press Ctrl+U). The IDE compiles the sketch and flashes it to the NodeMCU. Open the Serial Monitor at 115200 baud to verify Wi-Fi and MQTT connection messages appear.
....
Wi-Fi connected.
MQTT connected.
After connecting, the node publishes telemetry every 2 seconds.
If MQTT connect failed appears in the Serial Monitor with state -2, the broker hostname is unreachable. Confirm your Wi-Fi credentials are correct and the broker URL is reachable from the device’s network. The sketch calls wifiClientSecure.setInsecure() to skip TLS certificate verification, which is suitable for development — add certificate pinning for production deployments.

Build docs developers (and LLMs) love