Interfacing Xiaomi LYWSD03MMC to HA using ESP32

I recently published my project to interface the Xiaomi LYWSD03MMC temperature and humidity sensor with HA using a Raspberry Pi W. I then realised that the ESP32 supports BLE and can connect to a wifi network, so I set to and developed this project.
Xiaomi_LYWSD03MMC
I have posted the code and given a detailed description on how to use it here

It is, in fact, more sophisticated than the Pi project as it auto discovers the LYWSD03MMC devices. You just need to tell the program what wifi credentials to use and where to find your MQTT server, and it will do the rest.

I have only two devices, so I look forward to people with many devices trying this and letting me know how they get on!

5 Likes

https://github.com/AnthonyKNorman/Xiaomi_BLE_Tempertaure_Display_for_HA

Is this perhaps the correct url?

EDIT no, that’s the pi zero code, your link is a 404.

Yup, a 404 to the link.

Sorry all. I failed to make the repository public! Should work now,

1 Like

Yup working now. Thanks.

Just uploaded a new version. Found a bug that can cause crashing.

Hello,
I have got problem with connection to MQTT

Msg from putty:

>>> import mqtt
I (4500) modsocket: Initializing
I (10484) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0
Connection successful
('192.168.1.14', '255.255.255.0', '192.168.1.1', '176.103.130.130')
2020-06-19 18:10:53
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mqtt.py", line 55, in <module>
  File "mqtt.py", line 28, in connect
  File "umqtt/simple.py", line 99, in connect
MQTTException: 5

I found information that in mqtt.py should be username and password to MQTT. Do you know how to add it?
I tired:
MQTT_USERNAME = ‘my username’
MQTT_PASSWORD = ‘my password’

but still the same error.

Can you help?

Thank you in advance

1 Like

Try here https://github.com/micropython/micropython-lib/blob/b89114c8345e15d360c3707493450805c114bc8c/umqtt.simple/umqtt/simple.py

@nickrout I correct.
The init parameters for MQTTClient are (self, client_id, server, port=0, user=None, password=None, keepalive=0, ssl=False, ssl_params={})

On line 23 of mqtt.py
client = MQTTClient(client_id, mqtt_server),
you should change this to
client = MQTTClient(client_id, mqtt_server, user='your_user', password='your_password')

Let me know how you get on.

1 Like

Now everything working :slight_smile: thank you for fast answer.
How long this sensors can work on battery?

I’m not sure. Mine have been running for several months now.

Is it possible turn off scan and set in file MAC address our sensor? Im asking bcs one of my sensor is far from esp32 and 2-3 time per a day it lost connection with esp32 and reset of esp take to much time.

Thank you in advance for your answer

The scan takes place on boot up. The software then remembers the MAC addresses found until another reboot.
It shouldn’t need a reboot if the connection fails. It should just keep trying each time around the loop…

I Use ESPHome for my ESP32 using the esp32_ble_tracker: component

works flawlessly and integration is seamless

my .yaml for the ESP Luanode

esphome:
  name: esp32_luanode_1
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "redacted"
  password: "redacted"
  power_save_mode: none
 
  
  manual_ip:  
   static_ip: 192.168.0.208
   gateway: 192.168.0.2
   subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32 Luanode 1 Fallback Hotspot"
    password: "23OuQ5M2a3rL"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


# Enable Web server.
web_server:
  port: 80


text_sensor:
  - platform: version
    name: "ESPHome Version esp32_luanode_1"
    
# Example configuration entry

  - platform: wifi_info
    ip_address:
      name: ESP IP Address esp32_luanode_1
    ssid:
      name: ESP Connected SSID esp32_luanode_1
    bssid:
      name: ESP Connected BSSID esp32_luanode_1


# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time


esp32_ble_tracker:


sensor:
  - platform: xiaomi_lywsdcgq
    mac_address: 58:2D:34:38:19:70
    temperature:
      name: "Xiaomi FIRST FLOOR Temperature"
    humidity:
      name: "Xiaomi FIRST FLOOR Humidity"
    battery_level:
      name: "Xiaomi FIRST FLOOR Battery Level"
      

# Sensors with general information.
  # Uptime sensor.
  - platform: uptime
    name: esp32_luanode_1 Uptime

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: esp32_luanode_1 WiFi Signal
    update_interval: 60s      

    
status_led:
  pin:
     number: GPIO2
     inverted: TRUE
1 Like

Your totally off-topic here. This is the Xiaomi LYWSDCGQ

This is the Xiaomi LYWSD03MMC

image

The Xiaomi LYWSD03MMC DOES NOT WORK with esphome.

Actually, there has been some progress and now there is a new LYWSD03MMC sensor available in the ESPHome-dev branch. However, it requires to find some unique Bind Keys in order to be able to communicate with the device. Check the thread.

sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: "a4:c1:38:80:4f:79"
    bindkey: "26d11deb1fcfae7862a462839a5156a9"
    temperature:
        name: "Test Temperature"
    humidity:
        name: "Test Humidity"
    battery_level:
        name: "Test Battery Level"

So far it doesn’t work as expected, for example in my case the sensor updates every 2 hours or so.

So, I would like to ask the OP @Beantree if it is possible to implement this in order to read measurements from both LYWSD03MMC and LYWSDCGQ sensors.

@Beantree Today I had error: [OSError 28] this concerning memory allocation
It was after 2days of working. Reset of ESP32 didn’t solve this issue I have to reflash node.

Do you have this kind of problem?

No I don’t have this problem. Maybe you could capture some REPL output to show what happens?

@Beantree I have the same problem like I wrote few post above. After 3 days of perfect working my esp module just connect to my WiFi nothing more. I did reset few times and still the same problem. When i connect to Putty are below information:

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:4988
load:0x40078000,len:11408
load:0x40080400,len:6108
entry 0x400806bc
I (592) psram: This chip is ESP32-D0WD
E (592) spiram: SPI RAM enabled but initialization failed. Bailing out.
I (592) cpu_start: Failed to init external RAM; continuing without it.
I (600) cpu_start: Pro cpu up.
I (603) cpu_start: Application information:
I (608) cpu_start: Compile time:     Dec 20 2019 07:58:58
I (614) cpu_start: ELF file SHA256:  0000000000000000...
I (620) cpu_start: ESP-IDF:          v4.0-beta1
I (625) cpu_start: Starting app cpu, entry point is 0x400835e0
I (0) cpu_start: App cpu up.
I (636) heap_init: Initializing. RAM available for dynamic allocation:
I (643) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (649) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (655) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (661) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
I (667) heap_init: At 3FFCCA50 len 000135B0 (77 KiB): DRAM
I (673) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (680) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (686) heap_init: At 4009E2E4 len 00001D1C (7 KiB): IRAM
I (692) cpu_start: Pro cpu start user code
I (711) spi_flash: detected chip: generic
I (711) spi_flash: flash io: dio
I (712) cpu_start: Chip Revision: 1
W (713) cpu_start: Chip revision is higher than the one configured in menuconfig. Suggest to upgrade it.
I (723) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
E (40) spiram: SPI RAM not initialized
I (160) modsocket: Initializing
I (1824) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0
Connection successful
('192.168.1.14', '255.255.255.0', '192.168.1.1', '176.103.130.130')
2020-07-06 15:48:25
Connected to 192.168.1.99 MQTT broker
Traceback (most recent call last):
  File "main.py", line 1, in <module>
  File "mqtt.py", line 57, in <module>
  File "ble.py", line 48, in __init__
  File "ble.py", line 42, in debug
**OSError: 28**
MicroPython v1.12 on 2019-12-20; ESP32 module (spiram) with ESP32
Type "help()" for more information.

When I press right mouse button in putty are addtional informations:

>>>
>>> ets Jun  8 2016 00:22:57
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>>
>>> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> configsip: 0, SPIWP:0xee
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> mode:DIO, clock div:2
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> load:0x3fff0018,len:4
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> load:0x3fff001c,len:4988
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> load:0x40078000,len:11408
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> load:0x40080400,len:6108
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> entry 0x400806bc
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> I (592) psram: This chip is ESP32-D0WD
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> E (592)d. Bailing out.
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> I (592) cpu_start: Failed to init external RAM; continui8) cpu_start: Compile  cpu_start: ESP-IDF:  t: App cpu up.
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> I (636) he 000000F0 (0 KiB): DRAM
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> I 4108 (16 KiB): DRAM
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> I (660 (77 KiB): DRAM
...  0001BCB0 (111 KiB): D/IRAM
... I (686) hea1) spi_flash: detected start: Chip revision is higher than the one configured in menuconfig. Suggest to upgrade it.
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> I (723) cpu_start: StaAM not initialized
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> I (160) modsockion successful
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>> ('192.192.168.1.99 MQTT brokeceback (most recent call las File "main.py", line 1, in <module>
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax
>>>   File "mqtt.py", line 57, in <module>
Traceback (most recent call last):
  File "<stdin>", line 1
IndentationError: unexpected indent
>>>   File "ble.py", line 48, in __init__
Traceback (most recent call last):
  File "<stdin>", line 1
IndentationError: unexpected indent
>>>   File "ble.pESP32
Traceback (most recent call last):
  File "<stdin>", line 1
IndentationError: unexpected indent
>>> Type "help()" for more information.

So the error OSError: 28 appeared again ( I found that: OSError is “No space left on device”,)

I also download other bin file from: https://micropython.org/download/esp32/
for ESP32 version without SPI Ram but problem was the same.

For solve the problem i need to reflash esp32…but it isn’t solution because I should do it every 2-3 days.

Please look at this and maybe you can find solution

Asica10, how many sensors do you have?