The Project board is for sharing projects based on Silicon Labs' component with other community members. View Projects Guidelines ›

Hello everyone.

Few days ago I received the new Thunderboard Sense kit and I decide to make sound alarm which will notify me on telegram. 

Firstly I create bot and get token. How to do this you can read here https://core.telegram.org/bots 

Then I install on my raspberypi bluepy and telepot modules for python

 

sudo pip install telepot
sudo pip install bluepy

 

 

Ok! Now we are ready for experiments.

Firstly we need to know which characteristics are responsible for onboard LED and microphone sensor.

Let`s create simple python script  and discover all present characteristics on Thunderboard Sense kit.

 

from bluepy.btle import *
import struct


def ble_micro_handle():
    scanner = Scanner(0)
    devices = scanner.scan(3)
    for dev in devices:
         print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
       
         for (adtype, desc, value) in dev.getScanData():
             print "  %s = %s" % (desc, value)
    num_ble = len(devices)
    print num_ble
    if num_ble==0:
        return None
    ble_mic = []
    char_sensor = 0
    non_sensor = 0
    led_char = Characteristic
    mic_char = Characteristic
    led_handle =0
    
    for i in range(num_ble):
        try:
            devices[i].getScanData()
            ble_mic.append(Peripheral())
            ble_mic[char_sensor].connect(devices[i].addr,devices[i].addrType)
            char_sensor = char_sensor + 1
            print "Connected %s device with addr %s " % (char_sensor, devices[i].addr)
        except:
            non_sensor = non_sensor + 1
    try:
        for i in range(char_sensor):
            services = ble_mic[i].getServices()
            characteristics = ble_mic[i].getCharacteristics()
            for k in characteristics:
                print k
                if k.uuid=="fcb89c40-c603-59f3-7dc3-5ece444a401b":
                    print "LED CONTROL FOUND"
                    led_char = k
                    led_handle = k.getHandle()
                if k.uuid == "c8546913-bf02-45eb-8dde-9f8754f4a32e":
                    print "MIC SENSOR FOUND"
                    mic_char = k            
    except:
        return None

 

This is our result

 

Characteristic <Device Name>
Characteristic <Appearance>
Characteristic <Service Changed>
Characteristic <Manufacturer Name String>
Characteristic <Model Number String>
Characteristic <Serial Number String>
Characteristic <Hardware Revision String>
Characteristic <Firmware Revision String>
Characteristic <System ID>
Characteristic <Battery Level>
Characteristic <UV Index>
Characteristic <Pressure>
Characteristic <Temperature>
Characteristic <Humidity>
Characteristic <c8546913-bfd9-45eb-8dde-9f8754f4a32e>
Characteristic <c8546913-bf02-45eb-8dde-9f8754f4a32e>
MIC SENSOR FOUND
Characteristic <c8546913-bf03-45eb-8dde-9f8754f4a32e>
Characteristic <ec61a454-ed01-a5e8-b8f9-de9ec026ec51>
Characteristic <efd658ae-c401-ef33-76e7-91b00019103b>
Characteristic <efd658ae-c402-ef33-76e7-91b00019103b>
Characteristic <efd658ae-c403-ef33-76e7-91b00019103b>
Characteristic <fcb89c40-c601-59f3-7dc3-5ece444a401b>
Characteristic <fcb89c40-c602-59f3-7dc3-5ece444a401b>
Characteristic <fcb89c40-c603-59f3-7dc3-5ece444a401b>
LED CONTROL FOUND
Characteristic <fcb89c40-c604-59f3-7dc3-5ece444a401b>
Characteristic <2a56>
Characteristic <2a56>
Characteristic <c4c1f6e2-4be5-11e5-885d-feff819cdc9f>
Characteristic <b7c4b694-bee3-45dd-ba9f-f3b5e994f49a>
Characteristic <71e30b8c-4131-4703-b0a0-b0bbba75856b>

Microphone Characteristic <c8546913-bf02-45eb-8dde-9f8754f4a32e> we can only READ, there is no notification or indication.We will periodically poll this characteristic  in this project to get current value from microphone.

 

LED Control Characteristic <fcb89c40-c603-59f3-7dc3-5ece444a401b> we can READ, WRITE and INDICATE. In this project we will use only WRITE possibility. To control LEDs we must write 4 bytes into this characteristic: 0xff,0x00,0x00,0x00.

First byte 0xff are used to ON all 4 led. Other 3 bytes are RED,GREEN,BLUE colors. Any value from 0x00 to 0xff can be written for specified color. Combination of them create many pretty colors.

So if we write 0xff,0x00,0x00,0ff then we get the bright BLUE color for all four onboard LEDs.

 

As I mentioned above we will poll the microphone and constrain it value to  the range from 0x00 to 0xff, and then write bytes array to the LED CONTROL Characteristic. But It would be bored and simple,so I add some smooth transition for our LED between values. 

And  finally ALARM. When  obtained value from microphone will be higher than established treshold, our bot must send a message to Telegram account.

Here is a code:

from bluepy.btle import *
import struct
import thread
import telepot
from datetime import datetime
print   datetime.now().time()
bot = telepot.Bot('HERE MUST BE YOUR TOKEN')
bot_data = bot.getMe()
print bot_data
response = bot.getUpdates()
print response
bot.sendMessage(HERE_ID_WHERE_YOU_WANT_TO_SEND_MESSAGE,"HELLO MAX")
def map(x, in_min, in_max, out_min, out_max):
    return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min)


def SENSE_handle():
    scanner = Scanner(0)
    devices = scanner.scan(3)
    for dev in devices:
         print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
      
         for (adtype, desc, value) in dev.getScanData():
             print "  %s = %s" % (desc, value)
    num_ble = len(devices)
    print num_ble
    if num_ble==0:
        return None
    ble_mic = []
    char_sensor = 0
    non_sensor = 0
    led_char = Characteristic
    mic_char = Characteristic
    led_handle =0
    
    for i in range(num_ble):
        try:
            devices[i].getScanData()
            ble_mic.append(Peripheral())
            ble_mic[char_sensor].connect(devices[i].addr,devices[i].addrType)
            char_sensor = char_sensor + 1
            print "Connected %s device with addr %s " % (char_sensor, devices[i].addr)
        except:
            non_sensor = non_sensor + 1
    try:
        for i in range(char_sensor):
            
            services = ble_mic[i].getServices()
            characteristics = ble_mic[i].getCharacteristics()
            for k in characteristics:
                print k
                if k.uuid=="fcb89c40-c603-59f3-7dc3-5ece444a401b":
                    print "LED CONTROL FOUND"
                    led_char = k
                    led_handle = k.getHandle()
                if k.uuid == "c8546913-bf02-45eb-8dde-9f8754f4a32e":
                    print "MIC SENSOR FOUND"
                    mic_char = k
            
    except:
        return None
    
    led_bytes =[0xFF,0x00,0x00,0x00]
    last_value = 0x00
    while True:
        mic_data = mic_char.read()
        mic_data_value =(ord(mic_data[1])<<8)+ord(mic_data[0])
        led =map(mic_data_value, 4100, 9999, 0, 0xff)
        print "LED     :",led
        print "LAST VAL:",last_value
        if led >150:
            bot.sendMessage(HERE_ID_WHERE_YOU_WANT_TO_SEND_MESSAGE,"SOUND ALERT"+str(datetime.now().time()))
        if last_value < led:
            while last_value<=led:
                print "LAST VALUE LOWER"
                led_bytes[1]=last_value
                # led_bytes[2]=led
                led_bytes[3]=last_value
                s =""
                for i in led_bytes:
                    s+=chr(i)
               
                ble_mic[0].writeCharacteristic(led_handle,s,True)
                last_value=last_value+10
        if last_value > led:
            print "LAST VALUE HIGHER"
            while last_value>led:              
                led_bytes[1]=last_value
                # led_bytes[2]=led
                led_bytes[3]=last_value
                s =""
                for i in led_bytes:
                    s+=chr(i)                
                ble_mic[0].writeCharacteristic(led_handle,s,True)
                last_value=last_value-10
        last_value = led       
    
      
while True:
    SENSE_handle()

And now video how it works

 

  • Bluetooth Low Energy
  • Projects
  • Wireless
  • Sensors
  • @Namo_Aton - yes, Bluepy is installed (from this link) https://github.com/IanHarvey/bluepy Perhaps I should uninstall and reinstall again?

     

    I'm not sure whether it is a version problem on Python... which one should I be using Python 2 or Python 3? 

     

    As I'm new to the RPI3... I assume I'm running this code through Python in the GUI and not in Terminal?

     

    Thought this might be an easy process, but seems to be stumping me... Thanks. 

    0
  • @neal_tommy this sample work on python2.

    and this code must be runnig from terminal and with superuser rights.

    sudo python test.py
    0
  • pi@raspberrypi:~ $ sudo python test.py
    python: can't open file 'test.py': [Errno 2] No such file or directory

    So am I doing something wrong here that I can't even get the most basic code to run? Thought I might need to be in the folder... but which one? It doesn't seem to be in the Bluepy directory. 

     

    Sorry for the lame questions here. 

    0
  • @neal_tommy test.py this must be filename that you created.

    next you must be in directory where your file is located. if no result write me in private message

    0
  • Hello  @Audun

    could you please share with us where did you attach the XML file describing all characteristics supported by the Thunderboard Sense?

    I'm trying to write a python script to stream in the Raspberry Pi the X, Y, Z motion data (and then control Philps Hue lights with these values) ;)

    Thank you, 

    Dan

    0