Elektroda.com
Elektroda.com
X

ESP8266 and Tasmota - controlling the WiFi relay step by step

p.kaczmarek2 17043 16
This content has been translated flag-pl » flag-en View the original version here.
  • ESP8266 and Tasmota - controlling the WiFi relay step by step
    Hello my dears
    I will show here how you can easily assemble your own IoT WiFi controller from ready-made modules (ESP8266, power supply, relay). In the example version, it will allow us to turn on/off one device via the Internet. It will be like a Sonoff DIY version, with a detailed description of the next steps. For the project I will use ready, free Tasmota firmware for ESP. In addition, I will also present how you can conveniently control Tasmota devices from a free phone application (without the use of an intermediary / broker).
    The theme, of course, is intended for slightly less advanced users. Rather, experienced here will not learn much.

    Entry
    Of course, the Sonoff itself does not make sense to make it yourself, because it can be easily purchased. But the method shown here is universal, it allows you to make any WiFi-controlled layout. A lot can be done without recompiling the Tasmota firmware (this soft allows you to set what is on which ESP pins via the website), and even more possibilities open up when you are ready to do your own firmware compilation and know a bit of the C language.
    In the same way, you can also transfer any ready-made 'smart home' device based on ESP8266 (e.g. from Blitzwolf) to Tasmota and use it normally with open source software, free from the intermediation of the manufacturer's cloud. Some often have various allegations that the manufacturer can, for example, track and save user activities, and the process from the subject excludes this risk.

    Modules used
    I used easy to buy modules for the project.
    First of all, the WiFi module, i.e. the board with ESP8266, here ESP8266-01 . Some photos from the auction:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Leads:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    NOTE: ESP8266 is powered from 3.3V. 5V voltage will damage it!
    5V power supply. The smaller the better, but it must provide sufficient current for the ESP and for what we want to connect. Current measurements at 5V for ESP8266-01 and one relay are placed at the end of the topic.
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    parameters:
    Quote:

    Input voltage AC85~265v 50/60HZ
    Output voltage DC5V
    Operating current 500mA Dimensions
    Volume: LWH 32*18*13(mm)

    Photos:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    The power supply above is of course a flyback switching power supply, based on the AP8012 chip, on the secondary side there is also a TL431 (i.e. a precise reference voltage source), under the large capacitor connecting the secondary side with the primary side there is an optocoupler. At the output, this power supply has two electrolytic capacitors and a choke, which together form a filter, but there is no fuse at the input, so one should also be supplied loose. I chose the fuse from an old 5V power supply of similar power.

    5V relay The SRD-05VDC-SL-C is very popular:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Internal diagram, pins:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Photo:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Additionally:
    - some USB to UART converter to upload the batch to ESP (or ESP to USB 'programmer')
    - 3.3V LDO regulator, for ESP power supply. I had the MCP1702 on hand, and I regularly use the TC1264
    - Transistor (to turn on the relay). I used BC547
    - The diode that is connected in parallel to the relay winding can be 1N4148
    - Button
    - Few capacitors (100nF before LDO, 100nF after LDO, some electrolytic after LDO)
    - goldpin 4x2 socket for ESP and additionally better also for the power supply (so that it can be easily removed)
    - Small items (resistors, jumpers)
    Below, I'll walk you through the step-by-step procedure for assembling your own Sonoff.

    Step 1 - ESP8266 programming
    First you need to upload the appropriate soft on the ESP8266. You can do it with any USB/UART adapter HW-597:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    This adapter supports the 3.3V UART logic level required by ESP8266; in order to set it, you need to put a jumper on the VCC and 3.3V pins; however, we cannot power the ESP8266 from the 3.3V pin there because it has too low current efficiency, you need to connect the 3.3V LDO regulator to 5V from USB (it is also derived on the adapter). In addition, we need 10k resistors on RST and CH\PD. Details below:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    (it is worth knowing that, for example, in the case of another module with ESP, e.g. something from Tuya, you may need to put a pull-up resistor on IO0, but the board used in this topic already has this resistor on it, so I did not put it on the diagram )
    I made these connections on the breadboard:
    ESP8266 and Tasmota - controlling the WiFi relay step by step ESP8266 and Tasmota - controlling the WiFi relay step by step
    First, I checked the operation of AT commands. Firmware from AT is loaded by default on this module with ESP, but of course it will not work if we overwrite it. No need to short IO0 to test AT.
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    After sending the "AT" command, the module replied "OK", so the communication is working.
    Then it was time to upload Tasmota's input. Download from here:
    http://ota.tasmota.com/tasmota/release/
    On the computer side I used esptool.py installed with the Arduino IDE ESP support package:
    https://github.com/esp8266/Arduino
    I installed it via Boards Manager.
    The programming procedure here is as follows:
    - disconnect everything from the power supply (remove the plug from the USB port)
    - short the GPIO0 pin to ground
    - reconnect the power supply
    - wait a few seconds
    - disconnect GPIO0 from ground
    - start esptool
    Of course, instead of cutting off the power supply, you can simply use the RESET pin, but I prefer to describe the method without using the RESET pin because some ready-made smart modules/devices, e.g. from Tuya, do not have the RESET pin led out, so this method is more universal.
    At the beginning, the communication check itself - the command esptool.py chip_id . It should correctly detect the COM port itself and then the ESP module, its MAC, etc.
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    If everything is ok, you can now program. Command esptool.py write_flash 0x0 tasmota.bin (I put the .bin file in the same folder as esptool).
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Since then ESP has Tasmota's flash loaded (and will no longer support AT commands, AT flash has been overwritten)

    Step 2 - configuring Tasmota - WiFi configuration
    After restarting the module (remember to disconnect GPIO0 from the ground), the Tasmota WiFi network should appear:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    After connecting to this network, you can start configuring Tasmota, i.e. give the device the location of our WiFi. The device is always available on IP 192.168.4.1 (of course only temporarily, then it will get its IP via DHCP from our router):
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Here we also set the hostname of our device (hostname) which we can use to recognize it instead of the IP address.
    We save the changes:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    At this point, the WiFi network from Tasmota should disappear. However, our router should detect a new device.
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    192.168.0.107 is the IP assigned to ESP via DHCP by our router. This IP is variable, it may change in the future. The message "lease host name not found" means that my router does not support setting the client's hostname, it is not necessary, although it could be useful if, for example, we were using an IP scanner to determine what devices we have connected.

    Step 3 - configuring Tasmota - configuration of output pins
    From now on, the device is available in our WiFi network at the address 192.168.0.107. There you can specify what is connected to which ESP pin and how it is controlled:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    We open Configuration:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Then Configure Module:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    We can do two things here. You can set a specific type of board we have (this limits the number of available pins):
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    We can also set the role of specific pins (and there are many roles, everything is ready to use):
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    We can set a different number of relays (you just choose their index) and Tasmota will create buttons for them on the website itself, physical buttons from the system will also connect to them (by index).
    The system made in this way works - the state of the LED can be changed both with the button and through the website. After changing the status of the LED with the button, the page refreshes itself after a while:
    ESP8266 and Tasmota - controlling the WiFi relay step by step ESP8266 and Tasmota - controlling the WiFi relay step by step


    Step 4 - the rest of the layout
    It's up to the creativity of the user. You can etch the PCB yourself, you can order it from a tile shop, you can solder the whole thing on a universal drilled plate. The setup is simple, shouldn't be a problem. You just need to remember to keep the right distance between the paths connected to the network and the rest of the system. In the case of a drilled plate, it is worth removing the copper eyelets between them at all (pressing them lightly with the soldering tip will make them fall off). It is also worth using the sockets for the power supply module and ESP8266, to be able to remove them if necessary, e.g. to update the firmware (although it can also be done via WiFi).
    Wiring diagram:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    In principle, everything is the same as in Sonoff, so it is also worth getting acquainted with its scheme:
    ESP8266 and Tasmota - controlling the WiFi relay step by step ESP8266 and Tasmota - controlling the WiFi relay step by step
    There are also two voltages in Sonoff, 5V for the relay and 3.3V for the ESP. The relay is controlled by 2N7002.
    Tasmota's final configuration:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    During submission:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Almost ready:
    ESP8266 and Tasmota - controlling the WiFi relay step by step


    Step 5 - housing
    There are life-threatening voltages in the system, so it is even more important to think about the housing. I designed my case in Blender and printed it from PLA filament on the Ender 3 Pro 3D printer. Case design:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Folding:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Complex relay:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    The housing consists of four parts:
    - base
    - cover
    - additional cover for screw terminals
    - button (actually it's an "extension" that presses the tactile switch button)

    Step 6 - Static IP
    There is still the issue of the IP address of the device. By default, it is dynamically assigned by the router via DHCP, so it may change. It is not always what we expect.
    Fortunately, Tasmota can be given a static IP, i.e. one that does not change.
    This is done in the Tasmota console:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    We enter there: IPAddress 192.168.0.50
    The following response should appear in the console:
    Quote:

    13:19:26 CMD:IPAddress 192.168.0.50
    13:19:26 RSL: stat/tasmota_EC3F8F/RESULT = {"IPAddress1":"192.168.0.50 (192.168.0.107)"}

    Then you need to restart the device:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    From then on, the device will always have this IP address. If we were using MQTT and a broker, it wouldn't be needed, but here I show how to do without it.

    Step 7 - Phone application
    Normally, you would still need to put your MQTT broker, i.e. an intermediary between our phone and Tasmota devices, but to some extent it can be omitted.
    There are applications that allow you to directly control Tasmota devices.
    They differ from Blitzwolf, Smart Life, and other companies' applications primarily in that they do not require a connection to the manufacturer's server, i.e. they do not track, do not collect data, and we have control over everything.
    (Although in an ideal scenario, we should compile such an application ourselves from the source code, and not download it from, for example, Google Play).
    An example of such an application can be Tasmota Control:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    NOTE: DHCP on the router does not guarantee that the same device will always get the same IP. If we want to use this app in the long run, we need to configure it to have a static IP. Otherwise, you will pair the app with the device, and then a few days (or months) will pass and suddenly the device's IP will change to a different one ... an alternative solution is to refer to the device by its hostname. It can be used instead of an IP address.
    After starting Tasmota Control, we have three buttons to choose from:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    In Configuration, we add a new device. It only comes down to entering its IP address (and port 80, as communication takes place over HTTP). Nothing else is needed:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    The interface here is a bit strange, because first we have to click "Add", then find the just added device on the list, click "Edit" and there in "General" set the IP. But it's a beta version.
    From now on, in Devices we have a new device:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    And we can control it over the phone, of course, without the intermediation of third parties (without external companies that can collect data about us) and without the need to connect to the Internet, all within our one WiFi.

    Step 8 - presentation
    The video below shows the operation of the complex relay, along with its website for control (the one based on ESP) and the Tasmota Control application. You can see how quickly the relay reacts to the button and how quickly the applications show its new state:



    It is worth noting here how it works internally. Applications here must constantly ask ESP about its state, they use the HTTP protocol, and more specifically queries like:
    http://192.168.0.50/cm?cmnd=Status%200
    The same can be sent via the browser and see what Tasmota responds. Tasmota's response:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    This response is in JSON format.
    In the same way, applications control the relay. They send the command via HTTP:
    http://192.168.0.50/cm?cmnd=Power%20TOGGLE
    ESP responds to this:
    {"POWER":"ON"}
    Of course, we also have MQTT, but it is not used here.
    In addition, there is one conclusion - the open application (or website) actively queries the ESP about its state. It is the application that establishes the connection (as a client) and ESP only responds.

    Measurement of voltage, current
    I also measured how much current the circuit presented here draws, separately with the relay on and off.
    I made the measurement at the output of the 5V power supply (i.e. losses on it are not included), i.e. before the 3.3V MCP1702 regulator used.
    The output is stable 5V, even under load:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Then power consumption. With Relay ON:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    With off:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    The current consumption varies a bit depending on whether the ESP is transmitting. In the photos above are the largest recorded measurements I had, sometimes they were also lower. During the measurements, the ESP was connected to my WiFi network and I had its website open (i.e. ESP communicated on an ongoing basis and must have been broadcasting, because both the website and the phone application constantly query ESP about its status).
    I noticed that when the ESP is not transmitting at all, the current consumption drops from the 60mA shown to even 20mA.
    In summary, with ESP network activity we have:
    - with the relay switched on until 140mA at 5V, which is 0.7W (+ power supply losses)
    - with the relay switched off until 70mA at 5V, which is 0.35W (+ power supply losses)
    When ESP is not transmitting, power consumption drops. ESP isn't transmitting all the time, so it's probably basically using less than that 0.7W.
    The results of my measurements are consistent with the measurements for the classic Sonoff that I found on the web.

    Tip: How does Tasmota's page refresh work?
    We can also see how the refresh of the website of the Tasmota device is performed. To do this, we will view its source by typing in the browser:
    view-source:http://192.168.0.50/?
    In this way, you can view the HTML code of any website we visit.
    Tasmota's website code looks like this:
    [syntax=html]





    Tasmota - Main Menu

    var x = null,
    lt, to, tp, pc = '';

    function eb(s) {
    return document.getElementById(s);
    }

    function qs(s) {
    return document.querySelector(s);
    }

    function sp(i) {
    eb(i).type = (eb(i).type === 'text' ? 'password' : 'text');
    }

    function wl(f) {
    window.addEventListener('load', f);
    }

    function la(p) {
    var a = '';
    if (la.arguments.length == 1) {
    a = p;
    clearTimeout(lt);
    }
    if (x != null) {
    x.abort();
    }
    x = new XMLHttpRequest();
    x.onreadystatechange = function() {
    if (x.readyState == 4 && x.status == 200) {
    var s = x.responseText.replace(/{t}/g, "").replace(/{s}/g, "").replace(/{m}/g, "").replace(/{e}/g, "").replace(/{c}/g, "%'>

    Cool? Ranking DIY
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 5827 posts with rating 5812, helped 278 times. Been with us since 2014 year.
  • #2
    khoam
    Level 42  
    p.kaczmarek2 wrote:
    It is also worth noting that the GPIO pin number does not always correspond to the pin number on the used board.

    The numbers on the board, i.e. Dx, are an alternative numbering method for the Arduino Core - these are constants to which the appropriate GPIO numbers are assigned.
    Code: c
    Log in, to see the code

    p.kaczmarek2 wrote:
    Only the static IP is important, otherwise our IoT device will be "lost" or even switched places with another device.

    Not necessarily. An IoT device can be identified by its DNS name, regardless of its IP address. This is what the WiFi.hostname() command is for: link
  • #3
    p.kaczmarek2
    Moderator Smart Home
    khoam wrote:

    p.kaczmarek2 wrote:
    Only the static IP is important, otherwise our IoT device will be "lost" or even switched places with another device.

    Not necessarily. An IoT device can be identified by its DNS name, regardless of its IP address. This is what the WiFi.hostname() command is for: link


    You're right, of course you can also just use the hostname, it solves the IP problem. In the phone application, you can also give the hostname instead of the IP, here:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    and this solves the matter, although I haven't tested it yet, because something about my router has a problem with hostnames (in the logs there is lease host name not found and the IP scanner does not detect hostnames of devices from ESP, though my NAS does. I'm just going to sit down to it and see what's wrong.
  • #4
    Bazic
    Level 15  
    A very detailed topic. I like. And there was no need to provide additional protection in the form of a clause about the advanced ones ;)
  • #5
    p.kaczmarek2
    Moderator Smart Home
    Next month I will also post a detailed description of uploading your own firmware via WiFi for any IoT device purchased in the store based on the ESP module from Tuya. So tuya-convert. Then you do not need to have any programmer or even open the housing of the device.
  • #6
    h3c4
    Level 14  
    That you wanted to create such a "core" with ready-made solutions available.
  • #7
    p.kaczmarek2
    Moderator Smart Home
    This is just for the presentation to show that Sonoff is not a "magic black box".
    Even now I have a normal clone of Sonoff from Blitzwolf (BW-SS1):
    ESP8266 and Tasmota - controlling the WiFi relay step by step
  • #8
    Stanley_P
    Level 27  
    p.kaczmarek2 wrote:
    Only the static IP is important, otherwise our IoT device will be "lost" or even switched places with another device.

    Just out of curiosity, I'm asking, because I'm at the beginning of the introduction of figuring out all these ESPs, smart sockets, etc.: don't "our IoT devices" have a unique MAC address? Most, if not all, routers have the option to reserve a fixed IP address based on the MAC address (usually around the DHCP configuration option).
  • #9
    xury
    Automation specialist
    Of course, each device must have a unique MAC and that's what I use.
  • #10
    p.kaczmarek2
    Moderator Smart Home
    And that's a good idea too. Of course, they have different MACs, but still the solution with the hostname is probably a bit better, because it can be set so that it tells us what role a given ESP plays.
    Assigning IP to MAC can be easily set up on the router as you say:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    MAC is seen by both router and Tasmota settings:
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    And besides, it can be changed to ESP with a function esp_wifi_set_mac .
  • #11
    khoam
    Level 42  
    p.kaczmarek2 wrote:
    And anyway, on ESP it can be changed with the function esp_wifi_set_mac.

    Such a command is available for ESP32. In the case of Arduino Core for ESP8266 it is a command wifi_set_macaddr ().
  • #12
    JarekC
    Level 32  
    Does the presented configuration allow for control from anywhere in the world or only from the local network?
  • #13
    xury
    Automation specialist
    As a rule, Tasmota works in a local network. If you want to control from outside the network, you should use redirections, clouds or VPN depending on the availability of possibilities.
  • #14
    p.kaczmarek2
    Moderator Smart Home
    I think it would be most convenient to do it already having the MQTT Broker placed. I'm going to describe it in the future.
    ESP8266 and Tasmota - controlling the WiFi relay step by step
    Port forwarding may also be useful (on our router and on the one from the ISP)

    although when I was playing 100% DIY controlling relays via Ethernet on PIC18F67J60, I had a panel available from all over the world via the Internet without an external IP at home, tunneling, etc. ... I just set up a website for FREE on a Polish hosting site and wrote there, my PHP script allowed everyone to control, while the PICs only queryed it for a state every second and then took that state. Of course, the reaction time (switching on the light, e.g. remotely) was a second or so, but it's not a problem.
  • #15
    JarekC
    Level 32  
    I would love to read/watch a guide on how to remotely configure and control devices a little more complicated than the on/off type.
    Perhaps I will implement a project of expanding or redesigning the device, but there are dozens of different parameters, switching schedule, etc., a total of about 1kB of data.
    There may be dozens of devices scattered all over the country.
  • #16
    hedo77
    Level 11  
    Hi,

    JarekC wrote:
    I would love to read/watch a guide on how to remotely configure and control devices a little more complicated than the on/off type.
    Perhaps I will implement a project of expanding or redesigning the device, but there are dozens of different parameters, switching schedule, etc., a total of about 1kB of data.
    There may be dozens of devices scattered around the country.


    For this I recommend Node-RED and MQTT. The sensor reports to the MQTT broker from which Node-RED retrieves values and performs a specific action.. I think Node-RED is on par with PLC controllers, it is mega flexible and simple ...

    Two channels from Polish youtube should help..
    ArturHome
    House Engineer
  • #17
    Karol966
    Level 31  
    I intended to write a PM to the author about the basics, where to start, and here I am, he has already written :)

    The question is, has anything changed since then? I want to finally delve into the world of wifi modules, control via www. I built projects based on GSM modules, I sent data there via http post/get. For ESP it is still a black box for me because everything is based on ready-made libraries for Arduino and so far I don't know how it works but I'm starting to think that it's the same way, i.e. via http - right?

    I would like to build my first simple project, use any ESP module and add to it, for example, 8 digital inputs and 8 digital outputs (e.g. through expanders) plus the standard ds18b20. Then write applications for the phone (it can be from the browser level only launched full screen so as not to launch it like a normal page). How to go about it? I mean the method of communication (for now definitely on the local network)