Giter Club home page Giter Club logo

mitemperature2's People

Contributors

antxxxx avatar blastar avatar cangelis avatar chiunownow avatar etpedro avatar federerer avatar francescop75 avatar fwieringen avatar hayorov avatar jorisvaesen avatar jsbergbau avatar leumasme avatar ligi avatar mvdklip avatar nordmike avatar poil avatar shadavinci avatar simplyryan avatar szuro avatar temeze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mitemperature2's Issues

Permission denied?

Not sure if its me or the script but when I run ./LYWSD03MMC.py --atc I get a permission denied. Anything I should check? I also ransudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -fwhich python3). I'm running on a pi zero w.

error

user@raspberrypi:~/MiTemperature2 $ ./LYWSD03MMC.py --atc
-bash: ./LYWSD03MMC.py: Permission denied

Script issue

Hi,
I have a problem can anyone help??

File "./LYWSD03MMC.py", line 20
temperature: float
^
SyntaxError: invalid syntax

Unable to Send to Influx DB

I'm not sure if you can help but I'll give it a try. It's most likely an issue on my setup but some guidance would be great. I used this Docker setup and I can "ping" the influx DB from the RaspberyPi but the --data-binary sending isnt working.

I made sendToInflux.sh executable by chmod -x sendToInflux.sh and I get the error "--data-binary: command not found". This is running on a Raspberry Pi 3.

Here's the error when I try to send to InfluxDB.

$ ./LYWSD03MMC.py --atc --debounce --skipidentical 50 --callback sendToInflux.sh
/home/pi/MiTemperature2/sendToInflux.sh sensorname,temperature,humidity,voltage,humidityCalibrated,timestamp [mac] 20.4 38 3.065 0 1608569300
/home/pi/MiTemperature2/sendToInflux.sh: line 9: --data-binary: command not found
Data couln't be send to Callback, retrying...
BLE packet: [mac] 00 1110161a18a4c1385e4dd8007729500b74c5 -58
Temperature:  11.9
Humidity:  41
Battery voltage: 2.932 V
RSSI: -58 dBm

HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 5939fdcd-43ac-11eb-81e1-0242ac120002
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.8.3
X-Request-Id: 5939fdcd-43ac-11eb-81e1-0242ac120002
Date: Mon, 21 Dec 2020 16:48:27 GMT

BLE packet: [mac] 00 1110161a18a4c138b2b99900c5275c0bde12 -84
Temperature:  19.7
Humidity:  39
Battery voltage: 3.038 V
RSSI: -84 dBm

sendToInflux.sh:
curl -i -u "user:pass" -XPOST http://12.34.56.78:8086/write?db=db0&precision=s --data-binary "myHouse,sensorname=$2 temperature=$3,calibratedHumidity=$6,voltage=$5 $7"

"Ping" Influx with Curl:

pi@pi:~/MiTemperature2 $ curl -sL -I http://12.34.56.78:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 1e0469cd-433b-11eb-8016-0242ac120002
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.8.3
X-Request-Id: 1e0469cd-433b-11eb-8016-0242ac120002
Date: Mon, 21 Dec 2020 03:17:55 GMT

Troubleshooting tools and tips

I have difficulty getting data from my LYWSD03MMC. I'm trying to connect from PiZeroW with python 3.7.
I can get the MAC with hcitool without any problem. Your script seems to connect and then loses the connection and then stays in waiting.
I have tried troubleshooting with gatttool and seem to be able to connect but cannot get any data. I'm not sure if it is important but I cannot seem to find the meter with the standard Xiaomi phone app, but I can find it with the MijiaTemp application: https://play.google.com/store/apps/details?id=com.smrtprjcts.mijiabt
Any troubleshooting tips would be greatly appreciated.
Thanks in advance.

Insert data on a Magic Mirror

Hello got this working on a pi3B.
Reads values from sensor and sores them on a txt file but don't know how to show this data.txt on my Magic Mirror. Any ideas? Sorry but Im not a coder...

output in json format

Hi,

I like the script very much, but is it a big deal to get the output in json format?
(e.g using it in Node Red flow)

thx
G0sch

option request: stop the script if connection is lost

Hello,

first of all, Thanks a lot for the script, works like a Charm!

I've created a script that cycles trough different sensors (connects to sensor number one, gets a reading, sends it to domoticz and disconnects, connects to sensor number 2, gets a reading, sends it to domoticz and disconnects, and so on.

Issue:

When trying to connect to get a reading there is a very usseful option (--count N) htat allos to get ONE reading and finish the script, but when the connection is not working, (maybe out of batterie, out of range, etc) the script keeps querying the thermometer and never stops, so In my scenario this means that if this happens on sensor2 It will loop trying to get sensor 2 data, and I'll never get sensor 3.

sudo python3 MiTemperature2-3/LYWSD03MMC.py -c 1 -d A4:C1:38:E9:0B:0E -2p -p2 75 -o2 -4 -p1 33 -o1 -6 -b 5 --callback sendToDomoticz.py

Trying to connect to A4:C1:38:E9:0B:0E
Connection lost
Waiting...
Trying to connect to A4:C1:38:E9:0B:0E
Connection lost
Waiting...
Trying to connect to A4:C1:38:E9:0B:0E
Connection lost
Waiting...
Trying to connect to A4:C1:38:E9:0B:0E

