Sunday, November 10, 2019

Connected Irrigation Controller

Well, the cheap sprinkler controller that I got with my house breathed its final breath on Tuesday.  It wasn't going to do to live without one.  I've been adding all kinds of fun to the smart house, so now was the time for the sprinklers!

After keeping an eye out for available smart irrigation controllers for a few years and now feeling a rush to get the water scheduled again, I decided to go with a Sonoff 4ch Pro with ESP home installed.



Pros:
- Wifi connection to Home Assistant for automation
- Node Red automation through Home Assistant
- Home Assistant services allow one message to enable start and duration
- Web server page available outside of Home Assistant
- Physical button and indicating light at the controller to manually start and stop watering cycles
- Programmable maximum water cycle times
- Fits in the housing of the old sprinkler controller
- Uses the 120V to 24VAC transformer from the old sprinkler controller
- Hardware and software interlocks available so only one zone is active at a time
- Captive portal Hotspot created when programmed access point not available (new owners)

Cons:
- Need to fabricate mounting adapter plate (3D printer simplifies this)
- More programming needed for dynamically changing watering times based on weather/seasons
- No visible indicator of time remaining in current watering cycle

I thought it was worth it, so I went for it.  I used the following yaml for ESPHome:
esphome:
  name: irrigation
  platform: ESP8266
  board: esp01_1m
wifi:
  ssid: !secret mywifissid
  password: !secret mywifipw
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Irrigation Fallback Hotspot"
    password: "S3crEt_P@$$w0rD"
captive_portal:
# Enable web server
web_server:
  port: 80
# Enable logging
logger:
# Enable Home Assistant API
api:
  services:
    - service: run_time_1
      variables:
        run_minutes: int
      then:
        - switch.turn_on: switch1
        - delay: !lambda 'return run_minutes*1000*60;'
        - switch.turn_off: switch1
    - service: run_time_2
      variables:
        run_minutes: int
      then:
        - switch.turn_on: switch2
        - delay: !lambda 'return run_minutes*1000*60;'
        - switch.turn_off: switch2
    - service: run_time_3
      variables:
        run_minutes: int
      then:
        - switch.turn_on: switch3
        - delay: !lambda 'return run_minutes*1000*60;'
        - switch.turn_off: switch3
    - service: run_time_4
      variables:
        run_minutes: int
      then:
        - switch.turn_on: switch4
        - delay: !lambda 'return run_minutes*1000*60;'
        - switch.turn_off: switch4
# Enable Over The Air updates
ota:
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "Sprinkler Button 1"
    internal: true
    on_release:
      then:
        - switch.toggle: switch1
  - platform: gpio
    pin:
      number: GPIO9
      mode: INPUT_PULLUP
      inverted: True
    name: "Sprinkler Button 2"
    internal: true
    on_release:
      then:
        - switch.toggle: switch2
  - platform: gpio
    pin:
      number: GPIO10
      mode: INPUT_PULLUP
      inverted: True
    name: "Sprinkler Button 3"
    internal: true
    on_release:
      then:
        - switch.toggle: switch3
  - platform: gpio
    pin:
      number: GPIO14
      mode: INPUT_PULLUP
      inverted: True
    name: "Sprinkler Button 4"
    internal: true
    on_release:
      then:
        - switch.toggle: switch4
  - platform: status
    name: "Sprinkler Status"
switch:
  - platform: gpio
    name: "Sprinkler Relay 1"
    icon: "mdi:sprinkler"
    pin: GPIO12
    id: switch1
    interlock: &interlockgroup [switch1, switch2, switch3, switch4]
    on_turn_on:
      then:
        - delay: 4hours
        - switch.turn_off: switch1
  - platform: gpio
    name: "Sprinkler Relay 2"
    icon: "mdi:sprinkler"
    pin: GPIO5
    id: switch2
    interlock: *interlockgroup
    on_turn_on:
      then:
        - delay: 30minutes
        - switch.turn_off: switch2
  - platform: gpio
    name: "Sprinkler Relay 3"
    icon: "mdi:sprinkler"
    pin: GPIO4
    id: switch3
    interlock: *interlockgroup
    on_turn_on:
      then:
        - delay: 30minutes
        - switch.turn_off: switch3
  - platform: gpio
    name: "Sprinkler Relay 4"
    icon: "mdi:sprinkler"
    pin: GPIO15
    id: switch4
    interlock: *interlockgroup
    on_turn_on:
      then:
        - delay: 12hours
        - switch.turn_off: switch4
