Skip to content

note 5: Watchdog for IOT

Marco Sillano edited this page Jan 3, 2024 · 92 revisions

The problem

It is normal for some devices to disconnect. They normally reconnect on their own after a few seconds. The cause may be a weak WiFi signal, or temporary disturbances (e.g. microwave oven). Other times the disconnection is physiological, e.g. a power plug removed from its socket. To identify extreme cases, see table 'offline' and to handle the normal cases on applications see dynamic retry.

However some devices, in the event of an error, hang up, disconnect and never reconnect on their own. This can be very dangerous in a remote unattended system, especially in the case of routers or gateways.

A possible solution is to use a relay that momentarily interrupts the power supply to the device: on the restart, usually, the device should reconnect.

The relay command must be provided by a watchdog, which triggers it when the device has been disconnected for too long. Let's limit ourselves to devices with USB power supply (5 V), we still have various options.

Use cases

  1. Initial configuration: setup parameters.
  2. Automatic operation: watchdog connection status and DC output cutoff for a few seconds.

Hardware

5V Battery Boost Module Uninterruptible Power Supply (optional UPS) see seller

  • Charge and discharge at the same time.
  • output: 5V, 1000 mA max.
  • 18650 Battery 3.7 V, 2000 / 3000 mAh
  • A) use a Tuya relay, with the control logic implemented via share in tuyaDAEMON:

    Example, see info - €10.43

    PROS:

  • Quick, the device is ready to use in tuyaDEAMON.
  • Whatcdog logic implemented in tuyaDEAMON using share: Different logic can be used in the various cases.

  • I already use this relay as tuyaTRIGGER (see here) so the relay is free and still available for this application.

    [added Oct. 2023] Switch USB info
    PROS:

  • Works 5-12 V
  • Quick, the device is with a case and ready to use in tuyaDEAMON.

  • B) use an ESP01 relay (schematic), programming it to use MQTT (or REST) via WIFI. In this case, the logic can be hardwired into the firmware, simplifying interventions in tuyaDEAMON.

    Example, see seller - €1,73

    PROS:

  • Smaller and cheaper than solution A.
  • Many firmware programmation alternatives (an 'ESP' program board required).
  • Possibility to choose the connection with tuyaDAEMON: MQTT, HTTP, etc...
  • Whatcdog logic implemented in firmware: in tuyaDEAMON required only the `_connected` echo.
  • I also made this version, because I wanted to use these versatile relays for some time and practice ESP01 programming.


    Watchdog01 - Tuya relay

    A simple implementation in a plastic box.

    The relay is used in reverse mode, i.e. when in 'ON' it cuts the output DC: it is wired using the NC terminal. Of course, the relay startup status is set to 'OFF'.

    tuyaDEAMON shares

    Two new DPs are needed to implement the watchdog logic:

                {
                        "dp": "_connected",
                        "capability": "SKIP",
                        "share": [{
                                "test": ["msg.info.value == false" ],
                                "action": [{
                                        "device": "_system",
                                        "property": "_timerON",
                                        "value": {
                                            "timeout": "60000",
                                            "id": "watchdog",
                                            "alarmPayload": {
                                                "device": "BLE MESH(SIG)Gateway",
                                                "property": "_watchdog",
                                                "value": "test now"
                                            }}}]
                                  }]
                    },
                    {
                        "dp": "_watchdog",
                        "capability": "SKIP",
                        "share": [{ 
                               "test": ["tuyastatus['BLE MESH(SIG)Gateway']['_connected'] == false" ],
                               "action": [{
                                      "device": "tuya_bridge",
                                      "property": "relay",
                                      "value": "ON"
                                    },{
                                      "property": "_connected",
                                      "value": false
                                    }]
                                 }]
                    }
    
    
    • These two DPs are added, in my tuyaDAEMON Global.alldevices, to the BLE MESH(SIG)Gateway device, whose power is controlled by the watchdog relay (i.e. the device tuya_bridge).
    • The share of the first DP, when the Gateway device disconnects, starts a timer, with the observation time.
    • The second share is activated when the timer expires, and, if the device is still disconnected, it cuts the DC power and retriggers another period of observation.
    • Power is restored automatically when the inching period expires.

    So 3 timings are involved:

    1. Tuya connection cycle time (as Interval for retry + Interval for find (+ exec_time) in tuya-smart-device node parameters): for a critical device use the 'static retry' subflow and a fast cycle e.g. 10s.
    2. Watchdog intervention time (defined by value.timeout [ms] in the _connected.share, e.g. 100s.
    3. Power outage, to allow capacitors to discharge in the device, e.g. 4s (device dependent), set as 'switch inching time' in the 'tuya_bridge' relay configuration (DP 19).

    note: ESP-01 programming

    ESP-01 ESP-01S Programmer Adapter CH340 see seller - €1,06

  • Required to program ESP-01 (3.3 V) using USB.
  • Install the USB serial CH340 driver (see here for Windows, and also here for MAC, Windows, Linux, Android).
  • There are many options to program the ESP-01 (ESP8266):

    1. Use the native ESP01 firmware (ESP-AT by Expressif) and provide an MCU (e.g. Arduino) that sends the required AT commands. In this way, the ESP01 module without modifications is a communication-only device.
    2. Change the firmware of the ESP-01 module to a more flexible global solution, for example using the nodeMCU. Once installed, you can add scripts in 'Lua' (see the ESPlorer IDE, also usable with AT) to get the required behavior.
      Instead of NodeMCU you can use the simpler ESP_MQTT by Martin-ger that uses its script language. Very good solution and I have used it in many projects with Sonoff switches (e.g. see the 'PDM Ozone timer' alone, and the tuyaDEAMON integration). Martin-ger provides the ESP_MQTT binaries, so you only need to flash it.
    3. A fairly quick solution consists of selecting and assembling modules ready to obtain the standard behavior on your HW device. This is how the Tuya development environment works, but Tasmota and ESPeasy also use this philosophy. A standard solution that requires extensive customization when the project includes custom logic. Caveat: It takes some initial effort to get used to these solutions.
    4. The most powerful and flexible solution is certainly the 'classic' one: create your own firmware in 'c' with Arduino IDE (or PlataformIO, or ESP-IDF). These guarantees that if something can be done with ESP01, you can achieve it. It also allows you to better exploit memory limits (500K or 1M) by inserting only the required functions and optimizing the code. While some programming background is required, the many examples and libraries available can help.
    5. note: the ESP01 board uses only one I/O pin. GPIO0, but there are expansion techniques to 4 pins and hardware expanders I2C to 8 and more digital I/O.
    Tool/Firmware Pros Cons Rating Recommended for
    Arduino IDE Easy to use, beginner-friendly. Wide range of libraries and tutorials available. You build firmware in C. 5/5 Beginners, hobbyists, advanced users
    ESP-IDF Provides full control over the ESP8266, designed for advanced users. Usable alone or as an Eclipse add-on. Can be difficult to learn and use. 4/5 Advanced users, developers
    PlataformIO Complete solution for Microsoft Visual Studio Can be difficult to learn and use. 4/5 Advanced users, developers
    Sming Open source framework for the creation of embedded C++ applications Can be difficult to learn and use. 3/5 Advanced users, developers
    ESP8266 Download Tool (win) and ESPTool Bootloader tools, can be used with other ESP8266 boards. You must provide the firmware in binary form. 4/5 Beginners, hobbyists, advanced users
    ESP_AT Firmware from Expressif, to add communication capabilities to an MCU Very specific use 3/5 Advanced users, developers
    ESP_MQTT Firmware, simple to use, specifically designed for MQTT communication. Custom script language event-driven. 4/5 MQTT projects
    NodeMCU Firmware, with a wide range of features and functionality. Lua metalanguage makes it easy to write scripts. Not as open-source as other options. Binary built 'a la carte'. 3/5 IoT projects, home automation
    esp8266basic Firmware, with a wide range of features and functionality. Basic programmable with WEB interface Not as open-source as other options. 2/5 Basic projects
    homie-esp8266 Firmware, MQTT using Homie protocol. Not as many features as other options. 2/5 Homie projects
    Tasmota Open-source, highly customizable. Can be used with a variety of hardware. Can be more difficult to learn than other options. 3/5 Hobbyists, advanced users
    espruino Simple to use, based on the JavaScript programming language. Limited functionality. 3/5 Beginners, hobbyists
    Epurna Open-source, for PlatformIO. Can be more difficult to learn than other options. 2/5 PlatformIO users
    esp-homekit-devices For Apple Homekit. Not as customizable as other options. 2/5 Homekit users
    ESPHome Easy to use, specifically designed for Home Assistant. Not as customizable as other options. 3/5 Home Assistant users
    ESPeasy Easy to use for beginners, provides a graphical user interface. Not as customizable as other options. 3/5 Beginners
    MicroPython Easy to learn and use, based on the Python programming language. Better with 2MB flash memory. Not as many features as other options. 2/5 Advanced users, developers

    Watchdog02: ESP01 relay + ESP-MQTT

    note: Upload the ESP01 firmware using bootloader
    When the binary code files are available (NodeMCU, ESP_MQTT, ESP_AT, etc.) the simplest way (windows), using ESP8266 Download Tool, is detailed here.

    As a less easy alternative, the use of ESPTool is detailed here and in many tutorials. Useful info also in this Sonoff project timerPDM.

    basic ESP-MQTT use

    1. Flash the ESP-MQTT firmware into the ESP01.

    2. Using Arduino 'Monitor Serial' @115200, or a terminal, do the basic configuration:

         CMD>set ssid <router_name> 
         CMD>set password <router_pass> 
         CMD>set ap_on 0 
      
         CMD>save
         CMD>reset 
      

      After restart ESP01 will connect itself to <router>, now you can access it:

      • Serial via USB, using the ESP Programmer adapter
      • Via Telnet (OTA: using 'Putty' or 'TCP Telnet Terminal' app, port 7777)
    3. The easiest way to load a script into ESP-MQTT in OTA mode is to use a WEB server.
      In development, it is convenient to have the local server on the main PC. In my case (Windows) I used the portable server wpp_pampa contained in a USB stick "winPenPack". Otherwise, you can use any WAMP/LAMP/MAMP server or even a remote server on the Internet, for example by downloading directly from Github. For Linux see here.
      In any case, the ESP01 must be connected to the router to reach the WEB server. The command is:

                 CMD> script <url_to_script>
      
    4. The file with the 'script' code is the watchdog.cde file
      For my (and your) convenience, the command to use on the console is written at the head of the script file: modify it and adapt it to the configuration used, then copy-paste.
      This implementation exposes 2 configuration DPs (_delay, _cutoff) and 2 DPs for use (_dev_conn, _outDC).

    % watchdog script for ESP01 relay + ESP-MQTT (https://github.com/msillano/tuyaDEAMON-applications/wiki/note-5:-Watchdog-for-IOT) (C)2023 m.sillano, CC:BY+NC+SA license
    % CMD>script http://192.168.100.116:8888/cde-server/watchdog.cde
    % Minimal static configuration:
    
       config ap_on 0
       config speed 160
    
       config @1 3000       % default cutoff time
       config @2 60000      % default test time
    
    % MQTT messages:
    % watchdog02/set/dev_conn     0|1  SET
    % watchdog02/set/cutoff       100..10000..   SET; SET(<100) -> GET (event)
    % watchdog02/set/delay        100..180000..  SET; SET(<100) -> GET (event)
    % watchdog02/set/switch       0|1 (0: fired) SET; SET(> 1)  -> GET (event)
    % watchdog02/event/xxx        response (GET)
    
    on init
    do
      setvar $GPIOn = 0   % GPIO used
      gpio_out $GPIOn 1
      setvar $relay = 1
      setvar $dev_conn = 1
    
      subscribe local "watchdog02/set/#"
      publish local "watchdog02/event/switch" $relay retained
    
    on topic local "watchdog02/set/dev_conn"
    do
        if  2 > $this_data then
           setvar $dev_conn = $this_data
           if  1 > $this_data then
              settimer 2 @2
              endif
           endif
    
    on topic local  "watchdog02/set/delay"
    do
       if $this_data > 99 then
           setvar @2 = $this_data
           endif
        publish local "watchdog02/event/delay" @2
    
    on topic local  "watchdog02/set/cutoff"
    do
        if $this_data > 99 then
           setvar @1 = $this_data
           endif
        publish local "watchdog02/event/cutoff" @1
    
    on topic local  "watchdog02/set/switch"
    do
        if  2 > $this_data then
             gpio_out $GPIOn $this_data
             setvar $relay = $this_data
             endif
          publish local "watchdog02/event/switch" $relay
    
    on timer 1
    do                 % end cutoff
    setvar $relay = 1
    gpio_out $GPIOn $relay
    publish local "watchdog02/event/switch" $relay
    
    on timer 2
    do                % dev_conn? not: start cutoff, restart
    if  1 > $dev_conn then
       setvar $relay = 0
       gpio_out $GPIOn $relay
       settimer 1 @1
       settimer 2 @2
       publish local "watchdog02/event/switch" $relay
       endif
    
    
  • The script file must be ASCII encoded (not UTF8!) otherwise incomprehensible errors are reported.
  • The values of the MQTT messages are a bit particular, compared to the usual, due to the difficulty of managing the NULL value in the script. It will be up to the `node-red driver` to carry out the appropriate conversions.
  • From TuyaDEAMON's point of view, the Watchdog02 device is defined by its info.
  • TuyaDAEMON update

    The 'driver_watchdog02' is implemented as a subflow (file driver_watchdog02.json), and converts the standard TuyaDAEMON messages to/from MQTT for the ESP01:

    1. The device _watchdog02 is added to CORE_devices flow as usual, using 'driver_watchdog02' as direct replacement of 'tuya-smart-device' node.

    1. You must add to global.alldevices the JSON fragment for _watchdog02, from the zip file, in the real section.

      Also a share is required for the controlled device, to send the connected status:
                  {
                        "dp": "_connected",
                        "capability": "SKIP",
                        "share": [{
                                "action": [{
                                        "device": "_watchdog02",
                                        "property": "_dev_conn"
                                    }]
                            }]
                    }
    

    Watchdog03: ESP01 relay + Arduino

    note: Install Arduino IDE

    note: Upload the ESP01 firmware using Arduino IDE

  • Install, if required, the USB serial CH340 driver (see here for Windows, and also here for MAC, Windows, Linux, Android).
  • Connect the serial adapter and ESP01 to your computer USB port, on the Arduino IDE set ( 'Tools > Port'). note: In Windows see at 'Device manager > Ports (COM and LPT)' else look for `tty.wchusbserial`.
  • Select `Generic ESP8266 Module` as your board (Tools > Board > Generic ESP8266 Module)
  • Load the sketch Examples > ESP8266 > Blink for a test.
  • Hit the upload button. After the LED on ESP01 will blink.
  • On MAC, in case of error ("Symbol not found: _getentropy") see here Ver. 2.5.0 worked for me.

    For more details, you can see here

  • Basic WiFi Arduino

    It is possible to follow the architecture of watchdog02 with Arduino. for example using the PicoMQTT library, but I chose to implement a WEB server, therefore with an HTTP interface.

    PROS:

    • Simple to implement, with many connection options, including any browser on a phone, in the same local network.
    • Can use standard TuyaDAEMON messages, without the need for conversions or drivers.

    CONS:

    • It is possible to implement GET, SET(value), and SET(null), but not PUSH: The initiative must always be from the client (TuyaDAEMON). In particular, there are no messages in tuyaDEAMON when watchdog03 intervenes in a timed manner.

    HTTP communication to/from a WEB server can be accomplished in several ways. In this device I have implemented various alternatives, to make the device more versatile and for testing, using the GET method (available GET, PUT, POST, PATCH, or DELETE):

    1. REST interface
      A command is simply added to the server's base URL. Example:
       http://192.168.100.124:8083/H
    

    It is simple to use this method from an HTML page, for example by associating a button:

       <input type=submit value=ON style=width:100px;height:45px onClick=location.href='/H'>
    
    1. URL-encoded parameters
      The values are in pairs, key=value, appended to the URL with a '?' and concatenated with '&'. This is the native format of HTML forms. Example:
       http://192.168.100.124:8083/?delay=10000&cutoff=3000
    

    In an HTML page, we can use the 'form' tag and the various 'input' tags. Example:

       <form action='.' method='GET'>
          <input type='text' id='delay' name='delay' size='10'  value='tdelay'> <p>
          <input type='text' id='cutoff' name='cutoff' size='8'  value='cutoff'> <p>
          <input type=submit value='save'>
       </form>
    
    1. URL-encoded from JSON
      The node HTTP request can transform an object in msg.payload in parameters URL-encoded. Example (SET):
    msg.payload {
       devId: "60a453fffe*****",
       dps: "_dv_conn",
       set: false
       }
    

    Becomes

       http://192.168.100.124:8083/?devId=60a453fffe*****&dps=_dv_conn&set=false
    

    The difference with the previous standard case is that now more parameters are needed even for a single SET: dp and value.

    1. Result as HTML page Using a browser, as output you can create an HTML page that contains both the current state and the interface for editing it.

    2. Result as JSON The node HTTP request can parse an HTTP response JSON from the ESP01 in an object in msg.payload. Example:

       {"deviceId":"_watchdog03", "data": {"dps":{"_dv_conn": false}}}
    

    Becomes a standard tuyaDAEMON message:

       msg.payload{
           deviceId: "_watchdog03",
           data {
              dps {
                  _dv_conn: false
                  }
              }
          } 
    

    See also here https://randomnerdtutorials.com/esp32-http-get-post-arduino/

    The other details of the implementation are very simple, the configuration values are hardwired into the code. The code file is 'watchdog03.ino'.

    Possible extensions:

    • Using the EEPROM library you can use persistent configuration variables. This is convenient, for example, in the case of an initial configuration HTML page (WiFi: ssid, passw), useful if the device is configured by default as an AP with a known name or address: the configuration of the device can be done with a browser. Search among the numerous published WiFiManager libraries for ESP8266.
    • _The watchdog03 solutions (i.e. HTTP) can be implemented also using ESP_MQTT.-

    Some code examples:

    TuyaDAEMON update

    1. The device _watchdog03 is added to CORE_devices flow as usual, using the 'http request' node as a replacement of 'tuya-smart-device' node. 'http request' configuration:
      • Method: GET
      • URL: http://192.168.100.124:8083/ (defined in watchdog03.ino)
      • Payload: Append to query-string parameters
      • Return: a parsed JSON object

    1. You must add to global.alldevices the JSON fragment for _watchdog03, from the zip file, in the real section.

      Also, a share is required for the controlled device, to send the connected status:
                   {
                        "dp": "_connected",
                        "capability": "SKIP",
                        "share": [{
                                "action": [{
                                        "device": "_watchdog03",
                                        "property": "_dv_conn"
                                    }]
                            }]
                    },