I created a very simple demo based on ESP-01 module and MQTT protocol.
A video of the demo is also available here:
MQTT is a popular protocol for IoT, the client implementation is small in footprint and there are all kinds of example codes available on the internet, from C to Java or Java Script.
This demo is based on C code, which is provided as an example in the esp-open-rtos SDK. The circuit is very simple, it has 2 LEDs connected to the ESP-01 and a mobile phone is used to control these LEDs.
There are several open MQTT servers providing free access to public on the internet, such as test.mosquitto.org, and a private server is also very easy to setup, if you use Ubuntu, you can simply use command
sudo apt-get install mosquitto
to install the open source MQTT server on your computer. I'm using a VPS as the server, to verify that this concept will actually work in the cloud.
The red and blue LEDs are belonging to different topics, and are subscribed to the topic. The mobile phone App has two buttons, one for each LED, when you press the button, the App will publish a new message to the corresponding topic. The published message will toggle between '0' and '1' every time the button is pressed. Then the message will be delivered to the subscribed client (ESP8266), the microcontroller will decide whether to turn the LED on or off according to the message content.
Nordic's nRF5xxxxx series chips are very popular BLE chipsets, according to my own experience, comparing to the CSR solution, nRF is more flexible and powerful. For example, the nRF52832, which is used in this demo, is a full functioning Cortex M4 MCU with FPU, if you don't use their SoftDevice, it will just be a Cortex M4 MCU like any other one on the market plus a radio interface, while oh the other hand, the CSR1010 which I had a demo project with it here before is a more dedicated BLE chip with all the BLE stack inside and the user program has to stay in the external serial EEPROM. The user has very limited access to the hardwares inside, the only way is through their APIs.
Despite their chip being a really wonderful product, the nRF SDK is quiet disappointing. The only want you to use their examples and adopt it to your needs, but never tell you how you should use the SoftDevice. I felt very frustrated when I read the code in the examples, I've never read such bad structured C code ever. So many unnecessary macros and function calls, you often have to jump 3 or 4 times among the defined macros to find out which is the final SoftDevice API it calls.
Anyway, I finally figured out how to isolate the SDK code with my own, so then I can write my own code in C++, and don't use their provided peripheral libraries. I really don't like such libraries, you still have to learn how to use it and get yourself familiar with tons of API functions, won't save you any time. On the other side, controlling the hardware via the registers is much more strait forward and simpler.
The demo does a very simple job, it will generate a random value every a few seconds, and if the notification is turned on by the mobile device, the value is pushed to the mobile whenever it gets updated. Meanwhile, the mobile can send a message to the nRF52832, the message will be displayed on the 1602 LCD module when received. It is not necessarily a text message, but I use ascii text here to make it displayable.
The "Gpio" class initialises the I/O ports, and the important thing: to introduce the bt_ext_handler() and adv_ext_handler() functions to the original main.c (which is renamed as start.c) by the ext_handler.h file, and adopt these 2 event handlers to the BLE stack:
1. in the ble_stack_init(), add this line to register BLE event handler: