Skip to content

Instantly share code, notes, and snippets.

@mugifly

mugifly/mag.md

Last active Jun 3, 2019
Embed
What would you like to do?
A simplest and unofficial client command for leafee mag (IoT sensor for doors and windows)

mag.py

A simplest and unofficial client command for leafee mag

Installation

It tested on Raspberry Pi Zero W with Raspbian Stretch.

$ sudo apt-get install python-pip libglib2.0-dev bluez-tools
$ sudo pip install bluepy
$ sudo hciconfig hci0 down
$ sudo btmgmt le on
$ sudo hciconfig hci0 up

Usage

$ wget https://gist.githubusercontent.com/mugifly/ccbd49bccaa57b8d98626f67437b618d/raw/mag.py
$ chmod u+x mag.py

$ sudo hcitool lescan

$ ./mag.py ff:ff:ff:ff:ff:ff

Related Projects

  • lmag-server

    • This project allows you to build an unofficial hub (and WebAPI server) for leafee mag on your Raspberry Pi.
  • homeassistant-leafeemag

    • This project allows you to easily make the home automation with using window / door status.
#!/usr/bin/env python
# A simplest and unofficial client command for leafee mag
# https://gist.githubusercontent.com/mugifly/ccbd49bccaa57b8d98626f67437b618d/
import sys
import binascii
from bluepy.btle import Peripheral
if len(sys.argv) != 2:
print '[Usage] python %s ff:ff:ff:ff:ff:ff' % sys.argv[0]
print '\nYou can get a MAC address of device with using $ sudo hcitool lescan'
quit(1)
mac = sys.argv[1]
print 'Connecting... ' + mac
p = Peripheral(mac)
sensor_service = p.getServiceByUUID('3c111002-c75c-50c4-1f1a-6789e2afde4e')
sensor_characteristic = sensor_service.getCharacteristics('3c113000-c75c-50c4-1f1a-6789e2afde4e')[0]
sensor_value = sensor_characteristic.read().encode('unicode_escape')
print 'Sensor Value = ' + sensor_value
battery_service = p.getServiceByUUID('0000180f-0000-1000-8000-00805f9b34fb')
battery_characteristic = battery_service.getCharacteristics('00002a19-0000-1000-8000-00805f9b34fb')[0]
battery_value = int(battery_characteristic.read().encode('hex'), 16)
print 'Battery Value = ' + str(battery_value) + '%'
if sensor_value == '\\x01':
print 'Door = Closed'
elif sensor_value == '\\x00':
print 'Door = Opened'
else :
print 'Door = Unknown'
p.disconnect()
@mugifly

This comment has been minimized.

Copy link
Owner Author

@mugifly mugifly commented Mar 17, 2019

Now, we can check the status of doors and windows of the home via HTTP (WebAPI) 😄
It doesn't require an official hub device.
Instead you need to build your own hub on Raspberry Pi or compatible devices.
https://github.com/mugifly/lmag-server

@mugifly

This comment has been minimized.

Copy link
Owner Author

@mugifly mugifly commented Apr 14, 2019

I made a custom component of Home Assistant for leafee mag.
Let's enjoy to home automation 😊
https://github.com/mugifly/homeassistant-leafeemag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.