Store air quality sensor data from Bluetooth device to google firebase cloud

In this article, we will see how to send BLE data to the cloud. Together with the BleuIO javascript library and BleuIO USB dongle, we will get BLE data from HibouAir. After getting the advertised data, we will pass it to a simple function that will give us an object with meaningful air quality data numbers. Then we will send the data to the firebase cloud. 

For this project, we will need 

Step 1:

Clone the GitHub repository using git clone https://github.com/smart-sensor-devices-ab/hbiouAirToFirebase.git

Step 2:

  • Create a firebase account from https://firebase.google.com/.
  • After creating a firebase account, create a project and an app under this project. Make sure its a web app.
  • Once the app is ready, collect your app information API key, apiKey etc.
  • Add a real time database

Also, make sure your firebase realtime database read-write rules are true.

{ “rules”: { “.read”: true, “.write”: true } }

Now open firebaseconfig.js file from the root folder and paste your information collected.

Note : if the database information is not on the list, please update it manually by getting the realtime database url

Follow the detailed guideline to setup firebase for this script.

https://hibouair.com/cloud-storage/firebase-api-instructions.pdf

Step 3:

Let’s start the app. To run the app, we need to have a website bundler. We can use parceljs. Install parceljs from https://parceljs.org/getting-started/webapp/

Once it’s installed, go to the app folder and type parcel index.html

You will have your app running on the browser.

Step 4:

  • Connect your dongle to your pc. 
  • Open the app on your browser and click connect. 
  • Click on the device information. If you see the device is in peripheral mode than you have to make it central. 
  • Click on the central button. And recheck device information. 

Step 4:

  • Lets scan for nearby devices. 
  • Click on the scan button and look at the list of the scanned device on your console log. Pick any HibouAir devices and copy the device information. 
  • Open the index.js file and go to line 95
  • replace the scan target information with the one you copied.
  • Update firebaseconfig.js with

Now save the page and click on send data to the cloud. If your firebase configuration is correct, you should see data showing on your cloud database. 

Here is the code for index.html file

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
      crossorigin="anonymous"
    />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-database.js"></script>
    <title>BLE data to google firebase</title>
  </head>
  <body>
    <div class="container">
      <h2>Send BLE data to cloud</h2>
      <p>
        This script helps you to connect to BleuIO and sends data to Firebase
        cloud.
      </p>
      <p>
        Learn more about BleuIO.
        <a href="https://www.bleuio.com/">https://www.bleuio.com/</a>
      </p>
      <br />
      <button class="btn btn-success" id="connect">Connect</button>
      <button class="btn btn-success" id="deviceinfo">Device Info</button>
      <!-- To get Ble data from HibouAir (Peripheral device), BleuIO has to be on central mode -->
      <button class="btn btn-success" id="central">Central Mode</button>

      <button class="btn btn-success" id="scan">Scan for devices</button>

      <button class="btn btn-success" id="sendDataTCloudBtn">
        Send data to cloud Data
      </button>

      <button class="btn btn-success" id="stopSendingData">
        Stop Sending Data
      </button>
      <br /><br />
      <h5><div id="log"></div></h5>
    </div>

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

Here is the code for index.js file

import * as my_dongle from 'bleuio'
document.getElementById('connect').addEventListener('click', function(){
  my_dongle.at_connect()
})
document.getElementById('deviceinfo').addEventListener('click', function(){
  my_dongle.ati().then((data)=>console.log(data))
})
document.getElementById('central').addEventListener('click', function(){
    my_dongle.at_central().then((data)=>console.log(data))
})
document.getElementById('scan').addEventListener('click', function(){
my_dongle.at_gapscan(2).then((data)=>console.log(data))
})
const parseSensorData = ((input) =>{
    let counter = 13;
    if (input.includes("5B070503")) {
      counter = 17;
    }
    let sensorData = {
      sensorid:
        input[counter + 1] +
        input[counter + 2] +
        input[counter + 3] +
        input[counter + 4] +
        input[counter + 5] +
        input[counter + 6],
      pressure:
        parseInt(
          input[counter + 13] +
            input[counter + 14] +
            input[counter + 11] +
            input[counter + 12],
          16
        ) / 10,
      temperature:
        parseInt(
          input[counter + 17] +
            input[counter + 18] +
            input[counter + 15] +
            input[counter + 16],
          16
        ) / 10,
      humidity:
        parseInt(
          input[counter + 21] +
            input[counter + 22] +
            input[counter + 19] +
            input[counter + 20],
          16
        ) / 10,
        voc:
        parseInt(
          input[counter + 25] +
            input[counter + 26] +
            input[counter + 23] +
            input[counter + 24],
          16
        ) / 10,
      als: parseInt(
        input[counter + 9] +
          input[counter + 10] +
          input[counter + 7] +
          input[counter + 8],
        16
      ),
      pm1:
        parseInt(
          input[counter + 29] +
            input[counter + 30] +
            input[counter + 27] +
            input[counter + 28],
          16
        ) / 10,
      pm25:
        parseInt(
          input[counter + 33] +
            input[counter + 34] +
            input[counter + 31] +
            input[counter + 32],
          16
        ) / 10,
      pm10:
        parseInt(
          input[counter + 37] +
            input[counter + 38] +
            input[counter + 35] +
            input[counter + 36],
          16
        ) / 10}
    return sensorData
  })

