Control your water consumption in your home using simple IOT devices and the telegram app.

Mohamed El Hlafi
6 min readJan 3, 2021

In this tutorial I’ll show how can you control your water consumption using few simple devices. So after this tutorial, you can follow your consumption on your mobile phone or your computer, so there is no need to prolong introductions, let’s start !

Required Components :

  • ESP32 Devkit v4.0
  • yfs201 water flow sensor .
  • 3 wires
  • The ESP boards will be programmed using Arduino IDE.
  • Telegram app .

Introducing Telegram App:

Telegram Messenger is a cloud-based instant messaging and voice over IP service. You can easily install it in your smartphone (Android and iPhone) or computer (PC, Mac and Linux). It is free and without any ads. Telegram allows you to create bots that you can interact with.

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to Telegram Bot API“.

The ESP32 will interact with the Telegram bot to receive and handle the messages, and send responses. In this tutorial you’ll learn how to use Telegram to send messages to your bot to request sensor readings from anywhere (you just need Telegram and access to the internet).

Creating a Telegram Bot :

Go to Google Play or App Store, download and install Telegram:

Open Telegram and follow the next steps to create a Telegram Bot. First, search for “botfather” and click the BotFather as shown below:

The following window should open and you’ll be prompted to click the start button :

Type /newbot and follow the instructions to create your bot. Give it a name and username.

If your bot is successfully created, you’ll receive a message with a link to access the bot and the bot token. Save the bot token because you’ll need it so that the ESP32 can interact with the bot.

Get Your Telegram User ID :

Anyone that knows your bot username can interact with it. To make sure that we ignore messages that are not from our Telegram account (or any authorized users), you can get your Telegram User ID. Then, when your telegram bot receives a message, the ESP can check whether the sender ID corresponds to your User ID and handle the message or ignore it.

In your Telegram account, search for “IDBot

Start a conversation with that bot and type /getid. You will get a reply back with your user ID. Save that user ID, because you’ll need it later :

Preparing Arduino IDE:

We’ll program the ESP32 boards using Arduino IDE, so make sure you have them installed in your Arduino IDE.

Universal Telegram Bot Library:

To interact with the Telegram bot, we’ll use the Universal Telegram Bot Library created by Brian Lough that provides an easy interface for the Telegram Bot API.

Follow the next steps to install the latest release of the library :

  1. Click here to download the Universal Arduino Telegram Bot library.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Add the library you’ve just downloaded.

You also have to install the ArduinoJson library. Follow the next steps to install the library.

  1. Go to Skech > Include Library > Manage Libraries.
  2. Search for “ArduinoJson”.
  3. Install the library.

We’re using ArduinoJson library version 6.15.2.

YFS201 Water Flow Sensor

The sensor has 3 wires RED, YELLOW, and BLACK as shown in the figure below. The red wire is used for supply voltage which ranges from 5V to 18V and the black wire is connected to GND. The yellow wire is used for output(pulses), which can be read by an MCU. The water flow sensor consists of a pinwheel sensor that measures the quantity of liquid that has passed through it.

The working of the YFS201 water flow sensor:

An integrated turbine wheel with a magnet is placed on a closed plastic casing and an effect sensor Hall placed, when water flows through the pipe, it turns the turbine wheel and therefore the magnetic flux interferes with the sensor Hall effect, the interference rate depends on the speed of the water flow, so that the Hall effect sensor produces a pulse signal output, this output pulse rate can be assessed as the volume of water in effect from the data sheet of this sensor we have two main relations :

Frequency (Hz) = 7.5 * Flow (L / min) and pulse per liter= 450

so Flow (L / min) = Frequency (Hz) /7.5

and :

Total (liters) = Number of pulses / 450

Circuit diagram :

Code (C++):

Demonstration :

Upload the code to your ESP board, open the Tools menu > Board and select the board you’re using. Go to Tools > Port and select the COM port your board is connected to.

After uploading the code, press the ESP on-board EN/RST button so that it starts running the code. Then, open the Serial Monitor to check what’s happening in the background.

Go to your Telegram account and open a conversation with your bot. Send the following commands and see the bot responding:

  • /start shows the welcome message with the valid commands.
  • /readings returns the current Rate and Consumption readings from YFS201 sensor.

here is the result as you see in this figure, so I hope this tutorial was useful, it’s an amazing experience with simple tools ,try to do it ,good luck and see you in other tutorials.

--

--