How to Make a BLE Enabled Smart Bulb With STM32
by bleuio in Circuits > Wireless
15 Views, 0 Favorites, 0 Comments
How to Make a BLE Enabled Smart Bulb With STM32
Home automation involves automating household environment equipment. To achieve that, we have created a smart bulb that is easy to install, and the attached equipment can be controlled over a web browser or smartphone app. The aim of this project is to control different home appliances using a web browser or smartphone.
Supplies
- Two BleuIO dongles (https://www.bleuio.com/)
- The script for the dongle (available on the source code inside web script folder)
- 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 a “USB A to Micro USB B”-cable with a USB A female-to-female adapter can be used.)
- STM32CubeIDE (https://www.st.com/en/development-tools/stm32cubeide.html)
- A 5V Relay
- A light bulb
Introduction
The project is showcasing a simple way of using the the BleuIO Dongle to turn on and off a light bulb that is connected to the STM32 Nucleo-144 via a 5V Relay.
You will need two dongles, one connected to the Nucleo board and one connected to a computer, running the web script.When the BleuIO Dongle is connected to the Nucleo boards USB port the STM32 will recognize it and directly start advertising. This allows the other Dongle to connect to it.
It will also accept 3 different inputs from the UART:
input result
0 Send ATI (Request device information) command to BlueIO Dongle.
1 Manually turn the light bulb on
2 Manually turn the light bulb off
We have used a STM32 Nucleo-144 development board with STM32H743ZI MCU (STM32H743ZI micro mbed-Enabled Development Nucleo-144 series ARM® Cortex®-M7 MCU 32-Bit Embedded Evaluation Board) for this example.
If you want to use another setup you will have to make sure it support USB Host and beware that the GPIO setup might be different and may need to be reconfigured in the .ioc file.
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
Pinout and Connection to STM32 For the DC part of the Relay circuit connect S (signal) to pin PE4 on the STM32 NUCLEO board, also connect the Power supply (+) and ground (-) to +5V and GND respectively.
About the Code
You can get project
https://github.com/smart-sensor-devices-ab/stm32_b...
This project based on our previous STM32 project (https://github.com/smart-sensor-devices-ab/stm32_bleuio_example) with these changes in the .ioc file:
In the pinout view we set the GPIO PE4 to OUTPUT and labeled it to ‘lightbulb’.
In the USBH_CDC_ReceiveCallback function in USB_HOST\usb_host.c we copy the CDC_RX_Buffer into a external variable called dongle_response that is accessable from the main.c file.
In main.c we create a simple intepreter so we can react to the data we are recieving from the dongle.
We also update the handleUartInput function so we can have manual control over the light bulb via the UART.
We put the intepreter function inside the main loop.
How to Setup Project
Downloading the project from GitHub
https://github.com/smart-sensor-devices-ab/stm32_b...
Either clone the project, or download it as a zip file and unzip it, into your STM32CubeIDE workspace.
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_example”, check it and click ‘Finish’.
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.
Serial port Setup:
Baudrate: 115200
Data Bits: 8
Parity: None
Stop Bits: 1
Flow Control: None
Connect the BleuIO Dongle before running the example .
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.
You should be greeted by this welcome message:
Wait until the message: “[BleuIO Dongle Ready]” is shown.
You can now connect with the other dongle using the script.
You can also use the uart commands (0, 1 or 2):
Press 0 to get device information.
1 to turn on lightbulb.
2 to turn off lightbulb.
Dongle response will be printed to UART.
Controlling the Light From a Web Browser
We wrote a simple script that connects to the dongle and sends signals to toggle the light from the web browser.
BleuIO USB dongle connected to the computer.
BleuIO javascript library
Chrome 78 or later, and you need to enable the #enable-experimental-web-platform-features flag in chrome://flags
A web bundler – (parcel js)
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 signal to the remote dongle, which is connected to stm32.
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.
The script js file has three button actions; connect and control light.
Now we need to know the ID of the other dongle connected to STM32 so that we can connect to it. You can use this web terminal to get the dongle ID.
- Open this site https://bleuio.com/web_terminal.html and click connect to dongle.
- Select the appropriate port to connect.
- Once it says connected, type **"ATI"**. This will show dongle information and current status.
- If the dongle is on peripheral role, set it to central by typing **"AT+CENTRAL"**
- Now do a gap scan by typing **"AT+GAPSCAN"** - Once you see your dongle on the list ,stop the scan by pressing control+c
- Copy the ID and paste it into the script (script.js) line #2
You will need a web bundler. You can use parcel.js
Once parcel js installed, go to the root directory and type “parcel index.html”. This will start your development environment.
Open the script on a browser using parcel js.
You can easily connect to the dongle and turn on-off light from there.