Exemplo n.º 1
0
    def __init__(self):
        """
        Create instance of _PoweredUPHandler
        """
        # constants
        self.__IRQ_SCAN_RESULT = const(1 << 4)
        self.__IRQ_SCAN_COMPLETE = const(1 << 5)
        self.__IRQ_PERIPHERAL_CONNECT = const(1 << 6)
        self.__IRQ_PERIPHERAL_DISCONNECT = const(1 << 7)
        self.__IRQ_GATTC_SERVICE_RESULT = const(1 << 8)
        self.__IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9)
        self.__IRQ_GATTC_READ_RESULT = const(1 << 11)
        self.__IRQ_GATTC_NOTIFY = const(1 << 13)

        self.__LEGO_SERVICE_UUID = ubluetooth.UUID(
            "00001623-1212-EFDE-1623-785FEABCD123")
        self.__LEGO_SERVICE_CHAR = ubluetooth.UUID(
            "00001624-1212-EFDE-1623-785FEABCD123")

        # class specific
        self.__ble = ubluetooth.BLE()
        self.__ble.active(True)
        self.__ble.irq(handler=self.__irq)
        self.__decoder = _Decoder()
        self.__reset()
        self.debug = False

        # callbacks
        self.__scan_callback = None
        self.__read_callback = None
        self.__notify_callback = None
        self.__connected_callback = None
        self.__disconnected_callback = None
Exemplo n.º 2
0
    def __init__(self):
        # constants
        self.__IRQ_SCAN_RESULT = const(1 << 4)
        self.__IRQ_SCAN_COMPLETE = const(1 << 5)
        self.__IRQ_PERIPHERAL_CONNECT = const(1 << 6)
        self.__IRQ_PERIPHERAL_DISCONNECT = const(1 << 7)
        self.__IRQ_GATTC_SERVICE_RESULT = const(1 << 8)
        self.__IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9)
        self.__IRQ_GATTC_DESCRIPTOR_RESULT = const(1 << 10)
        self.__IRQ_GATTC_READ_RESULT = const(1 << 11)
        self.__IRQ_GATTC_NOTIFY = const(1 << 13)

        # used to enable notifications
        self.__NOTIFY_ENABLE = const(1)

        # enter device specific service and characteristic UUIDs (from nRF Connect app)
        # the service and characteristic UUIDs below are for the HM-10 BluetoothLE module
        self.__PERIPHERAL_SERVICE_UUID = ubluetooth.UUID(0xFFE0)
        self.__PERIPHERAL_SERVICE_CHAR = ubluetooth.UUID(0xFFE1)
        # enter peripheral device ID here
        self.__DEVICE_ID = b'\x00\x00\x00\x00\x00\x00'

        # class specific
        self.__ble = ubluetooth.BLE()
        self.__ble.active(True)
        self.__ble.irq(handler=self.__irq)
        self.__decoder = Decoder()
        self.__reset()
Exemplo n.º 3
0
def _decode_services(payload):
    services = []
    for u in _decode_field(payload, _ADV_TYPE_UUID16_COMPLETE):
        services.append(ubluetooth.UUID(struct.unpack("<h", u)[0]))
    for u in _decode_field(payload, _ADV_TYPE_UUID32_COMPLETE):
        services.append(ubluetooth.UUID(struct.unpack("<d", u)[0]))
    for u in _decode_field(payload, _ADV_TYPE_UUID128_COMPLETE):
        services.append(ubluetooth.UUID(u))
    return services
Exemplo n.º 4
0
 def decode_services(self, payload):
     services = []
     for u in self.__decode_field(payload, const(0x3)):
         services.append(ubluetooth.UUID(struct.unpack("<h", u)[0]))
     for u in self.__decode_field(payload, const(0x5)):
         services.append(ubluetooth.UUID(struct.unpack("<d", u)[0]))
     for u in self.__decode_field(payload, const(0x7)):
         services.append(ubluetooth.UUID(u))
     return services
