Using bluetooth to get sensor data off my board

For my tinyML project I’ve been evaluating MQTT and HTTP to get data off my board. I’ve also been looking at using BLE.

I’ve been investigating the ArduinoBLE library. It seems quite nice. It looks like it supports Central and Peripheral. At this stage I think I’m mostly interested in using the microcontroller as a Peripheral. It seems well documented. It can be configured to be event driven (useful if I expect the Central to be sending the Peripheral any data). Supports sending data as notifications (and I believe indications).

On the Central side, it looks like my best option will be the python bleak module. I expect most of the code surrounding BLE handling will be python, so that would work well. Again can be configured to be event driven (I saw lots of mention of async, so I suspect this may even be strongly encouraged.

If I need to any python Peripheral work, there is also the python bless module. I haven’t looked at this much yet.

I’ve done extensive work with bluetooth previously. However, so much of it has been implementing bluetooth stacks. There has been much less using it as part of an actual application to do stuff. It might be nice to use it for this purpose.

Further MQTT thoughts

Having spent the last few days fiddling with MQTT, I have some thoughts and observations worth documenting.

Mosquitto out of the box isn’t configured for anonymous access. This is better for security, but if you haven’t used the software before can be a bit confusing and lead to a bit of head scratching. To allow anonymous access add the following to /etc/mosquitto/mosquitto.conf:

allow_anonymous true
listener 1883

This will allow anonymous access on clear port 1883. Still not interested in using TLS for the local server, so my microcontroller can talk to it easily.

Using JSON in a MQTT value is really nice. It’s trivial to extract it into a python dictionary. Should be easy to construct simple JSON in C on a microcontroller.

Lots of documentation I’ve seen recommends using https://www.pubnub.com/ as a cloud MQTT server. However, it doesn’t support MQTT over TCP any more. It looks like support was cancelled last year. This doesn’t seem to be made very clear at all. In general pubnub documentation doesn’t feel very user friendly. Its very bitty. It has lots of broken links. There’s a fair bit of stale documentation that is no longer relevant, which isn’t marked as such. Because of this I spent quite a long time analysing my firewall config, as nmap suggested port 1883 and 8883 were being filtered (newsflash, they weren’t). There was also a lot of confusion over which URL to use, which as it turned out, due to the aforementioned cancellation were all nonsense. Servers were present in DNS, but did not respond to ping which also didn’t help. ChatGPT and Gemini were also content to lie through their teeth about the service status, which is hardly surprising since their source material was outdated, obfuscated nonsense.

https://www.hivemq.com/ seems much better. It does actually support MQTT over TCP. It has a free, openly accessible test server. It has a free tier with login for basic messing about. It has good documentation. It has a decent AI chat bot that did a good job of answering my noob questions and got me up and running quickly. Open server allows anonymous access, TLS and clear. Logged in server requires TLS and username/password.

It seems like the best way to handle a microcontroller with weak TLS is to have a local MQTT server which can handle TLS, to forward stuff to the cloud. Doesn’t need to be anything fancy, as its not high traffic. Export data from microcontroller to local MQTT using clear MQTT, MQTT-SN, COAP, BLE etc.

For the purposes of my project, its feeling like a combination of HTTP for bulk payload and MQTT for general sensor data and notifications will be best. The clue is in the name: Message Queuing Telemetry Transport. Use it for sensor readings and management.

The paho MQTT python module seems quite nice.

All in all, quite nifty so far.

Learning MQTT

I’ve just started looking at a TinyML project I want to do. The first thing I need to do is get some sensor data off my board.

I thought about just bunging it over a uart. In some ways it would be the quickest easiest way to go about things. However, this thing is likely to evolve into something a bit more structured over time, with several different data types, which would necessitate building packets and coming up with some sort of protocol. That’s beginning to sound a bit too much like hard work.

Next thing I considered was sending some JSON. That would make the protocol rather easier, as I could just create some JSON on the microcontroller send it over to the host and use any number of JSON parsers to extract the data.

But, if I’m using JSON I should really send it over HTTP. The board has ethernet and I can get an HTTP server up and running without too trouble.

However, I’ve seen plenty of people talking about MQTT in reference to sensor data. I’ve never used it before. No time like the present!

I’ve got mosquitto installed locally. I’ve managed to setup two subscribers. I’ve published some mock data. I’ve played with wildcards. All in the cli on my PC so far, but I only started a bit before lunch so early days yet. So far it shows promise.

I’m not sure if some sort of hybrid approach where I use MQTT for notifications and HTTP for the actual data (which later down the line may be audiovisual) would be a good idea. I’m not sure what the strengths/weaknesses of MQTT are yet. I know HTTP can do all this without too much bother.

Also, not sure about cloud services for MQTT yet. The microcontroller I’m using doesn’t have hardware crypto, so TLS might be a bit pants. If I do everything locally, I don’t need to bother so much about security. The board I’m using has a ton of other gear that looks useful though. I can’t find another board that has equivalent sensors/connectors for other sensors, has crypto support and is a sane price though.

I think for now, keeping it local will suit my purposes. There’s plenty I don’t know how to do yet, so adding another layer of things I don’t know will just add burden and stress. Maybe I’ll do something more cloudy later.

I think now though its time for a walk in the sun!