is there a away to have a variable similart to --count N that specifies the number of connection retries if it fails instead to loop to infinite?

thanks!

Asking for community help

Hi everyone,
I know that this thread is not for the ESP32 / Arduino platform, but I wanted to ask you for some help.
I have already published my problem at espressif/arduino-esp32#3856, but at the moment I do not have a solution, since to reproduce the problem almost certainly a Xiaomi-Mijia-LYWSD03MMC device is needed that they probably don't have.
So I wanted to ask for help in two ways:
1.- If someone works with ESP32, please test my program with the LYWSD03MMC device, and publish the result at espressif/arduino-esp32#3856
2.- Test with your script / platform and with the LYWSD03MMC, but with the conditions that I have in my ESP32, that is:

  • Activate the communication interval at 500 ms
  • Send command to turn off notifications and wait a minute
  • Send the command to activate the notifications and as soon as the first information from T / H / Batt is received, deactivate the notifications again and wait a minute.
  • All this while keeping the BLE connection active.
    With this we can see if the LYWSD03MMC device has any problem in BLE communications that affects different platforms.

I think it is reproducible, although it is random and sometimes you have to wait hours.
Thank you very much in advance.
MijiaTemp2_Util_test.txt

BlueGiga support?

The LYWSD03MMC.py is built on bluepy so it works with bluez Linux Bluetooth implementation.

It therefore depends on a bluez supported Bluetooth adaptors (obviously) and limits the use to Linux only.

Mind supporting (alternatively) Bluegiga's BGAPI through pygatt too?
https://www.silabs.com/wireless/bluetooth/bluegiga-low-energy-legacy-modules/device.bled112

This would open the script working on non-Linux systems as well allowing to use "remote" serial Bluetooth dongles from Bluegiga:

pygatt provides a Pythonic API by wrapping two different backends:

  • BlueZ (requires Linux), using the gatttool command-line utility.
  • Bluegiga's BGAPI, compatible with USB adapters like the BLED112.

Sorry, if this is a dumb question and the api's are too different to easily move from one to the other ;-)

HA component

Since this is the only known working method to get the data from this new xiaomi sensor we would really appreciate if you can also create Home Assistant component!

AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'

I've just installed and it runs normally, unless I select ATC mode (