status_led:
  pin: GPIO13

Saturday, January 20, 2018

Blog or Side Hustle Idea

Here's a side hustle idea that I had when I woke up this morning.  Unfortunately, I don't see how I can make this happen as it would take a lot of time for me to learn how to do it let alone get it done.

Buy domain DoIWantASmartHome.com and/or SpecMySmartHome.com and build a support site.  The jist of it is that YES, you want a smart home, but you don't know it yet just like you didn't know you wanted a smart phone before you got one.

The blog opens to a simple view of what a smart home is along with the first questions that people have to ask themselves.  Where to start?

The site is a side hustle because it obviously uses affiliate programs to get money from the purchases that people make after visiting the site.

There are YouTube videos that address all sorts of smart home topics.  Voice assistants, doorbells, alarm systems, cameras, hubs, etc.  There are also social media pages that point to the website: Facebook, Twitter, Instagram, Google+.

The real draw that I see to this site which should be a main feature is a calculator/guide.  This will collect a little data and make three generic suggestions for three levels of smart home complexity.  Each of the suggestions will have details into the cost and time investment to set up as well as the features.  The first level should recommend the biggest bang for your buck, but not get too carried away.  The second level should recommend the rest of the items that make a noticeable difference in your lifestyle.  The third level would recommend all the shiny gadgets that cost lots but end up being forgotten or are purely excessive, higher quality or maybe just over-doing it.

There should be a focus on how the smart home does make life nicer and is worth the expense.  There should be links to other sites that promote smart home products.  One page to showcase what people have done and what is possible, what do-it-yourselfers have done.

I'm thinking the three levels will go somewhere along the lines of 1) voice assistant 2)voice assistant with hub 3)professional installation.

Using my home as an example, I would tell the site that I have a 2500 sq ft home. 

1) The first level would suggest two small voice assistant speakers (I prefer google to amazon personally) a doorbell camera, three connected wall plugs, and three light bulbs with an estimated time commitment of 20 minutes for each of the speakers, 60 minutes for the doorbell, 10 minutes for each of the wall plugs and 10 minutes for each of the light bulbs for a total of 2 hours 40 minutes estimated installation time.

2) The second level would suggest a hub (I only have experience using smart things which just happens to work well with google Home) two to four multi-sensors, 2 chromecasts, two more full sized voice assistant speakers, five connected light switches, two thermostats and two indoor cameras.  Time estimated: indoor cameras 30 minutes each, light switches 25 minutes each, speakers 20 minutes each, chromecasts 20 minutes each, multisensors 15 minutes each, hub 1-4 hours depending on number of integrations and automations to set up, thermostats 45 minutes each.

3) The third level would suggest some outlandish stuff and might not have a time estimate.  It could recommend washer/dryer, refrigerator, outdoor strip cords, indoor cameras, outdoor cameras, irrigation controller, garage door control, more multi-sensors indoor and out, Home Assistant, wall switches, shower heads, high end audio, voice assistants for all rooms, color lighting, window blinds/shades, door locks, mesh wifi routers, and the list goes on!

... and that's all today with DoIWantASmartHome.com 

Saturday, January 13, 2018

Simple MQTT Doorbell Notifier

Welcome to the first project of my blog.  It's a doorbell notifier for my smart home.

I'll start out with some of the tech I'm using.  Home Assistant on a Raspberry Pi 3 (Hass.io).  I have an MQTT server on the Hass.io that I like to use for sensors that I build.  I've been using NodeMCU boards recently to make WiFi projects, but I've also played with the Wemos ESP8266 boards as well.

