BleuIO’s new firmware version 2.2.0 allows changing the output format

A new AT command has been added that allows the user to see detailed response for every AT commands. This level of details can be helpful for troubleshooting problems as it explains the different error codes, disconnection reason codes and event codes. For that reason, users can take advantage of verbose mode for troubleshooting purposes and turn it off when it’s not needed.

This new mode changes the output to a more structured and unified format which includes command IDs for all command-related messages. The command-related outputs are more easily separatable from the event outputs. The idea is to make the BleuIO output more consistent, easier to use in scripts and generate more useful error messages.

The command to turn this new feature on and off is: ATV1 (on) ATV0 (off).

List of response format and code list is given below.

Response Format

Response TypesDescritonFormat
CCommand response. Assign a response index.{“C”:Command Index,”cmd”:”command“}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
AAcknowledgement response.{“A”:Command Index,”err”:error code in hex,”errMsg”:”Error Message String“}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
RReply response. Different reply data for different commands. Not all commands have reply data.{“R”:Command Index,Reply data}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
EEnd response. Signify end of the command.{“E”:Command Index,”nol”:number of lines belonging to this command (excluding scan responses))}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
SScan data response.{“S”:Command Index,”rssi”:rssi value,”addr”:”mac address“,(if available)”name”:”device name”}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
SFScan find data response.{“SF”:Command Index,(if AT+SHOWRSSI turned on)“rssi”:rssi value,”addr”:”mac address“,”type”:advertising type,”data”:”data in hex”}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
STScan target response.{“ST”:Command Index,(if AT+SHOWRSSI turned on)“rssi”:rssi value,”addr”:”mac address“,”type”:advertising type,”data”:”data in hex”}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
SEScan ended response.{“SE”:Command Index,”action”:”scan completed”}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)
EventsEvent response. Different event response data for different events. All events have event response data.{event code:”Connection Index in hex if any otherwise 0xFFFF“,Event response data}[Carriage Return] (ascii:\r\n hex:0x0A and 0x0D)

Error Code List

Error CodeDescription
0x00Success
0x01Generic failure
0x02Already done
0x03Operation already in progress
0x04Invalid parameter
0x05Not allowed
0x06Not connected
0x07Not supported
0x08Not accepted
0x09Busy
0x0ARequest timed out
0x0BNot supported by peer
0x0CCanceled by user
0x0DEncryption key missing
0x0EInsufficient resources
0x0FNot found
0x10No credits available on L2CAP CoC
0x11MTU exceeded on L2CAP CoC
0x12Insufficient bandwidth

Event Code List

Event CodeDescription
256Connection established
258Disconnection event
260Advertising operation completed
263Connection parameters updated
264Pairing request
265Pairing completed
266Security request from peer
268Passkey request
269Security level changed indication
271Set security level failed
272Connection parameters update completed
273Data length changed
276Numeric request
278Long Term Key missing
279SPS Service Event
768Service found during browsing procedure
769Browsing procedure completed
770Service found during discovery
771Included service found during discovery
772Characteristic found during discovery
773Characteristic descriptor found during discovery
774Discovery completed
775Read attribute value completed
776Write attribute value completed
777Value notification received
778Value indication received

Disconnection Reason Code List

Status CodeDescription
0x08Connection timeout
0x13Remote User terminated connection
0x14Remote device terminated connection due to low resources
0x15Remote device terminated connection due to power off
0x16Connection terminated by local host
0x1FUnspecified error
0x3DConnection terminated due to MIC failure

List of AT commands and their sample output can be found at Our Getting Started Guide.

Share this post on :

Smart Phone Controlled Home Automation using Raspberry Pi and BleuIO

Home automation involves automating household environment equipment. To achieve that, we have created a smart bulb that can be controlled remotely using smart phone app. The aim of this project is to control different home appliances using smartphone at your home.

Introduction

This example is showing how to control a GPIO pin on a RaspberryPi remotely from a smart phone (or another BleuIO Dongle).

