All Projects → samco182 → SwiftFlowMeter

samco182 / SwiftFlowMeter

Licence: MIT license
⚡️ A Swift library for using Hall effect based water flow sensors.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to SwiftFlowMeter

Rpi gpio
Ruby conversion of RPi.GPIO Python module
Stars: ✭ 185 (+740.91%)
Mutual labels:  gpio, raspberry-pi-3
52-Weeks-of-Pi
Inspired by Shekhar Gulati's "52 technologies in 2016", I've decided to set a goal of 52 Pi ideas over the next year.
Stars: ✭ 54 (+145.45%)
Mutual labels:  gpio, gpio-pins
w1-gpio-cl
Command line configured kernel mode 1-wire bus master driver. w1-gpio standard Linux module enhancement/substitution.
Stars: ✭ 17 (-22.73%)
Mutual labels:  gpio, gpio-pins
gpio
A RaspberryPi GPIO library written in PHP.
Stars: ✭ 16 (-27.27%)
Mutual labels:  gpio
Waveshare.EPaperDisplay
.Net Core Library to show images on Waveshare E-Paper Displays
Stars: ✭ 17 (-22.73%)
Mutual labels:  gpio
Anar
laravel package : artisan commands for create Repository and provider
Stars: ✭ 25 (+13.64%)
Mutual labels:  repository
motionEye app HomeSurveillanceSystem
git repo for motionEye app - Home Surveillance System, available on Google Play Store
Stars: ✭ 22 (+0%)
Mutual labels:  repository
mmpm
MagicMirror Package Manager
Stars: ✭ 104 (+372.73%)
Mutual labels:  raspberry-pi-3
BetterRepository
Better Enhanced Repository Pattern Implementation in .NET C#
Stars: ✭ 27 (+22.73%)
Mutual labels:  repository
rpi2mqtt
Connect RaspberryPi GPIOs and 1-Wire temperature sensors to MQTT 🍰🔘📡
Stars: ✭ 20 (-9.09%)
Mutual labels:  gpio
PiBuilder
Ideas for building a Raspberry Pi from "bare metal" to ready-to-run IOTstack
Stars: ✭ 26 (+18.18%)
Mutual labels:  raspberry-pi-3
ioBroker.repositories
Repositories for ioBroker project
Stars: ✭ 55 (+150%)
Mutual labels:  repository
repology-webapp
Repology web application
Stars: ✭ 114 (+418.18%)
Mutual labels:  repository
linux.gpio.clj
Use the standard Linux GPIO API from Clojure JVM
Stars: ✭ 24 (+9.09%)
Mutual labels:  gpio
jackrabbit-filevault
Apache Jackrabbit FileVault
Stars: ✭ 37 (+68.18%)
Mutual labels:  repository
homeberry
HomeBerry is an Android remote control app for your Raspberry PI
Stars: ✭ 31 (+40.91%)
Mutual labels:  raspberry-pi-3
nexus3-docker
ARM Docker image of Sonatype Nexus Repository Manager (NXRM) (Raspberry Pis - armv7l, aarch64)
Stars: ✭ 55 (+150%)
Mutual labels:  repository
laravel-repository
Repository pattern implementation for Laravel
Stars: ✭ 49 (+122.73%)
Mutual labels:  repository
gitlab-runner
GitLab Runner (Docker image) for ARM devices, this is a mirror repository of
Stars: ✭ 17 (-22.73%)
Mutual labels:  raspberry-pi-3
repository-beta
BETA - Home Assistant Community Add-ons
Stars: ✭ 25 (+13.64%)
Mutual labels:  repository

SwiftFlowMeter

A Swift library for using Hall effect based water flow sensors.

Summary

This is a SwiftyGPIO built on library for using Hall effect based water flow sensors.

You will be able to read, via GPIO pin, the current flow rate per second and read the total volume flowed per requested time.

Working Principle

The illustration above gives a detailed working explanation of Hall effect based water flow sensors: a turbine wheel embedded with a magnet is placed in a closed case along with a Hall effect sensor. When water flows through the pipe, it makes the turbine wheel rotate and hence the magnet flux interferes the Hall sensor. The rate of interference depends directly on water flow rate, so the Hall effect sensor produces a pulse signal output. This pulse output can be transformed to water volume passed through the pipe per minute.

Hall effect based water flow sensors can provided by different manufacturers, but they all follow the same working principle. So, what makes them different? The formula that describes the sensor's pulse frequency transformation to flow rate.

The formula usually follows the form of:

Where:

  • F: Pulse frequency in 1/s
  • k: Pulses per second per unit of measure (1/s)/(l/min)
  • Q: Flow rate in l/min

Hardware Details

The sensor should be powered using 5V.

The Raspberry Pi’s GPIO pins operate at 3.3V ⚠️, and since the output signal from the water flow sensor comes at 5V, you will need to use a voltage divider in order to bring it down to 3.3V. If you don't do this, you might burn out the GPIO pin! 💥.

Supported Boards

Every board supported by SwiftyGPIO: RaspberryPis, BeagleBones, C.H.I.P., etc...

To use this library, you'll need a Linux ARM board running Swift 5.x 🚗.

The example below will use a Raspberry Pi 3B+ board, but you can easily modify the example to use one of the other supported boards. A full working demo project for the RaspberryPi3B+ is available in the Example directory.

Installation

First of all, make sure your board is running Swift 5.x ⚠️!

Since Swift 5.x supports Swift Package Manager, you only need to add SwiftFlowMeter as a dependency in your project's Package.swift file:

let package = Package(
    name: "MyProject",
    dependencies: [
        .package(url: "https://github.com/samco182/SwiftFlowMeter", from: "1.0.0"),
    ]
    targets: [
        .target(
            name: "MyProject", 
            dependencies: ["SwiftFlowMeter"]),
    ]
)

Then run swift package update to install the dependency.

Usage

The first thing is to initialize an instance of SwiftFlowMeter.

The SwiftFlowMeter initializer requires you to add a SensorCharacteristic instance. This object represents the formula that describes the sensor's pulse frequency transformation to flow rate.

Once you have your meter object, you can start getting the total flow per minute, or request flow rate readings per second, for example.

import Foundation
import SwiftFlowMeter

// Some sensors have an additional parameter, so their formula looks like F=k*Q+C. That is the `modifier` parameter for.
// If your sensor does not contains it, you can easily instantiate with SensorCharacteristic(kFactor: 10) instead.
let characteristic = SensorCharacteristic(kFactor: 10, modifier: .negative(4))

// You can also initialize the object without the .RaspberryPi3 parameter, since that is the default board.
let meter = SwiftFlowMeter(for: .RaspberryPi3, pinName: .P27, pulseCharacteristic: characteristic)

meter.readTotalVolume(every: .minutes(1), onVolumeCalculation: { totalVolume in
    print("Total flow per minute: \(totalVolume)")
}, onFlowRateCalculation: { flowRate in
    print("Flow rate: \(flowRate.value) l/min")
})

RunLoop.main.run()

There is another method you can use to obtain flow rate calculations per second only:

meter.readFlowRate { flowRate in
    print("Flow rate: \(flowRate.value) l/min")
}

⚠️ You should not call both methods at the same time, that is why readTotalVolume has an injectable closure to be executed every time the flow rate is calculated. If you call them both at the same time, you might start getting weird readings ⚠️!

If for some reason you need to stop receiving readings for either total volume or flow rate, you can easily do it by calling the following:

meter.stopReadings()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].