pi@raspi:~/MiTemperature2 $ python3.9 ./LYWSD03MMC.py --atc --watchdogtimer 5
Script started in ATC Mode
----------------------------
In this mode all devices within reach are read out, unless a namefile and --namefileonlydevices is specified.
Also --name Argument is ignored, if you require names, please use --namefile.
In this mode rounding and debouncing are not available, since ATC firmware sends out only one decimal place.
ATC mode usually requires root rights. If you want to use it with normal user rights, 
please execute "sudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -f `which python3`)"
You have to redo this step if you upgrade your python version.
----------------------------
Traceback (most recent call last):
  File "/home/pi/MiTemperature2/./LYWSD03MMC.py", line 423, in <module>
    toggle_device(dev_id, True)
  File "/home/pi/MiTemperature2/bluetooth_utils.py", line 113, in toggle_device
    hci_sock = socket.socket(socket.AF_BLUETOOTH,
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'

I'm a bit lost on this and appreciate any suggestions. Some additional info as follows:

pi@raspi:~/MiTemperature2 $ sudo hcidump
HCI sniffer - Bluetooth packet analyzer ver 5.43
device: hci0 snap_len: 1500 filter: 0xffffffff
pi@raspi:~/MiTemperature2 $ sudo hcitool lescan
LE Scan ...
5F:D1:A9:B0:3D:9F (unknown)
E0:01:85:47:A6:53 (unknown)
E0:01:85:47:A6:53 Hue Lamp
A4:C1:38:9C:53:CF (unknown)
A4:C1:38:9C:53:CF LYWSD03MMC
A4:C1:38:4F:B1:F6 (unknown)
A4:C1:38:4F:B1:F6 LYWSD03MMC
F9:59:29:A0:E1:C4 (unknown)
68:B2:43:9C:47:1C (unknown)
68:B2:43:9C:47:1C (unknown
A4:C1:38:BC:6E:55 (unknown)
A4:C1:38:BC:6E:55 ATC_BC6E55
A4:C1:38:EF:E5:1A (unknown)
A4:C1:38:EF:E5:1A ATC_EFE51A

problem int

hello i have a problem with this script
when I want to send the data to my home automation, the script answers me

pi@raspberrypi:~ $ /usr/bin/python3 /home/pi/domoticz/scripts/python/MiTemperature2/LYWSD03MMC.py -d A4:C1:38:B0:A0:FE -r -deb -b -c 1 -call ./sendToDomoticz.py
Trying to connect to A4:C1:38:B0:A0:FE
Temperature: 20.0
Humidity: 73
Battery voltage: 2.869 V
Battery level: 77
1 measurements collected. Exiting in a moment.
argument of type 'int' is not iterable
Traceback (most recent call last):
File "/home/pi/domoticz/scripts/python/MiTemperature2/LYWSD03MMC.py", line 78, in thread_SendingData
if ' ' in mea.sensorname:
TypeError: argument of type 'int' is not iterable

Cannot connect

Hi, I'm trying your script on a home assistant docker container running Python 3.7.6

I have two sensors which I can find:

/config/www/scripts # hcitool lescan | grep LYWSD03MMC
A4:C1:38:B7:D7:7F LYWSD03MMC
A4:C1:38:B5:A3:32 LYWSD03MMC

But when connecting with python3 LYWSD03MMC.py -d A4:C1:38:B5:A3:32 all I get is

Trying to connect to A4:C1:38:B5:A3:32
Connection lost
Waiting...
Trying to connect to A4:C1:38:B5:A3:32
Connection lost
Waiting...
Trying to connect to A4:C1:38:B5:A3:32
Connection lost
Waiting...

I tried to add some exception details and this is what I have

Failed to connect to peripheral A4:C1:38:B7:D7:7F, addr type: public
Traceback (most recent call last):
  File "LYWSD03MMC.py", line 267, in <module>
    p=connect()
  File "LYWSD03MMC.py", line 182, in connect
    p = btle.Peripheral(adress)
  File "/usr/local/lib/python3.7/site-packages/bluepy/btle.py", line 391, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python3.7/site-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral A4:C1:38:B7:D7:7F, addr type: public

I'm using the following adapter https://www.gmb.nl/item.aspx?id=7856
Thanks

Measured values red don't change

Hello there,
i'm using this repository since 2 or 3 releases and i really like it. But now i have some problems and i can't figure out why.

I'm using the Mitemperature2 v1.8 script three times in seperate instances. After some time (~2 hours), the script will allways output the same values even if the temperature/humidity changes.

I noticed that the Raspberry Pi Zero W reacts really slowly while connected via SSH. The connection via bluetooth to the LYWSD03MMCs is maintained.

My start values are:
./LYWSD03MMC.py -d "MAC" -r -b

ATC compatible with Callback (domoticz)?

Hello,

JsBergbau, thanks a lot for the effort on this. I'm giving it a try again as the non-ATC version never worked fine with me, sooner or later the process ate 100% of the processor and stoipped working.

Now I'm trying to use the ATC option, I was able to flash my sensors (big shoutout to @atc1441 in https://github.com/atc1441/ATC_MiThermometer) and I'm able to read my 6 sensors. with this line:

./LYWSD03MMC.py -a --watchdogtimer 5 --devicelistfile sensors.ini --onlydevicelist --callback sendToDomoticz.py
In domoticz I have created dummy sensors (TEMP+HUMIDITY) for each one of my thermometers, I copied their IDX.

in my sensors.ini I have the devices, and their domoticz IDX:

[info]
info1=MAC Adresses must be in UPPERCASE otherwise sensor won't be found by the script
info2=now all available options are listet. If offset1, offset2, calpoint1 and calpoint2 are given 2Point calibration is used instead of humidityOffset.
info3= Note options are case sensitive
;info4=Use semicolon to comment out lines
sensorname=Specify an easy readable name
humidityOffset=-5
offset1 = -10
offset2 = 10
calpoint1 = 33
calpoint2 = 75

;[AA:BB:CC:DD:EE:FF]
; sensorname=Bathroom
; humidityOffset=-30
; offset1 = -10
; offset2 = -10
; calpoint1 = 33
; calpoint2 = 75

[A4:C1:38:6C:13:E1]
sensorname=86
[A4:C1:38:8D:46:23]
sensorname=87
[A4:C1:38:E9:0B:0E]
sensorname=88
[A4:C1:38:A6:2D:67]
sensorname=89
[A4:C1:38:9F:47:24]
sensorname=90
[A4:C1:38:C0:F3:DA]
sensorname=91

but in domoticz nothing appears. I wonder if sendtodomoticz.py has been updated to use ATC method...

I know that @ JsBergbau is not a domoticz user, so maybe he could not help, anyone else can guide me?

Thanks a lot in advance

---EDIT---

If I modify sendToDomoticz adding one of the IDX it will work in domoticz, (it will get some random sensor data on that idx. So.. yes... seems the sendToDomoticz needs to be updated for this net ATC method, right? anyone knows who made the original script so I can ping him/her?

Option to connect, get data, than go to sleep for defined time

Hallo JS Bergbauer,

what do you think about adding an option to connect to to the sensor based on defined time intervals (e.g. 1h), get the data, and put it to sleep for the defined time period (1h). This should extend battery life in cases when measurements don't need to be very frequent.

Gruß
Christo

when executed via cron no data is sent to influxdb via callback

Perhaps not an issue with script itself (great job, btw!) but when run on cron, it does not send data to influx via callback. once run from terminal, works fine.

measureSensor01Now.sh

#!/bin/bash
./LYWSD03MMC.py -d A4:C1:38:87:25:CF -b -r --debounce --skipidentical 50 --name sensor01 -c 10 --callback sendToInflux.sh

cron:

*/15 * * * * /home/mat/xiaomiSensorToInflux/measureSensor01Now.sh >/dev/null 2>&1
*/15 * * * * /home/mat/xiaomiSensorToInflux/measureSensor02Now.sh >/dev/null 2>&1

grep CRON /var/log/syslog:

Apr  6 19:30:01 middleMan CRON[28597]: (mat) CMD (/home/mat/xiaomiSensorToInflux/measureSensor02Now.sh >/dev/null 2>&1)
Apr  6 19:30:01 middleMan CRON[28599]: (mat) CMD (/home/mat/xiaomiSensorToInflux/measureSensor01Now.sh >/dev/null 2>&1)

any help would be much appreciated!
thanks

[Feature Request] Connection lost callback

Would it be possible to implement a connection lost callback? This would make it easier to debug the reliability of the connection or to make a trigger when the connection is lost for more than x minutes.

In Hassio (HassOS) cant install bluepy

Hi, the python script wont work in my hassio because missing bluepy:

~ $ ./LYWSD03MMC.py -d A4:C1:38:XX:XX:XX -r -b 5
Traceback (most recent call last):
File "./LYWSD03MMC.py", line 5, in
from bluepy import btle
ModuleNotFoundError: No module named 'bluepy'

and installing pip3 install bluepy canot find make

Python 3.9 compatibility?

I've recently upgraded python to 3.9.1 and I'm getting following error:

# /usr/local/bin/LYWSD03MMC.py -a
Script started in ATC Mode
----------------------------
In this mode all devices within reach are read out, unless a namefile and --namefileonlydevices is specified.
Also --name Argument is ignored, if you require names, please use --namefile.
In this mode rounding and debouncing are not available, since ATC firmware sends out only one decimal place.
ATC mode usually requires root rights. If you want to use it with normal user rights,
please execute "sudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -f `which python3`)"
You have to redo this step if you upgrade your python version.
----------------------------
Power ON bluetooth device 0
Traceback (most recent call last):
  File "/usr/local/bin/LYWSD03MMC.py", line 421, in <module>
    toggle_device(dev_id, True)
  File "/usr/local/bin/bluetooth_utils.py", line 122, in toggle_device
    fcntl.ioctl(hci_sock.fileno(),
OSError: [Errno 19] No such device

Problem sending data to domoticz

i have installed domoticz on new raspi4
python 3.7 installed i have this
pi@raspberrypi:~ $ /usr/bin/python3 /home/pi/domoticz/scripts/python/MiTemperatu re2/LYWSD03MMC.py -d A4:C1:38:B0:A0:FE -r -deb -b -c 1 -call ./sendToDomoticz.py
Trying to connect to A4:C1:38:B0:A0:FE
Connection lost
Waiting...
Trying to connect to A4:C1:38:B0:A0:FE
Temperature: 18.7
Humidity: 70
Battery voltage: 2.924 V
Battery level: 82
1 measurements collected. Exiting in a moment.
/home/pi/domoticz/scripts/python/MiTemperature2/./sendToDomoticz.py sensorname,t emperature,humidity,voltage,batteryLevel,timestamp A4:C1:38:B0:A0:FE 18.7 70 2.9 24 82 1606554984
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 159, in _new _conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 80, in create_connection
raise err
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 70, in create_connection
sock.connect(sa)
OSError: [Errno 113] No route to host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.7/http/client.py", line 1244, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.7/http/client.py", line 1290, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.7/http/client.py", line 1239, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.7/http/client.py", line 1026, in _send_output
self.send(msg)
File "/usr/lib/python3.7/http/client.py", line 966, in send
self.connect()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 181, in conn ect
conn = self._new_conn()
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 168, in _new _conn
self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0xb5fd3b50>: Failed to establish a new connection: [Errno 113] No route to h ost

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 398, in incr ement
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.1.36', port=8 0): Max retries exceeded with url: /json.htm?type=command&param=udevice&idx=52&n value=0&svalue=18.7;70;1&battery=82 (Caused by NewConnectionError('<urllib3.conn ection.HTTPConnection object at 0xb5fd3b50>: Failed to establish a new connectio n: [Errno 113] No route to host'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/domoticz/scripts/python/MiTemperature2/./sendToDomoticz.py", li ne 36, in
+ val_bat
File "/usr/lib/python3/dist-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in reque st
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.1.36', por t=80): Max retries exceeded with url: /json.htm?type=command&param=udevice&idx=5 2&nvalue=0&svalue=18.7;70;1&battery=82 (Caused by NewConnectionError('<urllib3.c onnection.HTTPConnection object at 0xb5fd3b50>: Failed to establish a new connec tion: [Errno 113] No route to host'))
Data couln't be send to Callback, retrying...

Problem with Domoticz

Hi,

I can't get a virtual sensor on Domoticz to be updated. I changed the IP-address to "127.0.0.1" and set the "idx_temp" value.

With a command:

sudo python3 LYWSD03MMC.py -d A4:C1:38:C3:A0:01 -r -deb -b -c 2 --callback sendToDomoticz.py

I get the response:

/home/riho/mijia2/sendToDomoticz.py sensorname,temperature,humidity,voltage,batteryLevel,timestamp A4:C1:38:C3:A0:01 27.7 48 2.666 56 1594067106 Temperature: 27.7 Humidity: 48 Battery voltage: 2.666 Battery level: 56 2 measurements collected. Exiting in a moment.

But the values of the sensor on Domoticz don't update. What could be wrong?

Never finds my ATC flashed sensors

I have followed your instructions on two different PiS for installation. I run the command with the --atc switch, and it reports no errors, but just shows that it is listening without ever discovering any sensors. I also went through the Troubleshooting reccomendaations, but no go. The sensors picked up and read properly by my ESP32 modules programmed with ESPhome. This failure happened for two different RPi0WS running buster.

Any suggestions to troubleshoot?

bluepy-helper crashes

bluepy-helper crashes on a PI 4 after some time. Sometimes a few hours, sometimes longer. Quite difficult to track. bluepy-helper then uses 100 % CPU and connection to this MAC address. Watchdog does not seem to work.

Short battery life with devices with bad signal strength

Since 25.02.2020 my script dramatically reduces power consumption. However I have one sensor which needed 4 batteries sice then. That's a lot. Look at the graph
grafik

So I dug deeper into this. Powersaving of the script is working. I've measured the power draw and noticed about 100 μA on bad reception. It seems that there is some error correction in the BLE protocol that just retransmits the data and thus causing the high power consumption. I even had the situation when it drew 450 μA and even moving it 50 cm beside the reading Raspberry PI Zero W didn't reduce power consumption. Only a reconnect did. However saidly I was never able to reproduce this situation. Seems to be quite rare hopefully.
I doubt that you would save power using the passive mode used by Homeassistant Plugin. I expect in this situations to miss a lot of the advertisments.

So what do do about? I tried to use RSSI as an idicator for high battery drain, but that wasn't successful. It is even not possible to get the RSSI while connected. You have to close the script and then scan for new bluetooth devices and get the RSSI with btmon. RSSI varied between -92 and -100 dBm. 8 dBm is almost 10 times more or less signal strength, so not very practicable.

Since I have a lot of sensors, I have also a lot of experience. If you notice a lot of reconnects (you also see them with btmon), then signal strength is definitively too weak. Try moving a bit. A few centimeters can do a lot. Not only the reconnecting process uses a lot of energy (it takes a few seconds until sensor goes to powersaving mode), but also the higher current with weak signals (see above).

So the only solution I can offer at the moment is to have an eye on your battery levels. If one sensor needs new batteries in a short time, than reduce distance between reciever and Xiaomi sensor.

grafik

Here is a diagram with the voltage level in the past 1.5 month. Lets focus on the 4 with the highest voltage. All 4 were deployed at the same day. Voltage level trend is quite parallel. All sensors should have good connection quality. So I think its just different battery capacity or individuell slightly different power draw or this sensor with red chart really has a slightly lower signal strength.
The sensor with the green chart has very low reception level and often disconnects. That uses a lot of energy, too.

So the only solution I can offer is to monitor for your battery level. If it drops by 0.1 V in 1.5 month, then everything is perfect.

Multiple sensors --> Multiple script instances?

Hello! many thanks for teh script, works like a charm with my domoticz.

one questio from a newbie, what would be the method to have multiple thermomethers? I see no options on the scripts, so I guess the only current way without coding is to have one script per sensor?

thanks!

Nice works -- a question

Thank you for this.

Your code works for me.

I've been trying to get a Golang example working but I don't understand the mapping between the devices handles and characteristics.

Your code appears to use 0x0038 and 0x0046

How were you able to identify these handles?

If I enumerate my devices handles:

handle = 0x0038, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x0046, uuid = ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6

I'm unable to find characteristics 00002902*

But I do have:

Service: ebe0ccb07a0a4b0c8a1a6ff2997da3a6
  Characteristic  ebe0ccd87a0a4b0c8a1a6ff2997da3a6
    properties    write 
  Descriptor      2901 (Characteristic User Description)
    value         73657420636f6e6e20696e74657276616c00 | "set conn interval\x00"

Am I understanding the mapping correctly?

Thanks!

Script execution stucks after some succesfull connections to device

Hi @JsBergbau ,

your script is perfect and I used it to show the climate-values of some rooms in my house in my home assistant. I work on a Raspberry PI 3B and the system works very well with only one sensor and with a permanent BLE-connection to the device.

I simply call your script like this LYWSD03MMC.py -d A4:C1:38:46:B7:2D -r -b.

First idea to to reduce the connection frequency was to use a simple cron job. With this job I want to call your script every minute (or every 5 minutes). Unfortunately, after some connections something get stucks. I try to analyse your script and I think I found some hints documented from you in your source code. You commented some thoughts about the bluepy-helper functions and that a stucking bluepy-helper can be killed with a system call before reconnecting to devices. I activate this parts of sourcecode and try again. But same error appears. After some connections something freezes. I checked my journal. There are following messages:

Mär 22 08:25:11 BennyPi blemqtt.sh[31954]: Trying to connect to A4:C1:38:82:2D:E3
Mär 22 08:25:11 BennyPi blemqtt.sh[31954]: Connection lost
Mär 22 08:25:12 BennyPi blemqtt.sh[31954]: Waiting...
Mär 22 08:25:12 BennyPi blemqtt.sh[31954]: bluepy-helper couldn't be determined, killing not allowed
Mär 22 08:25:12 BennyPi blemqtt.sh[31954]: Trying to connect to A4:C1:38:82:2D:E3
Mär 22 08:25:12 BennyPi blemqtt.sh[31954]: Connection lost
Mär 22 08:25:13 BennyPi blemqtt.sh[31954]: Waiting...
Mär 22 08:25:13 BennyPi blemqtt.sh[31954]: bluepy-helper couldn't be determined, killing not allowed
Mär 22 08:25:13 BennyPi blemqtt.sh[31954]: Trying to connect to A4:C1:38:82:2D:E3
Mär 22 08:25:13 BennyPi blemqtt.sh[31954]: Connection lost

I google some time and found two clues I would like to follow.

1.) Restart the bluetooth service
2.) Activating the watchdog and the restart on-failure capability in the bluetooth service
3.) Set the --compatargument to the bluetooth service

If I reset the bluetooth-service with sudo systemctl restart bluetooth.service the connection to my device could be established.

Mär 22 08:30:23 BennyPi blemqtt.sh[31954]: Trying to connect to A4:C1:38:82:2D:E3
Mär 22 08:30:29 BennyPi blemqtt.sh[31954]: Temperature: 17.2
Mär 22 08:30:29 BennyPi blemqtt.sh[31954]: Humidity: 51
Mär 22 08:30:29 BennyPi blemqtt.sh[31954]: Battery voltage: 2.921
Mär 22 08:30:29 BennyPi blemqtt.sh[31954]: Battery level: 82
Mär 22 08:30:29 BennyPi blemqtt.sh[31954]: 1 measurements collected. Exiting in a moment.

After some time the connection get stucks again.

I edited the bluetooth.service /etc/systemd/system/bluetooth.target.wants/bluetooth.service and try the points mentioned under 2.) and 3.).

