nodemcu+raspberry pi control relay realizes smart socket

This tutorial focuses on the configuration of HASS and nodeMCU.


Preliminary preparation

  • Raspberry Pi 3B
  • NodeMCU ESP8266 CH340
  • 5v relay

1. HASS installation

The Raspberry Pi system and HASS installation are not the focus of this article, so I will recommend a link for you: HASS installation based on Raspbian .
The website also has some other HASS content, including setting up the Raspberry Pi wifi connection and HASS self-starting, I also set it according to the website, and the personal test is effective.

2. NodeMCU configuration

For NodeMCU to flash firmware and connect to wifi, please refer to the following tutorials:

https://bbs.hassbian.com/forum.php?mod=viewthread&tid=651&highlight=%E9%97%A8%E7%A3%81

When NodeMCU is connected to the router, enter its IP in the browser to log in to its homepage, and then we start to configure the MQTT service.

In Controllers, click Edit for controller 1 (the content shown later in the picture is the configured state).



Select OpenHAB MQTT in Protocol


Then proceed as shown in the figure, do not fill in the empty space, it should be noted here that we are configuration switches, Controller Subscribe and Controller Publish must not be changed , if it is to configure sensors, %sysname% in these two items can be customized. Click submit, close.



Next, configure the relay switch, click Devices, Edit.



Configure according to the picture. It is best to write down the two customizable places in the picture, and you don’t need to look back when configuring later.



3. Configure HASS

Next we add the following code to configuration.yaml

switch:
  - platform: mqtt
    name: "switch"
    state_topic: "/ESP_Easy/switch/Switch01"
    command_topic: "/ESP_Easy/gpio/12"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true

The name can be customized, it is the name of the button, and it will be displayed on the HASS interface.

state_topic: ESP_Easy in "/ESP_Easy/switch/Switch01" is the name of your ESP device, switch is the Name set by yourself in Devices at that time, and Switch01 is the Name set in Values ​​in Devices.


command_topic: ESP_Easy in "/ESP_Easy/gpio/12" is also the name of the ESP device, gpio is fixed, and 12 is the GPIO pin number selected by 1st GPIO in Devices.

Save and restart HASS. You can see the following buttons appear in the HASS interface



4. Hardware connection

The VCC of the relay is connected to the 3V pin of the NodeMCU, the GND is connected to the GND pin of the NodeMCU, and the IN pin is connected to the GPIO12-D6 pin. (The rear socket circuit is omitted.)



After all settings are completed, you can control the relay through the buttons on the HASS interface.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325722310&siteId=291194637