Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

NodeMCU ESP8266 Based Ultrasonic Water Level Indicator with Wi-Fi Connectivity

View and Edit Project In Cirkit DesignerImage of NodeMCU ESP8266 Based Ultrasonic Water Level Indicator with Wi-Fi Connectivity

Circuit Documentation

Summary

This circuit is designed to measure water levels using an HC-SR04 Ultrasonic Sensor and display the status through LEDs and a buzzer. The NodeMCU V3 ESP8266 microcontroller is used to process the sensor data, control the LEDs and buzzer, and send the water level data to a ThingSpeak channel over WiFi. A 9V battery powers the circuit.

Component List

NodeMCU V3 ESP8266

LED: Two Pin (Green)

LED: Two Pin (Red)

HC-SR04 Ultrasonic Sensor

Buzzer

9V Battery

Wiring Details

NodeMCU V3 ESP8266

LED: Two Pin (Green)

LED: Two Pin (Red)

HC-SR04 Ultrasonic Sensor

Buzzer

9V Battery

Documented Code

#include <ESP8266WiFi.h>
String apiKey = "70O35NQFSPR4YI1S"; // write your "Write API key"
const char* ssid = "OnePlus Nord CE3 5G"; // write your "wifi name"
const char* password = "1223334444"; // write your "wifi password"
unsigned long channel = 2649966; // write your "wifi password"
const char* server = "api.thingspeak.com";

WiFiClient client;

const int trigPin = D6;
const int echoPin = D7;
long duration;
int waterlevel;

void setup() {
  pinMode(D2,OUTPUT);
  pinMode(D3,OUTPUT);
  pinMode(D4,OUTPUT);
  pinMode(D5,OUTPUT);
  Serial.begin(9600);
  Serial.println("Serial Begin");
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);
  WiFi.begin(ssid, password);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
}

void loop() {
  ultra();
  delay(1000);

  // we have made changes in program to decrease the latency.
  // but it will take minimum 15 second to post data on Thingspeak channel.

  if (client.connect(server,80)) {
    fwd_to_Thingspeak();
  }

  client.stop();
  Serial.println("Waiting");
  delay(1000);
}

void ultra() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  waterlevel= duration*0.034/2;
  Serial.println("waterlevel=" + String(waterlevel) + " cm");

  if(waterlevel<=10) {
    digitalWrite(D3,HIGH);
    digitalWrite(D5,LOW);
    digitalWrite(D2,LOW);
  } else if(waterlevel>=10) {
    digitalWrite(D2,HIGH);
    digitalWrite(D5,HIGH);
    digitalWrite(D3,LOW);
  }
}

void fwd_to_Thingspeak() {
  String postStr = apiKey;
  postStr +="&field1=";
  postStr += String(waterlevel);  // ultrasonic data
  postStr += "\r\n\r\n";

  client.print("POST /update HTTP/1.1\n");
  client.print("Host: api.thingspeak.com\n");
  client.print("Connection: close\n");
  client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  client.print("Content-Type: application/x-www-form-urlencoded\n");
  client.print("Content-Length: ");
  client.print(postStr.length());
  client.print("\n\n");
  client.print(postStr);

  Serial.println("Send data to thingspeak: ");
  Serial.print("Content-Length: ");
  Serial.print(postStr.length());
  Serial.print(" Field-1: ");
  Serial.print(waterlevel);  // ultrasonic data
  Serial.println(" data send");
}

This code is designed to be uploaded to the NodeMCU V3 ESP8266 microcontroller. It initializes the WiFi connection, reads the water level from the HC-SR04 Ultrasonic Sensor, and sends the data to a ThingSpeak channel. It also controls the green and red LEDs and the buzzer based on the water level.