For this example we will need:

  • A RaspberryPi
  • A BleuIO Dongle (https://www.bleuio.com/)
  • Our example python script (https://github.com/smart-sensor-devices-ab/bleuio_rpi_switch_example)
  • A way to connect to the GPIO Pin (Like a 5V Relay and a Lightbulb)

WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT.

Connecting the relay

Beware:

Always be very careful when experimenting with AC, electrical shock can result in serious injuries! NOTICE OF RISK; DISCLAIMER OF LIABILITY

alt text

Instructions for bleuio_rpi_switch_example.py

  • Connect the BleuIO Dongle to your RaspberryPi.
  • Edit the variable ‘switch’ in the script to the GPIO pin you want to use. (You can use the command pinout to get a graphical view showing you the GPIO pins for the board)
  • Finally just run python script and and use your phone to connect to the BleuIO Dongle and send on/off messages to controll the GPIO!

Instructions for connecting to the BleuIO from mobile

  • Download a BLE scanning App that can connect and read/write to a device. (Like nRFConnect or BLEScanner)
    AndroidIOS
  • Look for the dongle, it will be advertising as ‘BleuIO’.
  • Connect to the BleuIO Dongle.
  • To enable BleuIO to recieve commands you must first write 0x01 to the Flow Control characteristic (UUID: 0783b03e-8535-b5a0-7140-a304d2495cb9)
  • Now you can write to the Server RX Data characteristic (UUID: 0783b03e-8535-b5a0-7140-a304d2495cba) to control the GPIO.
    |CMD|Effect|
    |–|–|
    |“SW=1”| ON|
    |“SW=0”| OFF|

The script

Here is the python script that receives the messages from smart phone app and helps control the light.

#!/usr/bin/python3
# Copyright 2022 Smart Sensor Devices in Sweden AB
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import time
import serial.tools.list_ports
import serial
import RPi.GPIO as io


switch = 7  # Edit this to suit your setup! (7 = GPIO 04), use command pinout to graphically show you the GPIO pins for the board
io.setmode(io.BOARD)
io.setup(switch, io.OUT)

master_array = []
index = 1
dongle_port = ""

print("\nWelcome to BleuIO RaspberryPi Switch Example!\n")

print("\nPlease insert dongle...")
try:
    while len(master_array) == 0:
        m_ports = serial.tools.list_ports.comports(include_links=False)
        for port in m_ports:
            if str(port.hwid).__contains__("VID:PID=2DCF"):
                master = port.device + " " + port.hwid
                if master.__contains__("VID:PID=2DCF:6002"):
                    print("Found dongle in port: %s" % port.device)
                    master_array.append(master)
                    dongle_port = port
                    break

    for dongle in master_array:
        print("\nConnecting to BleuIO @ %s\n" % dongle)

    time.sleep(0.5)
    dongle_conn = serial.Serial(
        dongle_port.device,
        115200,
        timeout=1,
    )

    if not dongle_conn.is_open:
        dongle_conn.open()

    print("Starting Advertising...")
    dongle_conn.write("AT+GAPDISCONNECTALL\rAT+DUAL\rAT+ADVSTART\rATI\r".encode())
    read_tries = 0
    dongle_resp = ""
    while read_tries < 20:
        dongle_resp = dongle_conn.readline().decode()
        if "Not Advertising" in dongle_resp:
            dongle_conn.write("AT+ADVSTART\r")
        if b"Advertising\r\n" in dongle_resp.encode():
            break
        read_tries += 1
        time.sleep(0.01)

    if dongle_resp:
        print("BleuIO is %s" % dongle_resp)
    else:
        print("ERROR! No response...")
        exit()

    print(
        "Going into loop, waiting for signal to turn switch on/off...\n(Press Ctrl+C to abort)"
    )
    while True:
        try:
            dongle_resp = dongle_conn.readline().decode()
            if "SW=0" in dongle_resp:
                print("Turn Switch off!")
                io.output(switch, io.LOW)
            if "SW=1" in dongle_resp:
                print("Turn Switch on!")
                io.output(switch, io.HIGH)
        except KeyboardInterrupt:
            if dongle_conn.is_open:
                dongle_conn.write("AT+GAPDISCONNECTALL\rAT+ADVSTOP\r".encode())
                dongle_conn.close()
                io.cleanup()
            print("\nBye!")
            exit()

except Exception as e:
    print("(ERROR: %s)" % (e))

Output

We have tested the script using nRFConnect app from both IOS and Android phone to turn on/off the light bulb. Here is the output of this project.

Share this post on :

BleuIO firmware updater is now available for Mac users

BleuIO continues to release firmware versions and adds new features regularly. Therefore, it is important to keep the dongle updated.

BleuIO users were able to update the dongle from Windows and Linux system. This new updater allows updating the dongle from Mac as well.

The BleuIO comes with a bootloader to allow us to update the firmware or flash our own application to the dongle. To flash the dongle we will need an image file containing the new firmware or our own application and the updater script.

How to update a dongle

Requirements:

Steps

  • Download and extract the updater zip file.
  • Place the firmware image file into the same folder. (list of firmware image files are available here).
  • Open the command prompt and go to the extracted folder where bleuio_fw_updater.py is available.
  • Run: python bleuio_fw_updater.py image_file_name.img
  • “Success” message along with the updated firmware version will be shown on the screen once the process is completed.
Share this post on :

BleuIO firmware auto updater is now available

BleuIO continues to release firmware versions and adds new features regularly. Therefore, it is important to update our dongle regularly.

The BleuIO comes with a bootloader to allow us to update the firmware or flash our own application to the dongle. To flash the dongle we will need an image file containing the new firmware or our own application and a host USB loader application.

To update the firmware, we need to select the correct COM port where the BleuIO dongle is connected. Keep in mind that the bootloader only opens for about 10 seconds after inserting the Dongle, then it switches to the main app and the COM port number changes. We cannot use the main app’s Serial Port number to flash the dongles!

To overcome this COM port selection process while updating a firmware, BleuIO team has developed an auto updater. This updater selects the right COM port for the dongle before updating. All we need to do is, run the updater and follow the instructions on the screen.

How to update a dongle using Auto Updater

  • Download and extract the auto updater zip file.
  • Place the firmware image file into the same folder. (list of firmware image files are available here).
  • Open the command prompt as Administrator.
  • Go to the extracted folder where bleuio_auto_updater.py is available.
  • Run: python bleuio_auto_updater.py image_file_name.img
  • Insert dongle when prompted to.
  • “Update done” will be shown on the screen once the process is completed.
Share this post on :

BLE USB dongle throughput measurement

Introduction

Here we will describe two quick ways of measuring the data throughput of the BleuIO Dongle.
For both examples we are going to need a BleuIO Dongle, another Bluetooth device (like another Bleuio Dongle) and a computer with Python (minimum version: 3.6) installed.

For the first measurement example, measuring the BLE data throughput, you will need one of the following supported development kits from Nordic Semiconductor:

  • nRF52840 DK (PCA10056)
  • nRF52840 Dongle (PCA10059)
  • nRF52833 DK (PCA10100)
  • nRF52 DK (PCA10040)
  • nRF51 DK (PCA10028)
  • nRF51 Dongle (PCA10031)

The first measurement example is the actual BLE data throughput. For this we will use a BleuIO Dongle and Wireshark. (For help on how to setup Wireshark and requirements go to this link: https://infocenter.nordicsemi.com/topic/ug_sniffer_ble/UG/sniffer_ble/intro.html ).
We will also utilize a simple python script that sends a set amount of data. For this measurement you can ignore the throughput print at the end of the script.

The second measurement example is for measuring the actual data being transferred over the USB as a Virtual COM port (via the CDC protocol).
We will be using the same simple script that will send a set amount of data and time when the transfer starts and then stops. Then divide the amount of data with the time the transfer took to get the throughput.

Notice : Interference can be caused by other wireless networks, other 2.4 GHz frequency devices, and high voltage devices that generate electromagnetic interference. This have impact on the measurement of throughput. To avoid interference, select wireless free space or use a shield box.

Instructions for BLE data throughput

  • For best result place the nRF Dev Kit between the BleuIO Dongle and your target device.
  • Open Wireshark and double-click the ‘nRF Sniffer for Bluetooth LE’.
  • Make sure the target Bluetooth device is advertising and find in the the scroll-down list.
  • Choose ‘IO/Data’ under the ‘Analysis’ menu tab.
  • Click the ‘+’ button to add new graphs. Add ‘bytes per seconds’ and/or ‘bit per seconds’.
  • Modify the script by filling in the relevant information into the variables ‘your_com_port’‘target_mac_addr’ and ‘write_handle’.
  • Run the python script.
  • You can now observe the graph showing the BLE Data throughput!

Instructions for USB port data throughput

This is the second measurement example for measuring the actual point to point data transfer between the two USB ports.

  • Connect the dongle to your computer. (Look up the COM port your dongle uses and paste it in the script in the variable ‘your_com_port’)
  • Scan (Using AT+GAPSCAN) after the device you wish to send the data to. Copy the mac address of the device into the script in the variable ‘target_mac_addr’.
  • Connect to the device and look up the handle of the characteristic you want to write to and paste into the script in the variable ‘write_handle’.
  • Finally just run python script and the throughput will be displayed at the end!

The script

import datetime
import serial
import time
import string
import random

connecting_to_dongle = True
trying_to_connect = False

# Change this to the com port your dongle is connected to.
your_com_port = "COM20"
# Change this to the mac address of your target device.
target_mac_addr = "[0]40:48:FD:E5:2C:F2"
# Change this to the handle of the characteristic on your target device.
write_handle = "0011"

# You can experiment with the packet length, increasing or decreasing it and see how that effect the throughput
packet_length = 150
# 1 Megabytes = 1000000 Bytes
file_size = 0.5 * 1000000
end_when = file_size / packet_length
send_counter = 0

# Random data string generator
def random_data_generator(size=packet_length, chars=string.digits + string.digits):
    return "".join(random.choice(chars) for _ in range(size))


print("Connecting to dongle...")
while connecting_to_dongle:
    try:
        console = serial.Serial(
            port=your_com_port,
            baudrate=115200,
            parity="N",
            stopbits=1,
            bytesize=8,
            timeout=0,
        )
        if console.is_open.__bool__():
            connecting_to_dongle = False
    except:
        print("Dongle not connected. Please reconnect Dongle.")
        time.sleep(5)

print("Connected to Dongle.")
console.write(str.encode("AT+GAPDISCONNECT\r"))
start = input("Press Enter to start.\n\r>> ")

console.write(str.encode("ATE0\r"))
console.write(str.encode("AT+DUAL\r"))
connected = "0"
while connected == "0":
    time.sleep(0.5)
    if not trying_to_connect:
        # change to Mac address of the device you want to connect to
        console.write(str.encode("AT+GAPCONNECT=" + target_mac_addr + "\r"))
        trying_to_connect = True
    dongle_output2 = console.read(console.in_waiting)
    time.sleep(2)
    print("Trying to connect to Peripheral...")
    if not dongle_output2.isspace():
        if dongle_output2.decode().__contains__("\r\nCONNECTED."):
            connected = "1"
            print("Connected!")
            time.sleep(8)
        if dongle_output2.decode().__contains__("\r\nDISCONNECTED."):
            connected = "0"
            print("Disconnected!")
            trying_to_connect = False
        dongle_output2 = " "

start2 = input("Press Enter to sending.\n\r>> ")
start_time = time.mktime(datetime.datetime.today().timetuple())
console.write(
    str.encode(
        "AT+GATTCWRITEWRB=" + write_handle + " " + random_data_generator() + "\r"
    )
)
while 1:
    dongle_output = console.read(console.in_waiting)
    if send_counter > end_when:
        end_time = time.mktime(datetime.datetime.today().timetuple())
        break
    # Change to the handle of the characteristic you want to write to
    if "handle_evt_gattc_write_completed" in str(dongle_output):
        console.write(
            str.encode(
                "AT+GATTCWRITEWR=" + write_handle + " " + random_data_generator() + "\r"
            )
        )
        send_counter = send_counter + 1
    try:
        if not dongle_output.decode() == "":
            print(dongle_output.decode())
    except:
        print(dongle_output)

time_elapsed = end_time - start_time
time.sleep(0.1)

print("*" * 25)
print("Transfer Complete in: " + str(time_elapsed) + " seconds")
print(str(packet_length * send_counter) + "bytes sent.")
print("*" * 25)
print(
    "Throughput via USB (Virtual COM port): "
    + str((packet_length * send_counter) / time_elapsed)
    + " Bytes per seconds"
)
print("*" * 25)
Share this post on :

Send BLE sensor data over MQTT using BleuIO

We’re living in the world of connected devices. The internet of things helps us live and work smarter, as well as gain complete control over our lives. One of the latest technological advancements in IoT is the MQTT gateway, which acts as a mediator between the cloud and IoT platforms.

MQTT stands for Message Queuing Telemetry Transport. It’s among the key communication protocols for the internet of things devices and local networks. It’s an ideal protocol for communication between smart devices or machine-to-machine communication.

What Is MQTT Gateway?

Generally, the MQTT gateway can be defined as an intermediary between any internet of things platform and sensors. It works by getting data from these sensors or smart devices and translating it into MQTT. It then transmits that data to either the internet of things platform or to the MQTT broker.

The publish/subscribe pattern

The publish/subscribe pattern (also known as pub/sub) provides an alternative to a traditional client-server architecture. In the client-server model, a client communicates directly with an endpoint. The pub/sub model decouples the client that sends a message (the publisher) from the client or clients that receive the messages (the subscribers). The publishers and subscribers never contact each other directly. In fact, they are not even aware that the other exists. The connection between them is handled by a third component (the broker). The job of the broker is to filter all incoming messages and distribute them correctly to subscribers.

MQTT Broker

A broker helps in handling clients in MQTT technology. It can manage hundreds, thousands, or millions of connected MQTT clients at once, depending on the implementation. Its main functions are;

  • Receiving information
  • Decoding and filtering the messages received
  • Determining which client will be interested in which message
  • Transmitting these messages to clients depending on their interests

The project

Let’s make a simple BLE 2 MQTT project that collects sensor data from a BLE Air quality monitor device called HibouAir and sends it to a free public MQTT broker.

For this project, we will use Flespi. You can choose any public or private MQTT broker as you like.

Steps

Requirments 

  • BleuIO dongle.
  • air quality monitor device HibouAir.
  • A public MQTT broker (Flepsi token) https://flespi.com/mqtt-broker
  • BleuIO Javascript library. https://www.npmjs.com/package/bleuio
  • A build tool for Javascript (parcel) https://parceljs.org/docs/

Get the Flespi token

  • Create an account at Flespi.
  • Log into the Flespi dashboard.
  • Copy the token

Download source file

Get the source file from https://github.com/smart-sensor-devices-ab/ble2mqtt_bleuio.git

And run npm install

In the root folder, we will see two Html files called index.html and subscribe.html and two js files called pub.js and sub.js

Index.html file collects sensor data from a BLE Air quality monitor device called HibouAir with the help of BleuIO. It has three buttons. connect, device info and Scan and Send BLE Data.

First we need to connect a BleuIO dongle into the computer and connect to it using connect button. The device info button will show BleuIO dongle status on console log. And the Scan and Send BLE data will scan for Air quality data and send it to the cloud. For this script I am scanning and collecting a fixed device with the board id of 0578E0. You can change the value in pub.js file line number 4

Here is the index.html file

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
      crossorigin="anonymous"
    />
    <title>Publish</title>
  </head>
  <body>
    <div class="container mt-5">
      <button id="connect" class="btn btn-secondary">Connect</button>
      <button id="deviceinfo" class="btn btn-success d-none">
        Device Info
      </button>
      <button id="sendData" class="btn btn-primary">
        Scan and Send BLE Data
      </button>

      <h3 class="mt-5">Message Sent</h3>
      <div id="output"></div>
    </div>
  </body>
  <script src="./pub.js"></script>
</html>

After collecting advertised data, we try to decode it and get meaningful air quality data with co2, pressure, temperature, humidity, light values. Then we publish the data to Flepsi broker using topic name HibouAirTopic

The process is done in pub.js file.

Here is the pub.js file

const clientId = 'hibouair_' + Math.random().toString(16).substr(2, 8)
import * as my_dongle from 'bleuio'
import 'regenerator-runtime/runtime'
const sensorID = '0578E0'
import * as mqtt from "mqtt" 
document.getElementById('connect').addEventListener('click', function(){
  my_dongle.at_connect()
})
document.getElementById('deviceinfo').addEventListener('click', function(){
  my_dongle.ati().then((data)=>console.log(data))
})

const getTheBLEData = ( async()=>{
  return my_dongle.at_dual().then(()=>{
    return my_dongle.at_findscandata(sensorID,6).then((x)=>{
      let advData = x[x.length - 1].split(" ").pop()
      let positionOfID= advData.indexOf(sensorID);
      let tempHex = advData.substring(positionOfID+14, positionOfID+18)
      let temp = parseInt('0x'+tempHex.match(/../g).reverse().join(''))/10;

      let pressHex = advData.substring(positionOfID+10, positionOfID+14)
      let press = parseInt('0x'+pressHex.match(/../g).reverse().join(''))/10;

      let humHex = advData.substring(positionOfID+18, positionOfID+22)
      let hum = parseInt('0x'+humHex.match(/../g).reverse().join(''))/10;

      let lightHex = advData.substring(positionOfID+6, positionOfID+10)
      let light = parseInt('0x'+lightHex.match(/../g).reverse().join(''));

      let co2Hex = advData.substring(positionOfID+38, positionOfID+42)
      let co2 = parseInt('0x'+co2Hex);
      

      return {
        'CO2' :co2,
        'Temp' :temp,
        'Pressure':press,
        'Humidity':hum,        
        'Light':light,        
      }
    })
  })
})



//pass data
document.getElementById('sendData').addEventListener('click', function(){
    const host = 'wss://mqtt.flespi.io:443'

    const options = {
      keepalive: 60,
      clientId: clientId,
      protocolId: 'MQTT',
      username:'SET_YOUR_USERNAME',
      password:'SET_YOUR_PASSWORD',
      protocolVersion: 4,
      clean: true,
      reconnectPeriod: 1000,
      connectTimeout: 30 * 1000,
      will: {
        topic: 'HibouAirMsg',
        payload: 'Connection Closed abnormally..!',
        qos: 0,
        retain: false
      },
    }
    const client  = mqtt.connect(host, options)
    client.on('connect',()=>{
        console.log('connected mqtt client',clientId)

    })

    client.on('error', (err) => {
      console.log('Connection error: ', err)
      client.end()
    })

    client.on('reconnect', () => {
      console.log('Reconnecting...')
    })

    var topic = 'HibouAirTopic'

    // Publish
    setInterval(() => {

        getTheBLEData().then((x)=>{
          client.publish(topic, JSON.stringify(x), { qos: 0, retain: false })
        console.log("Message sent!", JSON.stringify(x));
        document.getElementById('output').innerHTML += JSON.stringify(x)+"<br>";
        })
      }, 5000);

    })

Make sure to set your username and password and change the Board ID of your device on pub.js file.

Subscribe.html file works as a subscriber which reads sensor data from the broker and shows it on the screen.

here is the subscribe.html file

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Subscribe</title>
  </head>
  <body>
    <h3>Message Received</h3>
    <div id="output"></div>
  </body>
  <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>

  <script src="sub.js"></script>
</html>

Again we need a js script that fetch data from cloud and show it on html file. Here is the sub.js file

const clientId = 'hibouair_' + Math.random().toString(16).substr(2, 8)

//const host = 'ws://broker.emqx.io:8083/mqtt'
const host = 'wss://mqtt.flespi.io:443'

const options = {
  keepalive: 60,
  clientId: clientId,
  protocolId: 'MQTT',
  username:'YOUR_USERNAME',
  password:'YOUR_PASSWORD',
  protocolVersion: 4,
  clean: true,
  reconnectPeriod: 1000,
  connectTimeout: 30 * 1000,
  will: {
    topic: 'HibouAirMsg',
    payload: 'Connection Closed abnormally..!',
    qos: 0,
    retain: false
  },
}

const client = mqtt.connect(host, options)
client.on('connect',()=>{
  console.log('connected mqtt client',clientId)
  client.subscribe('HibouAirTopic', { qos: 0 })
})
client.on('error', (err) => {
  console.log('Connection error: ', err)
  client.end()
})

client.on('reconnect', () => {
  console.log('Reconnecting...')
})


  
  // Received
client.on('message', (topic, message, packet) => {
  console.log(JSON.parse(message))
  //console.log('Received Message: ' + message + '\nOn topic: ' + topic)
  document.getElementById('output').innerHTML += message.toString()+"<br>";

})

To run the index.html we can just type

parcel index.html

Parcel JS can be installed from https://parceljs.org/

Project Video

Share this post on :

BleuIO Python library now supports firmware v2.1.3

BleuIO Python library is updated and supports firmware version 2.1.3 

Now you can easily access all the BleuIO AT commands using this library.
List of AT commands are available at https://www.bleuio.com/getting_started/docs/commands/

and how to access these AT commands using python library can be found at
https://pypi.org/project/bleuio/

Before starting to install our library, make sure you have the latest python installed on your system.

If you have never installed a library from PyPI, you must install the pip tool enabling you to download and install a PyPI package. There are several methods that are described on this page.

Now Install the library by running

pip install bleuio

Easy, right? pip automatically downloads and installs the most recent library on your system in the correct directory. To check that the installation went well, you can launch a Python interpreter and run the following lines:

from bleuio_lib.bleuio_funcs import BleuIo
my_dongle = BleuIo()
my_dongle.start_daemon()
print(my_dongle.ati())

Good luck on creating amazing Bluetooth Low Energy application using BleuIO

Share this post on :

Security Level 4 in Bluetooth LE connection

Protection of private information is essential for every wireless low energy device, from fitness bands to payment systems. Privacy mechanisms prevent devices from being tracked by untrusted devices.

Secure communications keep data safe while also preventing unauthorized devices from injecting data to trigger the system’s unintended operation.

In Bluetooth Low Energy (BLE), devices connected to a link can pass sensitive data by setting up a secure encrypted connection, which means making the data unreadable to all but the Bluetooth master and slave devices. 

A BLE connection is said to operate at a specific Security mode. Within each mode are several security levels. The required security mode/level of a connection may change from time to time, leading to procedures to increase that level.

To keep it simple, when two devices that initially do not have security wish to do something that requires security, the devices must pair first. This process could be triggered, for example, by a central device that is attempting to access a data value (a “characteristic”) on a peripheral device that requires authenticated access. 

Pairing involves authenticating the identity of two devices, encrypting the link using a Short-Term Key (STKs), and then distributing Long-Term Keys (LTKs) (for faster reconnection in the future, i.e., bonding) used for encryption.

The new security level of the connection is based on the method of pairing performed and this is selected based on the I/O capabilities of each device. The security level of any subsequent reconnections is based on the level achieved during the initial pairing.

Each device’s role is defined in the Security Manager (SM) portion of the BLE stack. They are:

  • Initiator: Always corresponds to the Link Layer Master and the GAP central.
  • Responder: Always corresponds to the Link Layer Slave and the GAP peripheral.

Security by means of encryption contains four levels

  • Level 1: No Security (No authentication and no encryption)
  • Level 2: Unauthenticated pairing with encryption
  • Level 3: Authenticated pairing with encryption
  • Level 4: Authenticated LE Secure Connections pairing with encryption

BleuIO‘s security feature can handle all four security levels to establish a secure BLE connection. Users can use Numeric Comparison, Just Works or Passkey Entry to make data transmission more secure when working with Bluetooth low energy applications using BleuIO. 

  • Numeric Comparison: In this scenario, both devices have a display unit capable of displaying a six-digit number. Both displays output the same number, and the user is asked to confirm that these numbers match. 
  • Passkey Entry: The Passkey Entry is primarily intended for the case that one device has a keyboard, but no display unit and the other device has at least a display unit, for example, a PC and a BLE keyboard scenario. The user is shown a six-digit number (from “000000” to “999999”) on the device with a display and then is asked to enter the number on the other device. If the value entered on the second device is correct, the pairing is successful.
  • Just Works: This model is primarily intended for the most constrained devices in I/O. The Just Works association model uses the Numeric Comparison protocol, but the user is never shown a number, and the application may ask the user to accept the connection. This method doesn’t offer protection against a Man in the Middle (MITM) attack, but it provides the same protection level against passive eavesdropping as the Numeric Comparison.

The table below is a reference for determining the pairing method based on the two devices I/O capabilities and each device’s role in the process.

Source: https://microchipdeveloper.com/wireless:ble-gap-security

Use the following AT commands to make your BLE connection more secure.

AT Commands :

  • AT+SETPASSKEY for setting or querying set passkey for passkey authentication.
  • AT+ENTERPASSKEY for entering the 6-digit passkey to continue the pairing request.
  • AT+SECLVL for setting or querying minimum security level used when connected to other devices.
  • AT+NUMCOMPA accepts a numeric comparison authentication request or enables/disabling auto-accepting numeric comparisons.
  • AT+GAPADDRTYPE Sets or queries what address type the dongle will use. Changing address type cannot be done while advertising or while connected to other devices. Read more at https://www.bleuio.com/getting_started/docs/commands/#atgapaddrtype

Following video shows how two BleuIO dongles can connect using passkey over security level 4.

In this example, the central dongle is using GAP IO CAPABILITY 2, which is ‘Keyboard only’ and the peripheral dongle is using GAP IO CAPABILITY 0, which is ‘display only’

The following table explains Input/Output Capabilities and supported security levels.

Share this post on :

Sensor data collection from STM32 and SHT85 using Bluetooth Low Energy

The project is showcasing a simple way of using the the BleuIO Dongle to advertise data that the STM32 reads from a sensor which is connected to the STM32 Nucleo-144.

Requirments :

  • A BleuIO dongle (https://www.bleuio.com/)
  • A SHT85 sensor (https://sensirion.com/products/catalog/SHT85/)
  • A board with a STM32 Microcontroller with a USB port. (A Nucleo-144 development board: NUCLEO-H743ZI2, was used developing this example. (https://www.st.com/en/evaluation-tools/nucleo-h743zi.html)
  • To connect the dongle to the Nucleo board we used a “USB A to Micro USB B”-cable with a USB A female-to-female adapter.)
  • STM32CubeIDE (https://www.st.com/en/development-tools/stm32cubeide.html)

When the BleuIO Dongle is connected to the Nucleo boards USB port, the STM32 will recognize it and start advertising the sensor values that it reads from the SHT85 along with the sensor serial number. It will update these values every 10 seconds.

Setup the project

Part 1 : Download the project

Get project HERE

https://github.com/smart-sensor-devices-ab/stm32_bleuio_SHT85_example

Either clone the project, or download it as a zip file and unzip it, into your STM32CubeIDE workspace.

Part 2 : Importing as an Existing Project

From STM32CubeIDE choose File>Import…

Then choose General>Existing Projects into Workspace then click ‘Next >’

Make sure you’ve choosen your workspace in ‘Select root directory:’

You should see the project “stm32_bleuio_SHT85_example”, check it and click ‘Finish’.

If you download the project as a zip file you will need to rename the project folder from ‘stm32_bleuio_SHT85_example-master’ to ‘stm32_bleuio_SHT85_example’

Connect the SDA to PF0 on the Nucleo board and SCL to PF1.

Then setup I2C2 in the STM32Cube ioc file like this:

Running the example

In STMCubeIDE click the hammer icon to build the project.

  • Open up the ‘STMicroelectronics STLink Viritual COM Port’ with a serial terminal emulation program like TeraTerm, Putty or CoolTerm.

Baudrate: 115200

Data Bits: 8

Parity: None

Stop Bits: 1

Flow Control: None

  • In STMCubeIDE click the green play button to flash and run it on your board. The first time you click it the ‘Run Configuration’ window will appear. You can just leave it as is and click run.
  • Connect the BleuIO Dongle.

Access sensor data from a web browser

We wrote a simple script that connects to the BleuIO dongle and reads advertised data from STM32.

For this script to work, we need

Steps

Create a simple Html file called index.html which will serve as the frontend of the script. This Html file contains some buttons that help connect and read advertised data from the remote dongle, which is connected to stm32.

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <!-- Bootstrap CSS -->
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
      crossorigin="anonymous"
    />

    <title>STM32 Read sensor value</title>
  </head>
  <body>
    <div class="container mt-5">
      <h1>Sensor data collection from stm32 using Bluetooth Low Energy</h1>
      <button id="connect" class="btn btn-primary">Connect</button>
      <button id="getdata" class="btn btn-success">Get device data</button>
      <div id="loader"></div>
      <br />
      <div id="response" class="fw-bold"></div>

      <script src="./index.js"></script>
    </div>
  </body>
</html>

Create a js file called script.js and include it at the bottom of the Html file. This js file uses the BleuIO js library to write AT commands and communicate with the other dongle.

import * as my_dongle from 'bleuio'

//connect to BleuIO
document.getElementById('connect').addEventListener('click', function(){
  my_dongle.at_connect()
})
//get sensor data
document.getElementById('getdata').addEventListener('click', function(){
  document.getElementById('loader').innerHTML = 'Loading'
  //set the BleuIO dongle into dual role
    my_dongle.at_dual().then(()=>{
      // sensor id of the device that we are trying to get data from
      let sensorID='05084FA3'

      //look for advertised data of with the sensor id
        my_dongle.at_findscandata(sensorID,4).then(x=>{        

          //split the advertised data from the respnse
          let advdata= x[x.length-1].split(" ").pop()

          //trim the advertised string to only get sensor response
          const result = advdata.split(sensorID).slice(1).join(sensorID) 

          //get temperature and humidity value
          let temp = result.substring(0, 4);
          let hum = result.substring(4, 8);

          //convert from hex to decimal and device by 100
          temp = parseInt(temp, 16)/100
          hum = (parseInt(hum, 16)/100).toFixed(1)  

          document.getElementById('loader').innerHTML = ''
          document.getElementById('response').innerHTML = `Sensor ID : 05084FA3 <br/>
          Temperature : ${temp} °C<br/>
          Humidity : ${hum} %rH<br/>`              
        })
    })
    
  })

The script js file has two button actions; connect and read advertised data.

We also need to update the Sensor ID on line 13 of script js. The Sensor ID of this example project is 05084FA3, which we got from SHT85.

Therefore this script looks for advertised data that contains sensor ID 05084FA3. After getting advertised data , we split the temperature and humidity information and show it on our index.html page.

Now we need a web bundler. We can use parcel.js

Once parcel js installed, lets go to the root directory and type “parcel index.html”. This will start our development environment.

Lets open the script on a browser and select the right port where the dongle is connected.

The web script is available on web script folder of the GitHub repository.

Share this post on :

USB HID Enumeration with BleuIO to create BLE application

The BleuIO is a Bluetooth low energy USB dongle that can create new BLE 5.0 applications in the fastest and easiest way.

The BleuIO so far has been  supporting the USB Serial Port Profile (SPP).

A new firmware has been released to allow the use of USB Human Interface Device Profile (HID).

The USB HID is usually available in the operating system without the need of external drivers. The makes less dependency in developing BLE applications.

Choose your desired firmware for BleuIO by using built in bootloader and select between USB profiles:

  • Serial Port Profile (SPP) 
  • Human Interface Device Profile (HID)

HID Introduction

HID (Human Interface Device) defines a class of peripheral devices enables people to input data or interact directly with the computer, such as a mouse, keyboard, or joystick. The HID specification is a part of the USB standard, thus USB mice and other USB user input devices are HID compliant.

The host and the device are two entities in the HID protocol. The device is the entity that directly interacts with humans, such as a keyboard, mouse and BleuIO. The host communicates with the device and receives input data from the device in messages with 64 bytes size.

HID devices must meet a few general requirements that are imposed to keep the HID interface standardized and efficient. 

Demo application 

Bluetooth Special Interest Group (SIG) defines a HID profile that specifies how a device can support HID services over the Bluetooth LE protocol stack using the Generic Attribute Profile.

This example implementation shows how BleuIO can be used with the USB HID profile. The application allows to send BleuIO AT commands and displays the response on the screen.  

Share this post on :