- Excel can send commands HTTP or MQTT using macros in VBA
- With Azure IoT Hub and Mosquitto make it possible to establish direct communication with devices
- Complete flows can be created in Excel to send and receive sensor data.
- The environment requires configuration of libraries, SDKs and scripts for effective integration
Have you ever wondered if it's possible to control smart devices directly from Excel ? Thanks to Excel macros and protocols like HTTP or MQTT, this spreadsheet can become a true home automation control console . This combination is ideal for small home or business automation projects without the need to develop additional interfaces or software.
Beyond calculations and pivot tables, Excel can act as a control point for sending commands to devices connected via the internet. Using VBA (Visual Basic for Applications) macros and leveraging MQTT libraries or HTTP clients, it's possible to establish communication with IoT devices, whether to turn on lights , regulate temperature, or manage sensors.
Prerequisites and basic environment setup
Before we start writing a functional macro, it's important to have our working environment ready. Depending on whether you're working on Windows or Linux , the steps may vary slightly , although in both cases you'll need some specific tools.

First, make sure you have Visual Studio and CMake installed if you're on Windows . It's crucial that the C++ development option is enabled, as you'll be compiling some examples . It's also important to install the x64 version of Mosquitto, as it will be the foundation for the MQTT protocol.
For Linux users, simply run a few commands in the terminal to install the Mosquitto compilation tools and client library:
sudo apt install cmake g++
sudo apt install libmosquitto-dev
In both environments, having an active Azure account is recommended if you're going to integrate an IoT Hub as an intermediary between Excel and your devices.
Setting up IoT Hub and devices in Azure
The next step is to prepare your cloud to receive and send messages to and from devices. Azure IoT Hub provides a complete, free, small-scale infrastructure for testing.
Using the Azure command-line interface, you can create a resource group and hub with the following commands:
az group create --name mqtt-sample-rg --location eastus
az iot hub create --name my-hub --resource-group mqtt-sample-rg --sku F1
Once the infrastructure is created, you will need to register a virtual device that will act as a receiver or sender of the commands.
az iot hub device-identity create --hub-name my-hub --device-id mqtt-dev-01
For communication to be authorized, a SAS token must be generated:
az iot hub generate-sas-token --device-id mqtt-dev-01 --hub-name my-hub --du 7200

Downloading the repository and preparing the code
Azure provides a code repository with ready-to-use examples in C or Python . You can clone it from GitHub:
git clone https://github.com/Azure-Samples/IoTMQTTSample.git
Once cloned, locate the mosquitto/src/config.h file and add the variables containing your information: hub name, device code, and your SAS token. This information is essential for authenticating the connection.
#define IOTHUBNAME "my-hub"
#define DEVICEID "mqtt-dev-01"
#define SAS_TOKEN "SharedAccessSignature sr=my-hub.azure-devices.net..."
Compile the examples and test the connection
Once you've got everything ready, it's time to compile the examples and verify the communications. From the project directory, run:
cd mosquitto
cmake -Bbuild
cmake --build build
The binaries will be generated in `./build` on Linux or ` ./build/Debug` on Windows. From there, you can start running the different examples.
Sending messages from the device to the cloud
The mosquitto_telemetry binary allows you to send messages to the IoT Hub. Beforehand, you can open an event monitor with:
az iot hub monitor-events --hub-name my-hub
Then, run the binary:
./build/mosquitto_telemetry
The code functions use MQTT topics such as "devices/mqtt-dev-01/messages/events/" to send data , simulating a real-life data collection situation by a sensor.
Receiving messages from the cloud to the device
You can also do the opposite: send instructions from the cloud and capture them on the device. Run:
./build/mosquitto_subscribe
Then, from the CLI, send a message:
az iot device c2d-message send --hub-name my-hub --device-id mqtt-dev-01 --data "hello world"
The system listens on the DEVICEMESSAGE topic ("devices/mqtt-dev-01/messages/#") and displays the received message.
Device Twin Update
A "device twin" acts as the digital identity of your device. You can use it to report status or receive configurations. The example mosquitto_device_twin allows you to set properties like "temperature":
{"temperature": 32}
After posting them, you can also retrieve them from the cloud with another message . This is useful for maintaining a synchronized view of the physical situation.
Integration with macros in Excel
So far, we've seen how to communicate with Azure using MQTT. But how does this relate to Excel? The key lies in using VBA macros that make HTTP calls or run external scripts (like Python) that communicate via MQTT.
A simple example for sending data from Excel using HTTP would be this:
Sub EnviarComando()
Dim http As Object
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "POST", "https://miapi/mqtt", False
http.setRequestHeader "Content-Type", "application/json"
http.Send "{\"mensaje\":\"Encender luz\"}"
End Sub
Another alternative is to have Excel invoke a Python script with paho-mqtt to send MQTT messages in the background , which is very flexible and easy to implement.
Best practices, resource cleanup, and next steps
At the end of your testing, it's recommended to close the services to avoid unnecessary charges. You can completely delete your resource group with:
az group delete --name mqtt-sample-rg
Or just delete the registered device if you prefer:
az iot hub device-identity delete --hub-name my-hub --device-id mqtt-dev-01
You can also delete the hub and keep the group:
az iot hub delete --name my-hub
Programming macros in Excel to send commands to smart devices is a practical and innovative strategy for integrating everyday tools with the IoT . Thanks to protocols like MQTT and platforms like Azure, a simple spreadsheet can be transformed into a real control panel for your devices; moreover, with open-source libraries like Mosquitto and tools like Visual Studio or Python scripts, the possibilities are virtually limitless without requiring advanced programming knowledge.
Table of Contents
- 1 Prerequisites and basic environment setup
- 2 Setting up IoT Hub and devices in Azure
- 3 Downloading the repository and preparing the code
- 4 Compile the examples and test the connection
- 5 Sending messages from the device to the cloud
- 6 Receiving messages from the cloud to the device
- 7 Device Twin Update
- 8 Integration with macros in Excel
- 9 Best practices, resource cleanup, and next steps
