Water Tank Levels by WiFi on Your Phone

26,484

358

55

About: I'm retired, living in Queensland Australia with my wife. These are my hobbies, pretty much full time now. Isn't it great to retire and actually pursue your real interests!

Intro: Water Tank Levels by WiFi on Your Phone

This instructable is entered in the "Wireless" contest. Please vote for me, Thank you

When I moved to a rural property in Australia I was faced with the fact my water supply was at the mercy of the Gods; rain from the sky to fill the tanks.

I had a capacity of around 30,000 litres across 2 concrete tanks and checking the levels meant climbing up a ladder, lifting a very heavy concrete cover and peering down into the damp darkness to estimate the levels.

Not much fun, inaccurate and time consuming, there must be an easier way.

Using Blynk and my WLAN was the answer, I could see at a glance the levels on my phone.

Check out Blynk here

Step 1: The Hardware

The standard HC-SR04 ultrasonic sensors are not waterproof, waterproof ultrasonic transmitter/receiver sensors to the rescue. These are the same units used on motor vehicles for reversing and hazard detection. These coupled to an ESP8266 NodeMCU WiFi board plus a small power regulator board make up the hardware.

These parts can be bought from AliExpress very cheaply (links below) or the supplier of your choice.

ESP8266 NodeMcu-V3-Lua-WIFI

Waterproof Ultrasonic Sensor

DC-DC 3.3v supply

Plus a waterproof box big enough to fit all the boards in.

Breadboard for testing

Link wires

LED color of your choice

Plug pack, 9 - 12VDC with Arduino UNO compatible type connector

Step 2: Wiring It Together on the Bench for Initial Testing

I used a breadboard for initial setup and testing.

For testing purposes this can run off the USB supply from your PC but in the field I used a small 3v3 regulated DC-DC converter supplied by a 9v plug pack. A 12v supply could be used with these converters. Use what you have laying around.

Plug the 2 ultrasound sensor boards directly into the breadboard and run 2 wires for each to the Arduino as below. Be aware these ESP12E boards run on 3.3 volts. They have a 5v input supply pin (VIN) and also run on the USB supply when connected to your PC. Do not supply more than 5 volts to the VIN pin if you use that.

Wire using male to female link wires:

Sensor1 ping to Arduino GPIO5 (D1)

Sensor1 echo to Arduino GPIO4 (D2)

Sensor2 ping to Arduino GPIO14 (D5)

Sensor2 echo to Arduino GPIO12 (D6)

These sensors work ok at 3.3volts. This is good as level converters are not needed.

It's probably a good idea to take the supply and ground for the sensor boards from the DC-DC converter output pins when doing the final wiring in the box but for testing use the Arduino 3v3 and GND pins.

Wire both sensors VCC to Arduino 3v3

Wire both sensors GND to Arduino GND

The LED is only on for 50mS every 2 seconds so I didn't bother with a current limiting resistor but a 1/4W 470R resistor can be added if you wish.

LED Anode (long leg) to Arduino GPIO13 (D7) Through a resistor if desired

LED Cathode to Arduino GND

Now plug the Sensors into the back of the sensor boards, they have 2 mtr long co-axial leads.

I needed to extend these, my tanks are 6 mtrs apart. I used some old co-axial cable I had laying around.

If you do this, be sure to cover all soldered joints with heatshrink.

That's all the wiring, next is the software.

Step 3: Collecting and Installing the Software

This water level monitor uses Blynk to send info from the ESP8266 board to your phone via their servers.

The Blynk server in the cloud serves up your app with the information in the widgets on the phone. When signing up with Blynk you are given 2000 energy points. These points are used to 'buy' widgets for your program.

2000 points is enough to build this water monitor app.

I actually have a 'local server' running on a Raspberry Pi, this allows unlimited points and better security. It does not use the Blynk server at all, completely independent.

Installation and setup of a local server is beyond the scope of this instructable. There are many instructions and tutorials on the net for doing this starting with the Blynk website.

The final version I coded for OTA (over the Air) updating as it is remote and I didn't want to pull it all apart just to update the software. I'll discuss the OTA software changes and methods later.

While on the bench we have the USB serial to upload code.

