ESP Relay Board

From ElectroDragon
Jump to: navigation, search

Software and Use

Quick start Quide

  • Config Steps
Steps Descriptions Screenshot
0. Power up
  • Power up by AC power directly (or USB-TTL CP2102 for debugging)
  • Relay and relay LED will switch on/off twice for debugging purpose.
1. Smartconfig
  • Download IOT espressif apk from google play
  • in app, choose add devices, and enter password of your wifi setup
  • connect esp relay board on power
  • If you only want debug, can just CP2102 USB-TTL board, which provide sufficient power
  • Optionally press BTN2 button to clean in board config
2. finish smartconfig
  • click enter in iot app, wait until device found
3. Config your mqtt server
  • download mymqtt app or mqtt dashboad from google play
  • Use mymqtt for example, Config your mqtt server as follow
User: jtpfneeq
Password	: 4Og5kJ5WYQYT
Port: 14967
4. General commands test
  • Send commands "ON" or "OFF" to Topic "Light1" or "Light2"
  • You will here the relay coil click, and appliance connected with be switched on or off, LED on or off
5. Check mac address for unique control

There are three ways:

  • check the wifi spot, "AI-THINKER-D92069", D92069 is the MAC ID
  • In above IOT espress config, when config done, also can see D92069
  • Subscribe to topic "Light1", mac address will be published to this topic, all the time when power up
6. Switch to Mac IC control mode
  • Send commands, for example mac id is D92069, send "D92069ON" to mqtt topic Light1
  • Board will restart and switch to mac control mode
  • In new mode, use mac ID D92069 as topic
  • commands "1ON" "1OFF" "2ON" "2OFF" to control each relay
7. use station mode control
  • Connect to wifi spot, for example, "AI-THINKER_D92069"
  • download and use widget "socket control widget" from google play
  • create a new widget, and see the screenshot on right
  • a1, b1 are switch relay on, a0, b0 are switch relay off
  • board station server IP is, listen on port 80

Other Configuration

LED status:

LED Status
0.2 seconds very fast blink board is getting IP address from router, no IP address yet
5.0 seconds very slow blink connected to wifi, connect to mqtt server, wait for commands
0.5 seconds blink wait for smartconfig

In board config files for mqtt:

  • mqtt_server_1.lua, mqtt_server_2.lua - including server configuration details
  • wifi_config.txt - your wifi name and password
  • check_mode.txt - check if your board use mac control or not

Demo code and update Log

R3 version

  • Download R3 firmware here File:Relay board
  • mqtt sever info saved in mqtt_config.txt file
  • After smartconfig, ssid and password saved in config.txt, when power up will always use this setup for wifi
  • support to switch to unique ID (chipID) mode control by sending commands "chipID+ON" (162243ON) to ID topics
  • File:Esp relay board firmware, new firmware implemented on board from 4th Feburary 2017.

R4 version

  1. Including two servers config, in ID control mode, send commands "server1" or "server2" to swtich, or edit file "check_server.txt" change 1 for server1, or 2 for server2
  2. server 1 "mc" is mqttcloud free server, server 2 "sg" is Singarpore server, optimized for Asia area too.

Demo code of version R1

