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.
Â
Leave a Reply
You must be logged in to post a comment.