Exemplo n.º 1
0
    def __init__(self, advertising_packet):
        """Set up a beacon with the given AdvertisingPacket.

        :param AdvertisingPacket advertising_packet
        """
        self._broadcaster = bleio.Peripheral(name=None)
        self._advertising_packet = advertising_packet
lis3dh.range = adafruit_lis3dh.RANGE_2_G  #options are 2g,4g,8g,16g

greenLed = DigitalInOut(board.D3)
greenLed.direction = Direction.OUTPUT

# Create a Characteristic.
chara = bleio.Characteristic(bleio.UUID(0x2A37), read=True,
                             notify=True)  #heart rate measurement

# Create a Service providing that one Characteristic.
serv = bleio.Service(
    bleio.UUID(0x180D),
    [chara])  # Heart Rate Monitor... just for fun could be anything

# Create a peripheral and start it up.
periph = bleio.Peripheral([serv])
periph.start_advertising()

while not periph.connected:
    # Wait for connection.
    greenLed.value = False
    periph.start_advertising()
    time.sleep(.5)
    pass

while periph.connected:
    x, y, z = [
        value / adafruit_lis3dh.STANDARD_GRAVITY
        for value in lis3dh.acceleration
    ]
    print("x = %0.3f G, y = %0.3f G, z = %0.3f G" % (x, y, z))
Exemplo n.º 3
0
# Testa Bluepy på raspberryn
import bleio

# Create a Characteristic.
chara = bleio.Characteristic(bleio.UUID(0x2919), read=True, notify=True)

# Create a Service providing that one Characteristic.
serv = bleio.Service(bleio.UUID(0x180f), [chara])

# Create a peripheral and start it up.
periph = bleio.Peripheral([service])
periph.start_advertising()

while not periph.connected:
    # Wait for connection.
    pass

if periph.connected:
    print("Connected!!!")
Exemplo n.º 4
0
def main():
    global ledr, ledg, ledb, led
    global periph
    global serv_env_sense
    global chara_c, chara_v
    global sgp30
    global eCO2, eTVOC
    global _btconnected

    # start off with LED(1) off
    initLED()
    sgp30.iaq_init()
    sgp30.set_iaq_baseline(0x8973, 0x8aae)

    uuid_env_sense = bleio.UUID(
        UUID_ENV_SENSING)  # Environmental Sensing service
    uuid_char_eco2 = bleio.UUID(UUID_CHAR_ECO2)  # Temperature characteristic
    uuid_char_etvoc = bleio.UUID(UUID_CHAR_ETVOC)  # Temperature characteristic

    chara_c = bleio.Characteristic(uuid_char_eco2,
                                   notify=True,
                                   read=True,
                                   write=False)
    chara_v = bleio.Characteristic(uuid_char_etvoc,
                                   notify=True,
                                   read=True,
                                   write=False)
    serv = bleio.Service(uuid_env_sense, [chara_c, chara_v])
    periph = bleio.Peripheral([serv], name="CO2")

    periph.start_advertising()

    led_lt = time.monotonic()
    rep_lt = led_lt
    lastConn = False
    while True:
        if (time.monotonic() - led_lt) > 3:
            led_lt = time.monotonic()
            if theled.value == 1:
                theled.value = 0
        if (time.monotonic() - led_lt) > 0.01:
            theled.value = 1

        if periph.connected != lastConn:
            lastConn = periph.connected
            if periph.connected:
                onConnect(periph)
            else:
                onDisconnect(periph)

        if _btconnected:
            if (time.monotonic() - rep_lt) > 5:
                rep_lt = time.monotonic()
                ledb.value = 1
                theled = ledg
                eCO2, eTVOC = sgp30.iaq_measure()
                print("eCO2 = %d ppm \t TVOC = %d ppb" % (eCO2, eTVOC))
                send_data()
        else:
            ledg.value = 1
            theled = ledb
Exemplo n.º 5
0
accelerometer = adafruit_adxl34x.ADXL345(i2c)
adc.reset()

battery_level_chara = bleio.Characteristic(bleio.UUID(0x2919),
                                           read=True,
                                           notify=True)
battery_service = bleio.Service(bleio.UUID(0x180F), [battery_level_chara])

adc1_chara = bleio.Characteristic(bleio.UUID(0x2A58), read=True, notify=True)
adc2_chara = bleio.Characteristic(bleio.UUID(0x2A58), read=True, notify=True)
adc_service = bleio.Service(bleio.UUID(0x1815), [adc1_chara, adc2_chara])

device_uid = binascii.hexlify(microcontroller.cpu.uid)
periph_name = "".join(["TORQUE-", device_uid[-6:].decode("ascii")])
print("Starting peripherial with name: %s" % periph_name)
periph = bleio.Peripheral([battery_service, adc_service], name=periph_name)
periph.start_advertising()
advertising = True

while True:
    battery_voltage = (voltage_monitor.value * 3.3) / 65536 * 2
    battery_soc = int(250 * (battery_voltage - 3) / 3)

    mux_select.value = False
    ch1_value = adc.raw_read()

    mux_select.value = True
    ch2_value = adc.raw_read()

    if periph.connected:
        advertising = False