Exemplo n.º 5
0
 def register(self):
     # Nordic UART Service (NUS)
     NUS_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
     RX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
     TX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
         
     BLE_NUS = ubluetooth.UUID(NUS_UUID)
     BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE)
     BLE_TX = (ubluetooth.UUID(TX_UUID), ubluetooth.FLAG_NOTIFY)
         
     BLE_UART = (BLE_NUS, (BLE_TX, BLE_RX,))
     SERVICES = (BLE_UART, )
     ((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES)
Exemplo n.º 6
0
    def decode_services(self, payload):
        """
        decode services information from ble data

        :param payload: payload data to decode
        :returns: nothing
        """
        services = []
        for u in self.__decode_field(payload, const(0x3)):
            services.append(ubluetooth.UUID(struct.unpack("<h", u)[0]))
        for u in self.__decode_field(payload, const(0x5)):
            services.append(ubluetooth.UUID(struct.unpack("<d", u)[0]))
        for u in self.__decode_field(payload, const(0x7)):
            services.append(ubluetooth.UUID(u))
        return services
Exemplo n.º 7
0
    def __init__(self):
        # constants
        self.__IRQ_SCAN_RESULT = const(1 << 4)
        self.__IRQ_SCAN_COMPLETE = const(1 << 5)
        self.__IRQ_PERIPHERAL_CONNECT = const(1 << 6)
        self.__IRQ_PERIPHERAL_DISCONNECT = const(1 << 7)
        self.__IRQ_GATTC_SERVICE_RESULT = const(1 << 8)
        self.__IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9)
        self.__IRQ_GATTC_READ_RESULT = const(1 << 11)
        self.__IRQ_GATTC_NOTIFY = const(1 << 13)

        self.__LEGO_SERVICE_UUID = ubluetooth.UUID("00001623-1212-EFDE-1623-785FEABCD123")
        self.__LEGO_SERVICE_CHAR = ubluetooth.UUID("00001624-1212-EFDE-1623-785FEABCD123")

        # class specific
        self.__ble = ubluetooth.BLE()
        self.__ble.active(True)
        self.__ble.irq(handler=self.__irq)
        self.__decoder = Decoder()
        self.__reset()
Exemplo n.º 8
0
    def register(self):
        # Nordic UART Service (NUS)
        SCALE_UUID = ubluetooth.UUID(0x181D)
        SCALE_CHAR = (
            ubluetooth.UUID(0x2A9D),
            ubluetooth.FLAG_READ | ubluetooth.FLAG_NOTIFY,
        )
        SCALE_SERVICE = (
            SCALE_UUID,
            (SCALE_CHAR, ),
        )

        UART_UUID = ubluetooth.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')
        UART_TX = (
            ubluetooth.UUID('6E400003-B5A3-F393-E0A9-E50E24DCCA9E'),
            ubluetooth.FLAG_READ | ubluetooth.FLAG_NOTIFY,
        )
        UART_RX = (
            ubluetooth.UUID('6E400002-B5A3-F393-E0A9-E50E24DCCA9E'),
            ubluetooth.FLAG_WRITE,
        )
        UART_SERVICE = (
            UART_UUID,
            (
                UART_TX,
                UART_RX,
            ),
        )
        SERVICES = (
            SCALE_SERVICE,
            UART_SERVICE,
        )
        (
            (self.scale_ble, ),
            (
                self.tx,
                self.rx,
            ),
        ) = self.ble.gatts_register_services(SERVICES)
Exemplo n.º 9
0

def advertise():
    print("# Start advertising")
    ble.gap_advertise(100, to_byte('ESP32_ENV'))


print("config ble")
# Configuration
ble = bluetooth.BLE()
ble.active(True)
ble.irq(ble_irq)

