[chbot] Lazy and Blue tooth
Stephen Irons
stephen at irons.nz
Mon Dec 11 04:06:53 GMT 2023
Unless you want to re-implement your code in another microcontroller
with a built-in Bluetooth controller, you will need an external
Bluetooth device. A few ways spring to mind:
* Various modules that set up a Bluetooth Serial Port Profile (SPP)
connection with another Bluetooth device. For example, Bluetooth HC-05
module for Arduino, from Surplustronics, Jaycar, etc. At the PC end,
any Bluetooth module will work: either a USB dongle or your laptop's
built-in Bluetooth module (which is often just a USB device anyway), or
another HC-05 with a USB-serial dongle. The SPP should be usable with
Android phones, but I think iOS does not support SPP. Your device will
need a spare UART, or you can connect it to your 'engineering' UART.
Any data sent from your device is streamed to the other end. This is
simple to set up, but your software is responsible for formatting the
data that you send so that the receiving end can decode it correctly,
adding yet another ad-hoc protocol to the world. The default speed is
9600 baud, but there are AT commands to change this as well as other
parameters.
* General purpose Bluetoth modules from many suppliers (nRF, TI, etc)
available from Digikey etc, but generally not available at Jaycar and
such. Can do SPP; modern ones (BT 4.1 and above, I think) can do
Bluetooth Low Energy (BLE) profiles including Generic ATTribute Profile
(GATT, not to be confused with Generic Access Profile, GAP). They
connect via serial or SPI, and your device communicates with them using
the Bluetooth Host Controller Interface (HCI) API. HCI is well
documented, but quite complex.
* My preferred method. I have used the Adafuit modules called 'BLE UART
Friend': <https://www.adafruit.com/product/2479> with 3.3V UART
interface <https://www.adafruit.com/product/2267> with USB interface.
You can plug them in and set up a 2-way UART (note, it does not use
SPP; see later), or you can enter command mode and use their
well-documented AT-style commands to set up GATT services. These
AT-commands are easier to use than HCI, but not as flexible.
I can use these modules on both embedded devices and PCs without having
to learn multiple different operation-system specific APIs.
Not cheap (~US$15--20 last time I bought them)
GATT
GATT is intended to communicate attributes of one device to another:
your BT headphones uses a GATT attribute to tell your phone what the
battery level. They use other attributes to indicate that you have
pressed the 'increase volume' button, and your phone uses yet other
GATT attributes to adjust the volume. Updates can happen quite often
(10-100 ms).
Attributes are organised in groups called 'services'. Bluetooth has
many standardised GATT services. Some examples are: device information
service for hardware and software version numbers, battery service for
battery levels, current time service for ... the current time,
electronic shelf label service for those eInk displays at the
supermarket, location and navigation service for Bluetooth GPS
devices, health-sensor service for sports watches, human interface
device service for keyboard and mouses, and so on.
A device can support many services, and most do: device-information
service and battery service are common. A smart-watch might support
location and navigation, current-time and health-sensor services.
Headphones include device information, battery level, audio control and
human interface services. An electronic shelf label probably only
supports shelf-label and perhaps device information service.
Each service defines a number of so-called 'characteristics':
device-information service includes manufacturer, model number, serial
number, hardware rev, firmware rev, and so on. Battery level service
defines battery level (percent), battery voltage, energy, time until
discharged or charged, etc).
Services and characterics within each service are identified by a
128-bit UUID. If you can find suitable Bluetooth-defined services, then
your phone will just understand them and a suitable app will do The
Right Thing. If not, generate some random 128-bit UUIDs, plug them in
to both ends, and a value set at one end is transferred to the other
end as if by magic. Obviously, both ends have to Just-Know the same
UUIDs, and you will need an app to uses these attributes.
Nordic ad-hoc-standard UART service
GATT provides no way to stream data from one device to another.
However, Nordic have published a service (Nordic UART service or NUS)
that defines two characteristics (TX data and RX data). You write to
the TX data service on one end and read from the TX data service on the
other; there is no flow control so you can lose data. If you use these
UUIDs, there are many applications for Android and iOS that understand
NUS.
The Adafruit BLE Friend devices use NUS rather than SPP for its serial
transfer. This is transparent to you, if you just plug-in-and-use the
devices. At the default 9600 baud, you will not lose any data, and BLE
is quite robust. In this case, once again,l you need to have a way to
parse the received data.
Automation IO service
This is a standard service for transferring digital and analog values.
I have not used it so don't know the ins and outs. Analog values are
16-bit values, digital values are 2-bit values (active, inactive,
tristate, unknown), but it sounds useful for your application.
.
Debugging
nRF Connect is an app by nRF for Android and iPhone. After scanning for
and connecting to a remote device, it tells you what services and
characteristics are available and you can read and write the values. It
knows the standard services and characteristics, and gives them
sensible names. For user-defined services and characteristics, it gives
the full 128-bit UUID.
Adafruit has an app that supports the BLE friend modules.
Documentation
* BLE GATT service documentation can be found
<https://www.bluetooth.com/specifications/specs/?types=adopted> (look
for 'services', not 'profiles').
* Adafruit BLE Friend documentation on the Adafruit website. They call
it the Nordic UART 'profile'; this is wrong, it is the Nordic UART
'service'. 'Profile' in Bluetooth parlance means something else.
Hope this helps
Stephen Irons
On Sun, Dec 10 2023 at 12:04:15 +1300, Marshland Engineering
<marshland at marshland.co.nz> wrote:
> I have developed an EGT and CHT display for 2 strokes. Easy to see
> when riding
> the bike. Uses an ATMEG328
> Never having use Bluetooth before, I not sure where to start in
> getting the
> data to a phone or PC.
>
> Basically All I want is to transmit 2 analog signals to a phone/PC.
>
> Not sure whether to do it live or the ATMEG saves the info and
> connects and
> downloads later.
>
> A pointer in the right direction would be great.
>
> Thanks Wallace.
>
>
> _______________________________________________
> Chchrobotics mailing list Chchrobotics at lists.ourshack.com
> <mailto:Chchrobotics at lists.ourshack.com>
> <https://lists.ourshack.com/mailman/listinfo/chchrobotics>
> Mail Archives: <http://lists.ourshack.com/pipermail/chchrobotics/>
> Meetings usually 3rd Monday each month. See http://kiwibots.org
> <http://kiwibots.org/> for venue, directions and dates.
> When replying, please edit your Subject line to reflect new subjects.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20231211/b9a1e334/attachment-0001.html>
More information about the Chchrobotics
mailing list