How do I use the sensor module with an HC-SR04 sensor to measure cistern level?

Hello and thank you for taking the time to read this.

I’ve got a couple of esp32 devices and a G1 base station that I purchased pre-flashed to use for local communications. Recently I’ve been tasked with managing the water distribution for a small group of residents, this allows me to place a device very high up on top of a water tank on a mountain top.

I am responsible for water tank levels, I’d like to use the sensor detector module with a HC-SR04 to measure the water level and send that data the the G1.

I have no idea how to do this… I’m comfortable with command line and python, but I’m not sure how to use this sensor with Meshtastic to get a range measurement. I’ve read the docs at Detection Sensor Module Usage | Meshtastic

For a HC-SR04 sensor you would use a separate microcontroller to write your code checking tank level and then send that info to th mesh periodically using the serial module. The sensors that are built into the firmware have instant read, for an ultrasonic sensor you are going to need some code interpreting all the readings from the sensor.

Thanks for the reply, it’s never as simple as it first appears. Eventually I want to build this network out to include valve & pump controls, meters and leak sensors so I should get comfortable with adding other chips/sensors ect now. I’m not confident enough in my ability to put the processing on the same firmware.

Alternately, a moisture meter as an overflow alarm may work on the sensor module, a simple 1 or 0 shouldn’t require another processor and will keep the install simple (for now)… it’s a start.

Agreed, always harder than expected. I do a lot of automation of home/greenhouse/gardens/barns/etc.
You need a separate ESP32 module with your handling of sensors and controls and just use Meshtastic modules to distribute the data. I like ‘Home-Assistant’ and ESPHome and Node-Red working together for this. Some of my systems are on MQTT, which can be used with Meshtastic and the rest of my software. Good luck, it is kind of hard, but also fun (at least for me LOL).

1 Like

The names you dropped have helped me immensely! thanks

I took over this position on Feb 01 from an elderly plumber, we (as a community) purchased a piece of property with a well and serve water to 120 homes distributed in the mountains of Puerto Rico. I’ve got two cisterns, three pumps and seven valves that I have to operate manually twice a week. The well and cistern are on industrial PLCs that I need to put monitoring on too.

I am not a plumber, I’m a sysadmin, both deal with caca.

in your greenhouse and gardens are you using any flow meters? if so what kind?

I am retired and the greenhouse is just for our family, so only 12’ x 40’ and I use one flow sensor in the irrigation pump line to judge proper water flow (should there be water flowing or is the right amount flowing [clogged nozzles, etc]). I can not give advise on ‘best’ flow sensors.
When I was working, I developed algorithms for engine/transmission controls for vehicles. That involved a lot of instrumentation and testing. I had a team writing code/ instrumenting vehicles/evaluating operation. We had a lot of fun (but not the management part :wink: LOL). Now I do it for myself and that is really fun. I am waiting for my first Meshtastic boat from China to arrive, then more fun.
I wish you great success in your project. I suggest getting the monitoring working first and then tackle the control part.

Quick update for anyone else searching the forum: silly little things I learned

I used an arduino nano serial output to pin 46 & 45 on the Heltec V3 at a baud rate of 38400.

meshtastic --set serial.rxd 45
meshtastic --set serial.txd 46
meshtastic --set serial.mode SIMPLE

this sends text to the primary channel on the mesh with the result of the arduino’s action.

at 165m away I can read the cistern level on my laptop via the G1 meshLAN or on my phone with another Heltec device.

1 Like

Awesome, nice work! Can we see the arduino code?

Sure, I modified this code to suit my situation, changed baud rate and timeout.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-hc-sr04-ultrasonic-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

const int trigPin = 5;
const int echoPin = 18;

//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

void setup() {
  Serial.begin(115200); // Starts the serial communication
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}

void loop() {
  // 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);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance
  distanceCm = duration * SOUND_SPEED/2;
  
  // Convert to inches
  distanceInch = distanceCm * CM_TO_INCH;
  
  // Prints the distance in the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);
  Serial.print("Distance (inch): ");
  Serial.println(distanceInch);
  
  delay(1000);
}
1 Like

Random Nerd is great. Nice job. Happy automating :+1:

Thank you, these first steps are very important. For me, the next I must learn how to send tx to trigger the sensor.

Hi N8,
How is this project developing?
We are keen to know of your progress.
Regards
Greg

Good Morning,
After a few false starts I found the best sensor for my application and found the correct sensor for my application, JSN-SR04T is a waterproof module with a small board attached . It’s a very simple sensor for beginners and I’ve found that mounting on top of a sight-glass tube increases the accuracy.

the info follows this path: sensor > esp32 meshtastic > G1 base > web UI

I set it up to take a reading every hour and send that to the meshtastic device which re-broadcasts to the G1 station in my house. I’d like to find a way to send the G1 web UI info to a different server but life and time have gotten in the way and it works well enough for this project.

Here is the sensor that works for me JSN-SR04T DC5V Ultrasonic Module Distance Measuring Transducer Sensor Waterproof | eBay

Nathan,

Thanks for the prompt response.
Yes, I have a few of these sensors.

Could you share the code that you are using with them please, and maybe a screenshot of what you see on the Meshtastic app or map?

I’m hoping to use this to alert to flood conditions, at small bridges and causeways. A horn made from PVC plumbing might even extend the range.

Then to find a way to display multiple sites with recent history in an easy-to-understand way. Learn to use charts.js and Leaflet… :wink:

Regards
Greg

Hola Greg,

I’m traveling today so I will be in the air for a while, I can get screen shots of the web UI when I get home tomorrow. I’m not happy with the webUI for my use case, it’s acceptable for other things but it doesn’t do well for recording historic data.

I have found some difficulty with measuring levels in a cistern when water is entering, I assume there’s audio interfering with the sensor’s audio and the surface is not smooth, this is why I’m using the sight tube as the measuring point. Your use case may be better served with a series of moisture meters instead of audio sensor due to moving water.

Hi Greg, here’s a screen shot of the web UI receiving data from a range sensor during testing, this shows what comes through with an incorrect baud rate and 34800 baud.
The code I used is the same for the dual speaker HC-SR04 device and the single speaker waterproof device JSN-04T, set the trigger and echo pins to your needs.

My device array is : range sensor > Arduino nano > Esp32 Meshtastic > G1 BaseStation