print("start ble service")
# GATT Server
UART_UUID = bluetooth.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')
UART_TX = (
    bluetooth.UUID('6E400003-B5A3-F393-E0A9-E50E24DCCA9E'),
    bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
UART_RX = (
    bluetooth.UUID('6E400002-B5A3-F393-E0A9-E50E24DCCA9E'),
    bluetooth.FLAG_WRITE,
)
UART_SERVICE = (
    UART_UUID,
    (
        UART_TX,
        UART_RX,
    ),
)
Exemplo n.º 10
0
# Advertising payloads are repeated packets of the following form:
#1 byte data length (N + 1)
#1 byte type (see constants below)
#N bytes type-specific data

_ADV_TYPE_FLAGS = const(0x01)
_ADV_TYPE_NAME = const(0x09)
_ADV_TYPE_UUID16_COMPLETE = const(0x3)
_ADV_TYPE_UUID32_COMPLETE = const(0x5)
_ADV_TYPE_UUID128_COMPLETE = const(0x7)
# _ADV_TYPE_UUID16_MORE = const(0x2)
# _ADV_TYPE_UUID32_MORE = const(0x4)
# _ADV_TYPE_UUID128_MORE = const(0x6)
_ADV_TYPE_APPEARANCE = const(0x19)

_UART_UUID = ubluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_TX_UUID = ubluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")
_UART_RX_UUID = ubluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
_LEGO_SERVICE_UUID = ubluetooth.UUID("00001623-1212-EFDE-1623-785FEABCD123")
_LEGO_SERVICE_CHAR = ubluetooth.UUID("00001624-1212-EFDE-1623-785FEABCD123")
_UART_TX = (
    _UART_TX_UUID,
    _FLAG_NOTIFY,  # | _FLAG_WRITE,
)
_UART_RX = (
    _UART_RX_UUID,
    _FLAG_WRITE | _FLAG_WRITE_NO_RESPONSE,
)
_UART_SERVICE = (
    _UART_UUID,
    (_UART_TX, _UART_RX),
Exemplo n.º 11
0
        ble.gap_advertise(500000)
        print('[bluetooth] device disconnected')
    else:
        print('[bluetooth] received event {}'.format(event))


# entrypoint
i2c = I2C(0)
imu = MPU6050(i2c)
ble = bluetooth.BLE()
ble.active(True)
ble.irq(bt_irq)
ble.config(gap_name='Bosu')

# configure the services that will be advertised
ACCEL_UUID = bluetooth.UUID('7ED5A5BC-8013-4753-B199-0A364D52E5DE')
ACCEL_CHAR = (
    bluetooth.UUID('F477FD95-41F0-4C73-9093-5DA7DC624DF0'),
    bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,
)
ACCEL_SERVICE = (
    ACCEL_UUID,
    (ACCEL_CHAR, ),
)
SERVICES = (ACCEL_SERVICE, )

((accel, ), ) = ble.gatts_register_services(SERVICES)

connections = set()

# this advertising payload can't be too long
Exemplo n.º 12
0
_ble = ubluetooth.BLE()
_bleNetConfigStatus = None
_ble_adv_name = 'esp-node'
_ble_tx = None
_ble_rx = None
_ble_msg = ''

BLE_CONNECTED = const(0x00)
BLE_DISCONNECTED = const(0x01)
BLE_COMMINICATING = const(0x02)

NUS_UUID = 0xFFA0
RX_UUID = 0xFFA2
TX_UUID = 0xFFA3

BLE_NUS = ubluetooth.UUID(NUS_UUID)
BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE)
BLE_TX = (ubluetooth.UUID(TX_UUID),
          ubluetooth.FLAG_NOTIFY | ubluetooth.FLAG_READ)

BLE_UART = (BLE_NUS, (
    BLE_TX,
    BLE_RX,
))
SERVICES = [
    BLE_UART,
]


def send(data):
    _ble.gatts_notify(0, _ble_tx, data + '\n')
Exemplo n.º 13
0
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(1 << 9)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(1 << 10)
_IRQ_GATTC_READ_RESULT = const(1 << 11)
_IRQ_GATTC_WRITE_STATUS = const(1 << 12)
_IRQ_GATTC_NOTIFY = const(1 << 13)
_IRQ_GATTC_INDICATE = const(1 << 14)

_IRQ_GATTC_SERVICE_DONE = const(10)

_ADV_TYPE_UUID16_COMPLETE = const(0x3)
_ADV_TYPE_UUID32_COMPLETE = const(0x5)
_ADV_TYPE_UUID128_COMPLETE = const(0x7)

_COMPANY_IDENTIFIER_CODES = {"0397": "LEGO System A/S"}

_LEGO_SERVICE_UUID = ubluetooth.UUID("00001623-1212-EFDE-1623-785FEABCD123")
_LEGO_SERVICE_CHAR = ubluetooth.UUID("00001624-1212-EFDE-1623-785FEABCD123")


class PowerUPRemote:
    """Class to deal with LEGO(R) PowerUp(TM) Remote Control for Spike Prime"""
    def __init__(self):
        self._ble = ubluetooth.BLE()
        self._ble.active(True)
        self._ble.irq(handler=self._irq)
        self._decoder = Decoder()
        self._reset()

    def _reset(self):
        self._addr = None
        self._addr_type = None
Exemplo n.º 14
0
from micropython import const
import utime
import binascii
import re
import gc

_HANDLE_READ_NAME = 0x03
_HANDLE_READ_BATTERY = 0x18
_HANDLE_READ_VERSION = 0x24
_HANDLE_READ_SENSOR_DATA = 0x10

MI_TEMPERATURE = "temperature"
MI_HUMIDITY = "humidity"
MI_BATTERY = "battery"

_MITemp_UUID = ubluetooth.UUID('0000fe95-0000-1000-8000-00805f9b34fb')

import utime
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
_IRQ_GATTS_WRITE = const(3)
_IRQ_GATTS_READ_REQUEST = const(4)
_IRQ_SCAN_RESULT = const(5)
_IRQ_SCAN_DONE = const(6)
_IRQ_PERIPHERAL_CONNECT = const(7)
_IRQ_PERIPHERAL_DISCONNECT = const(8)
_IRQ_GATTC_SERVICE_RESULT = const(9)
_IRQ_GATTC_SERVICE_DONE = const(10)
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
_IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
Exemplo n.º 15
0
_IRQ_SET_SECRET                     = const(30)

# Advertising types (cf. https://docs.micropython.org/en/latest/library/ubluetooth.html)
_ADV_IND         = const(0x00)
_ADV_DIRECT_IND  = const(0x01)
_ADV_SCAN_IND    = const(0x02)
_ADV_NONCONN_IND = const(0x03)
_ADV_SCAN_RSP    = const(0x04)

# Address types (cf. https://docs.micropython.org/en/latest/library/ubluetooth.html)
ADDR_TYPE_PUBLIC = const(0x00)
ADDR_TYPE_RANDOM = const(0x01)

# Miflora Service / Characteristics UUIDs
# (ROOT_SERVICE could be used for discovery)
_GENERIC_ACCESS_SERVICE_UUID    = ubluetooth.UUID(0x1800)
_GENERIC_ATTRIBUTE_SERVICE_UUID = ubluetooth.UUID(0x1801)
_MIFLORA_ROOT_SERVICE_UUID      = ubluetooth.UUID('0000fe95-0000-1000-8000-00805f9b34fb')
_MIFLORA_DATA_SERVICE_UUID      = ubluetooth.UUID('00001204-0000-1000-8000-00805f9b34fb')
_MIFLORA_FIRM_CHAR_UUID         = ubluetooth.UUID('00001a02-0000-1000-8000-00805f9b34fb')
_MIFLORA_CMD_CHAR_UUID          = ubluetooth.UUID('00001a00-0000-1000-8000-00805f9b34fb')
_MIFLORA_DATA_CHAR_UUID         = ubluetooth.UUID('00001a01-0000-1000-8000-00805f9b34fb')

# Value handles and other magic spells specific to Miflora
_HANDLE_READ_VERSION_BATTERY = 0x38
_HANDLE_WRITE_MODE_CHANGE    = 0x33
_DATA_MODE_CHANGE            = bytes([0xA0, 0x1F])
_HANDLE_READ_SENSOR_DATA     = 0x35

# States of state machine
S_INIT                = const(0)
Exemplo n.º 16
0
_IRQ_PERIPHERAL_CONNECT = const(7)
_IRQ_PERIPHERAL_DISCONNECT = const(8)
_IRQ_GATTC_SERVICE_RESULT = const(9)
_IRQ_GATTC_SERVICE_DONE = const(10)
_IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
_IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
_IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
_IRQ_GATTC_DESCRIPTOR_DONE = const(14)
_IRQ_GATTC_READ_RESULT = const(15)
_IRQ_GATTC_READ_DONE = const(16)
_IRQ_GATTC_WRITE_DONE = const(17)
_IRQ_GATTC_NOTIFY = const(18)
_IRQ_GATTC_INDICATE = const(19)

# org.bluetooth.service.environmental_sensing
_SWITCHBOT_UUID = ubluetooth.UUID('cba20d00-224d-11e6-9fb8-0002a5d5c51b')
_SWITCH_UUID = ubluetooth.UUID('cba20002-224d-11e6-9fb8-0002a5d5c51b')


def decode_mac(addr):
    """
    Decode readable mac address 
    """
    assert isinstance(
        addr, bytes) and len(addr) == 6, ValueError("mac address value error")
    return ":".join(['%02X' % byte for byte in addr])


def encode_mac(mac):
    """
    Encode mac to addvertising addr
Exemplo n.º 17
0
            compile(boot_code, "snippet", 'exec')
            python_code = boot_code
    except Exception as error:
        print("main.py:", sumorobot.config['boot_code'], "compilation failed:",
              error)

# Start BLE
ble = ubluetooth.BLE()
ble.config(gap_name=sumorobot.config['sumorobot_name'])
ble.active(True)

# Register the BLE hander
ble.irq(ble_handler)

# BLE info serivce
INFO_SERVICE_UUID = ubluetooth.UUID(0x180a)
MODEL_CHARACTERISTIC = (
    ubluetooth.UUID(0x2a24),
    ubluetooth.FLAG_READ,
)
FIRMWARE_CHARACTERISTIC = (
    ubluetooth.UUID(0x2a26),
    ubluetooth.FLAG_READ,
)
MANUFACTURER_CHARACTERISTIC = (
    ubluetooth.UUID(0x2a29),
    ubluetooth.FLAG_READ,
)
INFO_SERVICE = (
    INFO_SERVICE_UUID,
    (
Exemplo n.º 18
0
"""
The MIT License (MIT)
Copyright © 2020 Walkline Wang (https://walkline.wang)
Gitee: https://gitee.com/walkline/esp32-ble-uart
Original repo: https://github.com/micropython/micropython/blob/master/examples/bluetooth/ble_uart_peripheral.py
"""
import ubluetooth as bt
from ble.tools import BLETools
from ble.const import BLEConst

__UART_UUID = bt.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
__RX_UUID = bt.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
__TX_UUID = bt.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")

__UART_SERVICE = (
	__UART_UUID,
	(
		(__TX_UUID, bt.FLAG_NOTIFY,),
		(__RX_UUID, bt.FLAG_WRITE,),
	),
)


class BLEUART:
	def __init__(self, ble, rx_callback=None, name="mpy-uart", rxbuf=100):
		self.__ble = ble
		self.__rx_cb = rx_callback
		self.__conn_handle = None

		self.__write = self.__ble.gatts_write
		self.__read = self.__ble.gatts_read
Exemplo n.º 19
0
# Use ampy to put the code and use rshell to REPL into ESP32 and run the script.
# Set it to run on boot by renaming the file to main.py
# IMPORTANT NOTE: Pairing is not supported by ubluetooth yet as of the time of writing of this code.
# Created by James Raphael Tiovalen (2019)

# Import libraries
import ubluetooth, utime
from micropython import const
import machine

# Initialize BLE singleton class object
bt = ubluetooth.BLE()

# Define UUIDs (or can use GATT service name in terms of bytes)
# UUID Generator: https://www.uuidgenerator.net/
LIGHTER_UUID = ubluetooth.UUID(0x1815)
# Indicate GATT characteristics
# List of characteristics: https://www.bluetooth.com/specifications/gatt/characteristics/
LIGHTER_CHAR = (ubluetooth.UUID(0x2B37), ubluetooth.FLAG_READ | ubluetooth.FLAG_NOTIFY,)
LIGHTER_SERVICE = ((LIGHTER_UUID, (LIGHTER_CHAR,),),)

# Define constants for BLE IRQ
_IRQ_ALL = const(0xffff)
_IRQ_CENTRAL_CONNECT                 = const(1 << 0)
_IRQ_CENTRAL_DISCONNECT              = const(1 << 1)
_IRQ_GATTS_WRITE                     = const(1 << 2)
_IRQ_GATTS_READ_REQUEST              = const(1 << 3)
_IRQ_SCAN_RESULT                     = const(1 << 4)
_IRQ_SCAN_COMPLETE                   = const(1 << 5)
_IRQ_PERIPHERAL_CONNECT              = const(1 << 6)
_IRQ_PERIPHERAL_DISCONNECT           = const(1 << 7)