Building your own IoT water level indicator with NodeMCU is a fun and educational project that combines hardware and software development. Below is a step-by-step guide to help you create your water level indicator:
Step 1: Gather the Required Components:
Before you begin, make sure you have all the necessary components:
- NodeMCU development board (ESP8266)
- Ultrasonic distance sensor (HC-SR04)
- Breadboard and jumper wires
- USB cable for NodeMCU
- Power source (e.g., USB power bank or USB wall adapter)
Step 2: Understand the Working Principle:
The water level indicator works on the principle of ultrasonic distance measurement. The HC-SR04 sensor emits ultrasonic waves, which bounce back when they encounter an obstacle (water surface in this case). The sensor then calculates the distance based on the time taken for the waves to return.
Step 3: Circuit Connection:
- Connect the VCC and GND pins of the HC-SR04 sensor to the 3.3V and GND pins of the NodeMCU board, respectively.
- Connect the TRIG and ECHO pins of the HC-SR04 sensor to any GPIO pins of the NodeMCU (e.g., D1 and D2).
- Double-check all connections to ensure they are correct.
Step 4: Set Up Arduino IDE:
- Download and install the Arduino IDE from the official website.
- Open the Arduino IDE, go to “File” > “Preferences,” and in the “Additional Boards Manager URLs” field, paste the following link: http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Go to “Tools” > “Board” > “Boards Manager.” Search for “esp8266” and install the “esp8266” board package.
Step 5: Install Necessary Libraries:
- In the Arduino IDE, go to “Sketch” > “Include Library” > “Manage Libraries.”
- Search for and install the “NewPing” library, which is essential for working with the HC-SR04 sensor.
Step 6: Write the Code:
Now, it’s time to write the code for the water level indicator. The code should read the distance measured by the HC-SR04 sensor and communicate it to a cloud service for remote monitoring.
Here’s a simple example code to get you started:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NewPing.h>
const char* ssid = “YOUR_WIFI_SSID”;
const char* password = “YOUR_WIFI_PASSWORD”;
const char* mqttBroker = “MQTT_BROKER_IP”;
const char* mqttTopic = “water_level”;
#define TRIGGER_PIN D1
#define ECHO_PIN D2
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to WiFi”);
client.setServer(mqttBroker, 1883);
}
void reconnect() {
while (!client.connected()) {
Serial.println(“Connecting to MQTT…”);
if (client.connect(“WaterLevelClient”)) {
Serial.println(“Connected to MQTT”);
} else {
Serial.print(“Failed with state “);
Serial.print(client.state());
delay(2000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
int distance = sonar.ping_cm();
Serial.print(“Distance: “);
Serial.print(distance);
Serial.println(” cm”);
char msg[10];
sprintf(msg, “%d”, distance);
client.publish(mqttTopic, msg);
delay(5000); // Update every 5 seconds
}
Step 7: Configure the Code:
Replace the placeholders in the code with your Wi-Fi network credentials (SSID and password) and the MQTT broker’s IP address.
Step 8: Upload the Code:
Connect your NodeMCU to your computer via the USB cable, select the correct board and port in the Arduino IDE, and then click “Upload” to upload the code to the NodeMCU.
Step 9: Monitor Water Level:
Once the code is uploaded, open the Serial Monitor in the Arduino IDE to view the distance readings. The water level indicator will also publish the distance values to the specified MQTT topic, allowing you to view the data remotely using an MQTT client or an IoT platform.
That’s it! You have now built your own IoT water level indicator with NodeMCU. You can expand the project by adding additional sensors, implementing notifications, or integrating it with cloud services for data logging and visualization.
Written by kanchan kan
- Industrial Automation141141 products
- Cooling Fan44 products
- Indicators1111 products
- Plastic Casing66 products
- Sensor2323 products
- Sleeve and cables55 products
- SMPS88 products
- Solid State Relay Module22 products
- Switches1818 products
- Temprature Sensor11 product
- 3D Printing180180 products
- 3D printing110110 products
- Combination Kit4747 products
- Electronic743743 products
- Audio55 products
- Battery/Charger Accessories2828 products
- Capacitors6464 products
- Connector4646 products
- cooling fan11 product
- Diode3535 products
- Displays1212 products
- IC and Chips6565 products
- IOT and Wireless99 products
- Leds2424 products
- Microcontrollers55 products
- Modules/Shield7575 products
- MOSFET / IGBT1414 products
- Power Supply2525 products
- Resistors130130 products
- Sensors5959 products
- SMD2828 products
- Switches1414 products
- Transistors6868 products
- Wire and Cables1212 products
- Hardware3232 products
- Hand Tools1414 products
- Hardware Accessories1414 products
- Printing22 products
- RC Plane and Drone99 products
- Robotic147147 products
- BO Motors1313 products
- General Purpose DC Motors2929 products
- Motor Drivers88 products
- Robotic Wheels & Tyres1414 products
- Servo Motor2727 products
- Stepper Motors88 products
- Synchronous Motor11 product
- Science Fair Project7070 products
- Final Year Ready Project Kit11 product
- STEM KIT2222 products
- E Books88 products
Leave a Reply
You must be logged in to post a comment.