Water Distance Sensor Using Jsn Sr04t
by anismurni in Circuits > Microcontrollers
204 Views, 2 Favorites, 0 Comments
Water Distance Sensor Using Jsn Sr04t
In the vast beauty of Tasik Kenyir, Southeast Asia's largest man-made lake, where nature harmonizes in an intricate dance of balance, accurate water measurement is now more critical than ever. This sprawling aquatic sanctuary not only hosts a diverse array of plants and animals but also serves as a lifeline for nearby communities. In our commitment to environmental care and sustainable growth, understanding the ever-changing water depths of Tasik Kenyir becomes a key mission.
Traditional depth measurement methods face challenges of accuracy, real-time data retrieval, and coverage. Introducing the JSN-SR04T waterproof ultrasonic sensor, our technological hero crafted for precise depth measurements in dynamic aquatic environments. This innovation aims to revolutionize how we monitor Tasik Kenyir's water depths. With strategic deployment of JSN-SR04T sensors, our goals include achieving precision, unlocking real-time insights, and providing comprehensive coverage. Join us on this technological adventure as we explore the secrets hidden in Tasik Kenyir's waters, pushing the boundaries of environmental monitoring and sustainable management. The JSN-SR04T sensor becomes our storyteller, narrating the untold tales of the lake's depths.
Supplies
- ESP32 - 1
- JSN-SR04T Sensor - 1
- BreadBoard - 1
- Male to Female - 2
- Female to Female - 4
- Resistor - 1
- LED - 1
- Transparent Container
- Powerbank
- Glue Gun
- Cable Tie
- Ice Cream Stick
- Double Side Tape
- Tape
Set Up the Circuit
Supplies:
- ESP32 - 1
- JSN-SR04T Sensor - 1
- BreadBoard - 1
- Male to Female - 2
- Female to Female - 4
- Resistor - 1
- LED - 1
Write the Coding
#include <WiFi.h>
#include <MQTT.h>
#include <esp_sleep.h>
// Ultrasonic sensor parameters
const unsigned int TRIG_PIN = 13; // RX
const unsigned int ECHO_PIN = 12; // TX
// LED pin
const int led1_pin = 23; // Change to pin 23
#define WIFI_SSID "FSKMPocketWiFi"
#define WIFI_PASSWORD "88888888"
#define MQTT_HOST "broker.hivemq.com"
#define MQTT_PREFIX_TOPIC "csm3313_umt/group05"
#define MQTT_PUBLISH_TOPIC1 "/distance"
#define MQTT_SUBSCRIBE_TOPIC1 "/led01"
WiFiClient net;
MQTTClient mqtt(1024);
unsigned long lastMillis = 0;
void connectToWiFi() {
Serial.print("Connecting to Wi-Fi '" + String(WIFI_SSID) + "' ...");
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// while wifi not connected yet, print '.'
// then after it connected, get out of the loop
Serial.println("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print a new line, then print WiFi connected and the IP address
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
}
void messageReceived(String topic, String payload) {
Serial.println("Incoming Status from topic " + topic + " -> " + payload);
// check if topic equals MQTT_SUBSCRIBE_TOPIC1
if (topic == (String(MQTT_PREFIX_TOPIC) + String(MQTT_SUBSCRIBE_TOPIC1))) {
if (payload == "1") {
digitalWrite(led1_pin, HIGH);
Serial.println("LED1 turned ON");
} else if (payload == "0") {
digitalWrite(led1_pin, LOW);
Serial.println("LED1 turned OFF");
}
} else {
Serial.println("Command not match.");
}
}
void connectToMqttBroker() {
Serial.print("Connecting to '" + String(WIFI_SSID) + "' ...");
mqtt.begin(MQTT_HOST, net);
mqtt.onMessage(messageReceived);
String uniqueString = String(WIFI_SSID) + "-" + String(random(1, 98)) + String(random(99, 999));
char uniqueClientID[uniqueString.length() + 1];
uniqueString.toCharArray(uniqueClientID, uniqueString.length() + 1);
while (!mqtt.connect(uniqueClientID)) {
Serial.print(".");
delay(500);
}
Serial.println(" connected!");
Serial.println("Subscribe to: " + String(MQTT_PREFIX_TOPIC) + String(MQTT_SUBSCRIBE_TOPIC1));
mqtt.subscribe(String(MQTT_PREFIX_TOPIC) + String(MQTT_SUBSCRIBE_TOPIC1));
}
void setup(void) {
Serial.begin(115200);
pinMode(led1_pin, OUTPUT); // Set LED pin as OUTPUT
digitalWrite(led1_pin, LOW); // Initialize LED as OFF
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
connectToWiFi();
connectToMqttBroker();
Serial.println();
}
void loop() {
mqtt.loop();
delay(10); // <- fixes some issues with WiFi stability
if (WiFi.status() != WL_CONNECTED) {
connectToWiFi();
}
if (!mqtt.connected()) {
connectToMqttBroker();
}
// Ultrasonic sensor reading
long duration;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration / 29.0 / 2.0 / 100.0; // Convert the time to distance in centimeters
float depth = 36.0 + (1.0 - distance);
// Convert sensor reading to String and publish to broker
if (distance > 0) {
String strDistance = String(depth);
String dist = String(distance);
Serial.println("Publish to topic: " + String(MQTT_PREFIX_TOPIC) + String(MQTT_PUBLISH_TOPIC1));
Serial.println("Depth: " + strDistance);
Serial.println("Distance: " + dist);
mqtt.publish(String(MQTT_PREFIX_TOPIC) + String(MQTT_PUBLISH_TOPIC1), strDistance);
Serial.println();
// Enter deep sleep for 10 seconds
Serial.println("Entering deep sleep for 10 seconds...");
delay(100); // Ensure that serial data is sent before entering deep sleep
esp_sleep_enable_timer_wakeup(1800 * 1000000); // 30 minutes in microseconds
esp_deep_sleep_start();
} else {
Serial.print(".");
}
delay(300);
}
Create a Dashboard Using Node Red
For Water Distance Dashboard
- Open node red
- Add MQTT In Node
- Add Debug Node
- Add Dashbard Node
- Add Gauge Node
- Add Chart Node
- Connect Debug, Dashboard, Gauge and Chart to MQTT In Node
For Led
- Add Switch Node
- Add Function Node
- Add MQTT Out Node
- Connect Switch and Function to MQTT Out Node
Download My MQTT on Your Phone
Prepare & Deploy the Circuit
- Get a transparent container that can fit all the component needed.
- Make a hole under the transparent container that can fit cable tie and a hole at side of transparent container that can fit ice cream stick and sensor cable.
- Attaching ice cream rods and sensors with tape as makeshift weights to ensure precise depth measurements.
- Stick the bread board and power bank using double side tape.
- Put the circuit on the transparent container and connect to the power bank / power supply.
- Take a glue gun to glue the hole to prevent the water to get inside.
- Sealed the transparent container with tape to secure and prevent the water to get inside.
- Find suitable place to deploy the sensor and make sure that sensor is one meter from the water surface(the distance of sensor from the surface of the water can be change, but make sure the distance in the coding also adjusted, here are the code snippet that need to be change, "float depth = 36.0 + (1.0 - distance);" note that the "1.0" in the coding are the distance in meter from the sensor to the water surface).
- Use cable tie to tie the container filled with the complete circuit to a holder.