Bluetooth

The article details the Edge101 mainboard's support for dual-mode Bluetooth, including Classic Bluetooth for serial communication and Bluetooth Low Energy (BLE) for dynamic device name changes. It features example codes using ESP32 IoT Programmable Controller, guiding users through setup, connectivity, and practical applications for enhanced IoT functionality.

Bluetooth

The Edge101 mainboard supports dual-mode Bluetooth, which means it can simultaneously support both classic Bluetooth and Bluetooth Low Energy (BLE). The host can run on the same device or be distributed across different devices. The Edge101 mainboard supports both configurations.

Example1: Classic Bluetooth (BL)

The mainboard's Bluetooth operates in Bluetooth Classic mode, generating a Bluetooth serial port.

Hardware Required:

Example Code:

// This example code is in the public domain (or under CC0 license, optional).
// Author: Evandro Copercini - 2018
//
// This example creates a bridge between the serial port and classic Bluetooth (SPP),
// and demonstrates that SerialBT has the same functionality as a regular serial port.

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("Edge101_Device"); // Set Bluetooth device name
  Serial.println("The device started, now you can pair it with Bluetooth!");
}

void loop() {
  // If there's data available in the serial port, send it to Bluetooth
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  // If there's data available in Bluetooth, send it to the serial port
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20); // Slight delay to reduce CPU usage
}

Install the Serial Bluetooth Terminal app on an Android phone. After enabling Bluetooth on the phone, search for the Edge101_Device Bluetooth device and connect to it.

Open the Serial Bluetooth Terminal app and select the Edge101_Device.

Once connected, data can be sent from the phone to the mainboard, and the mainboard will print out the data sent from the phone via the serial port. The mainboard can also send data back to the phone through the serial port.

Example2: Bluetooth Low Energy (BLE)

This program will create a BLE device. When the user button on the mainboard is pressed, the BLE device's name will be changed, and you can observe the change of the BLE device name on your phone.

Hardware Preparation:

Example Code:

// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Sketch shows how to use SimpleBLE to advertise the name of the device and change it on the press of a button
// Useful if you want to advertise some sort of message
// Button is attached between GPIO 0 and GND, and the device name changes each time the button is pressed

#include "SimpleBLE.h"  //Using the SimpleBLE library

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

SimpleBLE ble;

void onButton(){
    String out = "BLE32 name: ";
    out += String(millis() / 1000);
    Serial.println(out);
    ble.begin(out);  // Change the name of BLE
}

void setup() {
    Serial.begin(115200);
    Serial.setDebugOutput(true);
    pinMode(38, INPUT_PULLUP);	//The user button on the P38 motherboard is used to change the name of BLE devices
    Serial.print("Edge101WE SDK: ");
    Serial.println(ESP.getSdkVersion());
    ble.begin("Edge101WE SimpleBLE");//After enabling Bluetooth on the device, connect to FireBeetle MESH SimpleBLE. When the button is pressed, change the device name.
    Serial.println("Press the button to change the device's name");
}

void loop() {
    static uint8_t lastPinState = 1;
    uint8_t pinState = digitalRead(38); //After enabling Bluetooth on the device, connect to FireBeetle MESH SimpleBLE. When the button is pressed, change the device name.
    if(!pinState && lastPinState){
        onButton();  
    }
    lastPinState = pinState;
    while(Serial.available()) Serial.write(Serial.read());
}

Was this article helpful?

TOP