Write ESP32 and Arduino code with AI
Published: Updated:
An AI assistant knows neither your project nor MakerHex. Tell it both and it writes the code correctly. Otherwise you often get code that tries the unencrypted port, invents topic names or sends far too many messages.
It takes three steps:
- Describe your project in your own words: your board, your sensors and what you want to do.
- Paste the MakerHex description below it: the ready-made text below, without changing anything.
- Follow the AI's answer: together with the code you get a "What to do in MakerHex" list: how to add the device, which variables to create and what to put on your dashboard.
You do not need to prepare anything in the dashboard first; the AI suggests the variable names.
Already created your device in the dashboard? There is an easier way: the Generate code with AI button on the device page fills this text with that device's topic prefix, variables and plan. Just describe your project and copy the text.
1. Describe your project
You write this part, any way you like. It is enough to mention:
- Board: ESP32, Raspberry Pi Pico 2 W, Raspberry Pi…
- Environment: Arduino IDE, MicroPython, Python… If you are not sure, let the AI suggest one.
- Sensors and outputs: their models and which pins they are connected to. If you have not wired them yet, say "suggest the pins".
- What it should do: which values you want to see, what you want to switch from the dashboard, whether it should do something on its own.
- Your plan: Free, Maker, Pro or Ultimate (new accounts start with 14 days of Pro). The message rate and the number of variables depend on the plan.
- If you already created a device and variables in the dashboard, their MQTT Topic values.
Example:
I have an ESP32 DevKit and a DHT22 temperature and humidity sensor; the sensor's data pin is on GPIO 4.
I will also connect a 12 V fan through a relay module. I use the Arduino IDE.
I want to see temperature and humidity in MakerHex and switch the fan on and off from the dashboard. My plan is Free.
2. Add the MakerHex description below it
Copy the text below and paste it right below your project description. Do not change anything; there are no brackets or blanks to fill in.
Do not give your password to the AI. The text asks for code with placeholders. You enter the real user name, password and Wi-Fi details yourself after you get the code.
--- MAKERHEX SYSTEM INFORMATION ---
I described my project above. Write the program so that it connects to the MakerHex IoT platform (makerhex.com).
If something is missing from my description (board, pins, sensor model), ask me first; if you assume
something, say so clearly.
HOW MAKERHEX WORKS
- A "device" is created in the dashboard. Each device has its own MQTT user name, password and "Topic prefix"
(format: workspace/device-tag).
- Every measurement and every controlled output is a "variable" in the dashboard. A variable has three fields:
Variable name (shown on screen), MQTT Topic (the short name used in code), Writable? (can commands be sent
from the dashboard). There is no unit field; put the unit in the Variable name, e.g. "Temperature (°C)".
- Data that is not defined in the dashboard is silently ignored: every key in the code needs a variable with
the same MQTT Topic in the dashboard.
- Plan limits (devices / total variables / shortest send interval): Free 1 / 6 / 30 s, Maker 10 / 100 / 5 s, Pro 30 / 400 / 1 s, Ultimate 75 / 1500 / 1 s.
New accounts run with Pro limits for the first 14 days. Do not ask me to create unnecessary variables.
YOU CHOOSE THE MQTT TOPIC NAMES
- Lowercase English letters, digits and underscores; no spaces or special characters (e.g. temperature,
humidity, fan).
- If I already created variables in the dashboard and gave you their MQTT Topic values, use them as they are.
- Do not use data, set, veri, all_data, thermal or energy.
CONNECTION
1. mqtt.makerhex.com, port 8883, MQTT 3.1.1, TLS required. Port 1883, unencrypted connections and anything
that disables certificate verification (setInsecure, CERT_NONE, etc.) are not allowed.
2. Verify the certificate with the Let's Encrypt roots (ISRG Root X1 and X2). Do not make up certificate
contents: in microcontroller code, leave an empty PEM placeholder named ROOT_CA and I will paste it.
On Linux (Raspberry Pi), use the system trust store.
3. On a microcontroller, get the time via NTP (pool.ntp.org) before starting TLS.
4. Use clearly marked placeholders at the top of the code for the Wi-Fi name/password, MQTT user name,
MQTT password and TOPIC_PREFIX. I will enter the real values; do not ask me for passwords.
5. Make the client ID unique per device: MQTT user name + the chip's serial number or MAC address.
SENDING DATA AND RECEIVING COMMANDS
6. Use only topics under TOPIC_PREFIX.
7. Send all measurements in one message to TOPIC_PREFIX + "/data" as flat (not nested) JSON:
{"temperature":24.6,"humidity":61}. The keys are the MQTT Topic values; numbers without quotes.
8. Commands from the dashboard arrive on TOPIC_PREFIX + "/set/{MQTT Topic}" as plain text ("1", "0",
"true", "false", a number or text). Subscribe to the set topic of every writable variable.
9. The dashboard stores a sent value immediately; this does not mean the device applied the command. For every
output covered by rule 15 (relay, motor, pump, door), a separate, non-writable state variable is REQUIRED
(e.g. fan_state); add it to the /data JSON. When the connection drops and the output switches itself off,
this variable is the only way the dashboard can see it.
10. Send rate (per device): Free 1 every 30 s, Maker 1 every 5 s, Pro 1 every 1 s, Ultimate 1 every 1 s. If I did not mention my plan, use 30 s;
make the interval a single constant at the top of the code. After receiving a command you may send the new
state IMMEDIATELY, once (the system allows a short burst of up to 5 messages); regular sends then
continue at this interval. Messages that keep exceeding the limit are dropped, and a device that keeps
exceeding it is cut off.
11. Keep messages under 1 KB (anything over 4 KB is not processed). With PubSubClient, enlarge the buffer
with setBufferSize if a message can exceed 200 bytes.
11b. If you send a location, use two separate numeric keys named enlem (latitude) and boylam (longitude) in the
/data JSON (decimal degrees, e.g. 41.0151). While the GPS has no fix (0,0 or invalid), leave both keys out.
RELIABILITY AND SAFETY
12. Use millis() or time.monotonic(); do not block the loop with long delay/sleep calls.
13. If Wi-Fi or MQTT disconnects, reconnect every 5 s; after reconnecting, subscribe to the set topics again
and send all state variables IMMEDIATELY, once.
14. Do not send sensor values that could not be read (NaN, errors); print them to the serial port.
15. Relays, motors, pumps and similar outputs must be off at start-up and must switch off THE MOMENT Wi-Fi OR
MQTT disconnects (without waiting for the reconnect). Do this check on every loop, before the reconnect
code. If an output runs only while a button is held, add a watchdog that switches it off when commands stop.
15b. Most relay modules switch on with LOW (active low). If I did not name my module, put a single constant such
as RELAY_ACTIVE_LOW at the top of the code, default it to true and explain in the summary how to change it.
16. Print short status messages to the serial port: Wi-Fi connected, connected to MakerHex, JSON sent,
error code.
ANSWER IN THIS ORDER (the whole answer in English)
A. Short summary: what the program does and which assumptions you made.
B. Wiring diagram (which pin goes where).
C. Board package and libraries to install (full names and authors).
D. One complete file that compiles; comments in English. At the top: "MakerHex must not be used to control
life-critical or safety-critical systems."
The reader may be a beginner student: above every section and every important line, write a short comment
in plain words that explains what it does.
Below the code, add a "Functions used in the code" table: for every library function you use
(e.g. WiFi.begin, setCACert, connect, publish, subscribe, loop, millis), what it does and when it is called.
E. A "What to do in MakerHex" section, step by step:
1. Devices > Add device: suggested Device name and Tag (lowercase, no spaces), Device type: Other,
MQTT identity: Generate automatically (recommended).
2. Variable table: Variable name | MQTT Topic | Writable? (exactly as in the code).
3. After Create device, which line of the code the User, Password and Topic prefix shown in the window
go into.
4. Dashboard suggestion: which widget for which variable (Gauge, Chart, Control (send data)); for
controls, the mode (On / Off switch, Button, Slider...) and the State variable if there is one.
5. Alarm suggestions I can set up if I want (e.g. if the temperature goes above 35 °C).
F. A short troubleshooting list for common errors.
3. Follow the AI's answer in the dashboard
Work through the "What to do in MakerHex" list at the end of the answer:
- Devices → Add device. Enter the suggested Device name and Tag, set Device type to
Otherand MQTT identity toGenerate automatically (recommended). - In the Variables section, add each row of the table with Add variable. The MQTT Topic values must match the code; data that is not in the dashboard does not show up.
- Click Create device. The window that opens shows the User, Password and Topic prefix. Put them into the placeholders at the top of the code, together with your Wi-Fi details. You can also find them later in the MQTT connection details card on the device page.
- After uploading the code, add the suggested widgets to your dashboard.
If you do not like a name the AI suggested, you can change it in the dashboard; but if you change an MQTT Topic, change the key in the code too.
4. Paste the root certificates into the code
The AI leaves an empty ROOT_CA block in the code. Paste the two certificates below into it exactly as shown. They are not secret; they let the board verify that it is really talking to the MakerHex server. Linux computers such as the Raspberry Pi do not need this, because the system already trusts them.
-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
h77ct984kIxuPOZXoHj3dcK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cP9Ymd70/aTSVaYgLXTWNLxBo1BfASdW
tL4ndQavEi51mI38AjEAi/V3bNTIZargCyzuFJ0nN6T5U6VR5CmD1/iQMVtCnwr1
/q4AaOeMSQ+2b1tbFfLn
-----END CERTIFICATE-----
5. Check before uploading
AI assistants sometimes skip rules. Before uploading, check:
- Is the port
8883? Does the code containsetInsecure,CERT_NONEor1883? If so, tell the AI "you did not follow rule 1". - Is the
ROOT_CAblock identical to the two certificates above? - Do the JSON keys and set topics in the code match the dashboard MQTT Topic values?
- Does the number of variables fit your plan? The limits are on the Pricing page.
- Does the send interval fit your plan?
- Did you enter the user name, password and topic prefix yourself?
After uploading, also try this:
- When you turn Wi-Fi off (switch off the router or move the board away), does the relay / output switch off immediately?
- When you send a command from the dashboard, does the state variable change within a few seconds?
If the code does not work, give the serial output and the error code back to the AI. The meaning of the connection error codes is in the troubleshooting table on Connect your first device with an ESP32.
MakerHex must not be used to control life-critical or safety-critical systems. Always review AI-generated code yourself before running it.
Related examples
- Connect your existing project to MakerHexAlready have a working ESP32 project? Connect it to MakerHex with one library, a ready-made block and two lines. Send values or JSON, receive commands.
- ESP32 MQTT connection test (the simplest example)No sensor needed. Send values one by one from an ESP32 to MakerHex over MQTT and switch an LED from the dashboard. Every line explained for first-timers.
- Connect your first IoT device with an ESP32 and DHT11Send temperature and humidity from an ESP32 with a DHT11 or DHT22 to MakerHex and switch an LED from the dashboard. Encrypted (TLS), copy-paste code.
Try it with your own device: start free, 14 days of Pro features included, no credit card.
Start free