0

members

Easy Basics: Project 086c Raspberry PI 3 model B board, ESP8266 ESP-12E module, 8 channel relay module, 7

of Acoptex.com in Raspberry Pi 3

Basics: Project 086c

Project name: Raspberry PI 3 model B board, ESP8266 ESP-12E module, 8 relay module, 74HC595 with ESPhome in Home Assistant

Tags: Raspberry, Raspberry PI 3 model B board, vers 1.1, v 1.1, ESP8266 ESP-12E module, ESP, ESP8266, WI FI module, ESP-12E, LoLin NODEMCU V3, NODEMCU, relay module, 8 relay module, shigft register, 74HC595, home automation, esphome, home assistant, ha, garden, valves, bulbs, Plastic Water Solenoid Valve, electromagnetic valve, 12V solenoid valve, Gravity Feed Electric Solenoid Valve DDB-CD-12VDC, DIY sprinkler controller, DIY external lights controller

Attachments: shift_register_switch.hcopy4_new_node.yamlgroups.yaml

WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT. WE ARE NOT RESPONSIBLE FOR ANY DAMAGE, INJURY, DEATH AND OTHER THINGS CAUSED BY THIS PROJECT IMPLEMENTATION. 

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1.Raspberry PI 3 model B 1 pc

2. Micro SD card and SD card adapter 1 pc

3. Micro USB power supply (2.1 A, max 2.5 A) 1 pc

4. USB keyboard 1 pc

5. USB mouse 1 pc

6. TV or PC monitor 1 pc

7. HDMI cable 1 pc

8.ESP8266 ESP-12E module with micro USB cable 1pc

9. Jumper cables F-M, M-M

10. Breadboard 1 pc

11. 8-channel 5V relay module 1 pc

12. Shift register (chip 74HC595) 1 pc

13. Light bulb 60 W 220 V with connector 8 pcs (optional)

14. Hi Link AC to 5V DC converter (HLK-PM01) 1 pc

15. 12V solenoid valve 8 pcs (optional)

16. 12V external power supply for solenoid valves 1 pc (optional)

General

We will learn how to use ESP8266 ESP-12E module, 8-channel 5V relay module, shift register with ESPhome in Home assistant on Raspberry Pi.

 

Thinking about getting a smart sprinkler controller for your house? Want completely local control using no cloud services? Today’s article walks through the hardware I used to create my own DIY WiFi sprinkler controller using an ESP8266. I’ll be going over the software portion in part 2 of the series. The goals of the project are:
Control my sprinkler system in an automated fashion, but still turn on individual zones manually when needed
No reliance on the cloud, should work over local network
Be extendable to any number of zones relatively easy
Keep the controller simple, keep the scheduling logic on the home automation platform
Inexpensive to build, but must be reliable

 

Are you thinking about getting a smart solenoid valve (sprinkler) controller or external lights controller for your house? Do you want the completely local control without cloud services? Let's do it.

Project tasks:

  • Make control of the system in an automated mode.
  • Make selection of individual zones (relays) manually to turn them ON/OFF when needed.
  • Make it work over local network only.
  • Make it ready for extention - to add any number of zones (relays) easy.
  • Keep it simple.
  • keep the scheduling logic on the home automation platform.
  • Inexpensive to build and be reliable.

 

There are essentially three main parts of this projects. The power supply, which is responsible for converting AC to DC (5V) to run the microcontroller (Nodemcu 1.0) and 8-channel 5V relay module; 8-channel 5V relay module to control each zone (valve or bulb); the microcontroller (Nodemcu 1.0) and shift register to control the relays.

We will use the Hi-Link HLK-PM01 to convert AC power to 5V DC.

