Robin Cole
Published © GPL3+

MicroPython Leak Detector with Adafruit and Home Assistant

Detect water leaks using a MicroPython sensor node and receive mobile notifications via Home Assistant.

IntermediateFull instructions provided1 hour8,007
MicroPython Leak Detector with Adafruit and Home Assistant

Things used in this project

Hardware components

Soil Hygrometer Humidity Detection Module
×1
WiPy 3.0
Pycom WiPy 3.0
×1
Pycom Expansion board 2.0
×1
DS18B20
×1
Breadboard (generic)
Breadboard (generic)
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1

Software apps and online services

Home Assistant Home-assistant
Adafruit IO
CloudMQTT
MicroPython
MicroPython

Story

Read more

Schematics

Schematic

Code

main.py

MicroPython
Upload this to the Wipy
import pycom
import time
import machine
from machine import Pin
from onewire import DS18X20
from onewire import OneWire
from simple import MQTTClient
from network import WLAN
#
def settimeout(duration):
    pass
#
client = MQTTClient(client_id="example_client", server="io.adafruit.com",user="you_user", password="your_pass", port=1883)
client.settimeout = settimeout
client.connect()
#
#DS18B20 1-wire connected to pin P10
ow = OneWire(Pin('P10'))
temp = DS18X20(ow)
# Moisture sensor
adc = machine.ADC()
apin = adc.channel(pin='P16',attn=3)
# Function for taking average of 100 readings
def smooth_reading():
   avg = 0
   _AVG_NUM = const(100)
   for _ in range (_AVG_NUM):
       avg += apin()
   avg /= _AVG_NUM
   return(avg)
#
while True:
   curr_temperature = temp.read_temp_async()
   print(str(curr_temperature))
   client.publish("you_user/feeds/wipy-temperature", str(curr_temperature))

   analog_val = smooth_reading()
   print(analog_val)
   client.publish("you_user/feeds/wipy-moisture", str(analog_val))
   time.sleep(1)
   print("################")

configureation.yaml

YAML
For Home-assistant
homeassistant:
  name: Wipy
  latitude: 51.0
  longitude: -0.2
  elevation: 0
  unit_system: metric
  time_zone: Europe/London

##### Look of sensors, icons from https://cdn.materialdesignicons.com/1.1.34/
  customize:
    sensor.wipy_temperature:
      icon: mdi:thermometer
      friendly_name: "Wipy Temperature"
    sensor.wipy_moisture:
      icon: mdi:blur
      friendly_name: "Wipy Moisture"

######### Some stuff
frontend:
history:
logbook:
discovery:

############## Services
mqtt:
  broker: 192.168.0.11

notify:
  platform: pushbullet
  api_key: "Your_API_key"
  name: mypushbullet

#### Sesors
sensor:
  - platform: mqtt
    name: "wipy_temperature"
    state_topic: "HASS/wipy_temperature"
    unit_of_measurement: 'Celsius'

  - platform: mqtt
    name: "wipy_moisture"
    state_topic: "HASS/wipy_moisture"

  - platform: time_date
    display_options:
      - 'date_time'

######### Automation
automation:
  - alias: wipy_leak_detector
    trigger:
      platform: numeric_state
      entity_id: sensor.moisture
      below: 3000
    action:
      service: notify.mypushbullet
      data_template:
        title: "Wipy notification"
        message: Leak at {{states("sensor.date__time")}}

######## Set the main view
group:
  default_view:
    view: yes
    entities:
      - group.Wipy

  Wipy:
    entities:
      - sensor.wipy_temperature
      - sensor.wipy_moisture
      - sensor.date__time

Credits

Robin Cole

Robin Cole

7 projects • 124 followers
Pythonista & PhD physics

Comments