CR555
Published © GPL3+

Bluetooth BLE communication using STM boards

A simple application that uses STM NUCLEO-64 F401RE and a XNUCLEO-IDB5A2 for Bluetooth BLE communication

BeginnerFull instructions provided2 hours215
Bluetooth BLE communication using STM boards

Things used in this project

Hardware components

STM32 XNUCLEO IDB05A2
×1
STM32 Nucleo-64 Board
STMicroelectronics STM32 Nucleo-64 Board
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit STM32F401RE e DHT11

Code

Temperature and humidity measurement using STM technology

Arduino
#include <STM32duinoBLE.h>
// Include the DHT11 library for interfacing with the sensor.
#include <DHT11.h>

DHT11 dht11(2);

// Bluetooth® Service
BLEService myService("fff0");

// Bluetooth® Characteristics
// standard 16-bit characteristic UUID; remote clients will be able to get notifications if this characteristic changes
BLEIntCharacteristic Temperature("2A6E", BLERead | BLENotify);
BLEIntCharacteristic Humidity("2A6F", BLERead | BLENotify);

// Temperature and Humidity and timing variable initialization
int t = 27;
int h = 66;
long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  while (!Serial);
  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
  if (!BLE.begin()) {
    Serial.println("failed to initialize BLE!");
    while (1);
  }
  BLE.setLocalName("Test");
  BLE.setAdvertisedService(myService); // add the service UUID
  myService.addCharacteristic(Temperature);
  myService.addCharacteristic(Humidity);
  BLE.addService(myService);
  Temperature.writeValue(t);
  Humidity.writeValue(h);
  BLE.advertise();
  Serial.println("advertising ...");
}

void loop() {
  BLEDevice central = BLE.central();
   // if a central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    Serial.println(central.address());
    // turn on the LED to indicate the connection:
    digitalWrite(LED_BUILTIN, HIGH);

    // check environmental data 2000 ms
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 2000 ms have passed, check the values:
      if (currentMillis - previousMillis >= 2000) {
        previousMillis = currentMillis;
        updateLevel();
      }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

//update the environmental values
void updateLevel(){
  int result = dht11.readTemperatureHumidity(t, h);
  // Check the results of the readings.
  // If the reading is successful, print the temperature and humidity values.
  // If there are errors, print the appropriate error messages.
  if (result == 0) {
      Serial.print("Temperature: ");
      Serial.print(t);
      Serial.print(" °C\tHumidity: ");
      Serial.print(h);
      Serial.println(" %");
      Temperature.writeValue(100*t);
      Humidity.writeValue(100*h);
  } else {
      // Print error message based on the error code.
      Serial.println(DHT11::getErrorString(result));
    }
}

Credits

CR555
4 projects • 7 followers

Comments