We will use WiFi as the interface to the DIY controller. The NodeMCU 1.0 board provide a cheap microcontroller which we can program to receive commands from a Home Automation system for controlling the zones (relays). The board comes with its own regulator to convert 5V DC to 3.3V DC so we can power the board using the VIN and GND pins. The 3.3V power is useful for powering the shift register as well, as all logic on the NodeMCU is done at 3.3V. We need 8 general purpose IO pins (GPIO pins), one for each zone of our DIY contoller. One downside of the ESP8266 is there is not a lot of  (GPIOs). As you can see Nodemcu 1.0 (Lolin) does not have enough GPIO pins to accomodate our project so we will use the shift register (chip 74HC595). A shift register works by taking in data using serial communication to control additional IOs. These shift registers can be cascaded. So if you need to create a 16 zone DIY controller you add another chip 74HC595 to the circuit without using additional pins on the NodeMCU 1.0.

We will use the NO configuration ofor 8-channel 5V relay module. So, when an input pin is HIGH the relay is OFF, if it is LOW - the relay is ON and it connects the zone wire with the power supply so that the circuit is complete and the zone is activated (valve is open or bulb switched on).

Understanding the Hi-Link HLK-PM01

You can read more about it here.

Understanding the ESPhome

You can read more about it here.

Understanding the Home Assistant

You can read more about it here.

Understanding the Raspberry PI 3 model B

The Raspberry Pi 3 is the third-generation Raspberry Pi. It replaced the Raspberry Pi 2 Model B in February 2016.

Specification:

  • Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
  • 1GB RAM
  • BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
  • 40-pin extended GPIO
  • 4 USB 2 ports
  • 4 Pole stereo output and composite video port
  • Full size HDMI
  • CSI camera port for connecting a Raspberry Pi camera
  • DSI display port for connecting a Raspberry Pi touchscreen display
  • Micro SD port for loading your operating system and storing data
  • Upgraded switched Micro USB power source up to 2.5A

Understanding the 8-channel 5V relay module


The Relay Module 5V x 8 Relay w/ Opto-isolation has eight independent 5V logic compatible relays that control up to 125/250VAC@10A or up to 28/30VDC@10A each.

Features:

  • 8 independently controlled relays
  • Switch AC up to 125/250VAC @ 10A
  • Switch DC up to  28/30VDC @ 10A
  • Normally Open (NO) and Normally Closed (NC) contacts
  • LED indicators when relays are energized
  • 5V logic compatible

Relay modules are handy for controlling external loads.  Unlike transistors used for switching, relays use metallic switch contacts which avoids the power loss and heating inherent in transistor switching circuits.  They are also nicely contained little modules that are simple to hook-up and are especially useful for switching AC. The main downside to relays is that being an electro-mechanical device, like any mechanical switch relays are more prone to wear and tear over prolonged use.

These relays are fully 5V logic compatible. The inputs have a relay driver transistor and opto-isolator built-in which means the device needs < 5 mA on the logic control pin to drive it.  If you have a uC like an Arduino, you can drive the relays using any of the digital output pins. The relays are activated with a logic LOW signal.  There is a red LED that is lit when the control pin for that relay is driven low to activate the relay.  When the relay is activated an audible click will be heard.  A logic HIGH deactivates the relay and the LED will go off.

The board includes opto-isolators on the logic inputs. Opto-isolators provide complete electrical isolation between the logic control input and the relay power as an added layer of protection in case of a lighting strike or some other kind of major failure on the AC load of the relay. As-shipped, the module has a jumper between header pins JD-VCC and VCC (relay power is jumpered to logic power). This bypasses the opto-isolation circuit as it ties the relay power to the logic power, but it makes the device easier to use since just one power supply needs to be connected to the module.

If it is desired to enable opto-isolation, remove the jumper and provide a separate 5V power supply voltage to the JD-VCC and GND pins on that connector.

The module requires 5V power and GND to operate.  If opto-isolation is used, it will require two different 5V power sources.

The ground must be common with the uC.   Flyback diodes are included on the module in parallel with the relay coils to safely shunt current when the relay coil is de-energized.
When a relay is energized, the module draws about 70mA from the Vcc pin.  With both relays energized, the draw is about 140mA.

The output of the relay is rated to switch up to 30VDC at 10A per the relay markings or up to 125/250VAC at up to 10A. The output is SPDT type with both a NO (Normally Open) terminal and a NC (Normally Closed) terminal relative to the center unmarked common (COM) terminal.  A logic HIGH energizes the relay, so the NO–COM contact is closed and the NC-COM contact is open.  A logic LOW de-energizes the relay and the NO-COM contact is open and the NC-COM contact is closed.

