IoTZap Arduino library

Connect an ESP board to the IoT Zap app in a few lines · Library version 1.0.0 · Updated 2 December 2026

Already know MQTT? The library is a thin helper for WiFi, MQTT, the app’s topic layout, widget commands and optional AES-256-GCM. Nothing is sent to us: your device talks only to the MQTT broker you choose. New to it? The tutorials explain brokers and topics first.

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
}

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.

BoardExamples folderBoard package (Boards Manager)
ESP32 DevKitESP32esp32 by Espressif Systems, 3.x
Wemos / LOLIN D1 mini ESP32ESP32_Miniesp32 by Espressif Systems, 3.x
ESP32-S3 (DevKitC-1)ESP32_S3esp32 by Espressif Systems, 3.x
ESP32-C3 (DevKitM-1)ESP32_C3esp32 by Espressif Systems, 3.x
ESP8266 NodeMCU / dev boardsESP8266_NodeMCUesp8266 by ESP8266 Community, 3.x
Wemos / LOLIN D1 miniESP8266_D1Miniesp8266 by ESP8266 Community, 3.x
ESP-01 / ESP-01SESP01esp8266 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)

  1. Install Arduino IDE 2 from arduino.cc.
  2. 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 add https://espressif.github.io/arduino-esp32/package_esp32_index.json).
  3. Open Tools → Board → Boards Manager, search esp32 and/or esp8266, and install.
  4. 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.
Not listed yet? New libraries appear in the Library Manager within about a day of being accepted. Until then use the ZIP install below.

3.2 Install from GitHub (ZIP)

  1. Open github.com/zealyeninfotech/IoTZap, click Code → Download ZIP.
  2. In the Arduino IDE choose Sketch → Include Library → Add .ZIP Library and pick the file.
  3. 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

ExampleExtra library
Widget_Value_DHT22 and the SmartHome, WeatherStation and Greenhouse projectsDHT sensor library (Adafruit) and Adafruit Unified Sensor
Widget_Thermometer (DS18B20 probe)OneWire and DallasTemperature
All other examplesNone

4. Your first sketch (5 minutes)

  1. Choose your board in Tools → Board and the right Port.
  2. Open File → Examples → IoTZap → <your board> → <Board>_Basic_LED. For example ESP32_Basic_LED or ESP01_Basic_LED.
  3. 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/";
  4. 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_LED it is led).
  5. 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.

  1. In the app, open your project’s dashboard.
  2. Open the dashboard menu and choose Device setup info.
  3. Copy the value shown as the topic prefix, for example iz/7462108984f6/660fe81fbe14/. Keep the trailing slash.
  4. Paste it as IZ_PREFIX (or into IoTZap.setProject()).
Broker settings must match. Use the same broker host and port in the sketch and in the app’s Broker tab. If you changed the data topic or command topic in the app, pass them as the 2nd and 3rd argument: 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:

NameWhat it is
<Board>_Basic_LEDControl 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.

ESP-01 has only two usable pins. It has no analog input, so examples that need an ADC or more than two pins (Gauge, RC car and similar) are not provided for it. Use any other board for those.

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
});
Tip: always send the new state back after acting on a command. Switches, LEDs and sliders in the app then show what the device really did, even if someone changed it elsewhere.

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());
  }
}
Do not use 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).

WidgetDirectionValueExample
SwitchIn (+ Out to confirm)0 or 1Widget_Switch
Push buttonIn1 while held, 0 when releasedWidget_PushButton
Slider, Knob, StepperInnumber in the widget’s rangeWidget_Slider, Widget_Knob, Widget_Stepper
SelectorInchosen option valueWidget_Selector
Colour pickerInhex colour text such as #FF3B30Widget_ColorPicker
Direction padIntext: up, down, left, right, stopWidget_DirectionPad
Text boxIntextWidget_TextBox
Value, Gauge, Speedometer, Donut, Progress, Tank, Thermometer, BatteryOutnumberWidget_Value_DHT22, Widget_Gauge, Widget_Speedometer, Widget_DonutGauge, Widget_ProgressBar, Widget_Tank, Widget_Thermometer, Widget_Battery
LED indicatorOut0 or 1Widget_LED
Value with trend, Min / Max / AverageOutnumber, sent regularlyWidget_ValueTrend, Widget_MinMaxAverage
Line, Area, Bar, Multi-line, Pie chartsOutnumbers, one key per seriesChart_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:

DirectionApp settingIn the sketchExample
Device → appJSON (default)IoTZap.setDataFormat(IZ_DATA_JSON);Format_JSON_Nested
Device → appKey=ValueIoTZap.setDataFormat(IZ_DATA_KEYVALUE);Format_KeyValue
Device → appDelimited listIoTZap.sendText("28.4,65,1");Format_Delimited
Device → appPlain text per topicIoTZap.sendRaw("temp", "28.4");Format_PlainTextPerTopic
Device → appStart / end markersIoTZap.setMarkers('#', '$');Format_StartEndMarkers
App → deviceJSON (default)IoTZap.setCommandFormat(IZ_CMD_JSON);Basic_02_Receive_Commands
App → deviceKey=ValueIoTZap.setCommandFormat(IZ_CMD_KEYVALUE);Commands_KeyValue
App → deviceValue onlyIoTZap.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).

What is supported on the device: AES-256-GCM, key from the passphrase with SHA-256 and optional salt words, Base64 text. The app also offers other AES modes for devices with their own code; the library implements the default GCM mode only.
Never publish your passphrase or passwords. If you share a sketch or post a screenshot, remove WIFI_PASS, the broker password and the passphrase first.

12. Notes for each board

BoardThings to know
ESP32GPIO 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-S3The 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-C3The 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 miniD3 (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-01SOnly 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

ExampleWhat you build
Project_AllWidgets_DemoSends fake data to every output widget and prints every command. Needs no hardware: the best way to try a new dashboard.
Project_SmartHomeTwo relays (lamp and fan), room temperature and humidity, a door contact and an auto-off timer.
Project_WeatherStationDHT22 with dew point, “feels like”, daily min / max and WiFi quality, for a thermometer, donut gauge and charts.
Project_RC_CarDirection pad, speed slider and headlight for a two-motor car, with battery and speed feedback.
Project_RGB_LampPower switch, colour picker, brightness slider and effects (static, breathe, rainbow, blink).
Project_GreenhouseAutomatic 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.

ProblemCheck
Will not compile: IoTZap.h: No such fileThe 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 JsonDocumentInstall 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 notCheck 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 nothingThe 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 deviceThe 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 ignoredPassphrase, 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 checkUse 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 restartingWeak 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 MonitorSet Tools → USB CDC On Boot → Enabled and upload again.
Upload failsHold 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.

Next step: open File → Examples → IoTZap, pick your board, and try Basic_LED, then Project_AllWidgets_Demo.