Exemplo n.º 6
0
def main():
    global ledr, ledg, ledb, led
    global periph
    global serv_env_sense
    global chara_t, chara_p, chara_h, chara_g
    global _btconnected
    global _tempf, _humif, _presf, _gas, _altf

    # start off with LED(1) off
    initLED()

    uuid_env_sense = bleio.UUID(
        UUID_ENV_SENSING)  # Environmental Sensing service
    uuid_char_temp = bleio.UUID(
        UUID_CHAR_TEMPERATURE)  # Temperature characteristic
    uuid_char_humi = bleio.UUID(UUID_CHAR_HUMIDITY)  # Humidity characteristic
    uuid_char_pres = bleio.UUID(UUID_CHAR_PRESSURE)  # Pressure characteristic
    uuid_char_gas = bleio.UUID(UUID_CHAR_GAS)  # Gas characteristic

    try:
        chara_t = bleio.Characteristic(uuid_char_temp,
                                       notify=True,
                                       read=True,
                                       write=False)
        chara_h = bleio.Characteristic(uuid_char_humi,
                                       notify=True,
                                       read=True,
                                       write=False)
        chara_p = bleio.Characteristic(uuid_char_pres,
                                       notify=True,
                                       read=True,
                                       write=False)
        chara_g = bleio.Characteristic(uuid_char_gas,
                                       notify=True,
                                       read=True,
                                       write=False)
        serv = bleio.Service(uuid_env_sense,
                             [chara_t, chara_h, chara_p, chara_g])
        periph = bleio.Peripheral([serv], name="BME680")
    except:
        pass

    try:
        if periph is not None:
            periph.start_advertising()
    except:
        pass

    led_lt = time.monotonic()
    rep_lt = led_lt
    lastConn = False
    while True:
        if (time.monotonic() - led_lt) > 3:
            led_lt = time.monotonic()
            if theled.value == 1:
                theled.value = 0
        if (time.monotonic() - led_lt) > 0.01:
            theled.value = 1

        if periph.connected != lastConn:
            lastConn = periph.connected
            if periph.connected:
                onConnect(periph)
            else:
                onDisconnect(periph)

        if _btconnected:
            if (time.monotonic() - rep_lt) > 5:
                rep_lt = time.monotonic()
                ledb.value = 1
                theled = ledg
                try:
                    _tempf = float(bme680.temperature)
                    _humif = float(bme680.humidity)
                    _presf = float(bme680.pressure)
                    _gas = calcIAQ(bme680.gas)
                    _altf = float(bme680.altitude)
                except:
                    pass
                print("\nTemperature: %0.1f C" % _tempf)
                print("Gas: %d ohm" % _gas)
                print("Humidity: %0.1f %%" % _humif)
                print("Pressure: %0.3f hPa" % _presf)
                print("Altitude = %0.2f meters" % _altf)
                send_data()
        else:
            ledg.value = 1
            theled = ledb
# Create a characteristic for publishing battery level.
# 0x2919 is the characteristic UUID for a device to publish batery level percentage.
# UUIDs for common characteristics are defined by Bluetooth SIG.
# https://www.bluetooth.com/specifications/gatt/characteristics/
battery_level_chara = bleio.Characteristic(bleio.UUID(0x2919),
                                           read=True,
                                           notify=True)

# Create a service for battery information
# 0x180F is the service UUID for publishing characteristics related to the battery.
# UUIDs for common services are defined by Bluetooth SIG.
# https://www.bluetooth.com/specifications/gatt/services/
battery_service = bleio.Service(bleio.UUID(0x180F), [battery_level_chara])

# Create a peripherial with our service and start it
periph = bleio.Peripheral([battery_service])

while True:
    # Start advertising when not connected.
    periph.start_advertising()
    #  Wait for a connection
    while not periph.connected:
        pass

    while periph.connected:
        # Do some math to turn the analog value into a percentage.
        battery_voltage = (voltage_monitor.value * 3.3) / 65536 * 2
        # We assume the battery has a voltage between 3.0 and 4.2 volts.
        battery_percent = 250 * (battery_voltage - 3) / 3

        # Characteristic values must be Bytes. The Battery Level characteristic
Exemplo n.º 8
0
led = DigitalInOut(board.LED1)
led.direction = Direction.OUTPUT
ledG = DigitalInOut(board.LED2_G)
ledG.direction = Direction.OUTPUT
ledB = DigitalInOut(board.LED2_B)
ledB.direction = Direction.OUTPUT

chara = bleio.Characteristic(bleio.UUID(0xA003),
                             read=True,
                             write=True)

serv = bleio.Service(bleio.UUID(0xA000), [chara])

# Create a peripheral and start it up.
periph = bleio.Peripheral([serv], name="BLEtest")
periph.start_advertising(connectable=True, data=None)

ledB.value = ledG.value = led.value = 1

while not periph.connected:
    ledG.value = 1
    ledB.value = 0  # LED2 lyser Blått
    time.sleep(1)

while periph.connected:
    ledG.value = 0  # LED2 lyser Grønt
    ledB.value = 1

    if (chara.value == bytearray([0x01])):
        led.value = 0