Introduction: Flowmeter NodeMcu Counting Litres

About: Student of the Dep. of Electrical Engineering, University of Algarve, Portugal

This work aims to build an Internet based system that enables the remote monitoring of a water/flow meters.

Motivation: In a house or building it is very important to regularly analyze the consumption of water and thus take action, avoiding unnecessary costs. The possibility of track/control multiple points of water through a single web page is enabled by this project, or even the automatic cut of water supply when detecting a flooding.

In this project, the monitoring can be done locally or remotely, this way you can generate alerts programmatically in order to warn the administrator about an over-consumption in any part of a building. All data generated is sent and registered in both a local "web server" as a remote database (Emoncms or Thingspeak).

The measurements of the water consumption are stored in the EEPROM of the ESP8266. It is then displayed in a Web page and transmitted to IoT platforms.

Step 1: What's Needed?

For this work, it's needed:

1 Flowmeter http://pt.aliexpress.com/item/New-Black-G1-2-Wate...

1 NodeMcu http://pt.aliexpress.com/item/NodeMcu-Lua-WIFI-dev...

or http://pt.aliexpress.com/item/V2-4M-4FLASH-NodeMcu... (I prefer this one)

1 Breadboard (depends on the NodeMcu version, this works for the second one I mentioned above http://pt.aliexpress.com/item/400-Tie-Point-Interl...)

3 breadboard cables, like this ones http://pt.aliexpress.com/item/Free-shipping-Dupon... , you just need 3, so you can keep the rest for other projects.

All the code was developed in the Arduino IDE. (https://www.arduino.cc/en/Main/Software)

In the end I made an example with ESPlorer using the LUA language to try to get the same results (http://esp8266.ru/esplorer/)

The hardware connection is done by attaching 3 cables (jumper cables) between the flowmeter and the nodemcu, in order to get the on/off pulse generated by the flow of water and thus count for the litres *.

Cable connections (use the breadboard cables, male-to-male) :

  • Connect the red cable output from the flowmeter to the 3.3v pin on the nodemcu.
  • Connect the black cable output from the flowmeter to "ground" on the nodemcu.
  • Connect the yellow cable output from the flowmeter to the control pin on the nodemcu (in my case the D2 pin)

And that's it for a start, so the next step is to program the nodemcu (control of the litres passing in the flowmeter).

Note: Before any code, you may need to flash new firmware into the NodeMcu, use ESP8266Flasher (https://github.com/nodemcu/nodemcu-flasher).

I did 3 examples that I will show in the next steps, but I will only show the code for the first one.

*The flowmeter specs indicate that +/- 440 "pulses" is equivalent to 1 litre.

Step 2: Example1

In this first example, we have:

  • Litres counting in the serial monitor of the Arduino IDE;
  • Setup of a webserver in the ESP8266, where the data from the litre consumption is shown in response to an HTTP request. The access to the webserver can be done inside the network or from outside, requiring the corresponding setup of the Wi-Fi router.

  • Through an HTTP GET request, the total number of litres is sent to an external server (ThingSpeak). With that info a plot is shown in the ThingSpeak platform (see https://thingspeak.com/channels/120470), that can be accessed in any Internet terminal and analysed at any time.

  • The possibility to use an "html iframe" in the local webserver to show the graphic from ThingSpeak(illustrated on the image above).

Attached is the full code for this example.

Step 3: Example 2

In this second example, we have:

  • Litres counting in the serial monitor of the Arduino IDE;
  • Through an HTTP GET request, the total number of litres is sent to an external server (Emoncms). With that info a graphic is created in EmonCms, (see http://emoncms.org/a21210&id=32359) that can be access remotely and analysed in real time.

The code for this example can be obtained by replacing the URL in example 1, with the one from the input API of emoncms.

Step 4: Example 3 (ThingSpeak Only)

In this third example, we have:

  • Litres counting in the serial monitor of the Arduino IDE;
  • Through an HTTP GET request, the total number of litres is sent to an external server (ThingSpeak). With that info a plot is shown in the ThingSpeak platform (see https://thingspeak.com/channels/120470), that can be accessed in any Internet terminal and analysed at any time. (Now illustrated by the ThingSpeak website page.)

The code for this example can be achieve by modifying very easily the code from the example 1.

Step 5: Example With ESplorer (LUA Code)

Attached there are some files with the LUA example code.

Some notes about ESplorer and LUA: when using ESplorer (Lua code) to work with NodeMcu, it is possible to send multiples files to its memory. On one hand that makes the software more modular, on the other hand we can have the possibility of storing html pages in NodeMcu memory, that can be called anytime on the code.

Useful links:

Lua Examples: https://learn.adafruit.com/adafruit-huzzah-esp8266...

Software ESPlorer : http://esp8266.ru/esplorer/

Step 6: Last Words...?

As in any other project, something fails...

The NodeMcu sent random stack errors, sometimes by failing to connect to the wifi network, or by using the function attachInterrupt.

When I was starting the code, I was using the library ESP8266WiFiMulti.h, and decided to swap for the library ESP8266WiFi.h, and the error started to appear less often.

Although I'm a bit better at coding than electronics (no knowledge at all :P ), I kept reading about this issues on the web, but no luck. I used 3 units of NodeMcu (2 different models), to check if the errors were coming from a faulty one, but it didn't make any difference.

Well maybe someone can show me where I failed?

Step 7: Useful Links