const sendDataToCloud = (()=>{
    //get the scan target device id by scanning for device.
    my_dongle.at_scantarget('[1]F9:0D:35:E7:72:65',2).then((data)=>{
        let theAdvData = data.filter(element => element.includes("ADV"));
        if(theAdvData && theAdvData.length>0){
            console.log(theAdvData)
            let advData = theAdvData[0].split("[ADV]: ")
            // converting advertising string to meaningfull numbers 
            //and pass it to an array of objects
            let airQualityData = parseSensorData(advData[1])
            console.log(airQualityData)
            // save the data to database 
            let database = firebase.database(); // which gets the database 
            let ref = database.ref("records");
            //pushing the object to the reference
            ref.push(airQualityData)
        }
    })
})
var intervalId
document.getElementById('sendDataTCloudBtn').addEventListener('click', function(){
    sendDataToCloud()
    if (intervalId) {
        clearInterval(intervalId);
    }
    intervalId = setInterval(sendDataToCloud ,5000);
    document.getElementById("log").innerHTML="Sending data to cloud. Click stop sending data to stop the process.";

})
document.getElementById('stopSendingData').addEventListener('click', function(){
    clearInterval(intervalId)
    document.getElementById("log").innerHTML="Sending data stopped.";
 })
document.getElementById('stopProcess').addEventListener('click', function(){
   console.log(my_dongle.stop()) 
})

Right now, its sending data every 5 seconds. 

You can stop the process by clicking stop sending data.

Have a look at the following video for a better understanding. 

Share this post on :

Make your Bluetooth Low Energy connection secure using BleuIO

Protection of private information is essential for every wireless low energy device, from fitness band 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.

BleuIO has introduced security feature into its latest release (firmware v1.3.0 ). User can now use Numeric Comparison, Just Works or Passkey Entry to make data transmission more secure when working with Bluetooth low energy application 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 terms of I/O. The Just Works association model uses the Numeric Comparison protocol, but the user is never shown a number, and the application may simply 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.

Use the following AT commands to apply secure connection.

AT Commands :

  • AT+SETPASSKEY for setting or querying set passkey for passkey authentication.
  • AT+SECLVL for setting or querying minimum security level used when connected to other devices.
  • AT+NUMCOMPA for accepting a numeric comparison authentication request or enabling/disabling auto-accepting numeric comparisons.

Following video shows how to pair between two BleuIO devices and apply above mentioned security.

Share this post on :

A new firmware update (v 1.2.0) has been released for BleuIO

Smart Sensor Devices is announcing a firmware update for Bleuio and Smart USB dongle 2.0. We invite all the users to apply the updated firmware. The new firmware will be available to download on 5th February 2021, at https://www.bleuio.com/getting_started/docs/firmware/

Firmware Update Improvements 

  • It is now possible to access protected characteristics that need an increased security level. 
  • Security level can be increased by successfully pairing/bonding.
  • Security level will now be displayed when changed.

Newly Added features 

  • Added AT Command AT+GAPIOCAP for setting or querying dongle input and output capabilities. Important for what type of security responses is available.
  • Added AT Command AT+GAPPAIR for manually starting a pairing or bonding procedure.
  • Added AT Command AT+GAPUNPAIR for unpairing all or selected devices.
  • Added AT Command AT+ENTERPASSKEY for handling passkey requests when pairing/bonding.
  • You are now able to secure the connection between other devices and the dongle or between dongles via pairing/bonding.
  • The dongle is now capable of initialising or handling pairing and/or bonding requests. Depending on what Input/Output capability you’ve set on it.
  • The dongle can now handle numeric comparison authentication or passkey authentication (with the new AT+ENTERPASSKEY command).

To meet the demands of users, the BleuIO will continue to update and add new features.

Share this post on :