Giter VIP home page Giter VIP logo

esp8266-water-pump-mqtt's Introduction

esp8266-water-pump-mqtt

Avoid herb plants from running dry. Water them automatically with a pump whenever needed. Monitor moisture readings and receive notifications when level is too low through the openHAB smart home system and the MQTT messaging protocol. This was my final project for CS50x 2020.

First prototype of watering system

Parts

  • ESP8266 (I used a NodeMCU v2)
  • Moisture sensor (I used this one)
  • Water pump (I used this one, note: I found this typical Arduino pump is too weak)
  • Relay (I used this one)
  • Voltage regulator if your pump needs a Vcc incompatible with your board (I used this one)
  • Wiring, resistors, LEDs, a power source (I use a 5.5mm terminal) etc.

Board

Sketch including NodeMCU

Sketch including NodeMCU

Sketch without NodeMCU

What the code does

  • Connect to WiFi & MQTT
  • Read moisture level (x times, build average)
  • If moisture < threshold, start water pump
  • Stop pump when moisture threshold reached
  • Publish readings & pump status changes to MQTT topics
  • Check for MQTT command to start pump
  • If pump command received, enable pump for 5 seconds*

Note: I disabled automatic pumping seeing that the moisture sensor doesn't always return correct readings. Uncomment respective lines if you're feeling more adventurous.

MQTT & openHAB integration

openHAB integration is optional. You will need an MQTT client to read & control the pump, though. Make sure your WiFi & MQTT credentials are defined in the script.

MQTT

The program will send and receive these MQTT commands:

Topic Payload Comment
stat/water_pump/STATUS { "MOISTURE":"50", "PUMP_VCC":"OFF", "WATER_LEVEL":"OK" } Published 1x per loop
cmnd/water_pump/FLUSH any will turn on pump for 3 seconds
cmnd/water_pump/FLUSH2 any will pump until target moisture reached

Interpreting status messages:

  • MOISTURE: Last reading (0 = dry, 100 = wet)
  • PUMP_VCC: Pump ON or OFF?
  • WATER_LEVEL: OK or WARNING

openHAB things

Bridge mqtt:broker:broker "mqtt broker" [ host="openhabianpi", port=1883, secure=false, username="XXXXXX", password="XXXXXX" ]
{
    Thing topic water_pump "Water Pump" {
    Channels:
        Type number : water_pump_moisture_level "Moisture" [ stateTopic="stat/water_pump/STATUS", transformationPattern="JSONPATH:$.MOISTURE" ]
        Type switch : water_pump_power "Pump power" [ stateTopic="stat/water_pump/STATUS", transformationPattern="JSONPATH:$.PUMP_VCC" ]
        Type string : water_pump_water_level "Water level" [ stateTopic="stat/water_pump/STATUS", transformationPattern="JSONPATH:$.WATER_LEVEL" ]
        Type datetime: water_pump_last_flush "Last flush"
        Type datetime: water_pump_last_update "Last update"
    }
}

openHAB items

Number water_pump_moisture_level "Moisture level [%d %%]" {channel="mqtt:topic:broker:water_pump:water_pump_moisture_level"}
Switch water_pump_power "Pump power" {channel="mqtt:topic:broker:water_pump:water_pump_power"}
String water_pump_water_level "Pump water level warning" {channel="mqtt:topic:broker:water_pump:water_pump_water_level"}
DateTime water_pump_last_update "Last update [%1$td.%1$tm.%1$tY / %1$tH:%1$tM]" { channel="mqtt:topic:broker:water_pump:water_pump_last_update" }
DateTime water_pump_last_flush "Last flush [%1$td.%1$tm.%1$tY / %1$tH:%1$tM]" { channel="mqtt:topic:broker:water_pump:water_pump_last_flush" }

Note: Google Assistant natively supports sprinklers. If you'd like to use the water pump via Google Assistant, expose the water_pump_power item to openHAB cloud and tag it like so:

Switch water_pump_power "Pump power" {channel="mqtt:topic:broker:water_pump:water_pump_power", ga="Sprinkler"}

openHAB sitemap

Default item=water_pump_moisture_level
Text item=water_pump_power
Default item=water_pump_water_level
Default item=water_pump_last_update
Default item=water_pump_last_flush
Chart item=water_pump_moisture_level label="Moisture last 24h" period=D refresh=1000
Chart item=water_pump_moisture_level label="Moisture last 1h" period=h refresh=1000

openHAB rules

To get a push notification in the openHAB app when the water reservoir is assumed empty:

rule "water pump reservoir empty"
when
    Item water_pump_water_level changed to "WARNING"
then
    sendBroadcastNotification("Water pump: Out of water")
end

To update timestamps of last contact and last watering:

when
    Item water_pump_power received command ON or
    Item water_pump_power changed to ON
then
    water_pump_last_flush.postUpdate(new DateTimeType());
end

rule "water pump update timestamp for last update"
when
    Item water_pump_power received update or
    Item water_pump_moisture_level received update or
    Item water_pump_water_level received update
then
    water_pump_last_update.postUpdate(new DateTimeType());
end

openHab screenshot

The result as displayed in openHAB's 'Paper UI': openhab screenshot

esp8266-water-pump-mqtt's People

Contributors

nicolaus-hee avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

madgustoo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.