Water Distance Sensor Using Jsn Sr04t

by anismurni in Circuits > Microcontrollers

204 Views, 2 Favorites, 0 Comments

Water Distance Sensor Using Jsn Sr04t

kenyir.png
WhatsApp Image 2024-01-09 at 18.28.36_84473cd1.jpg

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

WhatsApp Image 2024-01-09 at 17.10.07_131e72be.jpg
WhatsApp Image 2024-01-09 at 17.10.06_b4711eda.jpg
WhatsApp Image 2024-01-09 at 17.10.06_9b01c1ba.jpg
  1. ESP32 - 1
  2. JSN-SR04T Sensor - 1
  3. BreadBoard - 1
  4. Male to Female - 2
  5. Female to Female - 4
  6. Resistor - 1
  7. LED - 1
  8. Transparent Container
  9. Powerbank
  10. Glue Gun
  11. Cable Tie
  12. Ice Cream Stick
  13. Double Side Tape
  14. Tape

Set Up the Circuit

WhatsApp Image 2024-01-09 at 17.30.34_1efb6251.jpg
circuit.jpg
CSM3313-GROUP 05- WATER DISTANCE

Supplies:

  1. ESP32 - 1
  2. JSN-SR04T Sensor - 1
  3. BreadBoard - 1
  4. Male to Female - 2
  5. Female to Female - 4
  6. Resistor - 1
  7. 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

WhatsApp Image 2024-01-08 at 23.03.55_d43a1b89.jpg
WhatsApp Image 2024-01-09 at 17.39.54_9e337c80.jpg
WhatsApp Image 2024-01-09 at 17.41.09_9aa822a1.jpg
WhatsApp Image 2024-01-09 at 17.39.14_06c408d9.jpg

For Water Distance Dashboard

  1. Open node red
  2. Add MQTT In Node
  3. Add Debug Node
  4. Add Dashbard Node
  5. Add Gauge Node
  6. Add Chart Node
  7. Connect Debug, Dashboard, Gauge and Chart to MQTT In Node

For Led

  1. Add Switch Node
  2. Add Function Node
  3. Add MQTT Out Node
  4. Connect Switch and Function to MQTT Out Node



Download My MQTT on Your Phone

MQTt.jpg
WhatsApp Image 2024-01-09 at 18.16.59_3cb1770c.jpg
broker.jpg

Prepare & Deploy the Circuit

WhatsApp Image 2024-01-09 at 18.25.36_938d192a.jpg
WhatsApp Image 2024-01-09 at 17.30.34_40ae19ea.jpg
WhatsApp Image 2024-01-09 at 18.27.24_2fe3a91e.jpg
WhatsApp Image 2024-01-07 at 18.04.02_73371a95.jpg
WhatsApp Image 2024-01-06 at 14.20.21_a8e0df5c.jpg
  1. Get a transparent container that can fit all the component needed.
  2. 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.
  3. Attaching ice cream rods and sensors with tape as makeshift weights to ensure precise depth measurements.
  4. Stick the bread board and power bank using double side tape.
  5. Put the circuit on the transparent container and connect to the power bank / power supply.
  6. Take a glue gun to glue the hole to prevent the water to get inside.
  7. Sealed the transparent container with tape to secure and prevent the water to get inside.
  8. 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).
  9. Use cable tie to tie the container filled with the complete circuit to a holder.

Monitoring the Sensor

Screenshot 2024-01-09 184732.png