1. What the library does
Without the library you write the WiFi code, the MQTT code, build the topic names, parse JSON, handle reconnects and (if you want it) implement AES-GCM yourself. With it, a sketch looks like this:
#include <IoTZap.h>
void setup() {
IoTZap.setWiFi("my-wifi", "my-password");
IoTZap.setBroker("broker.emqx.io", 1883);
IoTZap.setProject("iz/7462108984f6/660fe81fbe14/"); // copied from the app
IoTZap.on("relay1", [](const String& v) { // a Switch widget, pin key "relay1"
digitalWrite(IZ_LED_PIN, v.toInt() ? IZ_LED_ON : !IZ_LED_ON);
IoTZap.send("relay1", v.toInt()); // report the real state back
});
IoTZap.begin();
}
void loop() {
IoTZap.loop();
static uint32_t last = 0;
if (izEvery(last, 5000)) IoTZap.send("temp", 28.4); // a Value / Gauge / Chart widget
}
- Connects and reconnects to WiFi and the broker by itself.
- Subscribes to the right command topic and calls your function for each widget.
- Publishes your readings in the JSON (or key=value, list, plain text) format the app reads.
- Optional TLS (port 8883), broker username/password and AES-256-GCM that matches the app’s default.
- Comes with ready-made sketches for every widget and six small projects, for each board.
2. Supported boards
Every example sketch below was compiled with the Arduino tool chain for its board (esp32 core 3.x and esp8266 core 3.x). They have not been tested on every physical board, so check your wiring and tell us if something does not work.
| Board | Examples folder | Board package (Boards Manager) |
|---|---|---|
| ESP32 DevKit | ESP32 | esp32 by Espressif Systems, 3.x |
| Wemos / LOLIN D1 mini ESP32 | ESP32_Mini | esp32 by Espressif Systems, 3.x |
| ESP32-S3 (DevKitC-1) | ESP32_S3 | esp32 by Espressif Systems, 3.x |
| ESP32-C3 (DevKitM-1) | ESP32_C3 | esp32 by Espressif Systems, 3.x |
| ESP8266 NodeMCU / dev boards | ESP8266_NodeMCU | esp8266 by ESP8266 Community, 3.x |
| Wemos / LOLIN D1 mini | ESP8266_D1Mini | esp8266 by ESP8266 Community, 3.x |
| ESP-01 / ESP-01S | ESP01 | esp8266 by ESP8266 Community, 3.x |
Other ESP32 or ESP8266 boards usually work too: pick the nearest board folder and change the pin numbers at the top of the sketch.
3. Install it
3.1 Arduino IDE 2 (recommended)
- Install Arduino IDE 2 from arduino.cc.
- Open File → Preferences. In Additional boards manager URLs add the ESP8266 address
https://arduino.esp8266.com/stable/package_esp8266com_index.json(ESP32 boards are in the default list in recent versions; otherwise addhttps://espressif.github.io/arduino-esp32/package_esp32_index.json). - Open Tools → Board → Boards Manager, search esp32 and/or esp8266, and install.
- Open Sketch → Include Library → Manage Libraries (or the Library icon on the left), search IoTZap and click Install. When asked, choose Install all so PubSubClient and ArduinoJson come with it.
3.2 Install from GitHub (ZIP)
- Open github.com/zealyeninfotech/IoTZap, click Code → Download ZIP.
- In the Arduino IDE choose Sketch → Include Library → Add .ZIP Library and pick the file.
- Install the two helper libraries by hand in the Library Manager: PubSubClient (by Nick O’Leary) and ArduinoJson (by Benoit Blanchon, version 7 or newer).
3.3 Libraries some examples also need
| Example | Extra library |
|---|---|
Widget_Value_DHT22 and the SmartHome, WeatherStation and Greenhouse projects | DHT sensor library (Adafruit) and Adafruit Unified Sensor |
Widget_Thermometer (DS18B20 probe) | OneWire and DallasTemperature |
| All other examples | None |
4. Your first sketch (5 minutes)
- Choose your board in Tools → Board and the right Port.
- Open File → Examples → IoTZap → <your board> → <Board>_Basic_LED. For example
ESP32_Basic_LEDorESP01_Basic_LED. - Edit the settings at the top of the sketch (see the next section for the prefix):
const char* WIFI_SSID = "my-wifi"; const char* WIFI_PASS = "my-password"; const char* BROKER = "broker.emqx.io"; const char* IZ_PREFIX = "iz/7462108984f6/660fe81fbe14/"; - In the IoT Zap app add a Switch widget on a pin with the key written in the first comment of the sketch (for
Basic_LEDit isled). - Click Upload, open the Serial Monitor at 115200 baud, connect the app to the same broker and flip the switch. The LED follows it.
5. Get your project prefix from the app
The app builds private topics that look like iz/<your tag>/<project tag>/home/data (sensor data from the device) and .../home/cmd (commands to the device). In the sketch you give the part in front of home/data.
- In the app, open your project’s dashboard.
- Open the dashboard menu and choose Device setup info.
- Copy the value shown as the topic prefix, for example
iz/7462108984f6/660fe81fbe14/. Keep the trailing slash. - Paste it as
IZ_PREFIX(or intoIoTZap.setProject()).
IoTZap.setProject(prefix, "home/data", "home/cmd"); or set the full topics with IoTZap.setTopics(fullDataTopic, fullCmdTopic).
6. Use the ready-made examples
Open File → Examples → IoTZap. There is one folder per board. Every folder holds the same sketches, already set to the pins of that board:
| Name | What it is |
|---|---|
<Board>_Basic_LED | Control the on-board LED from the app. Start here. |
<Board>_Widget_* | One sketch per widget: Switch, PushButton, Slider, Knob, Stepper, Selector, ColorPicker, DirectionPad, TextBox, Value_DHT22, LED, Gauge, Speedometer, DonutGauge, ProgressBar, Tank, Thermometer, Battery, ValueTrend, MinMaxAverage. |
<Board>_Chart_* | Line, Line_XvsY, Area, Bar, MultiLine, Pie, Pie_Readings. |
<Board>_Project_* | AllWidgets_Demo (no hardware needed), SmartHome, WeatherStation, RC_Car, RGB_Lamp, Greenhouse. |
Common/* | Same for every board, no wiring: first steps, data formats, command formats, automations, alarms, TLS, broker login, AES-256-GCM, deep sleep. |
Every sketch begins with a comment listing the wiring and the pins and widgets to create in the app. Create exactly those keys, or change the key names in the sketch to match yours.
To change a pin, edit the const int ... = ...; lines at the top. Nothing else in the sketch depends on the board.
7. Receive commands from widgets
A widget with a pin key (such as relay1) sends its value when you use it. Register a function for that key:
IoTZap.on("relay1", [](const String& value) {
digitalWrite(RELAY_PIN, value.toInt() ? HIGH : LOW);
IoTZap.send("relay1", value.toInt()); // tell the app the real state
});
IoTZap.on("speed", [](const String& value) {
analogWrite(MOTOR_PIN, value.toInt()); // Slider 0..255
});
Other handlers:
IoTZap.onAny([](const String& key, const String& value) { // every command
Serial.printf("%s = %s\n", key.c_str(), value.c_str());
});
IoTZap.onConnect([]() { // after each (re)connect
IoTZap.send("status", "online");
});
IoTZap.subscribe("alerts", [](const String& topic, const String& body) {
Serial.println(body); // your own extra topic
});
8. Send data to widgets
IoTZap.send("temp", 28.4); // one value, one message
IoTZap.send("relay1", 1);
IoTZap.send("status", "online");
// several values in one message (less traffic, charts stay in step)
IoTZap.set("temp", 28.4, 1).set("hum", 65); // 1 = decimals
IoTZap.publishData();
// nested JSON, using the ArduinoJson document
IoTZap.data["room"]["temp"] = 21;
IoTZap.publishData();
// text formats
IoTZap.sendText("28.4,65,1"); // for the "Delimited" parse mode
IoTZap.sendRaw("temp", "28.4"); // for "Plain text per topic"
Send on a timer without blocking the loop, using the built-in helper:
void loop() {
IoTZap.loop(); // must run often
static uint32_t last = 0;
if (izEvery(last, 5000)) { // every 5 seconds
IoTZap.send("temp", readTemperature());
}
}
delay() for long waits in loop(). While your sketch sleeps, the library cannot keep the MQTT connection alive. Use izEvery or millis(). For battery projects see Common/Power_DeepSleep_Sensor.
9. Widget by widget: which code to use
The key is the name you give the widget’s pin in the app. In means the app sends it to the device (use IoTZap.on), Out means the device sends it to the app (use IoTZap.send).
| Widget | Direction | Value | Example |
|---|---|---|---|
| Switch | In (+ Out to confirm) | 0 or 1 | Widget_Switch |
| Push button | In | 1 while held, 0 when released | Widget_PushButton |
| Slider, Knob, Stepper | In | number in the widget’s range | Widget_Slider, Widget_Knob, Widget_Stepper |
| Selector | In | chosen option value | Widget_Selector |
| Colour picker | In | hex colour text such as #FF3B30 | Widget_ColorPicker |
| Direction pad | In | text: up, down, left, right, stop | Widget_DirectionPad |
| Text box | In | text | Widget_TextBox |
| Value, Gauge, Speedometer, Donut, Progress, Tank, Thermometer, Battery | Out | number | Widget_Value_DHT22, Widget_Gauge, Widget_Speedometer, Widget_DonutGauge, Widget_ProgressBar, Widget_Tank, Widget_Thermometer, Widget_Battery |
| LED indicator | Out | 0 or 1 | Widget_LED |
| Value with trend, Min / Max / Average | Out | number, sent regularly | Widget_ValueTrend, Widget_MinMaxAverage |
| Line, Area, Bar, Multi-line, Pie charts | Out | numbers, one key per series | Chart_Line, Chart_Line_XvsY, Chart_Area, Chart_Bar, Chart_MultiLine, Chart_Pie, Chart_Pie_Readings |
Alarms and automations are set up in the app on the same keys. For a custom message from an automation to your device see Common/Automation_CustomMessage and Common/Alarm_HighLow.
10. Data and command formats
The app can read several message layouts. The default (JSON) needs no setup. If your project uses another layout in the app, switch the library to match:
| Direction | App setting | In the sketch | Example |
|---|---|---|---|
| Device → app | JSON (default) | IoTZap.setDataFormat(IZ_DATA_JSON); | Format_JSON_Nested |
| Device → app | Key=Value | IoTZap.setDataFormat(IZ_DATA_KEYVALUE); | Format_KeyValue |
| Device → app | Delimited list | IoTZap.sendText("28.4,65,1"); | Format_Delimited |
| Device → app | Plain text per topic | IoTZap.sendRaw("temp", "28.4"); | Format_PlainTextPerTopic |
| Device → app | Start / end markers | IoTZap.setMarkers('#', '$'); | Format_StartEndMarkers |
| App → device | JSON (default) | IoTZap.setCommandFormat(IZ_CMD_JSON); | Basic_02_Receive_Commands |
| App → device | Key=Value | IoTZap.setCommandFormat(IZ_CMD_KEYVALUE); | Commands_KeyValue |
| App → device | Value only | IoTZap.setCommandFormat(IZ_CMD_VALUE_ONLY); | Commands_ValueOnly |
The settings in the app and in the sketch must be the same. If widgets stay empty, this is the first thing to check.
11. Security: TLS, broker login and AES-256
The library supports the same layers as the app (see secure connectivity). Use them together on a public broker.
11.1 Broker username and password
IoTZap.setBroker("my-broker.example.com", 1883, "user", "password");
Example: Common/Security_Broker_Login.
11.2 TLS (encrypted connection)
IoTZap.setBroker("my-broker.example.com", 8883, "user", "password");
IoTZap.setSecure(true); // TLS, certificate NOT checked (easy, protects from eavesdropping)
// or check the broker's certificate (needs the right time, the library sets it over NTP)
IoTZap.setCACert(ROOT_CA_PEM); // paste the broker's root CA as text
Examples: Common/Security_TLS_Insecure and Common/Security_TLS_CACert. Use port 8883 (not 1883) on the device and in the app.
11.3 AES-256-GCM message encryption
IoTZap.setEncryption("my long secret passphrase", "salt words");
Use the same passphrase and salt words as in the app’s Security tab with the default AES-GCM mode. The broker then only sees scrambled text. Examples: Common/Security_AES256_GCM and Common/Security_Full_TLS_Login_AES (all three layers together).
WIFI_PASS, the broker password and the passphrase first.
12. Notes for each board
| Board | Things to know |
|---|---|
| ESP32 | GPIO 34 to 39 are input only. WiFi and analog reading work together on ADC1 pins (GPIO 32 to 39), not on ADC2. |
| ESP32 Mini (D1 mini ESP32) | The pins used are listed in the wiring comment at the top of each sketch. Change the const int lines if your board’s labels differ. |
| ESP32-S3 | The on-board LED is an RGB LED. For Serial Monitor over the USB port set Tools → USB CDC On Boot → Enabled. GPIO 19/20 are USB; GPIO 26 to 37 may be used by flash and PSRAM. |
| ESP32-C3 | The on-board LED is an RGB LED. Set USB CDC On Boot → Enabled to see Serial. GPIO 2, 8 and 9 are boot pins. |
| ESP8266 NodeMCU / D1 mini | D3 (GPIO 0), D4 (GPIO 2) and D8 (GPIO 15) are boot pins: do not hold them at the wrong level at power-up. A0 reads 0 to 3.2 V on these boards. The LED is on when the pin is LOW. |
| ESP-01 / ESP-01S | Only GPIO 0 and GPIO 2 (plus TX/RX). GPIO 0 and 2 must be HIGH at boot, so do not let relays or sensors pull them LOW while it starts. No analog input. Needs a 3.3 V supply that can give 300 mA, and a USB-serial adapter (or the ESP-01S programmer) to upload. |
IZ_LED_PIN and IZ_LED_ON are defined by the library for the board you selected, so digitalWrite(IZ_LED_PIN, IZ_LED_ON) turns the on-board LED on, whatever its polarity.
ESP8266 memory: TLS together with AES-256 uses a lot of RAM on an ESP8266. Keep those sketches small and avoid large strings. If a secure sketch restarts by itself, use TLS only, or AES only.
13. Project examples
| Example | What you build |
|---|---|
Project_AllWidgets_Demo | Sends fake data to every output widget and prints every command. Needs no hardware: the best way to try a new dashboard. |
Project_SmartHome | Two relays (lamp and fan), room temperature and humidity, a door contact and an auto-off timer. |
Project_WeatherStation | DHT22 with dew point, “feels like”, daily min / max and WiFi quality, for a thermometer, donut gauge and charts. |
Project_RC_Car | Direction pad, speed slider and headlight for a two-motor car, with battery and speed feedback. |
Project_RGB_Lamp | Power switch, colour picker, brightness slider and effects (static, breathe, rainbow, blink). |
Project_Greenhouse | Automatic watering and ventilation from soil moisture and temperature, with a manual mode and a pump safety limit. |
Not every project exists for every board: an example is only provided when the board has the pins it needs. The wiring is written in the first comment of each sketch.
14. API cheat sheet
// setup
IoTZap.setWiFi(ssid, pass);
IoTZap.setBroker(host, port, user, pass); // user / pass optional
IoTZap.setProject(prefix, "home/data", "home/cmd"); // or setTopics(fullDataTopic, fullCmdTopic)
IoTZap.setSecure(true); // TLS, no certificate check
IoTZap.setCACert(rootCaPem); // TLS with certificate check
IoTZap.setEncryption("passphrase", "salt"); // AES-256-GCM
IoTZap.setMarkers('#', '$'); // start / end markers
IoTZap.setDataFormat(IZ_DATA_KEYVALUE); // IZ_DATA_JSON (default) | IZ_DATA_KEYVALUE
IoTZap.setCommandFormat(IZ_CMD_VALUE_ONLY); // IZ_CMD_JSON | IZ_CMD_KEYVALUE | IZ_CMD_VALUE_ONLY
IoTZap.setRetain(true); IoTZap.debug(true);
// handlers
IoTZap.on("key", [](const String& value) { ... });
IoTZap.onAny([](const String& key, const String& value) { ... });
IoTZap.onConnect([]() { ... });
IoTZap.subscribe("topic", [](const String& topic, const String& body) { ... });
// run
IoTZap.begin(); IoTZap.loop(); // begin() in setup, loop() every pass
// send
IoTZap.send("temp", 28.4);
IoTZap.set("temp", 28.4, 1).set("hum", 65); IoTZap.publishData();
IoTZap.data["room"]["temp"] = 21;
IoTZap.sendText("28.4,65,1"); IoTZap.sendRaw("temp", "28.4");
// helpers
izEvery(lastMillis, intervalMs); // true once per interval
IZ_LED_PIN, IZ_LED_ON // on-board LED of the selected board
The message buffer is 1024 bytes. For bigger messages call IoTZap.setBufferSize(n) before IoTZap.begin(). Publishing uses MQTT QoS 0. Devices connect over MQTT (TCP or TLS); WebSocket connections are for the phone only.
15. Troubleshooting
Turn on debug output first: IoTZap.debug(true); in setup(), and open the Serial Monitor at 115200 baud.
| Problem | Check |
|---|---|
Will not compile: IoTZap.h: No such file | The library is not installed, or you selected a board that is not ESP32 / ESP8266 (the library supports those two families only). |
Compile error in ArduinoJson or JsonDocument | Install ArduinoJson 7 or newer. Version 6 is not compatible. |
| Stays on “connecting to WiFi” | Check the SSID and password. ESP boards use 2.4 GHz WiFi only, not 5 GHz. |
| WiFi connects, MQTT does not | Check broker host and port. With TLS use 8883. A broker that needs a login also needs setBroker(host, port, user, pass). |
| Connected, but the app shows nothing | The prefix must be exactly the one from Device setup info and end with /. Broker, topics and the data format must be the same in the app and the sketch. Watch the app’s Live monitor. |
| Widget does nothing on the device | The key in IoTZap.on("key", ...) must match the widget’s pin key exactly (it is case sensitive). Use onAny to print what arrives. |
| Encrypted data shows as garbage or is ignored | Passphrase, salt words and mode must be identical on both sides (AES-GCM in the app). Use the app’s Test box in Security. |
| TLS fails with the certificate check | Use the broker’s root CA. The device clock is set over NTP, so it needs internet. Try setSecure(true) first to test the connection. |
| The board keeps restarting | Weak power supply (especially ESP-01 and ESP8266), a boot pin held at the wrong level, or too little RAM with TLS + AES on ESP8266. |
| ESP32-S3 / C3: nothing in the Serial Monitor | Set Tools → USB CDC On Boot → Enabled and upload again. |
| Upload fails | Hold the BOOT button while uploading, try another cable (some are charge-only), and pick the right Port. ESP-01 must have GPIO 0 pulled LOW while it is reset to enter upload mode. |
16. Questions
Does it work with the free public broker?
Yes. broker.emqx.io on port 1883 works for testing. Everyone can read a public broker, so for real projects use TLS, a broker login and AES-256 encryption (section 11).
Can I use it with the Arduino UNO or other boards?
No. The library is written for ESP32 and ESP8266 boards and needs their WiFi stack. For other boards, send the same messages with any MQTT library: see the tutorials for the message layout.
Can one board serve several widgets?
Yes. Register one IoTZap.on(...) per widget key and send readings with different keys.
Can I use my own topics?
Yes. Set the full topics with IoTZap.setTopics(dataTopic, cmdTopic) and use the same topics in the app (turn off Make my topics unique there if you want exact names).
Is it free?
Yes. The library is open source under the MIT licence.
Where do I report a bug or ask for a feature?
Open an issue on GitHub or write to support@zealyenlabs.in. Please remove passwords and passphrases from anything you send.
File → Examples → IoTZap, pick your board, and try Basic_LED, then Project_AllWidgets_Demo.