top of page
Writer's pictureRamesh G

IoT Web Server Based Object Distance Meter using Ultrasonic sensor

Updated: Jul 2, 2021

In this tutorial we build a Ultrasonic sensor to measure Object distance using NodeMCU ESP8266 12E dev Board.


Circuit diagram

Components required

NodeMCU ESP826612E

Ultrasonic Distance Sensor HC-SR04

Jumper wires


HC-SR04

This is the HC-SR04 ultrasonic distance sensor. This economical sensor provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit.

There are only four pins that you need to worry about on the HC-SR04:

VCC (Power) to NodeMCU VinPin

Trig (Trigger) to NodeMCU D1 Pin

Echo (Receive) to NodeMCU D2 Pin

GND (Ground) to NodeMCU GND Pin.

You will find this sensor very easy to set up and use for your next range-finding project!

This sensor has additional control circuitry that can prevent inconsistent "bouncy" data depending on the application.


Installing the ESP8266 Arduino Library

Download Ultrasonic library

Subscribe and Download code.

In your Arduino IDE, to install the libraries go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.

After installing the required libraries, copy the following code to your Arduino IDE.

Subscribe and Download code.

Arduino Code


#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

#include <FS.h> //Include File System Headers

#include <Ultrasonic.h>

Ultrasonic ultrasonic(D1, D2); //D1 trig, D2=echo

int distance;


const char* htmlfile = "/index.html";

//WiFi Connection configuration

const char* ssid = "TP-Link_3200"; //Your Wifi SSID

const char* password = "9500112137"; //Your wifi password

ESP8266WebServer server(80);

void handleADC(){

distance = ultrasonic.read();

String adc = String(distance);

Serial.print("Cm : ");

Serial.println(adc);

server.send(200, "text/plane",adc); //sends data to server

}

void handleRoot(){

server.sendHeader("Location", "/index.html",true); //Redirect to our html web page

server.send(302, "text/plane","");

}

void handleWebRequests(){

if(loadFromSpiffs(server.uri())) return;

String message = "File Not Detected\n\n";

message += "URI: ";

message += server.uri();

message += "\nMethod: ";

message += (server.method() == HTTP_GET)?"GET":"POST";

message += "\nArguments: ";

message += server.args();

message += "\n";

for (uint8_t i=0; i<server.args(); i++){

message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";

}

server.send(404, "text/plain", message);

Serial.println(message);

}

void setup() {

delay(1000);

Serial.begin(115200);

Serial.println();

//Initialize File System

SPIFFS.begin();

Serial.println("File System Initialized");

Serial.print("Setting AP (Access Point)…");

Serial.println();

WiFi.begin(ssid, password); // Connect to the network

Serial.print("Connecting to ");

Serial.print(ssid); Serial.println(" ...");


// Wait for connection

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}


//If connection successful show IP address in serial monitor

Serial.println("");

Serial.print("Connected to ");

Serial.println(ssid);

Serial.print("IP address: ");

Serial.println(WiFi.localIP()); //IP address assigned to your ESP


//Initialize Webserver

server.on("/",handleRoot);

server.on("/getADC",handleADC); //Reads ADC function is called from out index.html

server.onNotFound(handleWebRequests); //Set setver all paths are not found so we can handle as per URI

server.begin();

Serial.println("HTTP server started");

}

void loop() {

server.handleClient();

}

bool loadFromSpiffs(String path){

String dataType = "text/plain";

if(path.endsWith("/")) path += "index.htm";

if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));

else if(path.endsWith(".html")) dataType = "text/html";

else if(path.endsWith(".htm")) dataType = "text/html";

else if(path.endsWith(".css")) dataType = "text/css";

else if(path.endsWith(".js")) dataType = "application/javascript";

else if(path.endsWith(".png")) dataType = "image/png";

else if(path.endsWith(".gif")) dataType = "image/gif";

else if(path.endsWith(".jpg")) dataType = "image/jpeg";

else if(path.endsWith(".ico")) dataType = "image/x-icon";

else if(path.endsWith(".xml")) dataType = "text/xml";

else if(path.endsWith(".pdf")) dataType = "application/pdf";

else if(path.endsWith(".zip")) dataType = "application/zip";

File dataFile = SPIFFS.open(path.c_str(), "r");

if (server.hasArg("download")) dataType = "application/octet-stream";

if (server.streamFile(dataFile, dataType) != dataFile.size()) {

}

dataFile.close();

return true;

}


Then, upload the code to your NodeMCU board. Make sure you have selected the right board and COM port. Also, make sure you’ve inserted your WiFi Credentials in the code.

After a successful upload, open the Serial Monitor at a baud rate of 115200. Press the “EN/RST” button on the ESP8266 12E board. Now it should print its IP address. After That Open web address and Type IP address in address bar and get result.


2,671 views1 comment

1 Comment


Juanito Magdangal
Juanito Magdangal
May 07, 2023

hello, i would like to purchase the spiff file pls reply


Like
bottom of page