Skip to content

Commit 8436487

Browse files
committed
actual code
1 parent b84f95f commit 8436487

File tree

6 files changed

+94
-33
lines changed

6 files changed

+94
-33
lines changed

README.rst

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,14 @@ Dependencies
2525
This driver depends on:
2626

2727
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
28+
* `adafruit_ble <https://github.com/adafruit/Adafruit_CircuitPython_BLE>`_
2829

2930
Please ensure all dependencies are available on the CircuitPython filesystem.
3031
This is easily achieved by downloading
3132
`the Adafruit library and driver bundle <https://circuitpython.org/libraries>`_.
3233

3334
Installing from PyPI
3435
=====================
35-
.. note:: This library is not available on PyPI yet. Install documentation is included
36-
as a standard element. Stay tuned for PyPI availability!
37-
38-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
39-
If the library is not planned for PyPI, remove the entire 'Installing from PyPI' section.
40-
4136
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
4237
PyPI <https://pypi.org/project/adafruit-circuitpython-ble_lywsd03mmc/>`_. To install for current user:
4338

@@ -60,11 +55,6 @@ To install in a virtual environment in your current project:
6055
source .env/bin/activate
6156
pip3 install adafruit-circuitpython-ble-lywsd03mmc
6257
63-
Usage Example
64-
=============
65-
66-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
67-
6858
Contributing
6959
============
7060

adafruit_ble_lywsd03mmc.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,69 @@
1616
1717
**Hardware:**
1818
19-
.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST
20-
inline format: "* `Link Text <url>`_"
21-
2219
**Software and Dependencies:**
2320
2421
* Adafruit CircuitPython firmware for the supported boards:
2522
https://github.com/adafruit/circuitpython/releases
26-
27-
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either.
28-
29-
# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
30-
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3123
"""
3224

33-
# imports
25+
import struct
26+
27+
import _bleio
28+
from adafruit_ble.attributes import Attribute
29+
from adafruit_ble.services import Service
30+
from adafruit_ble.uuid import VendorUUID
31+
from adafruit_ble.characteristics import Characteristic, ComplexCharacteristic
3432

3533
__version__ = "0.0.0-auto.0"
3634
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_LYWSD03MMC.git"
35+
36+
37+
class _Readings(ComplexCharacteristic):
38+
"""Notify-only characteristic of temperature/humidity"""
39+
40+
uuid = VendorUUID("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6")
41+
42+
def __init__(self):
43+
super().__init__(properties=Characteristic.NOTIFY)
44+
45+
def bind(self, service):
46+
"""Bind to an LYWSD03MMCService."""
47+
bound_characteristic = super().bind(service)
48+
bound_characteristic.set_cccd(notify=True)
49+
# Use a PacketBuffer that can store one packet to receive the data.
50+
return _bleio.PacketBuffer(bound_characteristic, buffer_size=1)
51+
52+
53+
class LYWSD03MMCService(Service):
54+
"""Service for reading from an LYWSD03MMC sensor."""
55+
56+
def __init__(self, service=None):
57+
super().__init__(service=service)
58+
# Defer creating buffers until needed, since MTU is not known yet.
59+
self._settings_result_buf = None
60+
self._readings_buf = None
61+
62+
uuid = VendorUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6")
63+
64+
readings = _Readings()
65+
66+
@property
67+
def temperature_humidity(self):
68+
"""Return a tuple of (temperature, humidity)."""
69+
if self._readings_buf is None:
70+
self._readings_buf = bytearray(
71+
self.readings.packet_size # pylint: disable=no-member
72+
)
73+
data = self._readings_buf
74+
length = self.readings.readinto(data) # pylint: disable=no-member
75+
if length > 0:
76+
low_temp, high_temp, hum = struct.unpack_from("<BBB", data)
77+
sign = high_temp & 0x80
78+
temp = ((high_temp & 0x7F) << 8) | low_temp
79+
if sign:
80+
temp = temp - 32767
81+
temp = temp / 100
82+
return (temp, hum)
83+
# No data.
84+
return None

docs/api.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
2-
.. If you created a package, create one automodule per module in the package.
3-
4-
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
5-
.. use this format as the module name: "adafruit_foo.foo"
6-
71
.. automodule:: adafruit_ble_lywsd03mmc
82
:members:

docs/index.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
27-
the toctree above for use later.
28-
2926
.. toctree::
3027
:caption: Related Products
3128

32-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
33-
the toctree above for use later.
34-
3529
.. toctree::
3630
:caption: Other Links
3731

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
11
# SPDX-FileCopyrightText: 2021 Dan Halbert, written for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4+
5+
import time
6+
7+
import adafruit_ble
8+
from adafruit_ble.advertising.standard import (
9+
Advertisement,
10+
ProvideServicesAdvertisement,
11+
)
12+
from adafruit_ble_lywsd03mmc import LYWSD03MMCService
13+
14+
# PyLint can't find BLERadio for some reason so special case it here.
15+
ble = adafruit_ble.BLERadio() # pylint: disable=no-member
16+
17+
connection = None
18+
19+
while True:
20+
print("Scanning...")
21+
for adv in ble.start_scan(Advertisement, timeout=5):
22+
if adv.complete_name == "LYWSD03MMC":
23+
connection = ble.connect(adv)
24+
print("Connected")
25+
break
26+
27+
# Stop scanning whether or not we are connected.
28+
ble.stop_scan()
29+
30+
if connection and connection.connected:
31+
service = connection[LYWSD03MMCService]
32+
while connection.connected:
33+
34+
print(
35+
"Temperature, Humidity",
36+
service.temperature_humidity,
37+
)
38+
time.sleep(5)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
],
5454
# What does your project relate to?
5555
keywords="adafruit blinka circuitpython micropython ble_lywsd03mmc ble lywsd03mmc xiaomi "
56-
"mijia thermometer hygrometer",
56+
"mijia thermometer hygrometer",
5757
# You can just specify the packages manually here if your project is
5858
# simple. Or you can use find_packages().
5959
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,

0 commit comments

Comments
 (0)