I recently wanted to start tracking when the doorbell has been rung.  After brainstorming and researching many options, I decided to make a battery operated WiFi sensor that would send MQTT messages to Hass.io.

I started the project by heavily paring down some code from BruhAutomation for his multisensor.  That's a brilliant project and have found mine quite useful.  I used the Arduino IDE to load this onto a NodeMCU board:

/*
  .______   .______    __    __   __    __          ___      __    __  .___________.  ______   .___  ___.      ___   .___________. __    ______   .__   __.
  |   _  \  |   _  \  |  |  |  | |  |  |  |        /   \    |  |  |  | |           | /  __  \  |   \/   |     /   \  |           ||  |  /  __  \  |  \ |  |
  |  |_)  | |  |_)  | |  |  |  | |  |__|  |       /  ^  \   |  |  |  | `---|  |----`|  |  |  | |  \  /  |    /  ^  \ `---|  |----`|  | |  |  |  | |   \|  |
  |   _  <  |      /  |  |  |  | |   __   |      /  /_\  \  |  |  |  |     |  |     |  |  |  | |  |\/|  |   /  /_\  \    |  |     |  | |  |  |  | |  . `  |
  |  |_)  | |  |\  \-.|  `--'  | |  |  |  |     /  _____  \ |  `--'  |     |  |     |  `--'  | |  |  |  |  /  _____  \   |  |     |  | |  `--'  | |  |\   |
  |______/  | _| `.__| \______/  |__|  |__|    /__/     \__\ \______/      |__|      \______/  |__|  |__| /__/     \__\  |__|     |__|  \______/  |__| \__|
  Thanks much to @corbanmailloux for providing a great framework for implementing flash/fade with HomeAssistant https://github.com/corbanmailloux/esp-mqtt-rgb-led
  To use this code you will need the following dependancies: 
  
  - Support for the ESP8266 boards. 
        - You can add it to the board manager by going to File -> Preference and pasting http://arduino.esp8266.com/stable/package_esp8266com_index.json into the Additional Board Managers URL field.
        - Next, download the ESP8266 dependancies by going to Tools -> Board -> Board Manager and searching for ESP8266 and installing it.
  
  - You will also need to download the follow libraries by going to Sketch -> Include Libraries -> Manage Libraries
      - DHT sensor library 
      - Adafruit unified sensor
      - PubSubClient
      - ArduinoJSON
    
  UPDATE 16 MAY 2017 by Knutella - Fixed MQTT disconnects when wifi drops by moving around Reconnect and adding a software reset of MCU
             
  UPDATE 23 MAY 2017 - The MQTT_MAX_PACKET_SIZE parameter may not be setting appropriately do to a bug in the PubSub library. If the MQTT messages are not being transmitted as expected please you may need to change the MQTT_MAX_PACKET_SIZE parameter in "PubSubClient.h" directly.
*/

// MODIFIED BY D SMYTH TO DETECT A DOORBELL ELECTROMAGNET

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

/************ WIFI and MQTT INFORMATION (CHANGE THESE FOR YOUR SETUP) ******************/
#define wifi_ssid "YOUR_SSID" //type your WIFI information inside the quotes
#define wifi_password "YOUR_PASSWORD"
#define mqtt_server "192.168.X.X"
#define mqtt_user "MQTT_USER
#define mqtt_password "MQTT_PASSWORD"
#define mqtt_port 1883

/************* MQTT TOPICS (change these topics as you wish)  **************************/
#define state_topic "home/doorbell/frontdoor"
#define SENSORNAME "doorbell_sensor"
const char* on_cmd = "ON";
const char* off_cmd = "OFF";

/**************************** SENSOR DEFINITIONS *******************************************/

char message_buff[100];

int calibrationTime = 0;

const int BUFFER_SIZE = 300;