First thing you will need is the Arduino IDE if you don't already have it.

Get the Arduino IDE here Arduino IDE. Follow the getting started link on how to install this software, it is not part of this instructable.

Download the Blynk library here Blynk library, download link is at the bottom of the page.

Follow the instructions on that page to install the library.

Get the phone app here Blynk App.

An account can be created now for Blynk if you wish using the app. Remember the login email address you use, you'll need it later. Probably best to use your every day email account.

You will also need this library:

NewPing This is an excellent library for using ultrasound devices that does all the timing for you. No need to mess around with milliseconds etc.

These instructions below need to be done to install all the ESP8266 software.

Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit).

Start Arduino and open File > Preferences window.

Enter ( copy and paste - without the quote marks) ```http://arduino.esp8266.com/stable/package_esp8266com_index.json``` into *Additional Board Manager URLs* field. You can add multiple URLs, separating them with commas.

Open Boards Manager from Tools > Board menu and install *esp8266* platform (and don't forget to select your 'NodeMCU 1.0 (ESP-12E Module)' board from Tools > Board menu after installation).

From the menu select Sketch > Include Library > Manage Libraries. Allow it to finish downloading. You should see the Blynk libraries installed.

For your PC to communicate with the Arduino boards the USB to Serial software (We ESP12 USB-UART CH341SER_win driver) needs to be installed. This is needed for all Arduino comms over USB. Get it here

That's all the software set up hopefully.

Step 4: Test Code for the ESP8266 Board

When I write software I like to build it up from small beginnings and continue testing as I go.Writing a long program and finding it doesn't work is difficult to debug.

So we'll start with two sensors reporting back the information to the serial monitor built into the Arduino IDE.

No reporting to the Blynk server, OTA or WiFi yet.

Double click the 'water_code_1.ino' file below, after Arduino IDE starts go to File > 'save as' and save to your sketch folder.

A folder will be created with the ino file inside.

Confirm you have the correct board selected, click the 'verify' button (very left button on the IDE). It should compile without error.

Connect your board to the USB cable, select from the IDE menu Tools > Port > COM n.

The correct com port can be found by simply unplugging the board and see which port disappears.

Click the 'Upload' button in the Arduino IDE and allow it to finish compiling and uploading.

In the IDE select Tools . Serial Monitor.

The LED should flash every 2 seconds and the monitor should be reporting distance, depth and liters for each tank every 2 seconds similar to the picture shown.

These sensors have a range of about 3 mtrs They are set at 3 mtrs in software anyway.

Test each sensor by moving it in and out from a solid object like the wall, you should see the figures change in the monitor window. As the distance increases this is simulating a lower water level so less liters will be reported and vise-versa.

If all works as expected go to the next step.

Step 5: Code With WiFi and Blynk

Now lets get connected to the Blynk server.

download and open the water_code_2.ino

In the Arduino IDE select File > 'save as' to your sketch folder.

Open the Blynk app, sign in. Create a new project, chose device = ESP8266, connection type = WiFi, set the theme you prefer. press the Create Button.

An authority token will be sent to your nominated email account. press OK. Further instructions on the app program in the next step.

go to your email and copy the authority token, paste it into your arduino program where it says 'char auth[] = "YourAuthToken"

Also enter your SSID and your password.

Now upload it to your ESP board.The serial monitor should display connection status before it displays the water levels.

In the next step we will finish the Blynk project in the app.

Step 6: Setting Up the Blynk App Program

Continuation of new project in the Blynk app....

Press the + at the top of the screen in the app, scroll down and press Value Display. Do this 4 times

Press the + at the top of the screen in the app, scroll down and press LevelV. Do this twice

Press the + at the top of the screen in the app, scroll down and press SuperChart.

Lay out the widgets like the picture. A long press on the widget gives you handles to resize them.

Color them as you see fit.

Press the first Value Display (top left), name it Depth 1, select V1, 0 to 300 and PUSH

Press the second Value Display (top right), name Depth 2, select V4, 0 to 300 and PUSH

Press the third Value Display (2 down left), name Liters 1, select V2, 0 to 9999 and PUSH

Press the forth Value Display (second down right), name Liters 2, select V5, 0 to 9999 and PUSH

Press the left Vertical Level, name it Level 1, select V3, 0 to 1600, OFF and PUSH

Press the right Vertical Level, name it Level 2, select V6, 0 to 1600, OFF and PUSH

Press the SuperChart, names Liters 1 and Liters 2.

Press the icon (data stream) next to Name, PIN = V3, slide switch = VALUES, minn = 0, max = 1600

Press the icon (data stream) next to the second name, PIN = V6, slide switch = VALUES, min = 0, max = 1600

You can play around with the other settings to suit your preferences.

To delete a widget drag it to the top right of the screen.

If everything looks ok, press the play button top right of the screen. If your Arduino is running you should see the same numbers (except distance) come up as in the serial monitor.

Step 7: Setting Up OTA Updating

OTA or 'Over The Air' uploading of code is a really a useful feature, particularly in this case where the ESP board is in a box and outside.

This is a fairly simple process:

1. a OTA bootloader is first uploaded to the board.

2. The board is reset by pressing the reset button on the board.

3. your new code for the water tank project containing the OTA additions is uploaded to your board by USB.

Future uploads are achieved by selecting the WiFi port in Tools > Port.

Go to File > Examples > ArduinoOTA > BasicOTA

The program will load up, enter your Network Name and Password in the code near the top f the program.

Upload this to your board.

Now press the reset button on the board.

Go back to your water tank program and un-comment the 3 places shown in the pictures, these are required now for OTA uploading.

Upload this to your board, use serial USB.

If you look under Tools > Port in the Arduino IDE you will notice a new port. It will have 'esp8266', the board ID and the board IP address.

For future upload use this port, no need for a USB serial connection.

The board can now be powered by some other means but OTA will still work using power from the USB cable.

Step 8: Mount the Boards Into the Box

I mounted the ESP8266, both the Ultrasonic boards and the DC-DC converter in a IP66 rated box as it would be outside and exposed to the elements. I did however put the box inside a UV safe plastic bag just to be sure. I felt the connectors could leak water into the box.

I wont go into detail about mounting the boards; your box may be different to the one I used.

I didn't have any female connectors for the Sensor boards so I soldered the wires and covered with heatshrink.

If you have female-female wire links, all the better.

Mount the DC-DC converter to a position where the power connector can be plugged in from outside.

Take all the power and ground connections to this board so we avoid ground loops. Use 3.3 volts for everything.

Drill a hole for the LED, try to make it a tight fit so no adhesive is necessary.

Check everything, especially the power wiring.

Plug in the power pack and connect to the box, plug in the sensors and switch on.

You will not see anything on the serial monitor of course as we are not using the USB serial connection, but check your phone app.

Move the sensors around, the figures should change in the app.Bear in mind a ping and info transmission is only done every 2 seconds.

Step 9: Mount the Sensors in the Tanks

Again I cant go into detail about mounting the sensors in the water tanks, your tanks are most likely different to mine.

It does however have to be within your homes WLAN WiFi envelope. If your tanks are at a greater distance, WiFi extender could be used.

However, fix the sensors pointing directly vertically downwards toward the water at the height you measured before, see the picture, should be at 'Depth1 and Depth2. It is assumed the water can never reach this depth before overflowing. You don't want the sensors drowning.

I should also point out these sensors have a minimum range distance of 20 cm, this is because the transmitter and the receiver share the same ultrasonic speaker and has speed/timing problems under that distance.

Connect everything up and ensure everything is weather proof.

Apply power, it takes a few seconds to boot, you should see the water levels on your phones Blynk app.

Any questions I will try to answer as best I can. Good luck with this useful project.

Don't forget, if you like this instructable, please vote for me in the wireless Contest. Thanks

Wireless Contest

Runner Up in the
Wireless Contest

Share

    Recommendations

    • Fix It! Contest

      Fix It! Contest
    • Furniture Contest 2018

      Furniture Contest 2018
    • Tiny Home Contest

      Tiny Home Contest

    55 Discussions

    0
    None
    AntonioC271

    2 days ago

    Hi all, I would
    like to congratulate you for the great project, I'm really looking forward to
    doing it for my house, I tried to do the procedure and I could not do the
    installation to install Blynk, if I can help, I'll be very grateful:

    https://github.com/blynkkk/blynk-library/releases/tag/v0.4.10

    Here is the path on the computer that was performed:

    C:\Program Files (x86)\Arduino\libraries

    C:\Program Files (x86)\Arduino\tools

    In the Arduino program after procedure the blynk appears in ( Tools )


    But in (Tools \ board) it does not appear


    Thanks 

    Antonio

    0
    None
    SerkoM

    Question 4 days ago

    Can you please help me when I do the upload it claims that newping will not work with esp8266 any help will be welcome.Of course after upload of water level1 nothing happens

    0
    None
    TomatoSeed3

    6 weeks ago

    Hey! Just got my parts, but im only getting jibberish in the serial console?

    Ive done this:

    - tried different boards

    - tried a different computer

    - tried both sketches, nr2 acctually connected to my wifi for a second.

    - Baud is set to 115200

    - both sensor lights light up and blink in sync with the board led. It then writes the jibberish.

    Would you have the time to help out? What am i missing

    8 replies
    0
    None
    ItsGraGraTomatoSeed3

    Reply 6 weeks ago

    Check the serial console bottom right corner buad rate is set 115200

    0
    None
    TomatoSeed3ItsGraGra

    Reply 6 weeks ago

    Thanks for the super quick reply :)

    Yes i have made sure the baud is 115200 in the dropdown..


    Edit:

    https://www.aliexpress.com/item/10PCS-Wireless-module-CP2102-CH340-NodeMcu-V3-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/32447209352.html?

    CP2102 chip version was ordered.

    I see a big how-to in the product description, flashing the chips.. Is that nessesary? There it mentions 9600 baud several times. Could it be the chip needs configuration? Answer: no.

    Edit 2:

    Rebuilding everything from start.

    Now im on the right track i believe. Will update later.

    PS WARNING: library NewPing claims to run on (avr, arm), is nothing to worry about yes?

    0
    None
    ItsGraGraTomatoSeed3

    Reply 5 weeks ago

    First off, does your code compile? Use the left button in the IDE.
    Check all the wiring, what there is of it. If it compiles OK, upload it to the board, which you should have selected under the 'tools-boards'menu.
    If it doesn't compile check where the error is, it will be highlighted.
    Let me know what happens.

    0
    None
    TomatoSeed3ItsGraGra

    Reply 5 weeks ago

    Hi, thanks again.

    Everything is working great, not sure what the glitch was.

    I am going to consentrate next on getting the data sent to my lan blynk server i set up, i dont need the fancy graphs. only a .csv dump for instance. Perhaps i could use the serial print`s to store a file!

    0
    None
    TomatoSeed3TomatoSeed3

    Reply 5 weeks ago

    Hello again!
    In reference to my response about data storage, im going with Super Chart, its brilliant.

    I do however have some unstable sensors, one of them is better than the other, and i am not sure why they report "12" the most.
    The tubes are 8cm in diameter, and 35cm to the bottom. The water level is 5cm, the water rises when its raining and water floods in from the side. It has not rained :)

    Both sensors blink in paralell, but one of them keeps reporting 0 every now and then.
    Is this a calibration issue on "Tank1" and a bad sensor on "Tank2" perhaps?

    const int MAX_DISTANCE = 35;
    const int Diameter1 = 8;
    const int Diameter2 = 8;
    const int Depth1 = 34;
    const int Depth2 = 34;

    111.jpg
    0
    None
    ItsGraGraTomatoSeed3

    Reply 5 weeks ago

    Yes I found these a bit unstable.

    I did discover, by researching this device and the NewPing library, there is a multi ping function which then gives an average of your selected number of pings. Use instead of the normal ping function.

    unsigned int echoTime = sonar.ping_median(5);

    PingDepth1 = sonar.convert_cm(echoTime);

    delay(100);

    I found this stabilised it.

    Hope this helps.

    0
    None
    TomatoSeed3ItsGraGra

    Reply 5 weeks ago

    Where in the code do i place this?
    I guess its not in Global variables, and i guess that i need to do this twice since it says PingDepth1. Should it replace code in Readings Tank 1/ 2 ?

    0
    None
    TomatoSeed3TomatoSeed3

    Reply 5 weeks ago

    Hello again, i now understand the code needs to be placed in NewPing.h, and i think its around line 223, but i cant find any reference to PingDepth1 still. I am quite sure you understand, new to programming

    0
    None
    pmh11

    5 weeks ago

    Great project. I'm pretty much an IoT Noob right now but the instructions were thourough enough for me to be successful. I have adapted it to allow me to monitor the water level in my pool. I have placed the sensor on the underside of the diving board.

    I do have a question/problem thought. I'm trying to activate OTA so I can uplade from the comfort of my house!. I upload the BasicOTA code and then press the Reset button briefly (do I do this or need to hold it down?). In the serial monitor I see it gets an IP address. The problem comes when I re-upload my water level monitoring sketch (with the the OTA lines un-commented out). The sketch runs as before but I don't see the additional entry in the Ports section which if I understand it correctly is how I would connect to use OTA. I do still have the board powered by the serial jack so does this inhibit the Wifi port being available?

    Any help would be appreciated!

    1 reply
    0
    None
    ItsGraGrapmh11

    Reply 5 weeks ago

    You must have installed Python version 2.7

    If you have a later version installed, uninstall it. Download Python 2.7 and install that.

    Another thing I discovered is the Port entry for OTA is the board ID plus its IP address.

    I found it difficult when I have several systems around the place.

    In the Basic OTA sketch uncomment the line (below) and add a name, this will show up on the port listing:-

    // Hostname defaults to esp8266-[ChipID]

    ArduinoOTA.setHostname("water"); // UNCOMMENT THIS AND GIVE A NAME

    Do the same in the main sketch, in the Setup section, add the same name like below:-

    ArduinoOTA.setHostname("water"); // ADD THIS

    ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); }); //added for OTA

    ArduinoOTA.begin(); //added for OTA

    Hope this helps

    0
    None
    Ver2x

    5 weeks ago

    Question 1:
    did u put the center about 25 cm above your water tank? since the JSN-SR04T or the waterproof distance sensor has a minimum distance of 25cm?

    Question 2:
    how did u connect your nodemcu to the 3.3v dc-dc power supply? i got lost at the last part. is it possible to connect the wemos d1 to the 3.3v power supply?

    6 replies
    0
    None
    ItsGraGraVer2x

    Reply 5 weeks ago

    In the app, look in the widgets for 'Notifications' choose the type of notification you prefer and add it to the app.

    You will have to read up on how to use it on the Blynk website.

    0
    None
    ItsGraGraVer2x

    Reply 5 weeks ago

    Q1, yes.
    Q2, on a D1 power the board using a plugPak and the 3.3v pin can power the sensors. Don't need the dc-dc supply.
    I have now changed mine to run from a USB plugpak and the sensors from the board 3.3v pins.
    I will be updating this instructable when I get time.

    0
    None
    Ver2xItsGraGra

    Reply 5 weeks ago

    in what part of the code would I see where the sensor is placed above 25cm. i've been reading the code again and again just to see where that part of the code is. should it have like a 'distance of sensor to the base inside - sensor above maximum level' kind of command? i'm kind of new to this but here's my tweaked command of yours.


    #include
    #include
    #include
    #include //added for OTA
    #include // I2C library already built in Arduino IDE
    #include
    LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);
    //0x3F is i2c address, EN,RW ,RS ,D4,D5,D6,D7

    #define SONAR_NUM 1 // Number of sensors. Change to suit your requirements.
    #define PI 3.1415926535897932384626433832795

    //** CHANGE TO SUIT TANK DIMENSIONS
    const int MAX_DISTANCE = 300; //max distance to measure
    const int Diameter1 = 25.2; //internal Diameter of tank 1 in cm
    const int Depth1 = 30; //total depth of tank 1 in cm, from sensor to base inside

    const unsigned int Period = 2000; //period between pings, in milliseconds. i.e 1 munute = 60,000. max
    65535. Want longer? use unsigned long

    //** SENSOR PINS

    const int PingPin1 = 14; // GPIO14, D5
    const int EchoPin1 = 12; // GPIO12, D6

    const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2)); //area of base of tank 1

    // Global variables
    int Litres1, Distance1, WaterDepth1;

    // Set password to "" for open networks.
    char ssid[] = "********"; //local wifi network SSID
    char pass[] = "*********"; //local network password

    char auth[] = "**************"; // You should get Authority
    Token in your email.

    BlynkTimer timer; //config timer

    NewPing sonar[SONAR_NUM] = { // Sensor object array.
    NewPing(PingPin1, EchoPin1, MAX_DISTANCE) // Each sensor's trigger pin, echo pin, and max
    distance to ping.
    };

    void sendSensorReadings()
    {
    //***********Readings Tank 1
    Distance1 = sonar[0].ping_cm(); //get distance to the top of the water tank 1
    if (Distance1 >= Depth1 || Distance1 == 0 ) Distance1 = Depth1; //check it does not go negative
    WaterDepth1 = Depth1 - Distance1; //calculate the depth of the
    water
    Litres1 = (Area1 * WaterDepth1) / 1000; //calculate the volume of the water in litres



    //***********SEND INFO TO BLYNK
    Blynk.virtualWrite(V1, WaterDepth1); //send depth to Blynk server
    Blynk.virtualWrite(V2, Litres1); //send litres to Blynk server
    Blynk.virtualWrite(V3, Litres1); //send litres to Blynk server. for vertical level widget &
    chart,
    //scaled to 1/10 as Blynk only goes up to 9999
    and we need up to 16000
    //delay(50);
    //Blynk.virtualWrite(V4, WaterDepth2); //send depth to Blynk server
    //Blynk.virtualWrite(V5, Litres2); //send real litres to Blynk server
    //Blynk.virtualWrite(V6, Litres2 / 10); //send litres/10 to Blynk server

    digitalWrite(13, HIGH); //flash the LED on D7, just to let us know it's running
    delay(50);
    digitalWrite(13, LOW);

    //************************* can be commented out, test use only
    Serial.println();
    Serial.println();
    Serial.println("Tank 1 water distance: " + String(Distance1)); //print depth
    Serial.println("Tank 1 water depth: " + String(WaterDepth1)); //print depth
    Serial.println("Tank 1 Litres: " + String(Litres1)); //print litres

    //***********************************************

    }

    void setup() {
    ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); }); //added for OTA
    ArduinoOTA.begin(); //added for OTA

    pinMode(13, OUTPUT); //LED D7

    timer.setInterval(Period, sendSensorReadings); // Setup a function to be called every n seconds
    delay(10);
    //***********************************
    lcd.begin(16,2); // initializing the LCD 16 x 2
    lcd.setBacklightPin(3,POSITIVE); // Enable or Turn On the backlight
    lcd.setBacklight(HIGH);

    //** can be commented out, test only
    Serial.begin(115200); // Open serial console.
    Serial.println();
    Serial.println("Connecting to " + String(ssid)); // Connected to WiFi network
    //**
    Blynk.begin(auth, ssid, pass);
    delay(20);

    //** can be commented out, test only
    Serial.println(WiFi.localIP()); //this is local IP for this board
    Serial.println("WiFi connected");

    }

    void loop() {

    Blynk.run();
    timer.run();
    ArduinoOTA.handle(); //added for OTA
    lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
    lcd.print("Water Depth:"); // Prints string "Distance" on the LCD
    lcd.print(WaterDepth1); // Prints the distance value from the sensor
    delay(10);
    lcd.setCursor(0,1);
    lcd.print("Liters: ");
    lcd.print(Litres1);
    lcd.print(" L");
    delay(10);
    }

    1
    None
    ItsGraGraVer2x

    Reply 5 weeks ago

    depth1 is the distance from the sensor to the bottom of the tank.
    Distance1 is the distance measured.
    Subtract distance1 from depth1 and you have the depth of the water.
    Multiply that by the base area and you have the volume.

    0
    None
    Ver2xItsGraGra

    Reply 5 weeks ago

    so the code is specifically for a JSN-SR04T? or it can be used with other distance sensors like a HC-SR04?