Understanding the 74HC595 IC

The 74HC595 consists of an 8−bit shift register and a storage register with three - state parallel outputs. It converts serial input into parallel output so you can save IO ports of an MCU.When MR (pin10) is high level and OE (pin13) is low level, data is input in the rising edge of SHcp and goes to the memory register through the rising edge of SHcp. If the two clocks are connected together, the shift register is always one pulse earlier than the memory register. There is a serial shift input pin (Ds), a serial output pin (Q) and an asynchronous reset button (low level) in the memory register. The memory register outputs a Bus with a parallel 8-bit and in three states. When OE is enabled (low level), the data in memory register is output to the bus. 

You can find datasheet here.

Understanding the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

You can read more about it here.

Signals and connections of 8-channel 5V relay module

1 x 10 Header

GND - Connect to ground of the 5V power supply -

IN1 - Relay control input 1

IN2 - Relay control input 2

IN3 - Relay control input 3

IN4 - Relay control input 4

IN5 - Relay control input 5

IN6 - Relay control input 6

IN7 - Relay control input 7

IN8 - Relay control input 8

VCC - Connect to 5V power supply +

Relay Contacts

NC - Normally Closed side of the relay

Center Terminal - Common terminal usually connects to the load power source

NO - Normally Open side of the relay

1×3 Header

JD-VCC - Opto-isolator power input.  Jumper to Vcc to run off same power supply as the logic (default mode).  If opto-isolation is desired, remove jumper and apply a separate 5V supply to this pin +.

VCC - Same as VCC on the 1×10 header

GND - Same as GND on the 1×10 header.  Can be used to connect ground of separate 5V power supply (if used)

Signals and connections of the 74HC595 IC


Pins of 74HC595 and their functions:

Q0-Q7 - 8-bit parallel data output pins, able to control 8 LEDs or 8 pins of 7-segment display directly.

Q7’ - Series output pin, connected to DS of another 74HC595 to connect multiple 74HC595s in series

MR - Reset pin, active at low level; here it is directly connected to 5V.

SHcp - Time sequence input of shift register. On the rising edge, the data in shift register moves successively one bit, i.e. data in Q1 moves to Q2, and so forth. While on the falling edge, the data in shift register remain unchanged.

STcp - Time sequence input of storage register. On the rising edge, data in the shift register moves into memory register.

OE - Output enable pin, active at low level. Here connected to GND.

DS - Serial data input pin

VCC - Positive supply voltage

GND - Ground

You can check the specification of chip 74HC595 here.

Signals and connections of the Raspberry PI 3 model B



Signals and connections of the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

TX - transmit pin. GPIO pin

RX  - receive pin.  GPIO pin

3V3 (or 3V or 3.3V) - power supply pin (3-3.6V). 

GND ( or G) - ground pin.

RST - reset pin. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.

EN - Chip enable. Keep it on high (3.3V) for normal operation.

Vin - External power supply 5VDC.

D0-D8 - GPIO (General Purpose Input Output) pins 

D5-D8 - SPI interface

D1-D2– I²C/TWI Interface

SC (or CMD) - (Chip Select) - the pin that the master can use to enable and disable specific devices. GPIO pin

SO (or SDO) - Master In Slave Out (MISO) - SPI communication. The Slave line for sending data to the master. GPIO pin

SK (or CLK) - SCK (Serial Clock) - SPI communication.The clock pulses which synchronize data transmission generated by the master. GPIO pin

S1 (or SD1) - Master Out/Slave In (MOSI). SPI communication. The Master line for sending data to the peripherals. GPIO pin

S2 (or SD2) - GPIO pin

S3 (or SD3) - GPIO pin

VU (or VUSB) - external power 5VDC.

A0 - ADC output.

RSV - reserved

Wiring


WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT. WE ARE NOT RESPONSIBLE FOR ANY DAMAGE, INJURY, DEATH AND OTHER THINGS CAUSED BY THIS PROJECT IMPLEMENTATION. 

