Real-Time Distance Monitoring With ESP8266 and KiwisIoT

by prakatheeshwaranR in Circuits > Arduino

37 Views, 0 Favorites, 0 Comments

Real-Time Distance Monitoring With ESP8266 and KiwisIoT

WhatsApp Image 2026-09-09 at 2.51.02 PM.jpeg

In this project, I built a simple IoT-based distance monitoring system using a NodeMCU ESP8266, HC-SR04 ultrasonic sensor, and KiwisIoT platform. The main idea was to measure the distance of an object and make that information available online in real time.

The HC-SR04 ultrasonic sensor detects the distance between the sensor and the object. The ESP8266 reads the sensor data, calculates the distance in centimeters, and sends the result to the KiwisIoT cloud platform through Wi-Fi. Once the data is uploaded, it can be viewed and monitored easily from the KiwisIoT dashboard.

This project is a simple way to understand how sensors, ESP8266, Wi-Fi, and IoT cloud platforms can work together to create a real-time monitoring system.

Supplies

WhatsApp Image 2026-09-09 at 2.51.02 PM.jpg

NodeMCU ESP8266 – 1

HC-SR04 Ultrasonic Sensor – 1

Jumper Wires – Female to-Female

USB Cable – 1

Computer/Laptop – for programming the ESP8266

Wi-Fi Connection – for sending data to KiwisIoT

KiwisIoT Account – for viewing the distance data online

Arduino IDE – for programming the NodeMCU

How the Project Works

The HC-SR04 ultrasonic sensor sends out a short ultrasonic pulse and waits for it to bounce back from an object. The NodeMCU measures how long the echo takes to return and uses that time to calculate the distance.

Once the distance is calculated, the ESP8266 sends the reading to the KiwisIoT platform over Wi-Fi. This makes it possible to monitor the measured distance remotely from the KiwisIoT dashboard.

For example, if an object is placed about 30 cm away, the sensor will measure the distance and the KiwisIoT dashboard should display a value close to 30 cm.

Circuit Connections

WhatsApp Image 2026-09-09 at 3.25.37 PM.jpeg

The HC-SR04 is connected to the NodeMCU as follows:

  1. VCC → VIN
  2. GND → GN
  3. TRIG → D5
  4. ECHO → D6

Since the HC-SR04 ECHO pin can output 5V and the ESP8266 GPIO works at 3.3V, I use a voltage divider on the ECHO connection to protect the NodeMCU.

Programming the ESP8266

Screenshot 2026-09-09 124913_832x551.png
#include <KiwisIoT.h>

// WIFI
const char* ssid = "Wifi name "; // Replace with your WiFi network name
const char* pass = "Wifi password"; // Replace with your WiFi network password
const char* topic = "dash_1788941450719"; // Replace with your dashboard topic ID

KiwisIoT kiwisiot(ssid, pass, topic);
#define echoPin D6
#define trigPin D5

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT);
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with NODEMCU");
kiwisiot.begin();
}
void loop() {
kiwisiot.run();
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
kiwisiot.send("1",distance);
}


Testing the Project

Screenshot 2026-09-09 124924_832x551.png

The process starts with the HC-SR04 ultrasonic sensor, which sends out a short ultrasonic pulse toward the object. When the pulse hits the object, it reflects back to the sensor.

The NodeMCU measures the time taken for the echo to return and uses this information to calculate the distance of the object in centimeters.

Once the distance is calculated, the NodeMCU connects to the internet through Wi-Fi using the ESP8266. The measured distance is then sent to the KiwisIoT platform, where it can be viewed on the dashboard in real time.

This allows us to easily monitor the distance between the sensor and the object from the KiwisIoT dashboard.

Future Improvements

In the future, this project can be made more useful by adding features such as:

  1. Distance alerts when an object comes too close to the sensor.
  2. Real-time graphs to track how the distance changes over time.
  3. Mobile notifications to receive alerts directly on a phone.
  4. Multiple ultrasonic sensors to monitor different areas at the same time.
  5. Automatic object detection and monitoring for more advanced applications.
  6. Data storage and analysis to study distance measurements over a longer period.


Conclusion

This project successfully demonstrates how an HC-SR04 ultrasonic sensor can be used to measure the distance of an object. The NodeMCU reads the sensor data, calculates the distance, and sends the measured value to the KiwisIoT cloud platform through Wi-Fi. The distance can then be viewed in real time on the KiwisIoT dashboard.

By working on this project, I gained a better understanding of how ultrasonic sensors detect objects, how the ESP8266 processes sensor readings, and how data can be transmitted to an IoT cloud platform. Overall, this project gave me practical experience in connecting sensors, microcontrollers, Wi-Fi, and cloud-based IoT services