#define MQTT_MAX_PACKET_SIZE 512


/******************************** GLOBALS for fade/flash *******************************/
bool stateOn = true;
WiFiClient espClient;
PubSubClient client(espClient);

/********************************** START SETUP*****************************************/
void setup() {
  Serial.begin(115200);
  Serial.println("Starting Node named " + String(SENSORNAME));
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  Serial.println("Ready");
  reconnect();
  sendState();
  delay(1000);
  stateOn = false;
  sendState();
  delay(10);
  ESP.deepSleep(1);
}

/********************************** START SETUP WIFI*****************************************/
void setup_wifi() {

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

/********************************** START SEND STATE*****************************************/
void sendState() {
  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;

  JsonObject& root = jsonBuffer.createObject();

  root["state"] = (stateOn) ? on_cmd : off_cmd;
  char buffer[root.measureLength() + 1];
  root.printTo(buffer, sizeof(buffer));

  Serial.println(buffer);
  client.publish(state_topic, buffer, true);
}

/********************************** START RECONNECT*****************************************/
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(SENSORNAME, mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

/********************************** START MAIN LOOP***************************************/
void loop() {
}

/****reset***/
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
Serial.print("resetting");
ESP.reset(); 
}

Then I had to add a few lines to the "configuration.yaml" file on Hass.io.

binary_sensor:
  - platform: mqtt
    state_topic: "home/doorbell/frontdoor"
    name: "Doorbell"
    value_template: '{{ value_json.state }}'

This completed the programming for the board.  When the NodeMCU powered up, it sends an "ON" message to the MQTT server and then after a second it sends an "OFF" message.  I connected a simple reed switch between the RST and GND pins on the NodeMCU board to reset the code any time the doorbell rings.

I have some Z-wave sensors that I use and work on battery, so I had some extra 123 batteries around and wanted to use one to power the sensor.  I connected the battery directly to the GND and 3V3 pins and found that it worked great!

Then I measured the current draw and found that the battery had a 30mA drain on it while the ESP8266 was in deep sleep.  :(

A little googling later, I found this blog and followed it to disconnect the voltage regulator and the USB-to-serial adapter using a quick touch of the soldering iron, an exacto knife and a resistor.  I had to play with the resistor value because the board still didn't start correctly with a 1 Mohm resistor.  In the end, I left the deep sleep current around 80 microAmps.  That should be low enough to keep it going for a year or so.

I used my 3D printer to print a holder for the 123 battery and will print another to hold the whole sensor.  I will hang it up next to my doorbell and my home will be that much smarter.

That was fun!

I'm sure I skipped over a lot of details of what to do and such.  If you feel really lost, but want to learn more, I would recommend starting with the BruhAutomation guides.

Good Luck!

First Post

This may become my blog to record things that I have done.  I want to invent and build things that will make life better in a simple way.  Many people will not believe that my methods are simple because they do not have similar experiences to mine.  That is fine.  My life's path has waited a long time to get where I'm at and to have the collection of things and experience that I now have.

I may share some ideas.  I like to follow tech news and think of how it applies to me.  It has been interesting to have had some ideas and then seen that there have been others that have had similar plans and have done something with them.  One thought that I've had that I like to think I'm making progress on is on automated window shades.  I think that they should be simple and easy to implement, but yes, putting these in all the windows of your house would still get expensive.

I like modular systems.  I tried SmartThings for my home automation, but tired of knowing that if my internet connection had a hiccup, my home would become dumb and potentially unfriendly.  I'd like to document my home automation process and show people that it is quickly becoming an easy thing to do.  The smart home market is highly divided at the moment, but I see that the voice assistants are placing themselves to be the hubs of these smart homes.  This seems to fit better than other phone/wall panel systems of the past.  I am using Home Assistant now because I found the BruhAutomation videos to be very helpful.  I've been curious why he made so many videos so quickly and has now been silent for so long.  I hope it has only been for happy reasons.

So, without further ado, let's talk about a home automation project I've got in progress...