Skip to content

Alextros00/ESP32-MQTT-Relay-Control

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn GitHub search hit counter GitHub last commit


ESP32_RelayControl_Hardware

ESP32-MQTT-Relay-Control

project_description
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents

  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

About The Project

This project is part of my home automation series using Node-RED, ESP32s, Telegram and more.. This repository describes the steps required to get the ESP32 to control a relay over WIFI using MQTT.

Getting Started

To get a local copy up and running follow these simple steps.

Mosquitto MQTT Broker

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium [mqtt.org]. The Mosquitto broker will be installed on your Raspberry Pi as the broker and your ESP devices will be clients. Learn more here.

Navigate to the terminal and follow these steps for the installation.

  1. Update your Raspberry Pi
    sudo apt-get update
  2. Install Mosquitto
    sudo apt-get install mosquitto
  3. Install Mosquitto Client
    sudo apt-get install mosquitto-clients
    ‡: Note for later: Port of your Mosquitto Broker, most likely 1883; Server Mosquitto Broker is running on, most likely the ip address of your Raspberry Pi

Install ESP-IDF

Install ESP-IDF for Windows, Linux or Mac OS.

Clone my Repo

  1. Clone the repo
    git clone https://github.com/Alextros00/ESP32-MQTT-Relay-Control.git
  2. Install NPM packages
    npm install

Configuration

  1. Plug in your ESP32 to your laptop and open your command line.
  2. cd into the project directory.
  3. Open the configuration menu. It may take a minute to load.
    idf.py menuconfig
  4. Go to Serial flasher config
    • Set the Default baud rate to 115200 baud for the ESP32
    • Set Default serial port and set the port in which your ESP32 is connected
      †:Your serial port can be found using this guide.
      ‡:One problem I had was that it is not clearly documented that if using windows the port should be configured in the COMX for example COM0
  5. Click Save -> Ok -> Exit -> Exit to get back to the main configuration screen
  6. Go to Example Configuration
    • Set WiFI SSID and WiFi password to that of your local 2.4GHz network
    • Set Broker URL to your mqtt server:port. It will look something like mqtt://@192.168.1.142:1883 if you have no username and password configured for Mosquitto
    • Enter the ESP32-X Number that you are using. This can be left blank and has no impact on the functionality of the code besides messages sent.
    • Enter the Relay GPIO Number or the GPIO that will control the relay. What pin you can use can be found on your device specific pinout.
    • Select enter 1 or 2 to decide between subscribing to 1 or 2 relay. Default is 1.
    • Set the MQTT Topic to Subscribe To or recieve messages from
    • Set the 2nd MQTT Topic to Subscribe To. Can be left alone if not using.
    • Set the MQTT Topic to Publish To or send messages to
  7. Once done configuring the project exit out of the menu by clicking Save -> Ok -> Exit -> Exit -> Exit to go back to the terminal

Flash and Monitor

Build and flash the project onto your device.
idf.py build & flash
Monitor the logs of your device
idf.py monitor
To exit the monitor us Ctrl + ] or Ctrl and ] at the same time

Hardware

Wire the ESP32 such that

Usage

The relay connected to the ESP32 can now be controlled over WIFI using an MQTT client sending messages on the previously selected topic in the configuration section.

Roadmap

See the open issues for a list of proposed features (and known issues).

License

Distributed under the MIT License. See LICENSE for more information but basically you can take my code but I would appreciate a coffee!

Contact

Alex Trostle - GitHub - Email - LinkedIn - Instagram

Buy Me A Coffee

Acknowledgements

About

Control a Realy over wifi using MQTT

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published