ENG | RUS
The ESP32 r4sGate gateway in a minimal configuration (only ESP32 or ESP32C3 with a 3.3v power supply) allows you to connect BLE-compatible Redmond devices, Xiaomi MiKettle kettles and some other devices to the smart home system (Home Assistant, OpenHab, ioBroker, MajorDoMo, etc. ) via MQTT protocol. Initially, the project was only for Redmond, now other devices are being added. Hence the plus in the name of the project.
Electric kettles:
Multicookers
Coffee makers
Coffee machines
Sockets
Electric convectors
Climate stations
Humidifiers
Sensors
Irrigation controllers
Curtain/blind drivers
The gateway supports 5 simultaneous BLE connections. Device management is also possible from the gateway web interface. The web interface is simply protected with a password from Raerten. The login and password are output to the log when the gateway starts.
Home Assistant Mqtt Discovery is supported. To enable it, you need to check Hass Discovery in the settings. It is possible to delete all data created by the gateway in Mqtt and devices in Home Assistant. To do this, select the Delete Mqtt topics option in the Setting tab and then click Save setting . After rebooting the gateway, only devices connected to the gateway will be recreated. Recommended when connecting the gateway for the first time and reconfiguring by removing devices.
The BLE Gateway Monitor can be used to monitor up to 24 BLE beacon devices with a static MAC address. The presence/absence of a tag (beacon) and rssi is displayed. Supported are BLE beacons of the Home Assistant application on smartphones (binding by uuid), LYWSD02 watch with thermometer, LYWSD03MMC Xiaomi Mijia 2 thermometers with original firmware, firmware from atc1441in [custom mode}(https://github.com/atc1441/ATC_MiThermometer) and firmware from pvvx in custom mode, Xiaomi Mi Scale, Qingping Air Monitor Lite (CGDN1), Elehant counters, as well as Samsung Smart Tag.
There are 10 I/O ports, 5 of them can be used to control external devices (Out mode) and read their status (In mode). Three ports can be configured as buttons for turning on/off connected BLE devices (Sw mode, in which case the state of the buttons in mqtt is not displayed), the fourth port can be configured as a button for updating the image from the camera. When configured in input mode, pullup is enabled if possible (pin number less than 34). Another 2 ports are used by the I2C bus, and each of the 3 remaining ports can be used as a pulse width modulated (PWM) output, or as an input for connecting either one DS18B20 sensor with direct power supply, or one DHT22/AM2302 sensor (ports 7 and 8). Reading procedures are simplified, the checksum is not read or verified, and data is rounded to one decimal place. If the gateway is equipped with an audio emitter, then by connecting the PWM output to it (in m5stack basic this is gpio 25), you can output an audio signal. By changing the duty cycle of the pulses, you can adjust the volume. The frequency is fixed and equal to 3.136 kHz. The I2C bus supports sensors SHT3x/SHT4X(addresses 0x44, 0x45), AHT20(0x38), HTU21(0x40), BMP280/BME280/680/688(0x76, 0x77, 688 not yet tested), SGP30(0x58), SGP4x(0x59 ), SCD4x(0x62), as well as RTC DS3231(0x68) and battery controller IP5306(0x75). It is possible to save the SGP30 calibration data in NVRAM and restore it when the gateway starts. To do this, check the AQ base item in the settings. The Sensirion library is used to calculate VOC in SGP4x. The clock is used to store the date and time from the NTP server, the temperature sensor is output to Mqtt. The IP5306 controller is installed in m5stack and ttgo-t4 (SCL 22, SDA 21), allows you to determine the battery level in 25% increments and its mode (Discarging / Charging / Charged). When on battery power, the screen brightness decreases by 16 times. The gateway also supports the AXP192 power controller and PCF8563 RTC, allowing it to run on the M5Stack Tough, and also supports the ADC of the HX711 load cells. The measurement result from the HX711 can be displayed either in kilograms or as a percentage, depending on the calibration. HX711 is polled at intervals of 4 seconds, other sensors at intervals of 12 seconds. The gateway allows hot plugging of all sensors. Sensors 18B20 and DHT22 appear in Mqtt and Home Assistant immediately after the gateway starts, even if they are not connected, and I2C sensors as they are detected on the bus within 2 polling cycles (24 seconds).
In order to expand the capabilities of the gateway, it is possible to connect a 320 * 240 TFT screen on ili9341, ili9342 and ST7789 chips. The screen displays the current time, date, as well as temperature, voltage and current in the house (not a problem when powered by a generator), status (blue - off, red - on) and temperature at the boiler outlet, temperature and humidity outside. Everything comes from Mqtt. Next to the date, the status of BLE devices is displayed in color, 1 ... 5 - from the first to the fifth. Gray - not connected or not defined, blue - off, red - on, yellow - heating, white - program installed. More detailed status and some parameters of connected BLE devices are displayed in turn in the bottom line. It is possible to periodically or on request display a picture on the screen in jpeg format, for example, from a camera. Pictures with a horizontal resolution higher than 320 are displayed at a scale of 1:2. The buffer size for loading images can be changed within 20-65 kilobytes. Screen brightness can be changed by Mqtt. You can also display the weather in text form from the site wttr.in or just text by writing it in the Mqtt topic r4sx/jpg_url. The result was something similar to a clock with a thermometer. It is enough to look at the screen to make sure that everything is in order in the house or on the street today.
Picture 1. Components for assembling the gateway.
If the goal is to launch the gateway at minimal cost, you will have to buy spare parts and then assemble the gateway from them. I used an ESP32 WROOM ESP-32 4 MB with a built-in antenna (bottom left) or an ESP32 WROOM ESP-32U 4 MB with an external one (to the right of the first) . Issue price $2.5. Then I soldered the microcircuit onto an adapter board ($0.3) and then onto a breadboard. ESP32C3 is also suitable, I have ESP32C3-12F. Due to hardware limitations of this chip, the gateway uses port 8 only as a pulse width modulation (PWM) output. There is approximately 28 kilobytes more free RAM. And even with the screen connected, the ESP32C3-12F still has 6 free gpio. Power supply for 3.3 Hi-Link ($2-$4). I bought them for $1.65. You can avoid soldering if you use esp32-wroom-devkit (bottom center, $14). True, this board is very redundant for the project; you can get a simpler one for $3.54. In it, esp32 comes along with a board on which there are also converters from 5V to 3.3V, USB-RS232 and a standard mini-USB connector. Through it you can power the esp32 using a five-volt charger from a smartphone, and program it directly from the computer without any adapters. And on the right in the photo is a 3.2" 320 * 240 TFT screen ($18) , which I used in the gateway. You can also use compatible ready-made devices both with a screen ( TTGO T-Watcher BTC Ticker , M5Stack BASIC , M5Stack Tough ) and without ( m5atom lite).
To run the gateway you need to program the ESP32. You can use the flash_download_tools program. The file fr4sGate.bin in the build folder is an already assembled binary for esp32 @160MHz with 4 MB memory, DIO bootloader and is flashed with one file from address 0x0000 in DIO mode . If the DIO bootloader does not start, you can use the fqr4sGate.bin file with the QIO bootloader and program it in QIO mode. As I understand it, most esp32 can be programmed in any mode, but there were cases that the gate only worked when it was flashed with the fqr4sGate.bin file in QIO mode . The r4sGate.bin file is used to update the firmware via the web interface. ESP32C3 programming files in folder C3.
Then you need to create an access point on your smartphone with ssid r4s and password 12345678 , wait until the esp32 connects to it, find the connected r4s0Gate device and its IP in the access point parameters, enter this address in the web browser and then set it in the web in the Setting tab other parameters. After which the access point is no longer needed. Esp32 will only attempt to connect to the r4s network if the main network is unavailable, for example if the password is incorrect. If it cannot connect to the guest network, the esp32 reboots. The option with a guest network, in contrast to the generally accepted practice of launching an access point on esp32, is, in my opinion, more convenient, since if for some reason the Wi-Fi router fails (and it can be dedicated only for iot devices), the rest of the Wi-Fi does not get clogged by floating esp32.
Next, you need to enter the name or MAC address of the Redmond device and bind it to the gateway. The search for devices starts only when there is at least one identified but not connected device, or the BLE monitor is active. If the name of the device is not known exactly (and Redmonds do not always light up via BLE as a one-to-one model), then to start scanning you need to enter any name in the Name field in the settings, and then replace it with the one found during scanning and select the closest device type in the settings (field TYPE , for example, for teapots from RK-G(M)200S to RK-G(M)240S the protocol is the same, you can select both RK-G200S and RK-G240S). It should be taken into account that not all devices transmit the name during passive scanning (for example, Xiaomi Mikettle, AM43 Blinds). In any case, it is better to enter the MAC address in the name field, either with or without colons. You can find and copy the address BLE Last found name/address on the main page or on the BLE monitor page. Next, to bind, you need to press and hold the button + or *power on a kettle or a timer on a multicooker until the device enters the binding mode and after a while connects to the gateway. AM43 blinds also require entering a PIN code (Passkey) for connection.
It is possible to connect several gateways to one MQTT server. To do this, you need to install your own r4sGate Number in each gateway. Gateway number 0 will write to topic r4s0/devaddr/..., gateway number 1 - r4s1/devaddr/..., etc. You just need to take into account that the authorization request when binding depends on the gateway number and the connection number in the gateway. This allows you to link 2 identical kettles or multicookers to 2 different gateways or to 2 different connections within the same gateway. If there are two gateways with the same parameters working nearby, connected to different smart home systems (for example, a neighbor behind the wall), to exclude the possibility of connecting a device to the neighbor’s gateway, you can use the option to authorize devices using the gateway’s MAC address by selecting Use MAC in BLE Authentication in the settings. Then reset all bindings on the devices and then bind them to the gateway again.
To connect to the Mqtt broker, you need to enter its address and port, as well as login and password. If the gateway works with Home Assistant paired with a mosquitto broker, you should use the Hass Discovery option. Before using it, I recommend deleting all topics with r4s in the Mqtt broker, for which select Delete Mqtt topics in the settings.
Devices can also be managed in the gateway web interface. Examples of the main page and settings page are below in pictures 6 and 7.
Picture 6. Home page.
Picture 7. Settings page.
The monitor allows you to track tags (beacons) with static MAC addresses. The presence/absence of a tag and rssi are displayed.
Additionally supported:
To activate the monitor, in the Setting tab , set the BLE Monitoring option to Active or Passive for active or passive scanning and click Save setting. An active scanner provides more information, but uses more power on the devices being scanned. Passive mode is recommended for tag scanning. It should be taken into account that in Auto mode , when searching for devices before connecting, the scanner always works in active mode, and then switches to passive mode.
After installing the option, the BLE Monitor tab will appear in the menu , by opening which you can see the found devices. The Gap field displays the time interval between the last two received packets, and the Last field displays the time since the last packet arrived. The default timeout (if the Timeout field is empty) is 300 seconds, after which the device is considered lost and its data is deleted from the table. This line can later be overwritten with data from another device. To output data to Mqtt, you need to enter a non-zero value in the Timeout field and confirm the entry by clicking Ok. All values will be saved in non-volatile memory, and Mqtt Discovery will transfer everything to Home Assistant. Although scanning is ongoing, when setting the Timeout value , you need to take into account that maintaining connections also requires time, during which packets may be missed. Unnecessary data in Mqtt and Home Assistant can be deleted by selecting the Delete Mqtt topics option in the Setting tab when the BLE monitor is turned on and clicking Save setting.
Samsung Smart Tags that are not linked to a SmartThings account are not suitable for tracking, as they turn off after a few minutes. The advertising message of Smart Tag associated with the SmartThings account contains a static UUID 0xFD5A, a dynamic MAC address and an encrypted identifier, due to the presence of the RND byte in it, it changes along with the MAC address. The remaining fields (status, advertising message counter, region, battery status) are not unique. The advertising message also contains a digital signature. Standard BLE trackers, as far as I know, are able to recognize the presence of these tags by UUID, but are not able to uniquely identify each tag if there is more than one. The gateway uses verification of the digital signature of the advertising message. This allows you to reliably identify Samsung Smart Tag, but requires entering a key. After entering, the key is checked and, if everything is normal, you need to enter the timeout value. Only after entering the timeout and pressing Ok, the key and timeout are stored in NVS (Non-volatile storage - non-volatile memory).
Picture 9. BLE Monitor page.
Added determination of the best signal from a tag (beacon) among several gateways. The algorithm works like this. Each gateway monitors the r4s/DevId/rssi topic. If he has a signal from a tag (beacon) with a high level, he writes his level to this topic, as well as his number to the r4s/DevId/gtnum topic. After which the gateway periodically, once every 6 seconds, maintains its signal level in the topic, that is, it becomes the master. The remaining gateways check the level and whether there is an update. If any gateway detects no level update for more than 30 seconds, or its level is greater, it becomes the master. The best level and gateway number can be seen in the second RSSI line on the BLE monitor page. The entities of each device also have the best level and gateway number:
Picture 10. Tag entities in Home Assistant.
IIn the first version of the gateway, there was a reserve of both operational (~100 kByte) and programmable (~400 kByte) free memory, which made it possible to expand the capabilities of the firmware, in particular, to add screen support. In addition, I already had an assembled esp32 with a 3.2" 320x240 screen on an ili9341 chip , working with firmware from the wifi-iot website . It is also possible to use ready-made devices on ili9341, ili9342 or ST7789 chips for the gateway. In the gateway I used only the necessary procedures from Bodmer, not very well adapted, but as it is for esp-iot. Pins for connecting the default screen: MOSI-23, MISO-25, CLK-19, CS-16, DC-17, RST-18, LED-21 . Pins can be reassigned in the settings. If PWR, RST, LED are set to 0, then the gateway will not use these pins. There is also an option to rotate the screen 180°, as well as the ability to adjust the display brightness using Mqtt, using the r4s/screen topic. The program checks presence of a screen on the SPI bus at startup. It is possible to display images on the screen in jpeg format. To do this, you need to specify the url of the image. My camera has the following url: http://192.168.1.7/auto.jpg?usr=admin&pwd=admin. The image is loaded into a 20-65 kilobyte buffer in RAM.The update time and buffer size can be set in the settings. It is possible to download images via https. Certificate verification is disabled. It is possible to control the parameters for loading up to 4 images via Mqtt, using the topics r4sx/jpg_url1...r4sx/jpg_url4 and r4sx/jpg_time. To clear the url of an image, you need to enter the # symbol in the corresponding topic. If these topics are not registered in Mqtt, and also after saving the settings, these parameters are copied from the settings in Mqtt. Setting the refresh interval to zero returns the cat to the screen. The length of the link buffer is currently 384 bytes. Added loading and display of weather in text form from the website wttr.in. In principle, this can be done by any site that provides text and allows formatting. If the link does not contain the lines http:// or https:// , then the gateway considers this message to be plain text and displays it on the screen. Available in 2 fonts and 10 color options. Control characters: \ \ or \n - line feed, \F - 26 pixel font and line feed, \f - 16 pixel font and line feed, \0 ... \9 - colors. The Cyrillic alphabet is supported, I checked it, however, only from mosquitto. It actually supports Unicode, like other brokers, I don’t know. An example of displaying an image on the screen in picture 11.
Picture 11. Image.
Picture 12 shows an example of displaying Mqtt weather from the website wttr.in: