MQTT Is Easy When You Are Using Tibbo BASIC/C-Programmable Devices
Learn about how easy it is to create lightweight IoT devices using the new MQTT library with Tibbo BASIC/C and Tibbo hardware.
Join the DZone community and get the full member experience.Join For Free
Showcasing both the publisher and subscriber sides of MQTT, these projects demonstrate the remote control of three LEDs on the subscriber using three buttons on the publisher.
More importantly, the projects show how easy it is to create lightweight IoT devices with Tibbo BASIC/C and Tibbo hardware.
Unlike many competing platforms, Tibbo OS (TiOS) has TCP/IP networking woven into its core, giving you an unprecedented level of control over your network communications. At the same time, the simplicity of Tibbo BASIC and Tibbo C dramatically shortens the learning process, as well as the time needed to develop robust network-enabled applications.
About The Application
To illustrate the use of the MQTT library, we have created two simple Tibbo BASIC applications called mqtt_publisher and mqtt_subscriber.
In our MQTT demo, the publisher device is monitoring three buttons (Tibbits #38). This is done through the keypad
The three buttons on the publisher device correspond to the red, yellow, and green LEDs (Tibbits #39) on the subscriber device.
As buttons are pushed and released, the publisher device calls
mqtt_publish() with topics LED/Red, LED/Green, and LED/Red. Each topic's data is either 0 for "button released" or 1 for "button pressed". The related code is in the
on_kp() event handler.
The subscriber device subscribes to all three topics with a single call to
mqtt_sub() and the line "LED/#". This is done once, inside
With every notification message received from the server, the subscriber device gets
callback_mqtt_notif() invoked. The LEDs are turned on and off inside this functions's body.
Testing the MQTT demo
The demo was designed to run on our TPS3 boards, but you can easily modify it for other devices.
The easiest way to get the test hardware is to order "MQTTPublisher" and "MQTTSubscriber" TPS configurations.
You can also order all the parts separately:
- On the publisher side:
- On the subscriber side:
- Install a suitable MQTT server. We suggest HiveMQ (www.hivemq.com):
- Download the software here: www.hivemq.com/downloads/ (you will be asked to register).
- Unzip the downloaded file.
- Go to the windows-service folder and execute installService.bat.
- Go to the bin folder and launch run.bat.
- You do not need to configure any usernames or passwords.
- Open mqtt_publisher and mqtt_subscriber projects in two separate instances of TIDE, then correct the following in the projects' global.tbh files:
- OWN_IP - assign a suitable unoccupied IP to the publisher and to the subscriber (you know that they will use two different IPs, right?);
- MQTT_SERVER_HOST - set this to the address of the PC on which your run HiveMQ.
- Select your subscriber and publisher devices as debug targets, and run corresponding demo apps on them.
- Press buttons on the publisher to see the LEDs light up on the subscriber.
- If you are running in debug mode you will see a lot of useful debug info printed in the output panes of both TIDE instances.
- You can switch into the release mode to see how fast this works without the debug printing.
Published at DZone with permission of Dmitry Slepov. See the original article here.
Opinions expressed by DZone contributors are their own.