Summerized:

I always have to restart the bluetooth service if the connection can't be established. The editing of bluetooth.service to start the bluetooth service in compatibillity mode don't helps. Also setting the watchdog time to 10s and restarting the bluetooth service on-failure don't leads to success.

Do you have any ideas to solve this problem?

Best regards and I hope you can help me ...

Use usb bluetooth dongle instead of builtin bluetooth

Hello
I'm using your library on a raspberry pi 4.
i was using it with an odroid c2 and usb bluetooth adapter, the range was better.

is that possible to use this bluetooth dongle instead of the builtin bluetooth of pi 4 ?
thanks

Limit number of measures

Hi,

Could it be possible to add a new parameter (--count for instance).
This parameter would be an integer, and the script would exit after receiving this number of measures.
That would allow a limited bluetooth connection.
For instance, I would trigger the script from cron every 5 minutes, with 3 measures. The bluetooth connection would then be maybe 30 seconds every 5 minutes.
Thanks for considering my request.

Bouncing temperature compensation

This is an issue with the LYWSD03MMC Sensor. On successive readings/receiving of the temperature the value bounces a lot. Therefore the debouncing switch has been implemented. It works that way:
(We skip the rounding which is only used after startup until the second decimal place is not anymore within 0.2 and 0.7).
Say temperature read is 11.81 degrees. Than it is rounded downwards (floor) to 11.8 If the next reading is 11.86 it is still floored to 11.8
If it reaches 11.7 it rounded up to 11.8
If the next reading is 11.83 it is still rounded up to 11.9
When the next reading is 11.82 it is floored to 11.8
When next reading is 11.85 or 11.86 it is still florred to 11.8

Due to the way debouncing works it can only be used together with --round option. If round option is not specified, but --debounce than debouncing is ignored.

I have multiple BME280 and also DS18B20 sensors. Without any measures they show a lot less bouncing values. They just give a lot more decimal places and I'm using only 2 of them. Giving quite stable values.

LYWSD03MMC gives 17 bounces in that measurement without debouncing measures within about 20 minutes
grafik

LYWSD03MMC with debouncing. Within roughly more than half an hour "only" 6 temperature bounces. The humidity is bouncing, but there is no possibility to debounce without losing precision. Things like moving average can still be down when displaying the data
grafik

--> Debouncing ok

Python 3.5.3 updated LYWSD03MMC.py

Hello,

As proposed I would be very grateful of a bug free and updated version of the script for 3.5.3 !
As I'm running Stretch and would like to use callbacks scripts to update remote values...

Many thanks for this work,

Possible to use multiple sensors?

Hello,

Can you run multiple instance of this scripts in parallels if you have multiple sensors ? (I don't have yet multiple sensors, so I can't test at the moment...)

syntaxError

Hi,

I can't get a virtual sensor on Supla
With a command:

./LYWSD03MMC.py -d A4:C1:38:AA:6E:C6 -r -b

I get the response:

File "./LYWSD03MMC.py", line 20
temperature: float
^
SyntaxError: invalid syntax

Any help?

Measurements interval

After reading the power-saving discussion in #3 I think it would be awesome if we could specify the interval between reading measures while staying connected to the sensor. Something like:

./LYWSD03MMC.py --interval 300 ...

Would read new data every 5 minutes (and execute the callback, if given).

What do you think?

Docker build fails

Hi, first of all thank you for your great project! 💯

I would like to use your software on my Raspberry Pi in Docker, unfortunately the Docker build with the provided Dockerfile fails with:

$ docker build -t mitemperature2localimage -f docker/Dockerfile .
Sending build context to Docker daemon  316.9kB
Step 1/9 : FROM xavierrrr/xrrzero:stretchpython3.7
stretchpython3.7: Pulling from xavierrrr/xrrzero
31994f9482cd: Pull complete 
0e62b5dbedee: Pull complete 
2f4de8133c30: Pull complete 
9d4db4b0b711: Pull complete 
82cb2844df84: Pull complete 
615de40008cf: Pull complete 
2f64e792ad4a: Pull complete 
4171acf2106d: Pull complete 
922866fe3ee0: Pull complete 
a306030b8f1c: Pull complete 
38e601cf0581: Pull complete 
Digest: sha256:52d4a50a2a63fbfb23587fa5302cfa8209adda6ae64f7f88b971eca4cd46481c
Status: Downloaded newer image for xavierrrr/xrrzero:stretchpython3.7
 ---> bfefa81a0fdd
Step 2/9 : ENV LANG C.UTF-8
 ---> Running in a89ec52d01c7
Removing intermediate container a89ec52d01c7
 ---> 0b0d69b1835e
Step 3/9 : RUN sudo apt-get -y --no-install-recommends install python-pip libglib2.0-dev
 ---> Running in a793593988a7
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libglib2.0-bin libglib2.0-data libpcre16-3 libpcre3-dev libpcre32-3
  libpcrecpp0v5 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib
  python python-minimal python-pip-whl python2.7 python2.7-minimal
Suggested packages:
  libglib2.0-doc python-doc python-tk python2.7-doc binfmt-support
Recommended packages:
  python-all-dev python-setuptools python-wheel
The following NEW packages will be installed:
  libglib2.0-bin libglib2.0-data libglib2.0-dev libpcre16-3 libpcre3-dev
  libpcre32-3 libpcrecpp0v5 libpython-stdlib libpython2.7-minimal
  libpython2.7-stdlib python python-minimal python-pip python-pip-whl
  python2.7 python2.7-minimal
0 upgraded, 16 newly installed, 0 to remove and 33 not upgraded.
Need to get 13.6 MB of archives.
After this operation, 41.0 MB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian stretch/main armhf python-pip-whl all 9.0.1-2+rpt2 [1399 kB]
Get:2 http://archive.raspbian.org/raspbian stretch/main armhf libpcrecpp0v5 armhf 2:8.39-3 [149 kB]
Err:3 http://archive.raspbian.org/raspbian stretch/main armhf libpython2.7-minimal armhf 2.7.13-2+deb9u3
  404  Not Found
Err:4 http://archive.raspbian.org/raspbian stretch/main armhf python2.7-minimal armhf 2.7.13-2+deb9u3
  404  Not Found
Get:5 http://archive.raspbian.org/raspbian stretch/main armhf python-minimal armhf 2.7.13-2 [40.5 kB]
Err:6 http://archive.raspbian.org/raspbian stretch/main armhf libpython2.7-stdlib armhf 2.7.13-2+deb9u3
  404  Not Found
Err:7 http://archive.raspbian.org/raspbian stretch/main armhf python2.7 armhf 2.7.13-2+deb9u3
  404  Not Found
Get:8 http://archive.raspbian.org/raspbian stretch/main armhf libpython-stdlib armhf 2.7.13-2 [20.0 kB]
Get:9 http://archive.raspbian.org/raspbian stretch/main armhf python armhf 2.7.13-2 [154 kB]
Err:10 http://archive.raspbian.org/raspbian stretch/main armhf libglib2.0-data all 2.50.3-2+deb9u1
  404  Not Found
Err:11 http://archive.raspbian.org/raspbian stretch/main armhf libglib2.0-bin armhf 2.50.3-2+deb9u1
  404  Not Found
Get:12 http://archive.raspbian.org/raspbian stretch/main armhf libpcre16-3 armhf 2:8.39-3 [235 kB]
Get:13 http://archive.raspbian.org/raspbian stretch/main armhf libpcre32-3 armhf 2:8.39-3 [227 kB]
Get:14 http://archive.raspberrypi.org/debian stretch/main armhf python-pip all 9.0.1-2+rpt2 [179 kB]
Get:15 http://archive.raspbian.org/raspbian stretch/main armhf libpcre3-dev armhf 2:8.39-3 [565 kB]
Err:16 http://archive.raspbian.org/raspbian stretch/main armhf libglib2.0-dev armhf 2.50.3-2+deb9u1
  404  Not Found
Fetched 2969 kB in 0s (5248 kB/s)
E: Failed to fetch http://archive.raspbian.org/raspbian/pool/main/p/python2.7/libpython2.7-minimal_2.7.13-2+deb9u3_armhf.deb  404  Not Found
E: Failed to fetch http://archive.raspbian.org/raspbian/pool/main/p/python2.7/python2.7-minimal_2.7.13-2+deb9u3_armhf.deb  404  Not Found
E: Failed to fetch http://archive.raspbian.org/raspbian/pool/main/p/python2.7/libpython2.7-stdlib_2.7.13-2+deb9u3_armhf.deb  404  Not Found
E: Failed to fetch http://archive.raspbian.org/raspbian/pool/main/p/python2.7/python2.7_2.7.13-2+deb9u3_armhf.deb  404  Not Found
E: Failed to fetch http://archive.raspbian.org/raspbian/pool/main/g/glib2.0/libglib2.0-data_2.50.3-2+deb9u1_all.deb  404  Not Found
E: Failed to fetch http://archive.raspbian.org/raspbian/pool/main/g/glib2.0/libglib2.0-bin_2.50.3-2+deb9u1_armhf.deb  404  Not Found
E: Failed to fetch http://archive.raspbian.org/raspbian/pool/main/g/glib2.0/libglib2.0-dev_2.50.3-2+deb9u1_armhf.deb  404  Not Found
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
The command '/bin/sh -c sudo apt-get -y --no-install-recommends install python-pip libglib2.0-dev' returned a non-zero code: 100

Furthermore, I wonder what is behind xavierrrr/xrrzero:stretchpython3.7, I have not been able to find a source for it.

Retrieving logged data

According to this the sensor stores three months of values locally.

Does anyone know how to retrieve this logged data?

Connection lost => RPi temp. higher and higher

Hello,
I am newbie in general RPi and coding, I have RPi from few days.

I use this script to collecting data from my 4 xiaomi sensors.
Everythig work well, I use crontab to collect 1 data every 15 minutes from each sensor, but...
When I close door in my badroom sometimes I lost connecion with sensor there (I have RPi 4 with not good bluetooth range).
I noticed that then RPi ty to reconnect and roconnect, my CPU use more and more resources and temperature rise and rise (over 70 degrees). Reboot help for short time...

Could anyone help me to modify "my code" to achive: when connection lost stop/interrupt script?

"my code" in crontab:

*/15 * * * * /home/pi/mi-temp/LYWSD03MMC.py -d A4:C1:38:6C:65:46 -r -b -c 1 --callback sendToDomoticz14.py >dev/null 2>&1 */15 * * * * /home/pi/mi-temp/LYWSD03MMC.py -d A4:C1:38:80:56:FE -r -b -c 1 --callback sendToDomoticz15.py >dev/null 2>&1 */15 * * * * /home/pi/mi-temp/LYWSD03MMC.py -d A4:C1:38:40:76:76 -r -b -c 1 --callback sendToDomoticz16.py >dev/null 2>&1 */15 * * * * /home/pi/mi-temp/LYWSD03MMC.py -d A4:C1:38:46:6C:78 -r -b -c 1 --callback sendToDomoticz17.py >dev/null 2>&1

Thanks in advance!

Investigating on values

Hey, great job with getting this working!

For your reference, gattool output for my sensor is

Notification handle = 0x0036 value: eb 08 36 c0 0b 
[A4:C1:38:E9:2A:F3][LE]>

The unknown values for my sensor are c0 0b.
After a while it read e5 0b.

The other one

Notification handle = 0x0036 value: f9 07 3d 78 0c
[A4:C1:38:1F:50:35][LE]>

Powerdraw with this script 20 times higher than with official app

I've done some measurements by putting a multimeter between the battery and the Aqara device.
When it is on advertising mode it consumes about 20µA. It is hard to get an exact value because it fluctuates heavily even with a 4000 µF capacitor on the Xiaomi device side. With about 20 µA and a battery capacity of about 210 mAh we get a calculated lifetime of 10500 hours or 437 days with is roughly about 1 year and 2 month.

When connected the values are quite stable and I get around 500 µA power draw and very stable. But I'm a bit confused because 500 µA with a 210 mAh battery gives a batterylifetime of 17,5 days. Either some batteries have more capacity or the multimeter is bad. However some devices failed after quite short time. So round about are the values ok.
There is no noticable difference in the 500 µA power draw whether the temp/humidity/voltage notification is activated or not. Solely the bluetooth connection uses that much amount of power.

Thats why I'll try to implement a connection intervall of 5 minutes, as described here #1 (comment)

Update: With this simple multimeter there can be seen no difference in power draw when doing an active scan with sudo hcitool lescan --duplicate. On active scanning mode the device replies to the scans with its device name.

And now the very interessting part: When you use the official Xiaomi Mi App the power draw goes to about 500-700 µA shortly and then it falls down to about 25 µA even is the sensor is still connected with the app. If you touch and hold it so that the temperature rises it is almost immediately shown on smartphone. If we could reach that small power draw we could stay connected because there is only a very small additional energy usage and establishing the connection uses most energy.

So the question is whats going on that with official app the power draw is that kind of small and with this script so insane high.

LYWSD02 support?

I successfully connected two LYWSD03MMC sensors but I have also 02 sensor. Can this script support those?

mot de passe domoticz

hello the script works wonderfully however it doesn't work anymore since I put a login password on domoticz I must modify the script with my username and password to be able to send the data ???

domoticzserver = "192.168.1.36"
id =myID
pass =myPASS
idx_temp = "52"

val_temp = sys . argv [ 3 ] # changement en sys.argv [5] pour calibré
val_hum = sys . argv [ 4 ]
val_bat = sys . argv [ 6 ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.