Creating your own meter to calculate water usage

This solution utilises the NodeMCU microcontroller for IoT applications. NodeMCU facilitates transmitting sensor data via WiFi to another device. For detailed information, visit NodeMCU Official Website: NodeMcu -- An open-source firmware based on ESP8266 wifi-soc.

The steps below explain how to measure water usage at a tap by integrating a flow sensor.
The data collected will then be sent online to a ThingSpeak channel using WiFi connectivity.
Here’s how to do it:


Step 1: Gather Components

  • Microcontroller: NodeMCU
  • Input Sensor: Flow Sensor
  • Output Device: LED, ThingSpeak
  • Libraries Used: LiquidCrystalPCF584 and FastLED

Step 2: Setting Up the Code


#include

#include <SoftwareSerial.h>

#include <DNSServer.h>

#include <ESP8266WebServer.h>

#include <WiFiManager.h>

#include <LiquidCrystal_I2C.h>

// WiFi & API Configuration

String apiKey = “enter your api key”;

const char* host = “rbiot.solveninja.org”;

WiFiClient client;

// Initialize LCD

LiquidCrystal_I2C lcd(0x3F, 16, 2);

// Sensor Configurations & Variables

#define PULSE_PIN D6

#define LED_PIN D7

volatile long pulseCount = 0;

float calibrationFactor = 6.0;

float flowRate;

unsigned int flowMilliLitres;

unsigned long totalMilliLitres;

unsigned long oldTime;

// Interrupt Handler for Pulse Counter

void ICACHE_RAM_ATTR pulseCounter() {

pulseCount++;

}

void setup() {

// Initialize serial communication

Serial.begin(115200);

lcd.init();

lcd.backlight();

// Initializations & pin configurations…

}

void loop() {

// Continuously monitor flow, update LCD, and send data…

// Conditions to calculate flow rate, total liters, and display on LCD…

senddata(); // Function to send data to ThingSpeak

showonlcd(); // Function to display on LCD

}

Step 3: Sending Data to ThingSpeak

  • Connect to WiFi using WiFiManager library
  • Establish connection with ThingSpeak server
  • Send water usage data to ThingSpeak channel
  • Wait for server response and handle timeout

Step 4: Display Data on LCD

  • Utilises LiquidCrystal_I2C to display flow data
  • Updates LCD with current flow rate and total litres used

This code facilitates tracking water usage at a tap by integrating a flow sensor with NodeMCU. The sensor data is transmitted to ThingSpeak for monitoring and analysis.