GithubHelp home page GithubHelp logo

rroemhild / ubeacon Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 3.0 95 KB

MicroPython library for encode and decode BLE beacons

License: MIT License

Python 100.00%
altbeacon ble bluetooth eddystone ibeacon micropython ruuvitag lintech beacons mikrotik

ubeacon's Introduction

uBeacon

uBeacon is a simple Bluetooth Low Energy (BLE) beacon library for MicroPython. It can be used to create beacons for BLE advertisement or to decode advertised beacons. It does not handle the advertisement or scanning part directly, as different MicroPython forks handle Bluetooth in various ways.

Supported BLE beacons:

  • AltBeacon
  • Eddystone-UID
  • Eddystone-URL
  • iBeacon
  • LinTech Beacon
  • RuuviTag (decode only)
  • MikroTik (decode only)

Installation

Install with mip

You can install uBeacon with mip using the following command:

mpremote mip install github:rroemhild/ubeacon

Copy files

You can also manually copy the files from the ubeacon directory to your MicroPython device. You may exclude the beacon types that are not required for your project, ensure to keep the __init__.py file as it is mandatory.

mpremote mkdir lib/ubeacon
mpremote cp ubeacon/* :lib/ubeacon

Usage

The uBeacon library provides several examples in the examples directory, where you can find examples on how to advertise a beacon and decode beacons scanned by your device.

Advertise Beacon

To advertise a beacon, you first need to create a beacon object using one of the provided classes. For example, to create an iBeacon object:

from ubeacon.ibeacon import IBeacon

beacon = IBeacon(
    uuid="acbdf5ff-d272-45f5-8e45-01672fe51c47",
    major=42,
    minor=21,
)

Once you have created a beacon object, you can start advertising it using i.e. the bluetooth.BLE module:

import bluetooth

ble = bluetooth.BLE()
ble.active(True)
ble.gap_advertise(250_000, adv_data=beacon.adv_data, resp_data=beacon.resp_bytes, connectable=False)

Decode Beacon

To decode a beacon, you first need to obtain the beacon data from a scan result. The data is typically stored in a format like adv_data. For example:

from ubeacon.altbeacon import AltBeacon

adv_data = b"\x02\x01\x06\x1b\xff9\x05\xbe\xac=\xf9=Z\xa1\xf2G\xbb\xa3\xcf>I\xe6\xa8\x9b\xb6\x00\x11\x00*\xbb#"
beacon = AltBeacon(adv_data=adv_data)
print(f"{beacon,!r}")

Filter Beacon

The uBeacon library provides a BeaconFilter class that allows you to filter beacons based on their UUID, Major, and Minor. For example:

from ubeacon import BeaconFilter

# Initialize the filter object to filter by uuid and major
beacon_filter = BeaconFilter(
    uuid="7dc04cb6-ed25-420a-ae02-f31674a1f946",
    major=1337,
)

if not beacon_filter.match(beacon):
    print("Beacon does not match filter.")

Beacon Naming

The beacon name is included in the response data.

To ensure compatibility with different MicroPython forks, uBeacon utilizes micropython.unique_id to obtain a unique ID based on the Wi-Fi MAC address. To change the beacon name, it can be set after instantiation. For example, to use the last 2 bytes from the Bluetooth MAC address:

import bluetooth
from ubeacon.eddystone import EddystoneUID

NAMESPACE = "85b9ae954b59c3d6f69d"
INSTANCE = "000000001337"

ble = bluetooth.BLE()

beacon = EddystoneUID(NAMESPACE, INSTANCE)
beacon.name = b"ubeacon " + hexlify(ble.config("mac")[1][4:]).upper()

ubeacon's People

Contributors

rroemhild avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.