Do not upload a code to your Nodemcu board while your AC-DC converter connected to the mains. You should unplug it from mains before upload a sketch to your Nodemcu board.

We will use NO (Normally open) configuration, when the relay receives a HIGH signal the 120-240V switch closes and allows current to flow from the C terminal to the NO terminal. A LOW signal deactivates the relay and stops the current. 

Step by Step instruction

We recommend using a high-performance SD card for increased stability as well as plugging your device into an external display to see the default application booting up.

We assume that you have Windows 7 64 bit OS installed on your PC and empty micro SD card 32 GB with SD card adapter.

Configuration file attched for your reference  copy4_new_node.yaml

1. Do wiring.

  1. Do wiring for Lolin Nodemcu 1.0 (ESP8266 ESP-12E module) and breadboard.
  2. Plug in your Lolin Nodemcu 1.0 (ESP8266 ESP-12E module) to the Raspberry Pi 3 board USB port.

2. Installing Hass.io

It was explained in Raspberry basics: Project 29a Raspberry PI 3 model B board - Home assistant for beginners

3. Adding ESPhome addon

  1. In Home Assistant go to Hass.io ->ADD-ON STORE.
  2. In the Repositories part (on top), type the https://github.com/esphome/hassio in the Add new repository by URL field.
  3. Click on ADD.
  4. After that, wait a bit until the add-on is installed.
  5. Go to the add-on page. Click on INSTALL.
  6. Start the add-on by clicking on START.
  7. Click on Open Web UI.
  8. You will be greeted by a nice introduction wizard which will step you through creating your first configuration.
  9. Click on plus in orange circle.
  10. The ESPHome setup wizard opens. It will guide you through setting up your first ESP8266 or ESP32-powered device using ESPHome. This wizard will create a basic YAML configuration file for your "node" (the microcontroller). Later, you will be able to customize this file and add some of ESPHome's many integrations.
  11. First, we need to give this node a name. Choose this name wisely, it should be unique among all your ESPs. Names must be lowercase and must not contain spaces (allowed characters: a-z, 0-9 and _). Let's name it - new_node. Click on CONTINUE button.
  12. Select a type of microcontroller board you're using so that the ESPhome can compile firmware for it. If unsure you can also select a similar board or choose the "Generic" option.
  13. We have selected NodeMCU. Click on CONTINUEbutton.
  14. In next step you need to type the local WiFi network SSID and password (leave empty for no password) to be able to connect node to WiFi Access Point.
  15. ESPHome automatically sets up an Over-The-Air update server on the node so that you only need to flash a firmware via USB once. This password is also used to connect to the ESP from Home Assistant. Optionally, you can set a password for this upload process too. Click on CONTINUE button.
  16. You've successfully created your first ESPHome configuration file. 
  17. When you click on SUBMIT, the wizard will save a configuration file under /config/esphome/new_node.yaml
  18. If you have Configurator installed in Home Assistant skip installation notes. Go to Hass.io -> ADD-ON STORE  and select Configurator.
  19. Click on Install.
  20. Click on Start.
  21. Click on Open Web GUI.
  22. The configurator GUI opens. Go to Browse FileSystem.
  23. Go to config/esphome/ folder, make and save the new file shift_register_switch.h.
  24. There is no 74HC595 component in ESPhome so we will need to use Custom Switch. We will need to write a Csharp code to model each one of the shift register outputs as an ESPhome switch object.
  25. The code is using of the open source ShiftRegister74HC595 library. It creates a global object named sr for accessing the shift register pins. The code defines a new class called ShiftRegisterSwitch which implements some methods that are required to be an ESPhome switch. Each instance of this class resembles a single output from the shift register. So if you're using 8 channels, you'll need to create 8 instances of this class, one for each output pin. The code implements two methods that must exist to work as an ESPhome switch: setup() is called by ESPhome to setup the switch when the ESP8266 ESP-12E module turns on. In this implementation, we use the shift register library to set the pin high. write_state() is called when Home Assistant wants to set the state of the switch. Here the code just checks which state is being requested and uses the shift register library to set the pin's new value.
  26. Find the new_node.yaml file, it should be in the same folder /config/esphome
  27. Open this file and modify with code. You need to configure manual_IP with your static_IP and gateway. After putting a manual IP in your configuration, the ESP will no longer need to negotiate a dynamic IP address with the router, thus improving the time until connection. This can help with Over-The-Air updates if for example the home network doesn't allow for .local addresses. When a manual IP is in your configuration, the OTA process will automatically choose that as the target for the upload. 
  28. We need to integrate the Csharp code for Custom Switch into our ESPhome configuration file new_node.yaml. The shift register is going to be adding switches that switch the relays for each zone. To use the shifter register, we're going to include the ShiftRegister74HC595 Arduino library and the header file. We need to include our custom code, so we can add includes to the ESPhome configuration block to add additional source files to the compilation. Here we add the shift_register_switch.h file that contains our custom class. Our custom code uses an open source Arduino library. Using the libraries attribute we can pull in any open source libraries to use for our project. Finally, we actually create the custom switches for ESPhome. The lambda configuration block allows you to write custom C/C++ code that will be inserted into your application. Not to tangent too far into a C++ tutorial, but here we create a vector for storing all our switches. Using a for loop we create each one using the custom ShiftRegisterSwitch class. The lambda function ultimately returns the vector containing all the switch objects we created. The switches attribute maps one to one between the vector returned in the lambda function and the list of switches. 
  29. You might also want to customize the icon associated with each relay (switch). You can do it in the new_node.yaml. You need to modify the icon: mdi: xxxxx lines. You can find different icons here. Look below we search for water and find several icons (opened mdi:water-pump icon).
  30. The status_led component is added so that the LED on the NodeMCU 1.0 (Lolin) board blinks until it has successfully joined WiFi and Home Assistant has connected. The text_sensor component is used with the version platform to send version information to Home Assistant. This allows to see easily what version of ESPhome your devices were compiled with and when the firmware was deployed.
  31. Save your new_node.yaml file. Click on the diskette.
  32. Go to ESPhome, make sure that serial port selected.
  33. Click on UPLOAD to flash firmware. This compiles the firmware for your node and uploads it using any connected USB device or, if no USB devices are connected, over-the-air using the OTA Update ComponentPlease note that Hass.io Add-On is currently not capable of discovering new USB ports after the add-on has started due to some docker restrictions. Please go to the add-on details page and restart the add-on if a new USB device is not automatically found. If the USB device is still not found, try changing the USB cable and restarting the add-on.
  34.  Click on STOP as soon you will get the message: Client 'Home Assistant 0.95.4 (xxx.xxx.x.xxx)' connected successfully!
  35. Your node  
    Over-The-Air Updates: 192.168.1.240:8266
    Over-The-Air updates link is192.168.1.240:8266 and API Server link192.168.1.240:6053

  36. Go to Configuration->Integrations.
  37. Click on plus in orange circle.

  38. You need to setup a new Integration. Select ESPhome.
  39. Type the host and port (in our case it is 192.168.1.240 and 6053). Click on SUBMIT.
  40. You will need to type or paste the password witch you have assigned before when made the node (you can see it in new_node.yaml file as API password).
  41. Click on SUBMIT and FINISH.
  42. Click on ADD AREA if you want to assign this module to any area in the house (Living room, bedroom...).
  43. You can check the new_node integration configuration.
  44. Go to Overview and click on dots.
  45. Select Configure UI.
  46. Click on plus.
  47. Click on Entities.
  48. Type the title, select entities (in our case we had switch.zone_1switch.zone_2 and so on. Click on SAVE.
  49. Click on x to go back to normal mode or EDIT to change the card.
  50. Congrats! You have successfully completed this project and can turn on/off the Zone 1-8 with Home Assistant now.

Summary

We have learnt how to use ESP8266 ESP-12E module, 8-channel 5V relay module, shift register with ESPhome in Home assistant on Raspberry Pi.

Libraries

  • All libraries attached on the begining of this project description.

Sketch

  • See on the begining of this project


Other projects of Acoptex.com

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 17-07-2019
Viewed: 263 times