Module: LEDBlink init.lua mqtt2.lua
local M = {}
    gpio_16 = 0
    gpio.mode(gpio_16, gpio.OUTPUT)
    gpio_blink = 0
    tmr.alarm(2, 500, 1, function()
        if gpio_blink == 0 then
            gpio_blink = 1
            gpio_blink = 0
return M
LED = require "LEDblink"
print("set up wifi mode")
-- if you want to test smartconfig, disconnect the board first
tmr.alarm(1, 6000, 1, function() 
    if wifi.sta.getip()== nil then
        print("Waiting for smartconfig, IP unavailable..")
            function(ssid, password)
            print(string.format("Success. SSID: %s ; PASSWORD: %s", ssid, password))

        print("Config done, IP is "..wifi.sta.getip())
print("Running file mqtt")
gpio12 = 6
gpio.mode(gpio12, gpio.OUTPUT)
gpio.write(gpio12, gpio.LOW)
gpio13 = 7
gpio.mode(gpio13, gpio.OUTPUT)
gpio.write(gpio13, gpio.LOW)
-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("nodemcu", 120, "swqzxzhr", "9JMcxVeUNz3t")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline" 
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(client) print ("connected") end)
m:on("offline", function(client) print ("offline") end)
-- on publish message receive event
m:on("message", function(conn, topic, data)
  print(topic .. ":" )
  if topic == "Light1" then
    if data == "ON" then
        print("received message: ON@light1")
        gpio.write(gpio12, gpio.HIGH)
        print("receive OFF liked data on light1")
    if topic == "Light2" then
        if data == "ON" then
            print("received message: ON@light2")
            gpio.write(gpio13, gpio.HIGH)
            print("receive OFF liked data on light2")
m:connect("", 19311, 0, function(conn)
    m:subscribe({["Light1"]=0, ["Light2"]=0}, function(conn) 
        print("subscribe Light 1 and 2 success")


Websocket debugging:
CloudMQTT Websocket Console.png
  • Websocket debugging, login to cloudmqtt, please use your own server config (lua file mqtt-config.lua), see the screenshot on right
  • Commands can also send directly via websocket interface
Serial USB interface debugging:
Esp relay board demo.JPG
  • Debugging info can be read when you use it.
NodeMCU custom build by
	branch: master
	commit: c8037568571edb5c568c2f8231e4f8ce0683b883
	SSL: false
	modules: dht,enduser_setup,file,gpio,mqtt,net,node,tmr,uart,wifi
 build 	built on: 2016-03-29 10:06
 powered by Lua 5.1.4 on SDK 1.4.0
set up wifi mode
> Waiting for smartconfig, IP unavailable..
Waiting for smartconfig, IP unavailable..
Waiting for smartconfig, IP unavailable..
Waiting for smartconfig, IP unavailable..
Success. SSID: hcwork ; PASSWORD: electrodragon
Waiting for smartconfig, IP unavailable..
Waiting for smartconfig, IP unavailable..
Config done, IP is
Running file mqtt
receive OFF liked data on light2
receive OFF liked data on light1
received message: ON@light1
receive OFF liked data on light1
received message: ON@light2


  • You can use USB-TTL debug to re-program it.
  • If you want to customize it, please contact via
  • Use MQTT platform:
  • Use script interpreter LUA as embedded system programming
  • Demo code 1.0 available below, open source, support smartconfig and MQTT.


  • Wiring and programming- Use a USB-TTL board, for example PL2303, CH340, CP2102, FT232, to connect from PCB to relay board, Connect RX-TX, TX-RX, 5V-5V (OR 3V3-3V3), GND-GND, 3v3 more stable, configure your USB-TTL to 3v3 VCC for logic level
  • Reflashing Firmware - build custom firmware from custom-build nodemcu, hold the BTN2 button (connect to GPIO0) power up the board, open Nodemcu flasher/esp download tool/ to upload this firmware, more details see here, see some guidelines here
  • Restart module and upload lua scripts: LEDBlink.lua, init.lua, mqtt2.lua


  • Based on ESP-12F ESP8266 Wifi Board
  • This relay board use one AC input, and supports two relay AC output
  • Use mature AC-DC power module
  • Plastic enclosure design support, package includes the plastic case
  • Two Lead out buttons BTN1 and BTN2
  • Two relay indication LEDs
  • One status LED, indicating connecting status in demo code
  • Resevered design for temperature sensor DHT22
  • Screw terminal which are easy for wiring output

Hardware Extension

  • IO4 and IO5 hs pull up resistors for IIC bus to use
  • Demo code for IIC OLED screen for LUA can be found at nodemcu github, /master/lua_examples/u8glib/ this folder, or click here to browse.
  • DHT22 support plug on to use, feel free to use wires or socket to settle the DHT sensor, pin definition is 3V3, data, NC and GND
  • default firmware has DHT module enable, upload example code will work directly. demo code here., or download lua sketch, in esplorer run DHT22.lua direct to read temperature humidity

Hardware Update Logs

Update R1.6

Update ESP Relay Board R1.6.jpg
  • Optmized lead out pins, IO4, IO5 used for IIC bus, see the demo image here.

Updated R1.5:

  • Optimized board layout, replace a few higher quality parts

Updated version R1.1:

  • Fix isolation and current drop issue on old version, which cause the ESP-wifi board may have a 5% chance to reset/restart when you switch relays On/Off very very frequently.  R1.1 version is not absolutely can be eliminated this issue since the onboard AC-DC module has a limit, but the probability of this problem should be controlled to the least, low to one percent.
  • Maximum trace design for current passing, 2MM trace width to let 2A current pass through roughly for two relays together, so either 2A for running one channel device or 1A for two device. also open trace without soldermask, you can add more solders to get even higher current passing.


B004 box.jpg
  • Case size see on the image.
  • Board schematic for R1.6 version


  • Q: How to connect to serial uart port?
  • A: Connect USB-TTL tool 3V3 or 5V, GND, TX and RX to board 3V3 or 5V, GND, RX and TX.
  • Q: Why not response for command "ON", "OFF"?
  • A: Should be command "1ON" and "1OFF" to turn on and off for relay 1. Make sure board connected to internet, status LED should be 5 seconds on and off

Relevant Topics